1f4a2713aSLionel Sambuc //== CallGraph.cpp - AST-based Call graph ----------------------*- C++ -*--==//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the AST-based CallGraph.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc #include "clang/Analysis/CallGraph.h"
14f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
15f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
16f4a2713aSLionel Sambuc #include "clang/AST/StmtVisitor.h"
17f4a2713aSLionel Sambuc #include "llvm/ADT/PostOrderIterator.h"
18f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/GraphWriter.h"
20f4a2713aSLionel Sambuc
21f4a2713aSLionel Sambuc using namespace clang;
22f4a2713aSLionel Sambuc
23*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "CallGraph"
24*0a6a1f1dSLionel Sambuc
25f4a2713aSLionel Sambuc STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
26f4a2713aSLionel Sambuc STATISTIC(NumBlockCallEdges, "Number of block call edges");
27f4a2713aSLionel Sambuc
28f4a2713aSLionel Sambuc namespace {
29f4a2713aSLionel Sambuc /// A helper class, which walks the AST and locates all the call sites in the
30f4a2713aSLionel Sambuc /// given function body.
31f4a2713aSLionel Sambuc class CGBuilder : public StmtVisitor<CGBuilder> {
32f4a2713aSLionel Sambuc CallGraph *G;
33f4a2713aSLionel Sambuc CallGraphNode *CallerNode;
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc public:
CGBuilder(CallGraph * g,CallGraphNode * N)36f4a2713aSLionel Sambuc CGBuilder(CallGraph *g, CallGraphNode *N)
37f4a2713aSLionel Sambuc : G(g), CallerNode(N) {}
38f4a2713aSLionel Sambuc
VisitStmt(Stmt * S)39f4a2713aSLionel Sambuc void VisitStmt(Stmt *S) { VisitChildren(S); }
40f4a2713aSLionel Sambuc
getDeclFromCall(CallExpr * CE)41f4a2713aSLionel Sambuc Decl *getDeclFromCall(CallExpr *CE) {
42f4a2713aSLionel Sambuc if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
43f4a2713aSLionel Sambuc return CalleeDecl;
44f4a2713aSLionel Sambuc
45f4a2713aSLionel Sambuc // Simple detection of a call through a block.
46f4a2713aSLionel Sambuc Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
47f4a2713aSLionel Sambuc if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
48f4a2713aSLionel Sambuc NumBlockCallEdges++;
49f4a2713aSLionel Sambuc return Block->getBlockDecl();
50f4a2713aSLionel Sambuc }
51f4a2713aSLionel Sambuc
52*0a6a1f1dSLionel Sambuc return nullptr;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc
addCalledDecl(Decl * D)55f4a2713aSLionel Sambuc void addCalledDecl(Decl *D) {
56f4a2713aSLionel Sambuc if (G->includeInGraph(D)) {
57f4a2713aSLionel Sambuc CallGraphNode *CalleeNode = G->getOrInsertNode(D);
58f4a2713aSLionel Sambuc CallerNode->addCallee(CalleeNode, G);
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc
VisitCallExpr(CallExpr * CE)62f4a2713aSLionel Sambuc void VisitCallExpr(CallExpr *CE) {
63f4a2713aSLionel Sambuc if (Decl *D = getDeclFromCall(CE))
64f4a2713aSLionel Sambuc addCalledDecl(D);
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc
67f4a2713aSLionel Sambuc // Adds may-call edges for the ObjC message sends.
VisitObjCMessageExpr(ObjCMessageExpr * ME)68f4a2713aSLionel Sambuc void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
69f4a2713aSLionel Sambuc if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
70f4a2713aSLionel Sambuc Selector Sel = ME->getSelector();
71f4a2713aSLionel Sambuc
72f4a2713aSLionel Sambuc // Find the callee definition within the same translation unit.
73*0a6a1f1dSLionel Sambuc Decl *D = nullptr;
74f4a2713aSLionel Sambuc if (ME->isInstanceMessage())
75f4a2713aSLionel Sambuc D = IDecl->lookupPrivateMethod(Sel);
76f4a2713aSLionel Sambuc else
77f4a2713aSLionel Sambuc D = IDecl->lookupPrivateClassMethod(Sel);
78f4a2713aSLionel Sambuc if (D) {
79f4a2713aSLionel Sambuc addCalledDecl(D);
80f4a2713aSLionel Sambuc NumObjCCallEdges++;
81f4a2713aSLionel Sambuc }
82f4a2713aSLionel Sambuc }
83f4a2713aSLionel Sambuc }
84f4a2713aSLionel Sambuc
VisitChildren(Stmt * S)85f4a2713aSLionel Sambuc void VisitChildren(Stmt *S) {
86f4a2713aSLionel Sambuc for (Stmt::child_range I = S->children(); I; ++I)
87f4a2713aSLionel Sambuc if (*I)
88f4a2713aSLionel Sambuc static_cast<CGBuilder*>(this)->Visit(*I);
89f4a2713aSLionel Sambuc }
90f4a2713aSLionel Sambuc };
91f4a2713aSLionel Sambuc
92f4a2713aSLionel Sambuc } // end anonymous namespace
93f4a2713aSLionel Sambuc
addNodesForBlocks(DeclContext * D)94f4a2713aSLionel Sambuc void CallGraph::addNodesForBlocks(DeclContext *D) {
95f4a2713aSLionel Sambuc if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
96f4a2713aSLionel Sambuc addNodeForDecl(BD, true);
97f4a2713aSLionel Sambuc
98*0a6a1f1dSLionel Sambuc for (auto *I : D->decls())
99*0a6a1f1dSLionel Sambuc if (auto *DC = dyn_cast<DeclContext>(I))
100f4a2713aSLionel Sambuc addNodesForBlocks(DC);
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc
CallGraph()103f4a2713aSLionel Sambuc CallGraph::CallGraph() {
104*0a6a1f1dSLionel Sambuc Root = getOrInsertNode(nullptr);
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc
~CallGraph()107f4a2713aSLionel Sambuc CallGraph::~CallGraph() {
108*0a6a1f1dSLionel Sambuc llvm::DeleteContainerSeconds(FunctionMap);
109f4a2713aSLionel Sambuc }
110f4a2713aSLionel Sambuc
includeInGraph(const Decl * D)111f4a2713aSLionel Sambuc bool CallGraph::includeInGraph(const Decl *D) {
112f4a2713aSLionel Sambuc assert(D);
113*0a6a1f1dSLionel Sambuc if (!D->hasBody())
114f4a2713aSLionel Sambuc return false;
115f4a2713aSLionel Sambuc
116f4a2713aSLionel Sambuc if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
117f4a2713aSLionel Sambuc // We skip function template definitions, as their semantics is
118f4a2713aSLionel Sambuc // only determined when they are instantiated.
119*0a6a1f1dSLionel Sambuc if (FD->isDependentContext())
120f4a2713aSLionel Sambuc return false;
121f4a2713aSLionel Sambuc
122f4a2713aSLionel Sambuc IdentifierInfo *II = FD->getIdentifier();
123f4a2713aSLionel Sambuc if (II && II->getName().startswith("__inline"))
124f4a2713aSLionel Sambuc return false;
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc
127f4a2713aSLionel Sambuc return true;
128f4a2713aSLionel Sambuc }
129f4a2713aSLionel Sambuc
addNodeForDecl(Decl * D,bool IsGlobal)130f4a2713aSLionel Sambuc void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
131f4a2713aSLionel Sambuc assert(D);
132f4a2713aSLionel Sambuc
133f4a2713aSLionel Sambuc // Allocate a new node, mark it as root, and process it's calls.
134f4a2713aSLionel Sambuc CallGraphNode *Node = getOrInsertNode(D);
135f4a2713aSLionel Sambuc
136f4a2713aSLionel Sambuc // Process all the calls by this function as well.
137f4a2713aSLionel Sambuc CGBuilder builder(this, Node);
138f4a2713aSLionel Sambuc if (Stmt *Body = D->getBody())
139f4a2713aSLionel Sambuc builder.Visit(Body);
140f4a2713aSLionel Sambuc }
141f4a2713aSLionel Sambuc
getNode(const Decl * F) const142f4a2713aSLionel Sambuc CallGraphNode *CallGraph::getNode(const Decl *F) const {
143f4a2713aSLionel Sambuc FunctionMapTy::const_iterator I = FunctionMap.find(F);
144*0a6a1f1dSLionel Sambuc if (I == FunctionMap.end()) return nullptr;
145f4a2713aSLionel Sambuc return I->second;
146f4a2713aSLionel Sambuc }
147f4a2713aSLionel Sambuc
getOrInsertNode(Decl * F)148f4a2713aSLionel Sambuc CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
149*0a6a1f1dSLionel Sambuc if (F && !isa<ObjCMethodDecl>(F))
150*0a6a1f1dSLionel Sambuc F = F->getCanonicalDecl();
151*0a6a1f1dSLionel Sambuc
152f4a2713aSLionel Sambuc CallGraphNode *&Node = FunctionMap[F];
153f4a2713aSLionel Sambuc if (Node)
154f4a2713aSLionel Sambuc return Node;
155f4a2713aSLionel Sambuc
156f4a2713aSLionel Sambuc Node = new CallGraphNode(F);
157f4a2713aSLionel Sambuc // Make Root node a parent of all functions to make sure all are reachable.
158*0a6a1f1dSLionel Sambuc if (F)
159f4a2713aSLionel Sambuc Root->addCallee(Node, this);
160f4a2713aSLionel Sambuc return Node;
161f4a2713aSLionel Sambuc }
162f4a2713aSLionel Sambuc
print(raw_ostream & OS) const163f4a2713aSLionel Sambuc void CallGraph::print(raw_ostream &OS) const {
164f4a2713aSLionel Sambuc OS << " --- Call graph Dump --- \n";
165f4a2713aSLionel Sambuc
166f4a2713aSLionel Sambuc // We are going to print the graph in reverse post order, partially, to make
167f4a2713aSLionel Sambuc // sure the output is deterministic.
168f4a2713aSLionel Sambuc llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this);
169f4a2713aSLionel Sambuc for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator
170f4a2713aSLionel Sambuc I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
171f4a2713aSLionel Sambuc const CallGraphNode *N = *I;
172f4a2713aSLionel Sambuc
173f4a2713aSLionel Sambuc OS << " Function: ";
174f4a2713aSLionel Sambuc if (N == Root)
175f4a2713aSLionel Sambuc OS << "< root >";
176f4a2713aSLionel Sambuc else
177f4a2713aSLionel Sambuc N->print(OS);
178f4a2713aSLionel Sambuc
179f4a2713aSLionel Sambuc OS << " calls: ";
180f4a2713aSLionel Sambuc for (CallGraphNode::const_iterator CI = N->begin(),
181f4a2713aSLionel Sambuc CE = N->end(); CI != CE; ++CI) {
182f4a2713aSLionel Sambuc assert(*CI != Root && "No one can call the root node.");
183f4a2713aSLionel Sambuc (*CI)->print(OS);
184f4a2713aSLionel Sambuc OS << " ";
185f4a2713aSLionel Sambuc }
186f4a2713aSLionel Sambuc OS << '\n';
187f4a2713aSLionel Sambuc }
188f4a2713aSLionel Sambuc OS.flush();
189f4a2713aSLionel Sambuc }
190f4a2713aSLionel Sambuc
dump() const191f4a2713aSLionel Sambuc void CallGraph::dump() const {
192f4a2713aSLionel Sambuc print(llvm::errs());
193f4a2713aSLionel Sambuc }
194f4a2713aSLionel Sambuc
viewGraph() const195f4a2713aSLionel Sambuc void CallGraph::viewGraph() const {
196f4a2713aSLionel Sambuc llvm::ViewGraph(this, "CallGraph");
197f4a2713aSLionel Sambuc }
198f4a2713aSLionel Sambuc
print(raw_ostream & os) const199f4a2713aSLionel Sambuc void CallGraphNode::print(raw_ostream &os) const {
200f4a2713aSLionel Sambuc if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
201f4a2713aSLionel Sambuc return ND->printName(os);
202f4a2713aSLionel Sambuc os << "< >";
203f4a2713aSLionel Sambuc }
204f4a2713aSLionel Sambuc
dump() const205f4a2713aSLionel Sambuc void CallGraphNode::dump() const {
206f4a2713aSLionel Sambuc print(llvm::errs());
207f4a2713aSLionel Sambuc }
208f4a2713aSLionel Sambuc
209f4a2713aSLionel Sambuc namespace llvm {
210f4a2713aSLionel Sambuc
211f4a2713aSLionel Sambuc template <>
212f4a2713aSLionel Sambuc struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
213f4a2713aSLionel Sambuc
DOTGraphTraitsllvm::DOTGraphTraits214f4a2713aSLionel Sambuc DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
215f4a2713aSLionel Sambuc
getNodeLabelllvm::DOTGraphTraits216f4a2713aSLionel Sambuc static std::string getNodeLabel(const CallGraphNode *Node,
217f4a2713aSLionel Sambuc const CallGraph *CG) {
218f4a2713aSLionel Sambuc if (CG->getRoot() == Node) {
219f4a2713aSLionel Sambuc return "< root >";
220f4a2713aSLionel Sambuc }
221f4a2713aSLionel Sambuc if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
222f4a2713aSLionel Sambuc return ND->getNameAsString();
223f4a2713aSLionel Sambuc else
224f4a2713aSLionel Sambuc return "< >";
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc
227f4a2713aSLionel Sambuc };
228f4a2713aSLionel Sambuc }
229