blob: 1e9aec23dd62ce6dd4a936c0be981627ee45f87b [file] [log] [blame]
Anna Zaks196b8cf2012-03-08 00:42:23 +00001//== 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
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Decl.h"
Anna Zaksa2e589e2012-03-13 19:32:08 +000017#include "clang/AST/RecursiveASTVisitor.h"
Anna Zaks196b8cf2012-03-08 00:42:23 +000018#include "clang/AST/StmtVisitor.h"
19
20#include "llvm/Support/GraphWriter.h"
21
22using namespace clang;
23
24/// Determine if a declaration should be included in the graph.
25static bool includeInGraph(const Decl *D) {
26 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
27 // We skip function template definitions, as their semantics is
28 // only determined when they are instantiated.
29 if (!FD->isThisDeclarationADefinition() ||
30 FD->isDependentContext())
31 return false;
32
33 IdentifierInfo *II = FD->getIdentifier();
34 if (II && II->getName().startswith("__inline"))
35 return false;
36 }
37
38 if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {
39 if (!ID->isThisDeclarationADefinition())
40 return false;
41 }
42
43 return true;
44}
45
46namespace {
47/// A helper class, which walks the AST and locates all the call sites in the
48/// given function body.
49class CGBuilder : public StmtVisitor<CGBuilder> {
50 CallGraph *G;
51 const Decl *FD;
52 CallGraphNode *CallerNode;
53
54public:
55 CGBuilder(CallGraph *g, const Decl *D, CallGraphNode *N)
56 : G(g), FD(D), CallerNode(N) {}
57
58 void VisitStmt(Stmt *S) { VisitChildren(S); }
59
60 void VisitCallExpr(CallExpr *CE) {
61 // TODO: We need to handle ObjC method calls as well.
62 if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
63 if (includeInGraph(CalleeDecl)) {
64 CallGraphNode *CalleeNode = G->getOrInsertFunction(CalleeDecl);
65 CallerNode->addCallee(CalleeNode, G);
66 }
Daniel Dunbara50a5cd2012-03-08 20:28:59 +000067 }
Anna Zaks196b8cf2012-03-08 00:42:23 +000068
69 void VisitChildren(Stmt *S) {
70 for (Stmt::child_range I = S->children(); I; ++I)
71 if (*I)
72 static_cast<CGBuilder*>(this)->Visit(*I);
73 }
74};
Anna Zaksa2e589e2012-03-13 19:32:08 +000075
76/// A helper class which walks the AST declarations.
77// TODO: We might want to specialize the visitor to shrink the call graph.
78// For example, we might not want to include the inline methods from header
79// files.
80class CGDeclVisitor : public RecursiveASTVisitor<CGDeclVisitor> {
81 CallGraph *CG;
82
83public:
84 CGDeclVisitor(CallGraph * InCG) : CG(InCG) {}
85
86 bool VisitFunctionDecl(FunctionDecl *FD) {
87 // We skip function template definitions, as their semantics is
88 // only determined when they are instantiated.
89 if (includeInGraph(FD))
90 // If this function has external linkage, anything could call it.
91 // Note, we are not precise here. For example, the function could have
92 // its address taken.
93 CG->addToCallGraph(FD, FD->isGlobal());
94 return true;
95 }
96
97 bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
98 if (includeInGraph(MD))
99 CG->addToCallGraph(MD, true);
100 return true;
101 }
102};
103
Anna Zaks196b8cf2012-03-08 00:42:23 +0000104} // end anonymous namespace
105
106CallGraph::CallGraph() {
107 Root = getOrInsertFunction(0);
108}
109
110CallGraph::~CallGraph() {
111 if (!FunctionMap.empty()) {
112 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
113 I != E; ++I)
114 delete I->second;
115 FunctionMap.clear();
116 }
117}
118
119void CallGraph::addToCallGraph(Decl* D, bool IsGlobal) {
Anna Zaksd2776612012-03-08 23:16:26 +0000120 assert(D);
Anna Zaks196b8cf2012-03-08 00:42:23 +0000121 CallGraphNode *Node = getOrInsertFunction(D);
122
123 if (IsGlobal)
124 Root->addCallee(Node, this);
125
126 // Process all the calls by this function as well.
127 CGBuilder builder(this, D, Node);
128 builder.Visit(D->getBody());
129}
130
Anna Zaksa2e589e2012-03-13 19:32:08 +0000131void CallGraph::addToCallGraph(TranslationUnitDecl *TU) {
132 CGDeclVisitor(this).TraverseDecl(TU);
Anna Zaks196b8cf2012-03-08 00:42:23 +0000133}
134
Anna Zaksa5d531f2012-03-09 21:13:53 +0000135CallGraphNode *CallGraph::getNode(const Decl *F) const {
136 return FunctionMap.find(F)->second;
137}
138
Anna Zaks196b8cf2012-03-08 00:42:23 +0000139CallGraphNode *CallGraph::getOrInsertFunction(Decl *F) {
140 CallGraphNode *&Node = FunctionMap[F];
141 if (Node)
142 return Node;
143
144 Node = new CallGraphNode(F);
Anna Zaksd2776612012-03-08 23:16:26 +0000145 // If not root, add to the parentless list.
146 if (F != 0)
147 ParentlessNodes.insert(Node);
Anna Zaks196b8cf2012-03-08 00:42:23 +0000148 return Node;
149}
150
151void CallGraph::print(raw_ostream &OS) const {
152 OS << " --- Call graph Dump --- \n";
153 for (const_iterator I = begin(), E = end(); I != E; ++I) {
154 OS << " Function: ";
155 if (I->second == Root)
156 OS << "< root >";
157 else
158 I->second->print(OS);
159 OS << " calls: ";
160 for (CallGraphNode::iterator CI = I->second->begin(),
161 CE = I->second->end(); CI != CE; ++CI) {
162 assert(*CI != Root && "No one can call the root node.");
163 (*CI)->print(OS);
164 OS << " ";
165 }
166 OS << '\n';
167 }
168 OS.flush();
169}
170
171void CallGraph::dump() const {
172 print(llvm::errs());
173}
174
175void CallGraph::viewGraph() const {
176 llvm::ViewGraph(this, "CallGraph");
177}
178
179StringRef CallGraphNode::getName() const {
180 if (const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(FD))
181 if (const IdentifierInfo *II = D->getIdentifier())
182 return II->getName();
183 return "< >";
184}
185
186void CallGraphNode::print(raw_ostream &os) const {
187 os << getName();
188}
189
190void CallGraphNode::dump() const {
191 print(llvm::errs());
192}
193
194namespace llvm {
195
196template <>
197struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
198
199 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
200
201 static std::string getNodeLabel(const CallGraphNode *Node,
202 const CallGraph *CG) {
203 if (CG->getRoot() == Node) {
204 return "< root >";
205 }
206 return Node->getName();
207 }
208
209};
210}