diff --git a/clang/include/clang/Index/ASTLocation.h b/clang/include/clang/Index/ASTLocation.h index 6058623d768a..303d95ecc288 100644 --- a/clang/include/clang/Index/ASTLocation.h +++ b/clang/include/clang/Index/ASTLocation.h @@ -42,7 +42,11 @@ class ASTLocation { public: ASTLocation() : D(0), Stm(0) {} - explicit ASTLocation(const Decl *d, const Stmt *stm = 0); + explicit ASTLocation(const Decl *d, const Stmt *stm = 0) + : D(const_cast(d)), Stm(const_cast(stm)) { + assert((Stm == 0 || isImmediateParent(D, Stm)) && + "The Decl is not the immediate parent of the Stmt."); + } const Decl *getDecl() const { return D; } const Stmt *getStmt() const { return Stm; } @@ -58,6 +62,7 @@ public: /// \brief Checks that D is the immediate Decl parent of Node. static bool isImmediateParent(Decl *D, Stmt *Node); + static Decl *FindImmediateParent(Decl *D, Stmt *Node); friend bool operator==(const ASTLocation &L, const ASTLocation &R) { return L.D == R.D && L.Stm == R.Stm; diff --git a/clang/lib/Analysis/CallGraph.cpp b/clang/lib/Analysis/CallGraph.cpp index d49e8ec11be0..2ec6d2014d8e 100644 --- a/clang/lib/Analysis/CallGraph.cpp +++ b/clang/lib/Analysis/CallGraph.cpp @@ -69,7 +69,10 @@ void CGBuilder::VisitCallExpr(CallExpr *CE) { if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) { Entity *Ent = Entity::get(CalleeDecl, G.getProgram()); CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent); - CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode); + + Decl *Parent = ASTLocation::FindImmediateParent(FD, CE); + + CallerNode->addCallee(ASTLocation(Parent, CE), CalleeNode); } } diff --git a/clang/lib/Index/ASTLocation.cpp b/clang/lib/Index/ASTLocation.cpp index 3beff3f31a8f..41846055da09 100644 --- a/clang/lib/Index/ASTLocation.cpp +++ b/clang/lib/Index/ASTLocation.cpp @@ -33,7 +33,7 @@ static bool isContainedInStatement(Stmt *Node, Stmt *Parent) { return false; } -static Decl *FindImmediateParent(Decl *D, Stmt *Node) { +Decl *ASTLocation::FindImmediateParent(Decl *D, Stmt *Node) { assert(D && Node && "Passed null Decl or null Stmt"); if (VarDecl *VD = dyn_cast(D)) { @@ -61,16 +61,6 @@ static Decl *FindImmediateParent(Decl *D, Stmt *Node) { return 0; } -ASTLocation::ASTLocation(const Decl *d, const Stmt *stm) - : D(const_cast(d)), Stm(const_cast(stm)) { - if (Stm) { - Decl *Parent = FindImmediateParent(D, Stm); - assert(Parent); - D = Parent; - } -} - - bool ASTLocation::isImmediateParent(Decl *D, Stmt *Node) { assert(D && Node && "Passed null Decl or null Stmt"); return D == FindImmediateParent(D, Node);