clang  3.9.0
CallGraph.h
Go to the documentation of this file.
1 //== CallGraph.h - 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 declares the AST-based CallGraph.
11 //
12 // A call graph for functions whose definitions/bodies are available in the
13 // current translation unit. The graph has a "virtual" root node that contains
14 // edges to all externally available functions.
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_CLANG_ANALYSIS_CALLGRAPH_H
18 #define LLVM_CLANG_ANALYSIS_CALLGRAPH_H
19 
20 #include "clang/AST/DeclBase.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/GraphTraits.h"
24 #include "llvm/ADT/SetVector.h"
25 
26 namespace clang {
27 class CallGraphNode;
28 
29 /// \brief The AST-based call graph.
30 ///
31 /// The call graph extends itself with the given declarations by implementing
32 /// the recursive AST visitor, which constructs the graph by visiting the given
33 /// declarations.
34 class CallGraph : public RecursiveASTVisitor<CallGraph> {
35  friend class CallGraphNode;
36 
37  typedef llvm::DenseMap<const Decl *, CallGraphNode *> FunctionMapTy;
38 
39  /// FunctionMap owns all CallGraphNodes.
40  FunctionMapTy FunctionMap;
41 
42  /// This is a virtual root node that has edges to all the functions.
43  CallGraphNode *Root;
44 
45 public:
46  CallGraph();
47  ~CallGraph();
48 
49  /// \brief Populate the call graph with the functions in the given
50  /// declaration.
51  ///
52  /// Recursively walks the declaration to find all the dependent Decls as well.
53  void addToCallGraph(Decl *D) {
54  TraverseDecl(D);
55  }
56 
57  /// \brief Determine if a declaration should be included in the graph.
58  static bool includeInGraph(const Decl *D);
59 
60  /// \brief Lookup the node for the given declaration.
61  CallGraphNode *getNode(const Decl *) const;
62 
63  /// \brief Lookup the node for the given declaration. If none found, insert
64  /// one into the graph.
66 
67  /// Iterators through all the elements in the graph. Note, this gives
68  /// non-deterministic order.
70  typedef FunctionMapTy::const_iterator const_iterator;
71  iterator begin() { return FunctionMap.begin(); }
72  iterator end() { return FunctionMap.end(); }
73  const_iterator begin() const { return FunctionMap.begin(); }
74  const_iterator end() const { return FunctionMap.end(); }
75 
76  /// \brief Get the number of nodes in the graph.
77  unsigned size() const { return FunctionMap.size(); }
78 
79  /// \ brief Get the virtual root of the graph, all the functions available
80  /// externally are represented as callees of the node.
81  CallGraphNode *getRoot() const { return Root; }
82 
83  /// Iterators through all the nodes of the graph that have no parent. These
84  /// are the unreachable nodes, which are either unused or are due to us
85  /// failing to add a call edge due to the analysis imprecision.
87  typedef llvm::SetVector<CallGraphNode *>::const_iterator const_nodes_iterator;
88 
89  void print(raw_ostream &os) const;
90  void dump() const;
91  void viewGraph() const;
92 
94 
95  /// Part of recursive declaration visitation. We recursively visit all the
96  /// declarations to collect the root functions.
98  // We skip function template definitions, as their semantics is
99  // only determined when they are instantiated.
100  if (includeInGraph(FD)) {
101  // Add all blocks declared inside this function to the graph.
102  addNodesForBlocks(FD);
103  // If this function has external linkage, anything could call it.
104  // Note, we are not precise here. For example, the function could have
105  // its address taken.
106  addNodeForDecl(FD, FD->isGlobal());
107  }
108  return true;
109  }
110 
111  /// Part of recursive declaration visitation.
113  if (includeInGraph(MD)) {
114  addNodesForBlocks(MD);
115  addNodeForDecl(MD, true);
116  }
117  return true;
118  }
119 
120  // We are only collecting the declarations, so do not step into the bodies.
121  bool TraverseStmt(Stmt *S) { return true; }
122 
123  bool shouldWalkTypesOfTypeLocs() const { return false; }
124 
125 private:
126  /// \brief Add the given declaration to the call graph.
127  void addNodeForDecl(Decl *D, bool IsGlobal);
128 
129  /// \brief Allocate a new node in the graph.
130  CallGraphNode *allocateNewNode(Decl *);
131 };
132 
134 public:
136 
137 private:
138  /// \brief The function/method declaration.
139  Decl *FD;
140 
141  /// \brief The list of functions called from this node.
142  SmallVector<CallRecord, 5> CalledFunctions;
143 
144 public:
145  CallGraphNode(Decl *D) : FD(D) {}
146 
149 
150  /// Iterators through all the callees/children of the node.
151  inline iterator begin() { return CalledFunctions.begin(); }
152  inline iterator end() { return CalledFunctions.end(); }
153  inline const_iterator begin() const { return CalledFunctions.begin(); }
154  inline const_iterator end() const { return CalledFunctions.end(); }
155 
156  inline bool empty() const {return CalledFunctions.empty(); }
157  inline unsigned size() const {return CalledFunctions.size(); }
158 
160  CalledFunctions.push_back(N);
161  }
162 
163  Decl *getDecl() const { return FD; }
164 
165  void print(raw_ostream &os) const;
166  void dump() const;
167 };
168 
169 } // end clang namespace
170 
171 // Graph traits for iteration, viewing.
172 namespace llvm {
173 template <> struct GraphTraits<clang::CallGraphNode*> {
176  typedef std::pointer_to_unary_function<CallRecordTy,
178  static NodeType *getEntryNode(clang::CallGraphNode *CGN) { return CGN; }
179  typedef mapped_iterator<NodeType::iterator, CGNDerefFun> ChildIteratorType;
181  return map_iterator(N->begin(), CGNDerefFun(CGNDeref));
182  }
183  static inline ChildIteratorType child_end (NodeType *N) {
184  return map_iterator(N->end(), CGNDerefFun(CGNDeref));
185  }
187  return P;
188  }
189 };
190 
191 template <> struct GraphTraits<const clang::CallGraphNode*> {
194  static NodeType *getEntryNode(const clang::CallGraphNode *CGN) { return CGN; }
195  static inline ChildIteratorType child_begin(NodeType *N) { return N->begin();}
196  static inline ChildIteratorType child_end(NodeType *N) { return N->end(); }
197 };
198 
199 template <> struct GraphTraits<clang::CallGraph*>
200  : public GraphTraits<clang::CallGraphNode*> {
201 
203  return CGN->getRoot(); // Start at the external node!
204  }
205  typedef std::pair<const clang::Decl*, clang::CallGraphNode*> PairTy;
206  typedef std::pointer_to_unary_function<PairTy, clang::CallGraphNode&> DerefFun;
207  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
208  typedef mapped_iterator<clang::CallGraph::iterator, DerefFun> nodes_iterator;
209 
211  return map_iterator(CG->begin(), DerefFun(CGdereference));
212  }
214  return map_iterator(CG->end(), DerefFun(CGdereference));
215  }
217  return *(P.second);
218  }
219 
220  static unsigned size(clang::CallGraph *CG) {
221  return CG->size();
222  }
223 };
224 
225 template <> struct GraphTraits<const clang::CallGraph*> :
226  public GraphTraits<const clang::CallGraphNode*> {
227  static NodeType *getEntryNode(const clang::CallGraph *CGN) {
228  return CGN->getRoot();
229  }
230  typedef std::pair<const clang::Decl*, clang::CallGraphNode*> PairTy;
231  typedef std::pointer_to_unary_function<PairTy, clang::CallGraphNode&> DerefFun;
232  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
233  typedef mapped_iterator<clang::CallGraph::const_iterator,
235 
237  return map_iterator(CG->begin(), DerefFun(CGdereference));
238  }
240  return map_iterator(CG->end(), DerefFun(CGdereference));
241  }
243  return *(P.second);
244  }
245 
246  static unsigned size(const clang::CallGraph *CG) {
247  return CG->size();
248  }
249 };
250 
251 } // end llvm namespace
252 
253 #endif
The AST-based call graph.
Definition: CallGraph.h:34
FunctionDecl - An instance of this class is created to represent a function declaration or definition...
Definition: Decl.h:1561
Decl * getDecl() const
Definition: CallGraph.h:163
StringRef P
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:113
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
Definition: CallGraph.cpp:148
static ChildIteratorType child_end(NodeType *N)
Definition: CallGraph.h:196
mapped_iterator< NodeType::iterator, CGNDerefFun > ChildIteratorType
Definition: CallGraph.h:179
bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
static unsigned size(const clang::CallGraph *CG)
Definition: CallGraph.h:246
bool shouldWalkTypesOfTypeLocs() const
Definition: CallGraph.h:123
static NodeType * getEntryNode(const clang::CallGraph *CGN)
Definition: CallGraph.h:227
static nodes_iterator nodes_begin(clang::CallGraph *CG)
Definition: CallGraph.h:210
const_iterator begin() const
Definition: CallGraph.h:73
llvm::SetVector< CallGraphNode * >::iterator nodes_iterator
Iterators through all the nodes of the graph that have no parent.
Definition: CallGraph.h:86
std::pair< const clang::Decl *, clang::CallGraphNode * > PairTy
Definition: CallGraph.h:205
unsigned size() const
Get the number of nodes in the graph.
Definition: CallGraph.h:77
void addNodesForBlocks(DeclContext *D)
Definition: CallGraph.cpp:94
CallGraphNode * CallRecord
Definition: CallGraph.h:135
const_iterator end() const
Definition: CallGraph.h:74
static NodeType * getEntryNode(const clang::CallGraphNode *CGN)
Definition: CallGraph.h:194
static nodes_iterator nodes_begin(const clang::CallGraph *CG)
Definition: CallGraph.h:236
A class that does preordor or postorder depth-first traversal on the entire Clang AST and visits each...
CallGraphNode * getRoot() const
\ brief Get the virtual root of the graph, all the functions available externally are represented as ...
Definition: CallGraph.h:81
CallGraphNode(Decl *D)
Definition: CallGraph.h:145
void addCallee(CallGraphNode *N, CallGraph *CG)
Definition: CallGraph.h:159
void print(raw_ostream &os) const
Definition: CallGraph.cpp:199
SmallVectorImpl< CallRecord >::const_iterator const_iterator
Definition: CallGraph.h:148
static clang::CallGraphNode & CGdereference(PairTy P)
Definition: CallGraph.h:216
void dump() const
Definition: CallGraph.cpp:205
bool isGlobal() const
Determines whether this is a global function.
Definition: Decl.cpp:2635
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
Definition: CallGraph.cpp:142
std::pointer_to_unary_function< CallRecordTy, clang::CallGraphNode * > CGNDerefFun
Definition: CallGraph.h:177
static ChildIteratorType child_end(NodeType *N)
Definition: CallGraph.h:183
static ChildIteratorType child_begin(NodeType *N)
Definition: CallGraph.h:195
bool VisitFunctionDecl(FunctionDecl *FD)
Part of recursive declaration visitation.
Definition: CallGraph.h:97
static nodes_iterator nodes_end(clang::CallGraph *CG)
Definition: CallGraph.h:213
const TemplateArgument * iterator
Definition: Type.h:4233
FunctionMapTy::iterator iterator
Iterators through all the elements in the graph.
Definition: CallGraph.h:69
bool TraverseStmt(Stmt *S)
Definition: CallGraph.h:121
static ChildIteratorType child_begin(NodeType *N)
Definition: CallGraph.h:180
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
Definition: CallGraph.cpp:111
static nodes_iterator nodes_end(const clang::CallGraph *CG)
Definition: CallGraph.h:239
llvm::SetVector< CallGraphNode * >::const_iterator const_nodes_iterator
Definition: CallGraph.h:87
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1135
static NodeType * getEntryNode(clang::CallGraphNode *CGN)
Definition: CallGraph.h:178
const_iterator end() const
Definition: CallGraph.h:154
void dump() const
Definition: CallGraph.cpp:191
void print(raw_ostream &os) const
Definition: CallGraph.cpp:163
static NodeType * getEntryNode(clang::CallGraph *CGN)
Definition: CallGraph.h:202
unsigned size() const
Definition: CallGraph.h:157
bool empty() const
Definition: CallGraph.h:156
iterator begin()
Iterators through all the callees/children of the node.
Definition: CallGraph.h:151
mapped_iterator< clang::CallGraph::const_iterator, DerefFun > nodes_iterator
Definition: CallGraph.h:234
std::pointer_to_unary_function< PairTy, clang::CallGraphNode & > DerefFun
Definition: CallGraph.h:206
static unsigned size(clang::CallGraph *CG)
Definition: CallGraph.h:220
static clang::CallGraphNode & CGdereference(PairTy P)
Definition: CallGraph.h:242
void viewGraph() const
Definition: CallGraph.cpp:195
std::pointer_to_unary_function< PairTy, clang::CallGraphNode & > DerefFun
Definition: CallGraph.h:231
SmallVectorImpl< CallRecord >::iterator iterator
Definition: CallGraph.h:147
static clang::CallGraphNode * CGNDeref(CallRecordTy P)
Definition: CallGraph.h:186
mapped_iterator< clang::CallGraph::iterator, DerefFun > nodes_iterator
Definition: CallGraph.h:208
void addToCallGraph(Decl *D)
Populate the call graph with the functions in the given declaration.
Definition: CallGraph.h:53
iterator end()
Definition: CallGraph.h:72
const_iterator begin() const
Definition: CallGraph.h:153
bool VisitObjCMethodDecl(ObjCMethodDecl *MD)
Part of recursive declaration visitation.
Definition: CallGraph.h:112
FunctionMapTy::const_iterator const_iterator
Definition: CallGraph.h:70
iterator begin()
Definition: CallGraph.h:71
std::pair< const clang::Decl *, clang::CallGraphNode * > PairTy
Definition: CallGraph.h:230
clang::CallGraphNode::CallRecord CallRecordTy
Definition: CallGraph.h:175