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
198 return FD ? FD->getDefinition() : nullptr;
199 }
200
201 void print(raw_ostream &os) const;
202 void dump() const;
203};
204
205// NOTE: we are comparing based on the callee only. So different call records
206// (with different call expressions) to the same callee will compare equal!
208 const CallGraphNode::CallRecord &RHS) {
209 return LHS.Callee == RHS.Callee;
210}
211
212} // namespace clang
213
214namespace llvm {
215
216// Specialize DenseMapInfo for clang::CallGraphNode::CallRecord.
217template <> struct DenseMapInfo<clang::CallGraphNode::CallRecord> {
218 static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val) {
219 // NOTE: we are comparing based on the callee only.
220 // Different call records with the same callee will compare equal!
221 return DenseMapInfo<clang::CallGraphNode *>::getHashValue(Val.Callee);
222 }
223
226 return LHS == RHS;
227 }
228};
229
230// Graph traits for iteration, viewing.
231template <> struct GraphTraits<clang::CallGraphNode*> {
235
236 static NodeType *getEntryNode(clang::CallGraphNode *CGN) { return CGN; }
237 static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
238 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
239};
240
241template <> struct GraphTraits<const clang::CallGraphNode*> {
245
246 static NodeType *getEntryNode(const clang::CallGraphNode *CGN) { return CGN; }
247 static ChildIteratorType child_begin(NodeType *N) { return N->begin();}
248 static ChildIteratorType child_end(NodeType *N) { return N->end(); }
249};
250
251template <> struct GraphTraits<clang::CallGraph*>
252 : public GraphTraits<clang::CallGraphNode*> {
254 return CGN->getRoot(); // Start at the external node!
255 }
256
257 static clang::CallGraphNode *
258 CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
259 return P.second.get();
260 }
261
262 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
264 mapped_iterator<clang::CallGraph::iterator, decltype(&CGGetValue)>;
265
269
271 return nodes_iterator(CG->end(), &CGGetValue);
272 }
273
274 static unsigned size(clang::CallGraph *CG) { return CG->size(); }
275};
276
277template <> struct GraphTraits<const clang::CallGraph*> :
278 public GraphTraits<const clang::CallGraphNode*> {
280 return CGN->getRoot();
281 }
282
283 static clang::CallGraphNode *
284 CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
285 return P.second.get();
286 }
287
288 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
290 mapped_iterator<clang::CallGraph::const_iterator, decltype(&CGGetValue)>;
291
293 return nodes_iterator(CG->begin(), &CGGetValue);
294 }
295
297 return nodes_iterator(CG->end(), &CGGetValue);
298 }
299
300 static unsigned size(const clang::CallGraph *CG) { return CG->size(); }
301};
302
303} // namespace llvm
304
305#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:1462
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:2018
bool isThisDeclarationADefinition() const
Returns whether this specific declaration of the function is also a definition that does not contain ...
Definition Decl.h:2332
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition Decl.h:2300
bool isGlobal() const
Determines whether this is a global function.
Definition Decl.cpp:3608
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:207
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 unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val)
Definition CallGraph.h:218
static bool isEqual(const clang::CallGraphNode::CallRecord &LHS, const clang::CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:224
static ChildIteratorType child_begin(NodeType *N)
Definition CallGraph.h:237
static NodeType * getEntryNode(clang::CallGraphNode *CGN)
Definition CallGraph.h:236
static ChildIteratorType child_end(NodeType *N)
Definition CallGraph.h:238
static clang::CallGraphNode * CGGetValue(clang::CallGraph::const_iterator::value_type &P)
Definition CallGraph.h:258
static NodeType * getEntryNode(clang::CallGraph *CGN)
Definition CallGraph.h:253
static nodes_iterator nodes_end(clang::CallGraph *CG)
Definition CallGraph.h:270
static nodes_iterator nodes_begin(clang::CallGraph *CG)
Definition CallGraph.h:266
static unsigned size(clang::CallGraph *CG)
Definition CallGraph.h:274
mapped_iterator< clang::CallGraph::iterator, decltype(&CGGetValue)> nodes_iterator
Definition CallGraph.h:263
static ChildIteratorType child_begin(NodeType *N)
Definition CallGraph.h:247
static ChildIteratorType child_end(NodeType *N)
Definition CallGraph.h:248
static NodeType * getEntryNode(const clang::CallGraphNode *CGN)
Definition CallGraph.h:246
static nodes_iterator nodes_begin(const clang::CallGraph *CG)
Definition CallGraph.h:292
static unsigned size(const clang::CallGraph *CG)
Definition CallGraph.h:300
static clang::CallGraphNode * CGGetValue(clang::CallGraph::const_iterator::value_type &P)
Definition CallGraph.h:284
static NodeType * getEntryNode(const clang::CallGraph *CGN)
Definition CallGraph.h:279
mapped_iterator< clang::CallGraph::const_iterator, decltype(&CGGetValue)> nodes_iterator
Definition CallGraph.h:289
static nodes_iterator nodes_end(const clang::CallGraph *CG)
Definition CallGraph.h:296