clang 23.0.0git
CallGraph.h
Go to the documentation of this file.
1//===- CallGraph.h - AST-based Call graph -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file declares the AST-based CallGraph.
10//
11// A call graph for functions whose definitions/bodies are available in the
12// current translation unit. The graph has a "virtual" root node that contains
13// edges to all externally available functions.
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_CLANG_ANALYSIS_CALLGRAPH_H
18#define LLVM_CLANG_ANALYSIS_CALLGRAPH_H
19
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclObjC.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/GraphTraits.h"
25#include "llvm/ADT/STLExtras.h"
26#include "llvm/ADT/SetVector.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/iterator_range.h"
29#include "llvm/Support/TimeProfiler.h"
30#include <memory>
31
32namespace clang {
33
34class CallGraphNode;
35class Decl;
36class DeclContext;
37class Stmt;
38
39/// The AST-based call graph.
40///
41/// The call graph extends itself with the given declarations by implementing
42/// the recursive AST visitor, which constructs the graph by visiting the given
43/// declarations.
45 friend class CallGraphNode;
46
47 using FunctionMapTy =
48 llvm::DenseMap<const Decl *, std::unique_ptr<CallGraphNode>>;
49
50 /// FunctionMap owns all CallGraphNodes.
51 FunctionMapTy FunctionMap;
52
53 /// This is a virtual root node that has edges to all the functions.
54 CallGraphNode *Root;
55
56public:
57 CallGraph();
59
60 /// Populate the call graph with the functions in the given
61 /// declaration.
62 ///
63 /// Recursively walks the declaration to find all the dependent Decls as well.
65 llvm::TimeTraceScope TimeProfile("AddToCallGraph");
66 TraverseDecl(D);
67 }
68
69 /// Determine if a declaration should be included in the graph.
70 static bool includeInGraph(const Decl *D);
71
72 /// Determine if a declaration should be included in the graph for the
73 /// purposes of being a callee. This is similar to includeInGraph except
74 /// it permits declarations, not just definitions.
75 static bool includeCalleeInGraph(const Decl *D);
76
77 /// Lookup the node for the given declaration.
78 CallGraphNode *getNode(const Decl *) const;
79
80 /// Lookup the node for the given declaration. If none found, insert
81 /// one into the graph.
83
84 using iterator = FunctionMapTy::iterator;
85 using const_iterator = FunctionMapTy::const_iterator;
86
87 /// Iterators through all the elements in the graph. Note, this gives
88 /// non-deterministic order.
89 iterator begin() { return FunctionMap.begin(); }
90 iterator end() { return FunctionMap.end(); }
91 const_iterator begin() const { return FunctionMap.begin(); }
92 const_iterator end() const { return FunctionMap.end(); }
93
94 /// Get the number of nodes in the graph.
95 unsigned size() const { return FunctionMap.size(); }
96
97 /// Get the virtual root of the graph, all the functions available externally
98 /// are represented as callees of the node.
99 CallGraphNode *getRoot() const { return Root; }
100
101 /// Iterators through all the nodes of the graph that have no parent. These
102 /// are the unreachable nodes, which are either unused or are due to us
103 /// failing to add a call edge due to the analysis imprecision.
104 using nodes_iterator = llvm::SetVector<CallGraphNode *>::iterator;
105 using const_nodes_iterator = llvm::SetVector<CallGraphNode *>::const_iterator;
106
107 void print(raw_ostream &os) const;
108 void dump() const;
109 void viewGraph() const;
110
112
113 /// Part of recursive declaration visitation. We recursively visit all the
114 /// declarations to collect the root functions.
115 bool VisitFunctionDecl(FunctionDecl *FD) override {
116 // We skip function template definitions, as their semantics is
117 // only determined when they are instantiated.
119 // Add all blocks declared inside this function to the graph.
121 // If this function has external linkage, anything could call it.
122 // Note, we are not precise here. For example, the function could have
123 // its address taken.
124 addNodeForDecl(FD, FD->isGlobal());
125 }
126 return true;
127 }
128
129 /// Part of recursive declaration visitation.
131 if (includeInGraph(MD)) {
133 addNodeForDecl(MD, true);
134 }
135 return true;
136 }
137
138 /// Part of recursive declaration visitation. A global-storage variable's
139 /// initializer can define callables (lambdas, blocks) no function body
140 /// reaches; TraverseStmt is a no-op, so collect them here.
141 bool VisitVarDecl(VarDecl *VD) override {
142 addNodesForVarInit(VD);
143 return true;
144 }
145
146 // We are only collecting the declarations, so do not step into the bodies.
147 bool TraverseStmt(Stmt *S) override { return true; }
148
149private:
150 /// Add the given declaration to the call graph.
151 void addNodeForDecl(Decl *D, bool IsGlobal);
152
153 /// Walk a variable's initializer to add any callables defined within it.
154 void addNodesForVarInit(VarDecl *VD);
155};
156
158public:
159 struct CallRecord {
162
163 CallRecord() = default;
164
165 CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
166 : Callee(Callee_), CallExpr(CallExpr_) {}
167
168 // The call destination is the only important data here,
169 // allow to transparently unwrap into it.
170 operator CallGraphNode *() const { return Callee; }
171 };
172
173private:
174 /// The function/method declaration.
175 Decl *FD;
176
177 /// The list of functions called from this node.
178 SmallVector<CallRecord, 5> CalledFunctions;
179
180public:
181 CallGraphNode(Decl *D) : FD(D) {}
182
185
186 /// Iterators through all the callees/children of the node.
187 iterator begin() { return CalledFunctions.begin(); }
188 iterator end() { return CalledFunctions.end(); }
189 const_iterator begin() const { return CalledFunctions.begin(); }
190 const_iterator end() const { return CalledFunctions.end(); }
191
192 /// Iterator access to callees/children of the node.
193 llvm::iterator_range<iterator> callees() {
194 return llvm::make_range(begin(), end());
195 }
196 llvm::iterator_range<const_iterator> callees() const {
197 return llvm::make_range(begin(), end());
198 }
199
200 bool empty() const { return CalledFunctions.empty(); }
201 unsigned size() const { return CalledFunctions.size(); }
202
203 void addCallee(CallRecord Call) { CalledFunctions.push_back(Call); }
204
205 Decl *getDecl() const { return FD; }
206
209 return FD ? FD->getDefinition() : nullptr;
210 }
211
212 void print(raw_ostream &os) const;
213 void dump() const;
214};
215
216// NOTE: we are comparing based on the callee only. So different call records
217// (with different call expressions) to the same callee will compare equal!
219 const CallGraphNode::CallRecord &RHS) {
220 return LHS.Callee == RHS.Callee;
221}
222
223} // namespace clang
224
225namespace llvm {
226
227// Specialize DenseMapInfo for clang::CallGraphNode::CallRecord.
228template <> struct DenseMapInfo<clang::CallGraphNode::CallRecord> {
229 static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val) {
230 // NOTE: we are comparing based on the callee only.
231 // Different call records with the same callee will compare equal!
232 return DenseMapInfo<clang::CallGraphNode *>::getHashValue(Val.Callee);
233 }
234
237 return LHS == RHS;
238 }
239};
240
241// Graph traits for iteration, viewing.
242template <> struct GraphTraits<clang::CallGraphNode*> {
246
247 static NodeType *getEntryNode(clang::CallGraphNode *CGN) { return CGN; }
248 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
249 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
250};
251
252template <> struct GraphTraits<const clang::CallGraphNode*> {
256
257 static NodeType *getEntryNode(const clang::CallGraphNode *CGN) { return CGN; }
258 static ChildIteratorType child_begin(NodeType *N) { return N->begin();}
259 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
260};
261
262template <> struct GraphTraits<clang::CallGraph*>
263 : public GraphTraits<clang::CallGraphNode*> {
265 return CGN->getRoot(); // Start at the external node!
266 }
267
268 static clang::CallGraphNode *
269 CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
270 return P.second.get();
271 }
272
273 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
275 mapped_iterator<clang::CallGraph::iterator, decltype(&CGGetValue)>;
276
280
282 return nodes_iterator(CG->end(), &CGGetValue);
283 }
284
285 static unsigned size(clang::CallGraph *CG) { return CG->size(); }
286};
287
288template <> struct GraphTraits<const clang::CallGraph*> :
289 public GraphTraits<const clang::CallGraphNode*> {
291 return CGN->getRoot();
292 }
293
294 static clang::CallGraphNode *
295 CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
296 return P.second.get();
297 }
298
299 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
301 mapped_iterator<clang::CallGraph::const_iterator, decltype(&CGGetValue)>;
302
304 return nodes_iterator(CG->begin(), &CGGetValue);
305 }
306
308 return nodes_iterator(CG->end(), &CGGetValue);
309 }
310
311 static unsigned size(const clang::CallGraph *CG) { return CG->size(); }
312};
313
314} // namespace llvm
315
316#endif // LLVM_CLANG_ANALYSIS_CALLGRAPH_H
SmallVectorImpl< CallRecord >::iterator iterator
Definition CallGraph.h:183
FunctionDecl * getDefinition() const
Definition CallGraph.h:207
llvm::iterator_range< const_iterator > callees() const
Definition CallGraph.h:196
const_iterator begin() const
Definition CallGraph.h:189
llvm::iterator_range< iterator > callees()
Iterator access to callees/children of the node.
Definition CallGraph.h:193
Decl * getDecl() const
Definition CallGraph.h:205
iterator begin()
Iterators through all the callees/children of the node.
Definition CallGraph.h:187
void print(raw_ostream &os) const
bool empty() const
Definition CallGraph.h:200
const_iterator end() const
Definition CallGraph.h:190
SmallVectorImpl< CallRecord >::const_iterator const_iterator
Definition CallGraph.h:184
unsigned size() const
Definition CallGraph.h:201
void addCallee(CallRecord Call)
Definition CallGraph.h:203
The AST-based call graph.
Definition CallGraph.h:44
void viewGraph() const
bool VisitVarDecl(VarDecl *VD) override
Part of recursive declaration visitation.
Definition CallGraph.h:141
bool TraverseStmt(Stmt *S) override
Definition CallGraph.h:147
const_iterator begin() const
Definition CallGraph.h:91
iterator end()
Definition CallGraph.h:90
void addToCallGraph(Decl *D)
Populate the call graph with the functions in the given declaration.
Definition CallGraph.h:64
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
void dump() const
iterator begin()
Iterators through all the elements in the graph.
Definition CallGraph.h:89
void addNodesForBlocks(DeclContext *D)
bool VisitFunctionDecl(FunctionDecl *FD) override
Part of recursive declaration visitation.
Definition CallGraph.h:115
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
FunctionMapTy::const_iterator const_iterator
Definition CallGraph.h:85
llvm::SetVector< CallGraphNode * >::iterator nodes_iterator
Iterators through all the nodes of the graph that have no parent.
Definition CallGraph.h:104
FunctionMapTy::iterator iterator
Definition CallGraph.h:84
llvm::SetVector< CallGraphNode * >::const_iterator const_nodes_iterator
Definition CallGraph.h:105
void print(raw_ostream &os) const
friend class CallGraphNode
Definition CallGraph.h:45
static bool includeCalleeInGraph(const Decl *D)
Determine if a declaration should be included in the graph for the purposes of being a callee.
CallGraphNode * getRoot() const
Get the virtual root of the graph, all the functions available externally are represented as callees ...
Definition CallGraph.h:99
unsigned size() const
Get the number of nodes in the graph.
Definition CallGraph.h:95
bool VisitObjCMethodDecl(ObjCMethodDecl *MD) override
Part of recursive declaration visitation.
Definition CallGraph.h:130
const_iterator end() const
Definition CallGraph.h:92
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition DeclBase.h:1466
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
FunctionDecl * getAsFunction() LLVM_READONLY
Returns the function itself, or the templated function if this is a function template.
Definition DeclBase.cpp:273
virtual bool TraverseDecl(MaybeConst< Decl > *D)
This represents one expression.
Definition Expr.h:112
Represents a function declaration or definition.
Definition Decl.h:2027
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2341
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition Decl.h:2309
bool isGlobal() const
Determines whether this is a global function.
Definition Decl.cpp:3609
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
Stmt - This represents one statement.
Definition Stmt.h:86
Represents a variable declaration or definition.
Definition Decl.h:932
The JSON file list parser is used to communicate input to InstallAPI.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:218
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
Definition CallGraph.h:165
static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val)
Definition CallGraph.h:229
static bool isEqual(const clang::CallGraphNode::CallRecord &LHS, const clang::CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:235
static ChildIteratorType child_begin(NodeType *N)
Definition CallGraph.h:248
static NodeType * getEntryNode(clang::CallGraphNode *CGN)
Definition CallGraph.h:247
static ChildIteratorType child_end(NodeType *N)
Definition CallGraph.h:249
static clang::CallGraphNode * CGGetValue(clang::CallGraph::const_iterator::value_type &P)
Definition CallGraph.h:269
static NodeType * getEntryNode(clang::CallGraph *CGN)
Definition CallGraph.h:264
static nodes_iterator nodes_end(clang::CallGraph *CG)
Definition CallGraph.h:281
static nodes_iterator nodes_begin(clang::CallGraph *CG)
Definition CallGraph.h:277
static unsigned size(clang::CallGraph *CG)
Definition CallGraph.h:285
mapped_iterator< clang::CallGraph::iterator, decltype(&CGGetValue)> nodes_iterator
Definition CallGraph.h:274
static ChildIteratorType child_begin(NodeType *N)
Definition CallGraph.h:258
static ChildIteratorType child_end(NodeType *N)
Definition CallGraph.h:259
static NodeType * getEntryNode(const clang::CallGraphNode *CGN)
Definition CallGraph.h:257
static nodes_iterator nodes_begin(const clang::CallGraph *CG)
Definition CallGraph.h:303
static unsigned size(const clang::CallGraph *CG)
Definition CallGraph.h:311
static clang::CallGraphNode * CGGetValue(clang::CallGraph::const_iterator::value_type &P)
Definition CallGraph.h:295
static NodeType * getEntryNode(const clang::CallGraph *CGN)
Definition CallGraph.h:290
mapped_iterator< clang::CallGraph::const_iterator, decltype(&CGGetValue)> nodes_iterator
Definition CallGraph.h:300
static nodes_iterator nodes_end(const clang::CallGraph *CG)
Definition CallGraph.h:307