blob: 0cb5a3fe14ebc150e65561ba9975731299d73c6d [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//===----------------------------------------------------------------------===//
13#include "clang/Analysis/CallGraph.h"
Anna Zaksc000e7e2012-03-08 00:42:23 +000014#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/StmtVisitor.h"
Anna Zaks1ee76c12012-12-21 17:27:01 +000017#include "llvm/ADT/PostOrderIterator.h"
Anna Zaks5c32dfc2012-12-21 01:19:15 +000018#include "llvm/ADT/Statistic.h"
Anna Zaksc000e7e2012-03-08 00:42:23 +000019#include "llvm/Support/GraphWriter.h"
20
21using namespace clang;
22
Chandler Carruth10346662014-04-22 03:17:02 +000023#define DEBUG_TYPE "CallGraph"
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
Craig Topper25542942014-05-20 04:30:07 +000052 return nullptr;
Anna Zaks5c32dfc2012-12-21 01:19:15 +000053 }
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.
Craig Topper25542942014-05-20 04:30:07 +000073 Decl *D = nullptr;
Anna Zaks5c32dfc2012-12-21 01:19:15 +000074 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) {
Benjamin Kramer642f1732015-07-02 21:03:14 +000086 for (Stmt *SubStmt : S->children())
87 if (SubStmt)
88 this->Visit(SubStmt);
Anna Zaks8e078522012-04-12 22:36:48 +000089 }
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
Aaron Ballman629afae2014-03-07 19:56:05 +000098 for (auto *I : D->decls())
99 if (auto *DC = dyn_cast<DeclContext>(I))
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000100 addNodesForBlocks(DC);
101}
102
Anna Zaks8e078522012-04-12 22:36:48 +0000103CallGraph::CallGraph() {
Craig Topper25542942014-05-20 04:30:07 +0000104 Root = getOrInsertNode(nullptr);
Anna Zaks8e078522012-04-12 22:36:48 +0000105}
106
Justin Lebar5f3d1dc2016-10-10 16:26:48 +0000107CallGraph::~CallGraph() {}
Anna Zaks8e078522012-04-12 22:36:48 +0000108
109bool CallGraph::includeInGraph(const Decl *D) {
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000110 assert(D);
Anna Zaks87d404d2014-12-17 00:34:07 +0000111 if (!D->hasBody())
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000112 return false;
113
Anna Zaksc000e7e2012-03-08 00:42:23 +0000114 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.
Anna Zaks87d404d2014-12-17 00:34:07 +0000117 if (FD->isDependentContext())
Anna Zaksc000e7e2012-03-08 00:42:23 +0000118 return false;
119
120 IdentifierInfo *II = FD->getIdentifier();
121 if (II && II->getName().startswith("__inline"))
122 return false;
123 }
124
Anna Zaksc000e7e2012-03-08 00:42:23 +0000125 return true;
126}
127
Anna Zaks8e078522012-04-12 22:36:48 +0000128void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
Anna Zaks9a008bb2012-03-08 23:16:26 +0000129 assert(D);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000130
Anna Zaks8e078522012-04-12 22:36:48 +0000131 // Allocate a new node, mark it as root, and process it's calls.
132 CallGraphNode *Node = getOrInsertNode(D);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000133
134 // Process all the calls by this function as well.
Benjamin Kramerd1d76b22012-06-06 17:32:50 +0000135 CGBuilder builder(this, Node);
Ted Kremenek504957f2012-04-05 04:03:23 +0000136 if (Stmt *Body = D->getBody())
137 builder.Visit(Body);
Anna Zaksc000e7e2012-03-08 00:42:23 +0000138}
139
Anna Zaksc2555772012-03-09 21:13:53 +0000140CallGraphNode *CallGraph::getNode(const Decl *F) const {
Matt Beaumont-Gaydcc425e2012-03-14 20:21:25 +0000141 FunctionMapTy::const_iterator I = FunctionMap.find(F);
Craig Topper25542942014-05-20 04:30:07 +0000142 if (I == FunctionMap.end()) return nullptr;
Justin Lebar5f3d1dc2016-10-10 16:26:48 +0000143 return I->second.get();
Anna Zaksc2555772012-03-09 21:13:53 +0000144}
145
Anna Zaks8e078522012-04-12 22:36:48 +0000146CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
Anna Zaks87d404d2014-12-17 00:34:07 +0000147 if (F && !isa<ObjCMethodDecl>(F))
148 F = F->getCanonicalDecl();
149
Justin Lebar5f3d1dc2016-10-10 16:26:48 +0000150 std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
Anna Zaksc000e7e2012-03-08 00:42:23 +0000151 if (Node)
Justin Lebar5f3d1dc2016-10-10 16:26:48 +0000152 return Node.get();
Anna Zaksc000e7e2012-03-08 00:42:23 +0000153
Justin Lebar5f3d1dc2016-10-10 16:26:48 +0000154 Node = llvm::make_unique<CallGraphNode>(F);
Anna Zaks1ee76c12012-12-21 17:27:01 +0000155 // Make Root node a parent of all functions to make sure all are reachable.
Craig Topper25542942014-05-20 04:30:07 +0000156 if (F)
Justin Lebar5f3d1dc2016-10-10 16:26:48 +0000157 Root->addCallee(Node.get(), this);
158 return Node.get();
Anna Zaksc000e7e2012-03-08 00:42:23 +0000159}
160
161void CallGraph::print(raw_ostream &OS) const {
162 OS << " --- Call graph Dump --- \n";
Anna Zaks1ee76c12012-12-21 17:27:01 +0000163
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
Anna Zaksc000e7e2012-03-08 00:42:23 +0000171 OS << " Function: ";
Anna Zaks1ee76c12012-12-21 17:27:01 +0000172 if (N == Root)
Anna Zaksc000e7e2012-03-08 00:42:23 +0000173 OS << "< root >";
174 else
Anna Zaks1ee76c12012-12-21 17:27:01 +0000175 N->print(OS);
176
Anna Zaksc000e7e2012-03-08 00:42:23 +0000177 OS << " calls: ";
Anna Zaks1ee76c12012-12-21 17:27:01 +0000178 for (CallGraphNode::const_iterator CI = N->begin(),
179 CE = N->end(); CI != CE; ++CI) {
Anna Zaksc000e7e2012-03-08 00:42:23 +0000180 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
Yaron Kerencdae9412016-01-29 19:38:18 +0000189LLVM_DUMP_METHOD void CallGraph::dump() const {
Anna Zaksc000e7e2012-03-08 00:42:23 +0000190 print(llvm::errs());
191}
192
193void CallGraph::viewGraph() const {
194 llvm::ViewGraph(this, "CallGraph");
195}
196
Anna Zaksc000e7e2012-03-08 00:42:23 +0000197void CallGraphNode::print(raw_ostream &os) const {
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000198 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
199 return ND->printName(os);
200 os << "< >";
Anna Zaksc000e7e2012-03-08 00:42:23 +0000201}
202
Yaron Kerencdae9412016-01-29 19:38:18 +0000203LLVM_DUMP_METHOD void CallGraphNode::dump() const {
Anna Zaksc000e7e2012-03-08 00:42:23 +0000204 print(llvm::errs());
205}
206
207namespace llvm {
208
209template <>
210struct 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 }
Anna Zaks5c32dfc2012-12-21 01:19:15 +0000219 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
220 return ND->getNameAsString();
221 else
222 return "< >";
Anna Zaksc000e7e2012-03-08 00:42:23 +0000223 }
224
225};
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000226}