clang 20.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 <memory>
30
31namespace clang {
32
33class CallGraphNode;
34class Decl;
35class DeclContext;
36class Stmt;
37
38/// The AST-based call graph.
39///
40/// The call graph extends itself with the given declarations by implementing
41/// the recursive AST visitor, which constructs the graph by visiting the given
42/// declarations.
44 friend class CallGraphNode;
45
46 using FunctionMapTy =
47 llvm::DenseMap<const Decl *, std::unique_ptr<CallGraphNode>>;
48
49 /// FunctionMap owns all CallGraphNodes.
50 FunctionMapTy FunctionMap;
51
52 /// This is a virtual root node that has edges to all the functions.
53 CallGraphNode *Root;
54
55public:
56 CallGraph();
58
59 /// Populate the call graph with the functions in the given
60 /// declaration.
61 ///
62 /// Recursively walks the declaration to find all the dependent Decls as well.
65 }
66
67 /// Determine if a declaration should be included in the graph.
68 static bool includeInGraph(const Decl *D);
69
70 /// Determine if a declaration should be included in the graph for the
71 /// purposes of being a callee. This is similar to includeInGraph except
72 /// it permits declarations, not just definitions.
73 static bool includeCalleeInGraph(const Decl *D);
74
75 /// Lookup the node for the given declaration.
76 CallGraphNode *getNode(const Decl *) const;
77
78 /// Lookup the node for the given declaration. If none found, insert
79 /// one into the graph.
81
82 using iterator = FunctionMapTy::iterator;
83 using const_iterator = FunctionMapTy::const_iterator;
84
85 /// Iterators through all the elements in the graph. Note, this gives
86 /// non-deterministic order.
87 iterator begin() { return FunctionMap.begin(); }
88 iterator end() { return FunctionMap.end(); }
89 const_iterator begin() const { return FunctionMap.begin(); }
90 const_iterator end() const { return FunctionMap.end(); }
91
92 /// Get the number of nodes in the graph.
93 unsigned size() const { return FunctionMap.size(); }
94
95 /// Get the virtual root of the graph, all the functions available externally
96 /// are represented as callees of the node.
97 CallGraphNode *getRoot() const { return Root; }
98
99 /// Iterators through all the nodes of the graph that have no parent. These
100 /// are the unreachable nodes, which are either unused or are due to us
101 /// failing to add a call edge due to the analysis imprecision.
102 using nodes_iterator = llvm::SetVector<CallGraphNode *>::iterator;
103 using const_nodes_iterator = llvm::SetVector<CallGraphNode *>::const_iterator;
104
105 void print(raw_ostream &os) const;
106 void dump() const;
107 void viewGraph() const;
108
110
111 /// Part of recursive declaration visitation. We recursively visit all the
112 /// declarations to collect the root functions.
113 bool VisitFunctionDecl(FunctionDecl *FD) override {
114 // We skip function template definitions, as their semantics is
115 // only determined when they are instantiated.
117 // Add all blocks declared inside this function to the graph.
119 // If this function has external linkage, anything could call it.
120 // Note, we are not precise here. For example, the function could have
121 // its address taken.
122 addNodeForDecl(FD, FD->isGlobal());
123 }
124 return true;
125 }
126
127 /// Part of recursive declaration visitation.
129 if (includeInGraph(MD)) {
131 addNodeForDecl(MD, true);
132 }
133 return true;
134 }
135
136 // We are only collecting the declarations, so do not step into the bodies.
137 bool TraverseStmt(Stmt *S) override { return true; }
138
139private:
140 /// Add the given declaration to the call graph.
141 void addNodeForDecl(Decl *D, bool IsGlobal);
142};
143
145public:
146 struct CallRecord {
149
150 CallRecord() = default;
151
152 CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
153 : Callee(Callee_), CallExpr(CallExpr_) {}
154
155 // The call destination is the only important data here,
156 // allow to transparently unwrap into it.
157 operator CallGraphNode *() const { return Callee; }
158 };
159
160private:
161 /// The function/method declaration.
162 Decl *FD;
163
164 /// The list of functions called from this node.
165 SmallVector<CallRecord, 5> CalledFunctions;
166
167public:
169
172
173 /// Iterators through all the callees/children of the node.
174 iterator begin() { return CalledFunctions.begin(); }
175 iterator end() { return CalledFunctions.end(); }
176 const_iterator begin() const { return CalledFunctions.begin(); }
177 const_iterator end() const { return CalledFunctions.end(); }
178
179 /// Iterator access to callees/children of the node.
180 llvm::iterator_range<iterator> callees() {
181 return llvm::make_range(begin(), end());
182 }
183 llvm::iterator_range<const_iterator> callees() const {
184 return llvm::make_range(begin(), end());
185 }
186
187 bool empty() const { return CalledFunctions.empty(); }
188 unsigned size() const { return CalledFunctions.size(); }
189
190 void addCallee(CallRecord Call) { CalledFunctions.push_back(Call); }
191
192 Decl *getDecl() const { return FD; }
193
195 return getDecl()->getAsFunction()->getDefinition();
196 }
197
198 void print(raw_ostream &os) const;
199 void dump() const;
200};
201
202// NOTE: we are comparing based on the callee only. So different call records
203// (with different call expressions) to the same callee will compare equal!
205 const CallGraphNode::CallRecord &RHS) {
206 return LHS.Callee == RHS.Callee;
207}
208
209} // namespace clang
210
211namespace llvm {
212
213// Specialize DenseMapInfo for clang::CallGraphNode::CallRecord.
214template <> struct DenseMapInfo<clang::CallGraphNode::CallRecord> {
217 DenseMapInfo<clang::CallGraphNode *>::getEmptyKey(),
218 DenseMapInfo<clang::Expr *>::getEmptyKey());
219 }
220
223 DenseMapInfo<clang::CallGraphNode *>::getTombstoneKey(),
224 DenseMapInfo<clang::Expr *>::getTombstoneKey());
225 }
226
227 static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val) {
228 // NOTE: we are comparing based on the callee only.
229 // Different call records with the same callee will compare equal!
230 return DenseMapInfo<clang::CallGraphNode *>::getHashValue(Val.Callee);
231 }
232
235 return LHS == RHS;
236 }
237};
238
239// Graph traits for iteration, viewing.
240template <> struct GraphTraits<clang::CallGraphNode*> {
244
245 static NodeType *getEntryNode(clang::CallGraphNode *CGN) { return CGN; }
246 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
247 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
248};
249
250template <> struct GraphTraits<const clang::CallGraphNode*> {
254
255 static NodeType *getEntryNode(const clang::CallGraphNode *CGN) { return CGN; }
256 static ChildIteratorType child_begin(NodeType *N) { return N->begin();}
257 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
258};
259
260template <> struct GraphTraits<clang::CallGraph*>
261 : public GraphTraits<clang::CallGraphNode*> {
263 return CGN->getRoot(); // Start at the external node!
264 }
265
266 static clang::CallGraphNode *
267 CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
268 return P.second.get();
269 }
270
271 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
273 mapped_iterator<clang::CallGraph::iterator, decltype(&CGGetValue)>;
274
276 return nodes_iterator(CG->begin(), &CGGetValue);
277 }
278
280 return nodes_iterator(CG->end(), &CGGetValue);
281 }
282
283 static unsigned size(clang::CallGraph *CG) { return CG->size(); }
284};
285
286template <> struct GraphTraits<const clang::CallGraph*> :
287 public GraphTraits<const clang::CallGraphNode*> {
289 return CGN->getRoot();
290 }
291
292 static clang::CallGraphNode *
293 CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
294 return P.second.get();
295 }
296
297 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
299 mapped_iterator<clang::CallGraph::const_iterator, decltype(&CGGetValue)>;
300
302 return nodes_iterator(CG->begin(), &CGGetValue);
303 }
304
306 return nodes_iterator(CG->end(), &CGGetValue);
307 }
308
309 static unsigned size(const clang::CallGraph *CG) { return CG->size(); }
310};
311
312} // namespace llvm
313
314#endif // LLVM_CLANG_ANALYSIS_CALLGRAPH_H
StringRef P
const Decl * D
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2874
CallGraphNode(Decl *D)
Definition: CallGraph.h:168
SmallVectorImpl< CallRecord >::iterator iterator
Definition: CallGraph.h:170
FunctionDecl * getDefinition() const
Definition: CallGraph.h:194
llvm::iterator_range< const_iterator > callees() const
Definition: CallGraph.h:183
const_iterator begin() const
Definition: CallGraph.h:176
void dump() const
Definition: CallGraph.cpp:263
llvm::iterator_range< iterator > callees()
Iterator access to callees/children of the node.
Definition: CallGraph.h:180
Decl * getDecl() const
Definition: CallGraph.h:192
iterator begin()
Iterators through all the callees/children of the node.
Definition: CallGraph.h:174
void print(raw_ostream &os) const
Definition: CallGraph.cpp:257
bool empty() const
Definition: CallGraph.h:187
const_iterator end() const
Definition: CallGraph.h:177
SmallVectorImpl< CallRecord >::const_iterator const_iterator
Definition: CallGraph.h:171
unsigned size() const
Definition: CallGraph.h:188
void addCallee(CallRecord Call)
Definition: CallGraph.h:190
The AST-based call graph.
Definition: CallGraph.h:43
void viewGraph() const
Definition: CallGraph.cpp:253
bool TraverseStmt(Stmt *S) override
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
Definition: CallGraph.h:137
const_iterator begin() const
Definition: CallGraph.h:89
iterator end()
Definition: CallGraph.h:88
void addToCallGraph(Decl *D)
Populate the call graph with the functions in the given declaration.
Definition: CallGraph.h:63
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
Definition: CallGraph.cpp:200
void dump() const
Definition: CallGraph.cpp:249
iterator begin()
Iterators through all the elements in the graph.
Definition: CallGraph.h:87
void addNodesForBlocks(DeclContext *D)
Definition: CallGraph.cpp:140
bool VisitFunctionDecl(FunctionDecl *FD) override
Part of recursive declaration visitation.
Definition: CallGraph.h:113
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
Definition: CallGraph.cpp:206
FunctionMapTy::const_iterator const_iterator
Definition: CallGraph.h:83
llvm::SetVector< CallGraphNode * >::iterator nodes_iterator
Iterators through all the nodes of the graph that have no parent.
Definition: CallGraph.h:102
FunctionMapTy::iterator iterator
Definition: CallGraph.h:82
llvm::SetVector< CallGraphNode * >::const_iterator const_nodes_iterator
Definition: CallGraph.h:103
void print(raw_ostream &os) const
Definition: CallGraph.cpp:221
static bool includeCalleeInGraph(const Decl *D)
Determine if a declaration should be included in the graph for the purposes of being a callee.
Definition: CallGraph.cpp:166
CallGraphNode * getRoot() const
Get the virtual root of the graph, all the functions available externally are represented as callees ...
Definition: CallGraph.h:97
unsigned size() const
Get the number of nodes in the graph.
Definition: CallGraph.h:93
bool VisitObjCMethodDecl(ObjCMethodDecl *MD) override
Part of recursive declaration visitation.
Definition: CallGraph.h:128
const_iterator end() const
Definition: CallGraph.h:90
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
Definition: CallGraph.cpp:158
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1435
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:246
Recursive AST visitor that supports extension via dynamic dispatch.
virtual bool TraverseDecl(Decl *D)
Recursively visit a declaration, by dispatching to Traverse*Decl() based on the argument's dynamic ty...
This represents one expression.
Definition: Expr.h:110
Represents a function declaration or definition.
Definition: Decl.h:1935
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition: Decl.h:2249
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition: Decl.h:2217
bool isGlobal() const
Determines whether this is a global function.
Definition: Decl.cpp:3512
ObjCMethodDecl - Represents an instance or class method declaration.
Definition: DeclObjC.h:140
Stmt - This represents one statement.
Definition: Stmt.h:84
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
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:204
Diagnostic wrappers for TextAPI types for error reporting.
Definition: Dominators.h:30
CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
Definition: CallGraph.h:152
static clang::CallGraphNode::CallRecord getTombstoneKey()
Definition: CallGraph.h:221
static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val)
Definition: CallGraph.h:227
static bool isEqual(const clang::CallGraphNode::CallRecord &LHS, const clang::CallGraphNode::CallRecord &RHS)
Definition: CallGraph.h:233
static clang::CallGraphNode::CallRecord getEmptyKey()
Definition: CallGraph.h:215
static ChildIteratorType child_begin(NodeType *N)
Definition: CallGraph.h:246
static NodeType * getEntryNode(clang::CallGraphNode *CGN)
Definition: CallGraph.h:245
static ChildIteratorType child_end(NodeType *N)
Definition: CallGraph.h:247
static clang::CallGraphNode * CGGetValue(clang::CallGraph::const_iterator::value_type &P)
Definition: CallGraph.h:267
static NodeType * getEntryNode(clang::CallGraph *CGN)
Definition: CallGraph.h:262
static nodes_iterator nodes_end(clang::CallGraph *CG)
Definition: CallGraph.h:279
static nodes_iterator nodes_begin(clang::CallGraph *CG)
Definition: CallGraph.h:275
static unsigned size(clang::CallGraph *CG)
Definition: CallGraph.h:283
mapped_iterator< clang::CallGraph::iterator, decltype(&CGGetValue)> nodes_iterator
Definition: CallGraph.h:273
static ChildIteratorType child_begin(NodeType *N)
Definition: CallGraph.h:256
static ChildIteratorType child_end(NodeType *N)
Definition: CallGraph.h:257
static NodeType * getEntryNode(const clang::CallGraphNode *CGN)
Definition: CallGraph.h:255
static nodes_iterator nodes_begin(const clang::CallGraph *CG)
Definition: CallGraph.h:301
static unsigned size(const clang::CallGraph *CG)
Definition: CallGraph.h:309
mapped_iterator< clang::CallGraph::const_iterator, decltype(&CGGetValue)> nodes_iterator
Definition: CallGraph.h:299
static clang::CallGraphNode * CGGetValue(clang::CallGraph::const_iterator::value_type &P)
Definition: CallGraph.h:293
static NodeType * getEntryNode(const clang::CallGraph *CGN)
Definition: CallGraph.h:288
static nodes_iterator nodes_end(const clang::CallGraph *CG)
Definition: CallGraph.h:305