blob: 24705e534b59df3b08ee1ce27dfc3514cf4f6ef9 [file] [log] [blame]
Anna Zaksc000e7e2012-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//===----------------------------------------------------------------------===//
Anna Zaks5c32dfc2012-12-21 01:19:15 +000013#define DEBUG_TYPE "CallGraph"
14
Anna Zaksc000e7e2012-03-08 00:42:23 +000015#include "clang/Analysis/CallGraph.h"
Anna Zaksc000e7e2012-03-08 00:42:23 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/StmtVisitor.h"
Anna Zaks1ee76c12012-12-21 17:27:01 +000019#include "llvm/ADT/PostOrderIterator.h"
Anna Zaks5c32dfc2012-12-21 01:19:15 +000020#include "llvm/ADT/Statistic.h"
Anna Zaksc000e7e2012-03-08 00:42:23 +000021#include "llvm/Support/GraphWriter.h"
22
23using namespace clang;
24
Anna Zaks41277502012-12-21 17:27:04 +000025STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
Anna Zaks5c32dfc2012-12-21 01:19:15 +000026STATISTIC(NumBlockCallEdges, "Number of block call edges");
27
Anna Zaks8e078522012-04-12 22:36:48 +000028namespace {
29/// A helper class, which walks the AST and locates all the call sites in the
30/// given function body.
31class CGBuilder : public StmtVisitor<CGBuilder> {
32 CallGraph *G;
Anna Zaks8e078522012-04-12 22:36:48 +000033 CallGraphNode *CallerNode;
34
35public:
Benjamin Kramerd1d76b22012-06-06 17:32:50 +000036 CGBuilder(CallGraph *g, CallGraphNode *N)
37 : G(g), CallerNode(N) {}
Anna Zaks8e078522012-04-12 22:36:48 +000038
39 void VisitStmt(Stmt *S) { VisitChildren(S); }
40
Anna Zaks5c32dfc2012-12-21 01:19:15 +000041 Decl *getDeclFromCall(CallExpr *CE) {
Anna Zaks8e078522012-04-12 22:36:48 +000042 if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
Anna Zaks5c32dfc2012-12-21 01:19:15 +000043 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 0;
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
Anna Zaks41277502012-12-21 17:27:04 +000072 // Find the callee definition within the same translation unit.
Anna Zaks5c32dfc2012-12-21 01:19:15 +000073 Decl *D = 0;
74 if (ME->isInstanceMessage())
75 D = IDecl->lookupPrivateMethod(Sel);
76 else
77 D = IDecl->lookupPrivateClassMethod(Sel);
78 if (D) {
79 addCalledDecl(D);
80 NumObjCCallEdges++;
Anna Zaks8e078522012-04-12 22:36:48 +000081 }
Anna Zaks5c32dfc2012-12-21 01:19:15 +000082 }
Anna Zaks8e078522012-04-12 22:36:48 +000083 }
84
85 void VisitChildren(Stmt *S) {
86 for (Stmt::child_range I = S->children(); I; ++I)
87 if (*I)
88 static_cast<CGBuilder*>(this)->Visit(*I);
89 }
90};
91
92} // end anonymous namespace
93
Anna Zaks5c32dfc2012-12-21 01:19:15 +000094void CallGraph::addNodesForBlocks(DeclContext *D) {
95 if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
96 addNodeForDecl(BD, true);
97
98 for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end();
99 I!=E; ++I)
100 if (DeclContext *DC = dyn_cast<DeclContext>(*I))
101 addNodesForBlocks(DC);
102}
103
Anna Zaks8e078522012-04-12 22:36:48 +0000104CallGraph::CallGraph() {
105 Root = getOrInsertNode(0);
106}
107
108CallGraph::~CallGraph() {
Reid Kleckner588c9372014-02-19 23:44:52 +0000109 llvm::DeleteContainerSeconds(FunctionMap);
Anna Zaks8e078522012-04-12 22:36:48 +0000110}
111
112bool CallGraph::includeInGraph(const Decl *D) {
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000113 assert(D);
114 if (!D->getBody())
115 return false;
116
Anna Zaksc000e7e2012-03-08 00:42:23 +0000117 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
118 // We skip function template definitions, as their semantics is
119 // only determined when they are instantiated.
120 if (!FD->isThisDeclarationADefinition() ||
121 FD->isDependentContext())
122 return false;
123
124 IdentifierInfo *II = FD->getIdentifier();
125 if (II && II->getName().startswith("__inline"))
126 return false;
127 }
128
129 if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {
130 if (!ID->isThisDeclarationADefinition())
131 return false;
132 }
133
134 return true;
135}
136
Anna Zaks8e078522012-04-12 22:36:48 +0000137void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
Anna Zaks9a008bb2012-03-08 23:16:26 +0000138 assert(D);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000139
Anna Zaks8e078522012-04-12 22:36:48 +0000140 // Allocate a new node, mark it as root, and process it's calls.
141 CallGraphNode *Node = getOrInsertNode(D);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000142
143 // Process all the calls by this function as well.
Benjamin Kramerd1d76b22012-06-06 17:32:50 +0000144 CGBuilder builder(this, Node);
Ted Kremenek504957f2012-04-05 04:03:23 +0000145 if (Stmt *Body = D->getBody())
146 builder.Visit(Body);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000147}
148
Anna Zaksc2555772012-03-09 21:13:53 +0000149CallGraphNode *CallGraph::getNode(const Decl *F) const {
Matt Beaumont-Gaydcc425e2012-03-14 20:21:25 +0000150 FunctionMapTy::const_iterator I = FunctionMap.find(F);
151 if (I == FunctionMap.end()) return 0;
152 return I->second;
Anna Zaksc2555772012-03-09 21:13:53 +0000153}
154
Anna Zaks8e078522012-04-12 22:36:48 +0000155CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
Anna Zaksc000e7e2012-03-08 00:42:23 +0000156 CallGraphNode *&Node = FunctionMap[F];
157 if (Node)
158 return Node;
159
160 Node = new CallGraphNode(F);
Anna Zaks1ee76c12012-12-21 17:27:01 +0000161 // Make Root node a parent of all functions to make sure all are reachable.
Anna Zaks9a008bb2012-03-08 23:16:26 +0000162 if (F != 0)
Anna Zaks1ee76c12012-12-21 17:27:01 +0000163 Root->addCallee(Node, this);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000164 return Node;
165}
166
167void CallGraph::print(raw_ostream &OS) const {
168 OS << " --- Call graph Dump --- \n";
Anna Zaks1ee76c12012-12-21 17:27:01 +0000169
170 // We are going to print the graph in reverse post order, partially, to make
171 // sure the output is deterministic.
172 llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this);
173 for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator
174 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
175 const CallGraphNode *N = *I;
176
Anna Zaksc000e7e2012-03-08 00:42:23 +0000177 OS << " Function: ";
Anna Zaks1ee76c12012-12-21 17:27:01 +0000178 if (N == Root)
Anna Zaksc000e7e2012-03-08 00:42:23 +0000179 OS << "< root >";
180 else
Anna Zaks1ee76c12012-12-21 17:27:01 +0000181 N->print(OS);
182
Anna Zaksc000e7e2012-03-08 00:42:23 +0000183 OS << " calls: ";
Anna Zaks1ee76c12012-12-21 17:27:01 +0000184 for (CallGraphNode::const_iterator CI = N->begin(),
185 CE = N->end(); CI != CE; ++CI) {
Anna Zaksc000e7e2012-03-08 00:42:23 +0000186 assert(*CI != Root && "No one can call the root node.");
187 (*CI)->print(OS);
188 OS << " ";
189 }
190 OS << '\n';
191 }
192 OS.flush();
193}
194
195void CallGraph::dump() const {
196 print(llvm::errs());
197}
198
199void CallGraph::viewGraph() const {
200 llvm::ViewGraph(this, "CallGraph");
201}
202
Anna Zaksc000e7e2012-03-08 00:42:23 +0000203void CallGraphNode::print(raw_ostream &os) const {
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000204 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
205 return ND->printName(os);
206 os << "< >";
Anna Zaksc000e7e2012-03-08 00:42:23 +0000207}
208
209void CallGraphNode::dump() const {
210 print(llvm::errs());
211}
212
213namespace llvm {
214
215template <>
216struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
217
218 DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
219
220 static std::string getNodeLabel(const CallGraphNode *Node,
221 const CallGraph *CG) {
222 if (CG->getRoot() == Node) {
223 return "< root >";
224 }
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000225 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
226 return ND->getNameAsString();
227 else
228 return "< >";
Anna Zaksc000e7e2012-03-08 00:42:23 +0000229 }
230
231};
232}