From a8b51c1e200a8f7f8cb274b5d8d744d94e0fdb59 Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Mon, 29 Feb 2016 07:56:00 +0000 Subject: [PATCH] [index] Add a caller relation for a call reference. llvm-svn: 262207 --- clang/include/clang/Index/IndexSymbol.h | 3 ++- clang/lib/Index/IndexBody.cpp | 45 +++++++++++++++++++++------------ clang/lib/Index/IndexSymbol.cpp | 2 ++ clang/test/Index/Core/index-source.m | 6 +++-- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/clang/include/clang/Index/IndexSymbol.h b/clang/include/clang/Index/IndexSymbol.h index 506540b0..484edda 100644 --- a/clang/include/clang/Index/IndexSymbol.h +++ b/clang/include/clang/Index/IndexSymbol.h @@ -88,8 +88,9 @@ enum class SymbolRole : uint16_t { RelationBaseOf = 1 << 10, RelationOverrideOf = 1 << 11, RelationReceivedBy = 1 << 12, + RelationCalledBy = 1 << 13, }; -static const unsigned SymbolRoleBitNum = 13; +static const unsigned SymbolRoleBitNum = 14; typedef unsigned SymbolRoleSet; /// Represents a relation to another symbol for a symbol occurrence. diff --git a/clang/lib/Index/IndexBody.cpp b/clang/lib/Index/IndexBody.cpp index f716445..fda8388 100644 --- a/clang/lib/Index/IndexBody.cpp +++ b/clang/lib/Index/IndexBody.cpp @@ -88,7 +88,7 @@ public: } else if (auto CE = dyn_cast(Parent)) { if (CE->getCallee()->IgnoreParenCasts() == E) { - Roles |= (unsigned)SymbolRole::Call; + addCallRole(Roles, Relations); if (auto *ME = dyn_cast(E)) { if (auto *CXXMD = dyn_cast_or_null(ME->getMemberDecl())) if (CXXMD->isVirtual() && !ME->hasQualifier()) { @@ -120,6 +120,15 @@ public: return Roles; } + void addCallRole(SymbolRoleSet &Roles, + SmallVectorImpl &Relations) { + Roles |= (unsigned)SymbolRole::Call; + if (auto *FD = dyn_cast(ParentDC)) + Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, FD); + else if (auto *MD = dyn_cast(ParentDC)) + Relations.emplace_back((unsigned)SymbolRole::RelationCalledBy, MD); + } + bool VisitDeclRefExpr(DeclRefExpr *E) { SmallVector Relations; SymbolRoleSet Roles = getRolesForRef(E, Relations); @@ -169,11 +178,12 @@ public: }; if (ObjCMethodDecl *MD = E->getMethodDecl()) { - SymbolRoleSet Roles = (unsigned)SymbolRole::Call; + SymbolRoleSet Roles{}; + SmallVector Relations; + addCallRole(Roles, Relations); if (E->isImplicit()) Roles |= (unsigned)SymbolRole::Implicit; - SmallVector Relations; if (isDynamic(E)) { Roles |= (unsigned)SymbolRole::Dynamic; if (auto *RecD = E->getReceiverInterface()) @@ -206,39 +216,42 @@ public: Parent, ParentDC, SymbolRoleSet(), {}, E); } + bool passObjCLiteralMethodCall(const ObjCMethodDecl *MD, const Expr *E) { + SymbolRoleSet Roles{}; + SmallVector Relations; + addCallRole(Roles, Relations); + Roles |= (unsigned)SymbolRole::Implicit; + return IndexCtx.handleReference(MD, E->getLocStart(), + Parent, ParentDC, Roles, Relations, E); + } + bool VisitObjCBoxedExpr(ObjCBoxedExpr *E) { if (ObjCMethodDecl *MD = E->getBoxingMethod()) { - SymbolRoleSet Roles = (unsigned)SymbolRole::Call; - Roles |= (unsigned)SymbolRole::Implicit; - return IndexCtx.handleReference(MD, E->getLocStart(), - Parent, ParentDC, Roles, {}, E); + return passObjCLiteralMethodCall(MD, E); } return true; } bool VisitObjCDictionaryLiteral(ObjCDictionaryLiteral *E) { if (ObjCMethodDecl *MD = E->getDictWithObjectsMethod()) { - SymbolRoleSet Roles = (unsigned)SymbolRole::Call; - Roles |= (unsigned)SymbolRole::Implicit; - return IndexCtx.handleReference(MD, E->getLocStart(), - Parent, ParentDC, Roles, {}, E); + return passObjCLiteralMethodCall(MD, E); } return true; } bool VisitObjCArrayLiteral(ObjCArrayLiteral *E) { if (ObjCMethodDecl *MD = E->getArrayWithObjectsMethod()) { - SymbolRoleSet Roles = (unsigned)SymbolRole::Call; - Roles |= (unsigned)SymbolRole::Implicit; - return IndexCtx.handleReference(MD, E->getLocStart(), - Parent, ParentDC, Roles, {}, E); + return passObjCLiteralMethodCall(MD, E); } return true; } bool VisitCXXConstructExpr(CXXConstructExpr *E) { + SymbolRoleSet Roles{}; + SmallVector Relations; + addCallRole(Roles, Relations); return IndexCtx.handleReference(E->getConstructor(), E->getLocation(), - Parent, ParentDC, (unsigned)SymbolRole::Call, {}, E); + Parent, ParentDC, Roles, Relations, E); } bool TraverseCXXOperatorCallExpr(CXXOperatorCallExpr *E, diff --git a/clang/lib/Index/IndexSymbol.cpp b/clang/lib/Index/IndexSymbol.cpp index 96da7a7..39812c4 100644 --- a/clang/lib/Index/IndexSymbol.cpp +++ b/clang/lib/Index/IndexSymbol.cpp @@ -206,6 +206,7 @@ void index::applyForEachSymbolRole(SymbolRoleSet Roles, APPLY_FOR_ROLE(RelationBaseOf); APPLY_FOR_ROLE(RelationOverrideOf); APPLY_FOR_ROLE(RelationReceivedBy); + APPLY_FOR_ROLE(RelationCalledBy); #undef APPLY_FOR_ROLE } @@ -231,6 +232,7 @@ void index::printSymbolRoles(SymbolRoleSet Roles, raw_ostream &OS) { case SymbolRole::RelationBaseOf: OS << "RelBase"; break; case SymbolRole::RelationOverrideOf: OS << "RelOver"; break; case SymbolRole::RelationReceivedBy: OS << "RelRec"; break; + case SymbolRole::RelationCalledBy: OS << "RelCall"; break; } }); } diff --git a/clang/test/Index/Core/index-source.m b/clang/test/Index/Core/index-source.m index 3307ec5..c2604f6 100644 --- a/clang/test/Index/Core/index-source.m +++ b/clang/test/Index/Core/index-source.m @@ -10,9 +10,11 @@ void foo(); // CHECK: [[@LINE+1]]:6 | function/C | goo | c:@F@goo | _goo | Def | rel: 0 void goo(Base *b) { - // CHECK: [[@LINE+1]]:3 | function/C | foo | c:@F@foo | _foo | Ref,Call | rel: 0 + // CHECK: [[@LINE+2]]:3 | function/C | foo | c:@F@foo | _foo | Ref,Call,RelCall | rel: 1 + // CHECK-NEXT: RelCall | goo | c:@F@goo foo(); - // CHECK: [[@LINE+2]]:6 | objc-instance-method/ObjC | meth | c:objc(cs)Base(im)meth | -[Base meth] | Ref,Call,Dyn,RelRec | rel: 1 + // CHECK: [[@LINE+3]]:6 | objc-instance-method/ObjC | meth | c:objc(cs)Base(im)meth | -[Base meth] | Ref,Call,Dyn,RelRec,RelCall | rel: 2 + // CHECK-NEXT: RelCall | goo | c:@F@goo // CHECK-NEXT: RelRec | Base | c:objc(cs)Base [b meth]; } -- 2.7.4