Skip to content

Commit

Permalink
feat: update some relation extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
dingzifan719 committed Feb 9, 2023
1 parent 210c279 commit 38301db
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 27 deletions.
71 changes: 63 additions & 8 deletions src/main/java/cdt/CppVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ public int leave(ICPPASTNamespaceDefinition namespaceDefinition) {

@Override
public int visit(IASTDeclSpecifier declSpec) {
if(declSpec instanceof CPPASTElaboratedTypeSpecifier){
CPPASTElaboratedTypeSpecifier elaboratedTypeSpecifier = (CPPASTElaboratedTypeSpecifier)declSpec;
int type = elaboratedTypeSpecifier.getKind();
String methodName = elaboratedTypeSpecifier.getName().toString();
ArrayList<String> baseName = new ArrayList<String>();
boolean isTemplate = isTemplate(declSpec);
switch (type) {
case CPPASTElaboratedTypeSpecifier.k_struct -> {
this.specifierEntity = context.foundStructDefinition(methodName, baseName,
getLocation(elaboratedTypeSpecifier), isTemplate);
}
case CPPASTElaboratedTypeSpecifier.k_union -> {
this.specifierEntity = context.foundUnionDefinition(methodName,
getLocation(elaboratedTypeSpecifier));
}
case CPPASTElaboratedTypeSpecifier.k_class -> {
this.specifierEntity = context.foundClassDefinition(methodName, baseName,
getLocation(elaboratedTypeSpecifier), isTemplate);
}
}
}
if(declSpec instanceof CPPASTCompositeTypeSpecifier) {
// Extract Class/Struct/Union Entities
CPPASTCompositeTypeSpecifier typeSpecifier = (CPPASTCompositeTypeSpecifier) declSpec;
Expand All @@ -130,15 +151,19 @@ public int visit(IASTDeclSpecifier declSpec) {
for (ICPPASTBaseSpecifier baseSpecifier : baseSpecifiers) {
baseName.add(baseSpecifier.getNameSpecifier().toString());
}
boolean isTemplate = isTemplate(declSpec);
switch (type) {
case 1 -> {
this.specifierEntity = context.foundStructDefinition(methodName, baseName, getLocation(typeSpecifier));
case IASTCompositeTypeSpecifier.k_struct -> {
this.specifierEntity = context.foundStructDefinition(methodName, baseName,
getLocation(typeSpecifier), isTemplate);
}
case 2 -> {
this.specifierEntity = context.foundUnionDefinition(methodName, getLocation(typeSpecifier));
case IASTCompositeTypeSpecifier.k_union -> {
this.specifierEntity = context.foundUnionDefinition(methodName,
getLocation(typeSpecifier));
}
case 3 -> {
this.specifierEntity = context.foundClassDefinition(methodName, baseName, getLocation(typeSpecifier));
this.specifierEntity = context.foundClassDefinition(methodName, baseName,
getLocation(typeSpecifier), isTemplate);
}
}

Expand Down Expand Up @@ -174,7 +199,11 @@ public int visit(IASTStatement statement) {
|| statement instanceof IASTWhileStatement || statement instanceof IASTSwitchStatement) {
context.foundCodeScope(statement);
}
if (statement instanceof IASTProblemStatement) {
if(statement instanceof CPPASTReturnStatement){
if(((CPPASTReturnStatement) statement).getReturnArgument() instanceof CPPASTIdExpression){
context.dealExpressionNode((IASTExpression) ((CPPASTReturnStatement) statement).getReturnArgument(),
"Use");
}
}
return super.visit(statement);
}
Expand All @@ -191,12 +220,12 @@ public int leave(IASTStatement statement) {
@Override
public int visit(IASTDeclaration declaration) {
if (declaration instanceof ICPPASTUsingDeclaration) {
String ns = ASTStringUtil.getName((ICPPASTUsingDeclaration) declaration);
String ns = ((ICPPASTUsingDeclaration) declaration).getName().toString();
context.foundUsingImport(ns);
}
else if (declaration instanceof ICPPASTUsingDirective) {
// using namespace xxx
String ns = ((ICPPASTUsingDirective) declaration).getQualifiedName().toString().replace("::", ".");
String ns = ((ICPPASTUsingDirective) declaration).getQualifiedName().toString();
context.foundUsingImport(ns);
}
else if (declaration instanceof IASTSimpleDeclaration) {
Expand Down Expand Up @@ -224,6 +253,15 @@ else if(declSpecifier instanceof CPPASTSimpleDeclSpecifier) {
if(declarator instanceof CPPASTFunctionDeclarator){
this.FoundFunctionDeclaration((CPPASTFunctionDeclarator) declarator, declSpecifier);
}else if(declarator instanceof CPPASTDeclarator){
if(declarator.getName() != null){
String varName = this.resolveEntityName(declarator.getName());
String type = declSpecifier.getRawSignature();
context.foundVarDefinition(varName, getLocation(declarator), type);
if(declarator.getInitializer() instanceof CPPASTEqualsInitializer){
CPPASTEqualsInitializer equalsInitializer = (CPPASTEqualsInitializer)(declarator.getInitializer());
context.dealExpressionNode(equalsInitializer.getExpression(), "Use");
}
}

}else{
System.out.println( "INFO: Haven't deal with " + declarator.getClass().toString() + " in " + declSpec.getClass().toString());
Expand Down Expand Up @@ -392,6 +430,7 @@ public void FoundFunctionDeclaration(CPPASTFunctionDeclarator declarator, ICPPAS
}
FunctionEntity functionEntity = context.foundFunctionDeclare(rawName, returnType, getLocation(declarator), parameterLists);
if(declarator.isPureVirtual()) functionEntity.setPureVirtual();
if(isTemplate(declarator)) functionEntity.setTemplate(true);
}
}

Expand Down Expand Up @@ -555,6 +594,22 @@ public FileEntity getfile() {
public boolean isTemplate(IASTNode node) {
if (node instanceof CPPASTTemplateDeclaration)
return true;
if(node instanceof CPPASTCompositeTypeSpecifier){
CPPASTCompositeTypeSpecifier compositeTypeSpecifier = (CPPASTCompositeTypeSpecifier)node;
if(compositeTypeSpecifier.getParent().getParent() != null){
if(compositeTypeSpecifier.getParent().getParent() instanceof CPPASTTemplateDeclaration){
return true;
}
}
}
if(node instanceof CPPASTDeclarator){
CPPASTDeclarator declarator = (CPPASTDeclarator)node;
if(declarator.getParent().getParent() != null){
if(declarator.getParent().getParent() instanceof CPPASTTemplateDeclaration){
return true;
}
}
}
return false;
}

Expand Down
31 changes: 22 additions & 9 deletions src/main/java/cdt/HandlerContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ public Entity foundNamespace(String namespaceName, int startLine, int endLine) {
if(this.currentScope.getSymbolByKind(symbol.getName(), Configure.Namespace) == null){
this.currentScope.define(symbol, Configure.Namespace);
}
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), nsEntity, "Define"));
nsEntity.addScale(endLine - startLine);
entityStack.push(nsEntity);
return nsEntity;
Expand All @@ -219,7 +220,7 @@ public Entity foundNamespace(String namespaceName, int startLine, int endLine) {
* @param: String ClassName,List<String> baseClass, Location location
* @return: ClassEntity
*/
public ClassEntity foundClassDefinition(String ClassName,List<String> baseClass, Location location){
public ClassEntity foundClassDefinition(String ClassName,List<String> baseClass, Location location, boolean isTemplate){
this.currentScope = this.entityStack.peek().getScope();
int id = entityRepo.generateId();
ClassSymbol symbol = new ClassSymbol(ClassName, id);
Expand All @@ -232,10 +233,11 @@ public ClassEntity foundClassDefinition(String ClassName,List<String> baseClass,
ClassEntity classEntity = new ClassEntity(ClassName, resolveName(ClassName),
this.latestValidContainer(),id,symbol, location);
classEntity.addRelation(new Relation(this.currentFileEntity, classEntity,"Contain"));
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), classEntity, "Define"));
if(baseClass!= null) {
classEntity.addBaseClass(baseClass);

}
classEntity.setTemplate(true);
entityRepo.add(classEntity);
entityStack.push(classEntity);
return classEntity;
Expand All @@ -248,7 +250,7 @@ public ClassEntity foundClassDefinition(String ClassName,List<String> baseClass,
* @param: String StructName, List<String> baseStruct, Location location
* @return: StructEntity
*/
public StructEntity foundStructDefinition(String StructName, List<String> baseStruct, Location location) {
public StructEntity foundStructDefinition(String StructName, List<String> baseStruct, Location location, boolean isTemplate) {
this.currentScope = this.entityStack.peek().getScope();
int id = entityRepo.generateId();
if(StructName.equals("")) {
Expand All @@ -264,9 +266,11 @@ public StructEntity foundStructDefinition(String StructName, List<String> baseSt

StructEntity structEntity = new StructEntity(StructName, resolveName(StructName),
this.latestValidContainer(), id, symbol, location);
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), structEntity, "Define"));
if(baseStruct!= null) {
structEntity.addBaseStruct(baseStruct);
}
structEntity.setTemplate(true);
entityRepo.add(structEntity);
entityStack.push(structEntity);
return structEntity;
Expand All @@ -293,7 +297,7 @@ public UnionEntity foundUnionDefinition(String UnionName, Location location) {

UnionEntity unionEntity = new UnionEntity(UnionName, resolveName(UnionName),
this.latestValidContainer(), id, symbol, location);

this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), unionEntity, "Define"));
entityRepo.add(unionEntity);
entityStack.push(unionEntity);
return unionEntity;
Expand Down Expand Up @@ -323,6 +327,7 @@ public EnumEntity foundEnumDefinition(String enumName, Location location) {

EnumEntity enumeration = new EnumEntity(enumName, resolveName(enumName),
this.latestValidContainer(), id, symbol, location);
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), enumeration, "Define"));
entityRepo.add(enumeration);
entityStack.push(enumeration);
return enumeration;
Expand All @@ -344,7 +349,7 @@ public EnumeratorEntity foundEnumeratorDefinition(String enumeratorName, Locatio
if(this.latestValidContainer() instanceof EnumEntity) {
((EnumEntity)this.latestValidContainer()).addEnumerator(enumertorEntity);
}

this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), enumertorEntity, "Define"));
return enumertorEntity;
}

Expand Down Expand Up @@ -374,8 +379,10 @@ public void dealExpression(IASTExpression expression) {

if(expression instanceof CPPASTUnaryExpression) {
CPPASTUnaryExpression unaryExp = (CPPASTUnaryExpression)expression;
String unaryExpText = unaryExp.getRawSignature();
if(unaryExp.getOperator() == 9 || unaryExp.getOperator() == 10){
if(unaryExp.getOperator() == IASTUnaryExpression.op_postFixIncr ||
unaryExp.getOperator() == IASTUnaryExpression.op_postFixDecr ||
unaryExp.getOperator() == IASTUnaryExpression.op_tilde ||
unaryExp.getOperator() == IASTUnaryExpression.op_prefixIncr){
this.dealExpressionNode(unaryExp.getOperand(), "Modify");
}
}
Expand Down Expand Up @@ -541,7 +548,7 @@ public VarEntity foundVarDefinition(String varName, Location location, String ty
VarEntity varEntity = new VarEntity(varName, qualifiedName, this.latestValidContainer(), id, location, type);
entityRepo.add(varEntity);
if(this.latestValidContainer() instanceof DataAggregateEntity ) {
if(!(this.currentScope instanceof DataAggregateSymbol)) {
if(this.currentScope instanceof DataAggregateSymbol) {
if(this.currentScope.getSymbolByKind(varName, Configure.Variable)==null) {
VariableSymbol v = new VariableSymbol(varName, id);
this.currentScope.define(v, Configure.Variable);
Expand All @@ -551,6 +558,7 @@ public VarEntity foundVarDefinition(String varName, Location location, String ty
if(this.latestValidContainer() instanceof ClassEntity){
((ClassEntity) this.latestValidContainer()).addContainEntity(id);
}
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), varEntity, "Define"));
return varEntity;
}

Expand All @@ -567,6 +575,7 @@ public LabelEntity foundLabelDefinition(String labelName, Location location) {
}
}
}
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), labelEntity, "Define"));
return labelEntity;
}

Expand All @@ -581,6 +590,7 @@ public TypedefEntity foundTypedefDefinition(String Name, String originalName, Lo
TypedefEntity typedefEntity = new TypedefEntity(Name, resolveName(Name),this.latestValidContainer(),
entityRepo.generateId(), location );
entityRepo.add(typedefEntity);
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), typedefEntity, "Define"));
return typedefEntity;
}

Expand All @@ -597,6 +607,8 @@ public AliasEntity foundNewAlias(String aliasName, String originalName, Location
AliasEntity currentTypeEntity = new AliasEntity(aliasName,
this.resolveName(aliasName), this.latestValidContainer(),
entityRepo.generateId(), originalName, location );
currentTypeEntity.addBindingRelation("Alias", null, originalName);
this.latestValidContainer().addRelation(new Relation(this.latestValidContainer(), currentTypeEntity, "Define"));
entityRepo.add(currentTypeEntity);
return currentTypeEntity;
}
Expand All @@ -606,7 +618,8 @@ public NamespaceAliasEntity foundNewNamespaceAlias(String aliasName, String orig
if (aliasName.equals(originalName)) return null;
NamespaceAliasEntity currentTypeEntity = new NamespaceAliasEntity(aliasName,
this.resolveName(aliasName), this.latestValidContainer(),
entityRepo.generateId(), originalName, location );
entityRepo.generateId(), originalName, location);
currentTypeEntity.addBindingRelation("Alias", "Namespace", originalName);
entityRepo.add(currentTypeEntity);
return currentTypeEntity;
}
Expand Down
26 changes: 16 additions & 10 deletions src/main/java/relation/RelationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,14 @@ public void relationListDeal() {
for(BindingRelation bindingre :entity.getRelationListByBinding()) {
Entity toEntity = entityRepo.getEntityByLocation(bindingre.getLocationInfor());
if(toEntity == null) {
continue;
toEntity = this.findTheEntity(bindingre.getLocationInfor(), entity);
if(toEntity == null) continue;
}
Relation re = new Relation(entity, toEntity, bindingre.getRelationType());
relationRepo.addRelation(re);
}
for(Tuple scopere :entity.getRelationListByScope()) {
Entity toEntity = this.findTheEntity((String)scopere.getSecond(), entity.getScope());
Entity toEntity = this.findTheEntity((String)scopere.getSecond(), entity);
if(toEntity == null) toEntity = entityRepo.getEntityByName((String)scopere.getSecond());
if(toEntity == null) continue;
Relation re = new Relation(entity, toEntity,(String)scopere.getFirst());
Expand Down Expand Up @@ -107,7 +108,7 @@ public void AggregateDeal() {
List<String> usingList = ((DataAggregateEntity) entity).getUsingImport();
if (usingList.size() != 0) {
for (String usingEntity : usingList) {
Entity toEntity = this.findTheEntity(usingEntity, entity.getScope());
Entity toEntity = this.findTheEntity(usingEntity, entity);
if(toEntity!=null) {
Relation re = new Relation(entity, toEntity, "Using");
relationRepo.addRelation(re);
Expand Down Expand Up @@ -141,15 +142,15 @@ public void ClassDeal() {
List<String> friendClass = ((ClassEntity) entity).getFriendClass();
for (String friend_class : friendClass) {

Entity fromentity = this.findTheEntity(friend_class, entity.getScope());
Entity fromentity = this.findTheEntity(friend_class, entity);
if (fromentity != null) {
Relation re = new Relation(entity, fromentity, "Friend");
relationRepo.addRelation(re);
}
}
List<String> friendFunction = ((ClassEntity) entity).getFriendFunction();
for (String friend_function : friendFunction) {
Entity fromEntity = this.findTheEntity(friend_function, entity.getScope());
Entity fromEntity = this.findTheEntity(friend_function, entity);
if (fromEntity != null) {
Relation re = new Relation(entity, fromEntity, "Friend");
relationRepo.addRelation(re);
Expand Down Expand Up @@ -222,7 +223,7 @@ public void NamespaceAliasDeal() {
if (entity instanceof NamespaceAliasEntity) {
if(((NamespaceAliasEntity) entity).getToNamespaceName() != null){
if(entity.getScope() == null) continue;
Entity namespace = this.findTheEntity(((NamespaceAliasEntity) entity).getToNamespaceName(), entity.getScope());
Entity namespace = this.findTheEntity(((NamespaceAliasEntity) entity).getToNamespaceName(), entity);
if(namespace != null){
Relation aliasDep= new Relation(entity, namespace, "Alias");
relationRepo.addRelation(aliasDep);
Expand Down Expand Up @@ -274,7 +275,7 @@ public Entity findTheTypedEntity(String name, Scope current){
}
}
if(current.getEnclosingScope() != null) current = current.getEnclosingScope();
}while(current.getEnclosingScope() != null);
}while(current != null);
}
else if(scopeManages.length == 2){
do{
Expand All @@ -295,7 +296,10 @@ else if(scopeManages.length == 2){
return null;
}

public Entity findTheEntity(String name, Scope current){
public Entity findTheEntity(String name, Entity entity){
Scope current = entity.getScope();
if((current == null) && (entity.getParent()!=null))
current = entity.getParent().getScope();
try{
if(current == null) return null;
String[] scopeManages = name.split("::");
Expand All @@ -306,7 +310,8 @@ public Entity findTheEntity(String name, Scope current){
return entityRepo.getEntity(current.getSymbol(scopeManages[0]).getEntityID());
}
if(current.getEnclosingScope() != null) current = current.getEnclosingScope();
}while(current.getEnclosingScope() != null);
else return null;
}while(current != null);
}
else if(scopeManages.length == 2){
do{
Expand All @@ -322,7 +327,8 @@ else if(scopeManages.length == 2){
break;
}
if(current.getEnclosingScope() != null) current = current.getEnclosingScope();
}while(current.getEnclosingScope() != null);
else return null;
}while(current != null);
}
}catch (NullPointerException exception){
return null;
Expand Down

0 comments on commit 38301db

Please sign in to comment.