1 //== CallGraph.cpp - AST-based Call graph ----------------------*- C++ -*--==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the AST-based CallGraph. 11 // 12 //===----------------------------------------------------------------------===// 13 #include "clang/Analysis/CallGraph.h" 14 #include "clang/AST/ASTContext.h" 15 #include "clang/AST/Decl.h" 16 #include "clang/AST/StmtVisitor.h" 17 #include "llvm/ADT/PostOrderIterator.h" 18 #include "llvm/ADT/Statistic.h" 19 #include "llvm/Support/GraphWriter.h" 20 21 using namespace clang; 22 23 #define DEBUG_TYPE "CallGraph" 24 25 STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges"); 26 STATISTIC(NumBlockCallEdges, "Number of block call edges"); 27 28 namespace { 29 /// A helper class, which walks the AST and locates all the call sites in the 30 /// given function body. 31 class CGBuilder : public StmtVisitor<CGBuilder> { 32 CallGraph *G; 33 CallGraphNode *CallerNode; 34 35 public: 36 CGBuilder(CallGraph *g, CallGraphNode *N) 37 : G(g), CallerNode(N) {} 38 39 void VisitStmt(Stmt *S) { VisitChildren(S); } 40 41 Decl *getDeclFromCall(CallExpr *CE) { 42 if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) 43 return CalleeDecl; 44 45 // Simple detection of a call through a block. 46 Expr *CEE = CE->getCallee()->IgnoreParenImpCasts(); 47 if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) { 48 NumBlockCallEdges++; 49 return Block->getBlockDecl(); 50 } 51 52 return nullptr; 53 } 54 55 void addCalledDecl(Decl *D) { 56 if (G->includeInGraph(D)) { 57 CallGraphNode *CalleeNode = G->getOrInsertNode(D); 58 CallerNode->addCallee(CalleeNode, G); 59 } 60 } 61 62 void VisitCallExpr(CallExpr *CE) { 63 if (Decl *D = getDeclFromCall(CE)) 64 addCalledDecl(D); 65 } 66 67 // Adds may-call edges for the ObjC message sends. 68 void VisitObjCMessageExpr(ObjCMessageExpr *ME) { 69 if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) { 70 Selector Sel = ME->getSelector(); 71 72 // Find the callee definition within the same translation unit. 73 Decl *D = nullptr; 74 if (ME->isInstanceMessage()) 75 D = IDecl->lookupPrivateMethod(Sel); 76 else 77 D = IDecl->lookupPrivateClassMethod(Sel); 78 if (D) { 79 addCalledDecl(D); 80 NumObjCCallEdges++; 81 } 82 } 83 } 84 85 void VisitChildren(Stmt *S) { 86 for (Stmt *SubStmt : S->children()) 87 if (SubStmt) 88 this->Visit(SubStmt); 89 } 90 }; 91 92 } // end anonymous namespace 93 94 void CallGraph::addNodesForBlocks(DeclContext *D) { 95 if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) 96 addNodeForDecl(BD, true); 97 98 for (auto *I : D->decls()) 99 if (auto *DC = dyn_cast<DeclContext>(I)) 100 addNodesForBlocks(DC); 101 } 102 103 CallGraph::CallGraph() { 104 Root = getOrInsertNode(nullptr); 105 } 106 107 CallGraph::~CallGraph() {} 108 109 bool CallGraph::includeInGraph(const Decl *D) { 110 assert(D); 111 if (!D->hasBody()) 112 return false; 113 114 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 115 // We skip function template definitions, as their semantics is 116 // only determined when they are instantiated. 117 if (FD->isDependentContext()) 118 return false; 119 120 IdentifierInfo *II = FD->getIdentifier(); 121 if (II && II->getName().startswith("__inline")) 122 return false; 123 } 124 125 return true; 126 } 127 128 void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) { 129 assert(D); 130 131 // Allocate a new node, mark it as root, and process it's calls. 132 CallGraphNode *Node = getOrInsertNode(D); 133 134 // Process all the calls by this function as well. 135 CGBuilder builder(this, Node); 136 if (Stmt *Body = D->getBody()) 137 builder.Visit(Body); 138 } 139 140 CallGraphNode *CallGraph::getNode(const Decl *F) const { 141 FunctionMapTy::const_iterator I = FunctionMap.find(F); 142 if (I == FunctionMap.end()) return nullptr; 143 return I->second.get(); 144 } 145 146 CallGraphNode *CallGraph::getOrInsertNode(Decl *F) { 147 if (F && !isa<ObjCMethodDecl>(F)) 148 F = F->getCanonicalDecl(); 149 150 std::unique_ptr<CallGraphNode> &Node = FunctionMap[F]; 151 if (Node) 152 return Node.get(); 153 154 Node = llvm::make_unique<CallGraphNode>(F); 155 // Make Root node a parent of all functions to make sure all are reachable. 156 if (F) 157 Root->addCallee(Node.get(), this); 158 return Node.get(); 159 } 160 161 void CallGraph::print(raw_ostream &OS) const { 162 OS << " --- Call graph Dump --- \n"; 163 164 // We are going to print the graph in reverse post order, partially, to make 165 // sure the output is deterministic. 166 llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this); 167 for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator 168 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { 169 const CallGraphNode *N = *I; 170 171 OS << " Function: "; 172 if (N == Root) 173 OS << "< root >"; 174 else 175 N->print(OS); 176 177 OS << " calls: "; 178 for (CallGraphNode::const_iterator CI = N->begin(), 179 CE = N->end(); CI != CE; ++CI) { 180 assert(*CI != Root && "No one can call the root node."); 181 (*CI)->print(OS); 182 OS << " "; 183 } 184 OS << '\n'; 185 } 186 OS.flush(); 187 } 188 189 LLVM_DUMP_METHOD void CallGraph::dump() const { 190 print(llvm::errs()); 191 } 192 193 void CallGraph::viewGraph() const { 194 llvm::ViewGraph(this, "CallGraph"); 195 } 196 197 void CallGraphNode::print(raw_ostream &os) const { 198 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD)) 199 return ND->printName(os); 200 os << "< >"; 201 } 202 203 LLVM_DUMP_METHOD void CallGraphNode::dump() const { 204 print(llvm::errs()); 205 } 206 207 namespace llvm { 208 209 template <> 210 struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits { 211 212 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 213 214 static std::string getNodeLabel(const CallGraphNode *Node, 215 const CallGraph *CG) { 216 if (CG->getRoot() == Node) { 217 return "< root >"; 218 } 219 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl())) 220 return ND->getNameAsString(); 221 else 222 return "< >"; 223 } 224 225 }; 226 } 227