1 //== CallGraph.cpp - AST-based Call graph ----------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the AST-based CallGraph.
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"
21 using namespace clang;
23 #define DEBUG_TYPE "CallGraph"
25 STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
26 STATISTIC(NumBlockCallEdges, "Number of block call edges");
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> {
33 CallGraphNode *CallerNode;
36 CGBuilder(CallGraph *g, CallGraphNode *N)
37 : G(g), CallerNode(N) {}
39 void VisitStmt(Stmt *S) { VisitChildren(S); }
41 Decl *getDeclFromCall(CallExpr *CE) {
42 if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
45 // Simple detection of a call through a block.
46 Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
47 if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
49 return Block->getBlockDecl();
55 void addCalledDecl(Decl *D) {
56 if (G->includeInGraph(D)) {
57 CallGraphNode *CalleeNode = G->getOrInsertNode(D);
58 CallerNode->addCallee(CalleeNode, G);
62 void VisitCallExpr(CallExpr *CE) {
63 if (Decl *D = getDeclFromCall(CE))
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();
72 // Find the callee definition within the same translation unit.
74 if (ME->isInstanceMessage())
75 D = IDecl->lookupPrivateMethod(Sel);
77 D = IDecl->lookupPrivateClassMethod(Sel);
85 void VisitChildren(Stmt *S) {
86 for (Stmt::child_range I = S->children(); I; ++I)
88 static_cast<CGBuilder*>(this)->Visit(*I);
92 } // end anonymous namespace
94 void CallGraph::addNodesForBlocks(DeclContext *D) {
95 if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
96 addNodeForDecl(BD, true);
98 for (auto *I : D->decls())
99 if (auto *DC = dyn_cast<DeclContext>(I))
100 addNodesForBlocks(DC);
103 CallGraph::CallGraph() {
104 Root = getOrInsertNode(nullptr);
107 CallGraph::~CallGraph() {
108 llvm::DeleteContainerSeconds(FunctionMap);
111 bool CallGraph::includeInGraph(const Decl *D) {
116 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
117 // We skip function template definitions, as their semantics is
118 // only determined when they are instantiated.
119 if (FD->isDependentContext())
122 IdentifierInfo *II = FD->getIdentifier();
123 if (II && II->getName().startswith("__inline"))
130 void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
133 // Allocate a new node, mark it as root, and process it's calls.
134 CallGraphNode *Node = getOrInsertNode(D);
136 // Process all the calls by this function as well.
137 CGBuilder builder(this, Node);
138 if (Stmt *Body = D->getBody())
142 CallGraphNode *CallGraph::getNode(const Decl *F) const {
143 FunctionMapTy::const_iterator I = FunctionMap.find(F);
144 if (I == FunctionMap.end()) return nullptr;
148 CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
149 if (F && !isa<ObjCMethodDecl>(F))
150 F = F->getCanonicalDecl();
152 CallGraphNode *&Node = FunctionMap[F];
156 Node = new CallGraphNode(F);
157 // Make Root node a parent of all functions to make sure all are reachable.
159 Root->addCallee(Node, this);
163 void CallGraph::print(raw_ostream &OS) const {
164 OS << " --- Call graph Dump --- \n";
166 // We are going to print the graph in reverse post order, partially, to make
167 // sure the output is deterministic.
168 llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this);
169 for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator
170 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
171 const CallGraphNode *N = *I;
180 for (CallGraphNode::const_iterator CI = N->begin(),
181 CE = N->end(); CI != CE; ++CI) {
182 assert(*CI != Root && "No one can call the root node.");
191 void CallGraph::dump() const {
195 void CallGraph::viewGraph() const {
196 llvm::ViewGraph(this, "CallGraph");
199 void CallGraphNode::print(raw_ostream &os) const {
200 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
201 return ND->printName(os);
205 void CallGraphNode::dump() const {
212 struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
214 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
216 static std::string getNodeLabel(const CallGraphNode *Node,
217 const CallGraph *CG) {
218 if (CG->getRoot() == Node) {
221 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
222 return ND->getNameAsString();