clang  5.0.0
CallGraph.cpp
Go to the documentation of this file.
1 //== 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 //===----------------------------------------------------------------------===//
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"
20 
21 using namespace clang;
22 
23 #define DEBUG_TYPE "CallGraph"
24 
25 STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
26 STATISTIC(NumBlockCallEdges, "Number of block call edges");
27 
28 namespace {
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> {
32  CallGraph *G;
33  CallGraphNode *CallerNode;
34 
35 public:
36  CGBuilder(CallGraph *g, CallGraphNode *N)
37  : G(g), CallerNode(N) {}
38 
39  void VisitStmt(Stmt *S) { VisitChildren(S); }
40 
41  Decl *getDeclFromCall(CallExpr *CE) {
42  if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
43  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 nullptr;
53  }
54 
55  void addCalledDecl(Decl *D) {
56  if (G->includeInGraph(D)) {
57  CallGraphNode *CalleeNode = G->getOrInsertNode(D);
58  CallerNode->addCallee(CalleeNode);
59  }
60  }
61 
62  void VisitCallExpr(CallExpr *CE) {
63  if (Decl *D = getDeclFromCall(CE))
64  addCalledDecl(D);
65  VisitChildren(CE);
66  }
67 
68  // Adds may-call edges for the ObjC message sends.
69  void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
70  if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
71  Selector Sel = ME->getSelector();
72 
73  // Find the callee definition within the same translation unit.
74  Decl *D = nullptr;
75  if (ME->isInstanceMessage())
76  D = IDecl->lookupPrivateMethod(Sel);
77  else
78  D = IDecl->lookupPrivateClassMethod(Sel);
79  if (D) {
80  addCalledDecl(D);
81  NumObjCCallEdges++;
82  }
83  }
84  }
85 
86  void VisitChildren(Stmt *S) {
87  for (Stmt *SubStmt : S->children())
88  if (SubStmt)
89  this->Visit(SubStmt);
90  }
91 };
92 
93 } // end anonymous namespace
94 
96  if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
97  addNodeForDecl(BD, true);
98 
99  for (auto *I : D->decls())
100  if (auto *DC = dyn_cast<DeclContext>(I))
101  addNodesForBlocks(DC);
102 }
103 
105  Root = getOrInsertNode(nullptr);
106 }
107 
109 
111  assert(D);
112  if (!D->hasBody())
113  return false;
114 
115  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
116  // We skip function template definitions, as their semantics is
117  // only determined when they are instantiated.
118  if (FD->isDependentContext())
119  return false;
120 
121  IdentifierInfo *II = FD->getIdentifier();
122  if (II && II->getName().startswith("__inline"))
123  return false;
124  }
125 
126  return true;
127 }
128 
129 void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
130  assert(D);
131 
132  // Allocate a new node, mark it as root, and process it's calls.
134 
135  // Process all the calls by this function as well.
136  CGBuilder builder(this, Node);
137  if (Stmt *Body = D->getBody())
138  builder.Visit(Body);
139 }
140 
142  FunctionMapTy::const_iterator I = FunctionMap.find(F);
143  if (I == FunctionMap.end()) return nullptr;
144  return I->second.get();
145 }
146 
148  if (F && !isa<ObjCMethodDecl>(F))
149  F = F->getCanonicalDecl();
150 
151  std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
152  if (Node)
153  return Node.get();
154 
155  Node = llvm::make_unique<CallGraphNode>(F);
156  // Make Root node a parent of all functions to make sure all are reachable.
157  if (F)
158  Root->addCallee(Node.get());
159  return Node.get();
160 }
161 
162 void CallGraph::print(raw_ostream &OS) const {
163  OS << " --- Call graph Dump --- \n";
164 
165  // We are going to print the graph in reverse post order, partially, to make
166  // sure the output is deterministic.
167  llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this);
168  for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator
169  I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
170  const CallGraphNode *N = *I;
171 
172  OS << " Function: ";
173  if (N == Root)
174  OS << "< root >";
175  else
176  N->print(OS);
177 
178  OS << " calls: ";
179  for (CallGraphNode::const_iterator CI = N->begin(),
180  CE = N->end(); CI != CE; ++CI) {
181  assert(*CI != Root && "No one can call the root node.");
182  (*CI)->print(OS);
183  OS << " ";
184  }
185  OS << '\n';
186  }
187  OS.flush();
188 }
189 
190 LLVM_DUMP_METHOD void CallGraph::dump() const {
191  print(llvm::errs());
192 }
193 
194 void CallGraph::viewGraph() const {
195  llvm::ViewGraph(this, "CallGraph");
196 }
197 
198 void CallGraphNode::print(raw_ostream &os) const {
199  if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
200  return ND->printName(os);
201  os << "< >";
202 }
203 
204 LLVM_DUMP_METHOD void CallGraphNode::dump() const {
205  print(llvm::errs());
206 }
207 
208 namespace llvm {
209 
210 template <>
211 struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
212 
213  DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
214 
215  static std::string getNodeLabel(const CallGraphNode *Node,
216  const CallGraph *CG) {
217  if (CG->getRoot() == Node) {
218  return "< root >";
219  }
220  if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
221  return ND->getNameAsString();
222  else
223  return "< >";
224  }
225 
226 };
227 }
The AST-based call graph.
Definition: CallGraph.h:34
Defines the clang::ASTContext interface.
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1618
Smart pointer class that efficiently represents Objective-C method names.
Stmt - This represents one statement.
Definition: Stmt.h:60
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:1537
Decl * getDecl() const
Definition: CallGraph.h:164
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:81
STATISTIC(NumObjCCallEdges,"Number of Objective-C method call edges")
const Expr * getCallee() const
Definition: Expr.h:2246
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
Definition: CallGraph.cpp:147
void addCallee(CallGraphNode *N)
Definition: CallGraph.h:160
One of these records is kept for each identifier that is lexed.
child_range children()
Definition: Stmt.cpp:208
void addNodesForBlocks(DeclContext *D)
Definition: CallGraph.cpp:95
Selector getSelector() const
Definition: ExprObjC.cpp:306
Represents an ObjC class declaration.
Definition: DeclObjC.h:1108
detail::InMemoryDirectory::const_iterator I
CallGraphNode * getRoot() const
\ brief Get the virtual root of the graph, all the functions available externally are represented as ...
Definition: CallGraph.h:82
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:841
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
Definition: ExprObjC.h:1143
void print(raw_ostream &os) const
Definition: CallGraph.cpp:198
SmallVectorImpl< CallRecord >::const_iterator const_iterator
Definition: CallGraph.h:149
void dump() const
Definition: CallGraph.cpp:204
static std::string getNodeLabel(const CallGraphNode *Node, const CallGraph *CG)
Definition: CallGraph.cpp:215
BlockDecl - This represents a block literal declaration, which is like an unnamed FunctionDecl...
Definition: Decl.h:3557
Expr - This represents one expression.
Definition: Expr.h:105
StringRef getName() const
Return the actual identifier string.
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
Definition: CallGraph.cpp:141
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:4820
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition: ExprObjC.cpp:327
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:860
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
Definition: CallGraph.cpp:110
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:178
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:948
ast_type_traits::DynTypedNode Node
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1215
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return 0.
Definition: Expr.cpp:1216
void dump() const
Definition: CallGraph.cpp:190
void print(raw_ostream &os) const
Definition: CallGraph.cpp:162
iterator begin()
Iterators through all the callees/children of the node.
Definition: CallGraph.h:152
detail::InMemoryDirectory::const_iterator E
Expr * IgnoreParenImpCasts() LLVM_READONLY
IgnoreParenImpCasts - Ignore parentheses and implicit casts.
Definition: Expr.cpp:2486
void viewGraph() const
Definition: CallGraph.cpp:194
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2206
NamedDecl - This represents a decl with a name.
Definition: Decl.h:213
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition: DeclBase.h:954