clang API Documentation
00001 //== CallGraph.cpp - AST-based Call graph ----------------------*- C++ -*--==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the AST-based CallGraph. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 #include "clang/Analysis/CallGraph.h" 00014 #include "clang/AST/ASTContext.h" 00015 #include "clang/AST/Decl.h" 00016 #include "clang/AST/StmtVisitor.h" 00017 #include "llvm/ADT/PostOrderIterator.h" 00018 #include "llvm/ADT/Statistic.h" 00019 #include "llvm/Support/GraphWriter.h" 00020 00021 using namespace clang; 00022 00023 #define DEBUG_TYPE "CallGraph" 00024 00025 STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges"); 00026 STATISTIC(NumBlockCallEdges, "Number of block call edges"); 00027 00028 namespace { 00029 /// A helper class, which walks the AST and locates all the call sites in the 00030 /// given function body. 00031 class CGBuilder : public StmtVisitor<CGBuilder> { 00032 CallGraph *G; 00033 CallGraphNode *CallerNode; 00034 00035 public: 00036 CGBuilder(CallGraph *g, CallGraphNode *N) 00037 : G(g), CallerNode(N) {} 00038 00039 void VisitStmt(Stmt *S) { VisitChildren(S); } 00040 00041 Decl *getDeclFromCall(CallExpr *CE) { 00042 if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) 00043 return CalleeDecl; 00044 00045 // Simple detection of a call through a block. 00046 Expr *CEE = CE->getCallee()->IgnoreParenImpCasts(); 00047 if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) { 00048 NumBlockCallEdges++; 00049 return Block->getBlockDecl(); 00050 } 00051 00052 return nullptr; 00053 } 00054 00055 void addCalledDecl(Decl *D) { 00056 if (G->includeInGraph(D)) { 00057 CallGraphNode *CalleeNode = G->getOrInsertNode(D); 00058 CallerNode->addCallee(CalleeNode, G); 00059 } 00060 } 00061 00062 void VisitCallExpr(CallExpr *CE) { 00063 if (Decl *D = getDeclFromCall(CE)) 00064 addCalledDecl(D); 00065 } 00066 00067 // Adds may-call edges for the ObjC message sends. 00068 void VisitObjCMessageExpr(ObjCMessageExpr *ME) { 00069 if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) { 00070 Selector Sel = ME->getSelector(); 00071 00072 // Find the callee definition within the same translation unit. 00073 Decl *D = nullptr; 00074 if (ME->isInstanceMessage()) 00075 D = IDecl->lookupPrivateMethod(Sel); 00076 else 00077 D = IDecl->lookupPrivateClassMethod(Sel); 00078 if (D) { 00079 addCalledDecl(D); 00080 NumObjCCallEdges++; 00081 } 00082 } 00083 } 00084 00085 void VisitChildren(Stmt *S) { 00086 for (Stmt::child_range I = S->children(); I; ++I) 00087 if (*I) 00088 static_cast<CGBuilder*>(this)->Visit(*I); 00089 } 00090 }; 00091 00092 } // end anonymous namespace 00093 00094 void CallGraph::addNodesForBlocks(DeclContext *D) { 00095 if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) 00096 addNodeForDecl(BD, true); 00097 00098 for (auto *I : D->decls()) 00099 if (auto *DC = dyn_cast<DeclContext>(I)) 00100 addNodesForBlocks(DC); 00101 } 00102 00103 CallGraph::CallGraph() { 00104 Root = getOrInsertNode(nullptr); 00105 } 00106 00107 CallGraph::~CallGraph() { 00108 llvm::DeleteContainerSeconds(FunctionMap); 00109 } 00110 00111 bool CallGraph::includeInGraph(const Decl *D) { 00112 assert(D); 00113 if (!D->getBody()) 00114 return false; 00115 00116 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { 00117 // We skip function template definitions, as their semantics is 00118 // only determined when they are instantiated. 00119 if (!FD->isThisDeclarationADefinition() || 00120 FD->isDependentContext()) 00121 return false; 00122 00123 IdentifierInfo *II = FD->getIdentifier(); 00124 if (II && II->getName().startswith("__inline")) 00125 return false; 00126 } 00127 00128 if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) { 00129 if (!ID->isThisDeclarationADefinition()) 00130 return false; 00131 } 00132 00133 return true; 00134 } 00135 00136 void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) { 00137 assert(D); 00138 00139 // Allocate a new node, mark it as root, and process it's calls. 00140 CallGraphNode *Node = getOrInsertNode(D); 00141 00142 // Process all the calls by this function as well. 00143 CGBuilder builder(this, Node); 00144 if (Stmt *Body = D->getBody()) 00145 builder.Visit(Body); 00146 } 00147 00148 CallGraphNode *CallGraph::getNode(const Decl *F) const { 00149 FunctionMapTy::const_iterator I = FunctionMap.find(F); 00150 if (I == FunctionMap.end()) return nullptr; 00151 return I->second; 00152 } 00153 00154 CallGraphNode *CallGraph::getOrInsertNode(Decl *F) { 00155 CallGraphNode *&Node = FunctionMap[F]; 00156 if (Node) 00157 return Node; 00158 00159 Node = new CallGraphNode(F); 00160 // Make Root node a parent of all functions to make sure all are reachable. 00161 if (F) 00162 Root->addCallee(Node, this); 00163 return Node; 00164 } 00165 00166 void CallGraph::print(raw_ostream &OS) const { 00167 OS << " --- Call graph Dump --- \n"; 00168 00169 // We are going to print the graph in reverse post order, partially, to make 00170 // sure the output is deterministic. 00171 llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this); 00172 for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator 00173 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { 00174 const CallGraphNode *N = *I; 00175 00176 OS << " Function: "; 00177 if (N == Root) 00178 OS << "< root >"; 00179 else 00180 N->print(OS); 00181 00182 OS << " calls: "; 00183 for (CallGraphNode::const_iterator CI = N->begin(), 00184 CE = N->end(); CI != CE; ++CI) { 00185 assert(*CI != Root && "No one can call the root node."); 00186 (*CI)->print(OS); 00187 OS << " "; 00188 } 00189 OS << '\n'; 00190 } 00191 OS.flush(); 00192 } 00193 00194 void CallGraph::dump() const { 00195 print(llvm::errs()); 00196 } 00197 00198 void CallGraph::viewGraph() const { 00199 llvm::ViewGraph(this, "CallGraph"); 00200 } 00201 00202 void CallGraphNode::print(raw_ostream &os) const { 00203 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD)) 00204 return ND->printName(os); 00205 os << "< >"; 00206 } 00207 00208 void CallGraphNode::dump() const { 00209 print(llvm::errs()); 00210 } 00211 00212 namespace llvm { 00213 00214 template <> 00215 struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits { 00216 00217 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} 00218 00219 static std::string getNodeLabel(const CallGraphNode *Node, 00220 const CallGraph *CG) { 00221 if (CG->getRoot() == Node) { 00222 return "< root >"; 00223 } 00224 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl())) 00225 return ND->getNameAsString(); 00226 else 00227 return "< >"; 00228 } 00229 00230 }; 00231 }