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 // We are only collecting the declarations, so do not step into the bodies.
139 bool TraverseStmt(Stmt *S) override { return true; }
140
141private:
142 /// Add the given declaration to the call graph.
143 void addNodeForDecl(Decl *D, bool IsGlobal);
144};
145
147public:
148 struct CallRecord {
151
152 CallRecord() = default;
153
154 CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
155 : Callee(Callee_), CallExpr(CallExpr_) {}
156
157 // The call destination is the only important data here,
158 // allow to transparently unwrap into it.
159 operator CallGraphNode *() const { return Callee; }
160 };
161
162private:
163 /// The function/method declaration.
164 Decl *FD;
165
166 /// The list of functions called from this node.
167 SmallVector<CallRecord, 5> CalledFunctions;
168
169public:
170 CallGraphNode(Decl *D) : FD(D) {}
171
174
175 /// Iterators through all the callees/children of the node.
176 iterator begin() { return CalledFunctions.begin(); }
177 iterator end() { return CalledFunctions.end(); }
178 const_iterator begin() const { return CalledFunctions.begin(); }
179 const_iterator end() const { return CalledFunctions.end(); }
180
181 /// Iterator access to callees/children of the node.
182 llvm::iterator_range<iterator> callees() {
183 return llvm::make_range(begin(), end());
184 }
185 llvm::iterator_range<const_iterator> callees() const {
186 return llvm::make_range(begin(), end());
187 }
188
189 bool empty() const { return CalledFunctions.empty(); }
190 unsigned size() const { return CalledFunctions.size(); }
191
192 void addCallee(CallRecord Call) { CalledFunctions.push_back(Call); }
193
194 Decl *getDecl() const { return FD; }
195
197 return getDecl()->getAsFunction()->getDefinition();
198 }
199
200 void print(raw_ostream &os) const;
201 void dump() const;
202};
203
204// NOTE: we are comparing based on the callee only. So different call records
205// (with different call expressions) to the same callee will compare equal!
207 const CallGraphNode::CallRecord &RHS) {
208 return LHS.Callee == RHS.Callee;
209}
210
211} // namespace clang
212
213namespace llvm {
214
215// Specialize DenseMapInfo for clang::CallGraphNode::CallRecord.
216template <> struct DenseMapInfo<clang::CallGraphNode::CallRecord> {
219 DenseMapInfo<clang::CallGraphNode *>::getEmptyKey(),
220 DenseMapInfo<clang::Expr *>::getEmptyKey());
221 }
222
225 DenseMapInfo<clang::CallGraphNode *>::getTombstoneKey(),
226 DenseMapInfo<clang::Expr *>::getTombstoneKey());
227 }
228
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:172
FunctionDecl * getDefinition() const
Definition CallGraph.h:196
llvm::iterator_range< const_iterator > callees() const
Definition CallGraph.h:185
const_iterator begin() const
Definition CallGraph.h:178
llvm::iterator_range< iterator > callees()
Iterator access to callees/children of the node.
Definition CallGraph.h:182
Decl * getDecl() const
Definition CallGraph.h:194
iterator begin()
Iterators through all the callees/children of the node.
Definition CallGraph.h:176
void print(raw_ostream &os) const
bool empty() const
Definition CallGraph.h:189
const_iterator end() const
Definition CallGraph.h:179
SmallVectorImpl< CallRecord >::const_iterator const_iterator
Definition CallGraph.h:173
unsigned size() const
Definition CallGraph.h:190
void addCallee(CallRecord Call)
Definition CallGraph.h:192
The AST-based call graph.
Definition CallGraph.h:44
void viewGraph() const
bool TraverseStmt(Stmt *S) override
Definition CallGraph.h:139
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:1449
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:2000
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2314
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition Decl.h:2282
bool isGlobal() const
Determines whether this is a global function.
Definition Decl.cpp:3632
ObjCMethodDecl - Represents an instance or class method declaration.
Definition DeclObjC.h:140
Stmt - This represents one statement.
Definition Stmt.h:86
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:206
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
Definition CallGraph.h:154
static clang::CallGraphNode::CallRecord getTombstoneKey()
Definition CallGraph.h:223
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 clang::CallGraphNode::CallRecord getEmptyKey()
Definition CallGraph.h:217
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