clang 10.0.0git

CallGraph.cpp
Go to the documentation of this file.
1 //===- CallGraph.cpp - AST-based Call graph -------------------------------===//
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 defines the AST-based CallGraph.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/Analysis/CallGraph.h"
14 #include "clang/AST/Decl.h"
15 #include "clang/AST/DeclBase.h"
16 #include "clang/AST/DeclObjC.h"
17 #include "clang/AST/Expr.h"
18 #include "clang/AST/ExprObjC.h"
19 #include "clang/AST/Stmt.h"
20 #include "clang/AST/StmtVisitor.h"
21 #include "clang/Basic/IdentifierTable.h"
22 #include "clang/Basic/LLVM.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/DOTGraphTraits.h"
29 #include "llvm/Support/GraphWriter.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include <cassert>
32 #include <memory>
33 #include <string>
34
35 using namespace clang;
36
37 #define DEBUG_TYPE "CallGraph"
38
39 STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
40 STATISTIC(NumBlockCallEdges, "Number of block call edges");
41
42 namespace {
43
44 /// A helper class, which walks the AST and locates all the call sites in the
45 /// given function body.
46 class CGBuilder : public StmtVisitor<CGBuilder> {
47 CallGraph *G;
48 CallGraphNode *CallerNode;
49
50 public:
51 CGBuilder(CallGraph *g, CallGraphNode *N) : G(g), CallerNode(N) {}
52
53 void VisitStmt(Stmt *S) { VisitChildren(S); }
54
55 Decl *getDeclFromCall(CallExpr *CE) {
56 if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
57 return CalleeDecl;
58
59 // Simple detection of a call through a block.
60 Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
61 if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
62 NumBlockCallEdges++;
63 return Block->getBlockDecl();
64 }
65
66 return nullptr;
67 }
68
69 void addCalledDecl(Decl *D) {
70 if (G->includeInGraph(D)) {
71 CallGraphNode *CalleeNode = G->getOrInsertNode(D);
72 CallerNode->addCallee(CalleeNode);
73 }
74 }
75
76 void VisitCallExpr(CallExpr *CE) {
77 if (Decl *D = getDeclFromCall(CE))
78 addCalledDecl(D);
79 VisitChildren(CE);
80 }
81
82 void VisitLambdaExpr(LambdaExpr *LE) {
83 if (FunctionTemplateDecl *FTD = LE->getDependentCallOperator())
84 for (FunctionDecl *FD : FTD->specializations())
85 G->VisitFunctionDecl(FD);
86 else if (CXXMethodDecl *MD = LE->getCallOperator())
87 G->VisitFunctionDecl(MD);
88 }
89
90 void VisitCXXNewExpr(CXXNewExpr *E) {
91 if (FunctionDecl *FD = E->getOperatorNew())
92 addCalledDecl(FD);
93 VisitChildren(E);
94 }
95
96 void VisitCXXConstructExpr(CXXConstructExpr *E) {
97 CXXConstructorDecl *Ctor = E->getConstructor();
98 if (FunctionDecl *Def = Ctor->getDefinition())
99 addCalledDecl(Def);
100 VisitChildren(E);
101 }
102
103 // Include the evaluation of the default argument.
104 void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
105 Visit(E->getExpr());
106 }
107
108 // Include the evaluation of the default initializers in a class.
109 void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
110 Visit(E->getExpr());
111 }
112
113 // Adds may-call edges for the ObjC message sends.
114 void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
115 if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
116 Selector Sel = ME->getSelector();
117
118 // Find the callee definition within the same translation unit.
119 Decl *D = nullptr;
120 if (ME->isInstanceMessage())
121 D = IDecl->lookupPrivateMethod(Sel);
122 else
123 D = IDecl->lookupPrivateClassMethod(Sel);
124 if (D) {
125 addCalledDecl(D);
126 NumObjCCallEdges++;
127 }
128 }
129 }
130
131 void VisitChildren(Stmt *S) {
132 for (Stmt *SubStmt : S->children())
133 if (SubStmt)
134 this->Visit(SubStmt);
135 }
136 };
137
138 } // namespace
139
140 void CallGraph::addNodesForBlocks(DeclContext *D) {
141 if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
142 addNodeForDecl(BD, true);
143
144 for (auto *I : D->decls())
145 if (auto *DC = dyn_cast<DeclContext>(I))
146 addNodesForBlocks(DC);
147 }
148
149 CallGraph::CallGraph() {
150 Root = getOrInsertNode(nullptr);
151 }
152
153 CallGraph::~CallGraph() = default;
154
155 bool CallGraph::includeInGraph(const Decl *D) {
156 assert(D);
157 if (!D->hasBody())
158 return false;
159
160 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
161 // We skip function template definitions, as their semantics is
162 // only determined when they are instantiated.
163 if (FD->isDependentContext())
164 return false;
165
166 IdentifierInfo *II = FD->getIdentifier();
167 if (II && II->getName().startswith("__inline"))
168 return false;
169 }
170
171 return true;
172 }
173
174 void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
175 assert(D);
176
177 // Allocate a new node, mark it as root, and process its calls.
178 CallGraphNode *Node = getOrInsertNode(D);
179
180 // Process all the calls by this function as well.
181 CGBuilder builder(this, Node);
182 if (Stmt *Body = D->getBody())
183 builder.Visit(Body);
184
185 // Include C++ constructor member initializers.
186 if (auto constructor = dyn_cast<CXXConstructorDecl>(D)) {
187 for (CXXCtorInitializer *init : constructor->inits()) {
188 builder.Visit(init->getInit());
189 }
190 }
191 }
192
193 CallGraphNode *CallGraph::getNode(const Decl *F) const {
194 FunctionMapTy::const_iterator I = FunctionMap.find(F);
195 if (I == FunctionMap.end()) return nullptr;
196 return I->second.get();
197 }
198
199 CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
200 if (F && !isa<ObjCMethodDecl>(F))
201 F = F->getCanonicalDecl();
202
203 std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
204 if (Node)
205 return Node.get();
206
207 Node = std::make_unique<CallGraphNode>(F);
208 // Make Root node a parent of all functions to make sure all are reachable.
209 if (F)
210 Root->addCallee(Node.get());
211 return Node.get();
212 }
213
214 void CallGraph::print(raw_ostream &OS) const {
215 OS << " --- Call graph Dump --- \n";
216
217 // We are going to print the graph in reverse post order, partially, to make
218 // sure the output is deterministic.
219 llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(this);
220 for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
221 I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
222 const CallGraphNode *N = *I;
223
224 OS << " Function: ";
225 if (N == Root)
226 OS << "< root >";
227 else
228 N->print(OS);
229
230 OS << " calls: ";
231 for (CallGraphNode::const_iterator CI = N->begin(),
232 CE = N->end(); CI != CE; ++CI) {
233 assert(*CI != Root && "No one can call the root node.");
234 (*CI)->print(OS);
235 OS << " ";
236 }
237 OS << '\n';
238 }
239 OS.flush();
240 }
241
242 LLVM_DUMP_METHOD void CallGraph::dump() const {
243 print(llvm::errs());
244 }
245
246 void CallGraph::viewGraph() const {
247 llvm::ViewGraph(this, "CallGraph");
248 }
249
250 void CallGraphNode::print(raw_ostream &os) const {
251 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
252 return ND->printQualifiedName(os);
253 os << "< >";
254 }
255
256 LLVM_DUMP_METHOD void CallGraphNode::dump() const {
257 print(llvm::errs());
258 }
259
260 namespace llvm {
261
262 template <>
263 struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
264 DOTGraphTraits (bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
265
266 static std::string getNodeLabel(const CallGraphNode *Node,
267 const CallGraph *CG) {
268 if (CG->getRoot() == Node) {
269 return "< root >";
270 }
271 if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
272 return ND->getNameAsString();
273 else
274 return "< >";
275 }
276 };
277
278 } // namespace llvm
clang::CallGraph
The AST-based call graph.
Definition: CallGraph.h:41
clang::FunctionDecl::getDefinition
FunctionDecl * getDefinition()
Get the definition for this declaration.
Definition: Decl.h:2039
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1783
clang::Selector
Smart pointer class that efficiently represents Objective-C method names.
Definition: IdentifierTable.h:685
clang::CallGraph::dump
void dump() const
Definition: CallGraph.cpp:242
clang::ObjCMessageExpr::getSelector
Selector getSelector() const
Definition: ExprObjC.cpp:337
clang::Decl::getBody
virtual Stmt * getBody() const
getBody - If this Decl represents a declaration for a body of code, such as a function or method defi...
Definition: DeclBase.h:986
llvm
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion...
Definition: Dominators.h:30
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:66
clang::ObjCMessageExpr::getReceiverInterface
ObjCInterfaceDecl * getReceiverInterface() const
Retrieve the Objective-C interface to which this message is being directed, if known.
Definition: ExprObjC.cpp:358
clang::ast_type_traits::DynTypedNode::get
const T * get() const
Retrieve the stored node as type T.
Definition: ASTTypeTraits.h:251
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:88
clang::CXXNewExpr::getOperatorNew
FunctionDecl * getOperatorNew() const
Definition: ExprCXX.h:2218
clang::CXXConstructExpr
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1422
DeclObjC.h
clang::CallGraphNode::dump
void dump() const
Definition: CallGraph.cpp:256
DOTGraphTraits
DOTGraphTraits(bool isSimple=false)
Definition: ExprEngine.cpp:3120
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2383
clang::CallGraphNode::const_iterator
SmallVectorImpl< CallRecord >::const_iterator const_iterator
Definition: CallGraph.h:159
clang::DeclContext::decls
decl_range decls() const
decls_begin/decls_end - Iterate over the declarations stored in this context.
Definition: DeclBase.h:2033
clang::CallGraph::getOrInsertNode
CallGraphNode * getOrInsertNode(Decl *)
Lookup the node for the given declaration.
Definition: CallGraph.cpp:199
clang::CallGraphNode
Definition: CallGraph.h:144
clang::CallGraphNode::addCallee
void addCallee(CallGraphNode *N)
Definition: CallGraph.h:170
print
void print(llvm::raw_ostream &OS, const Pointer &P, ASTContext &Ctx, QualType Ty)
Definition: InterpFrame.cpp:62
clang::LambdaExpr::getDependentCallOperator
FunctionTemplateDecl * getDependentCallOperator() const
Retrieve the function template call operator associated with this lambda expression.
Definition: ExprCXX.cpp:1292
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:57
llvm::DOTGraphTraits< const CallGraph * >::DOTGraphTraits
DOTGraphTraits(bool isSimple=false)
Definition: CallGraph.cpp:264
LLVM.h
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
clang::Stmt::children
child_range children()
Definition: Stmt.cpp:224
clang::CallGraph::addNodesForBlocks
void addNodesForBlocks(DeclContext *D)
Definition: CallGraph.cpp:140
CallGraph.h
ExprObjC.h
clang::LambdaExpr
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition: ExprCXX.h:1818
clang::ObjCInterfaceDecl
Represents an ObjC class declaration.
Definition: DeclObjC.h:1186
clang::CXXDefaultArgExpr
A default argument (C++ [dcl.fct.default]).
Definition: ExprCXX.h:1202
clang::Decl::getCanonicalDecl
virtual Decl * getCanonicalDecl()
Retrieves the "canonical" declaration of the given declaration.
Definition: DeclBase.h:877
clang::CallGraphNode::getDecl
Decl * getDecl() const
Definition: CallGraph.h:174
DefaultDOTGraphTraits
clang::CXXDefaultInitExpr::getExpr
const Expr * getExpr() const
Get the initialization expression that will be used.
Definition: ExprCXX.h:1307
clang::CXXConstructExpr::getConstructor
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition: ExprCXX.h:1494
clang::CallGraph::CallGraph
CallGraph()
Definition: CallGraph.cpp:149
llvm::DOTGraphTraits< const CallGraph * >::getNodeLabel
static std::string getNodeLabel(const CallGraphNode *Node, const CallGraph *CG)
Definition: CallGraph.cpp:266
STATISTIC
STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges")
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4037
clang::Expr
This represents one expression.
Definition: Expr.h:108
clang::CallGraphNode::end
iterator end()
Definition: CallGraph.h:163
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5579
clang::CallExpr::getCallee
Expr * getCallee()
Definition: Expr.h:2663
DeclBase.h
clang::CallExpr::getDirectCallee
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition: Expr.h:2681
IdentifierTable.h
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
StmtVisitor.h
Stmt.h
clang::ObjCMessageExpr
An expression that sends a message to the given Objective-C object or class.
Definition: ExprObjC.h:950
clang::CallGraph::print
void print(raw_ostream &os) const
Definition: CallGraph.cpp:214
clang::LambdaExpr::getCallOperator
CXXMethodDecl * getCallOperator() const
Retrieve the function call operator associated with this lambda expression.
Definition: ExprCXX.cpp:1287
clang::Decl::hasBody
virtual bool hasBody() const
Returns true if this Decl represents a declaration for a body of code, such as a function or method d...
Definition: DeclBase.h:992
clang::CallGraph::VisitFunctionDecl
bool VisitFunctionDecl(FunctionDecl *FD)
Part of recursive declaration visitation.
Definition: CallGraph.h:106
clang::CXXNewExpr
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)"...
Definition: ExprCXX.h:2100
clang::CXXMethodDecl
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1931
clang::CallGraph::includeInGraph
static bool includeInGraph(const Decl *D)
Determine if a declaration should be included in the graph.
Definition: CallGraph.cpp:155
clang::CallGraph::~CallGraph
~CallGraph()
clang::StmtVisitor
StmtVisitor - This class implements a simple visitor for Stmt subclasses.
Definition: StmtVisitor.h:183
clang::CallGraph::viewGraph
void viewGraph() const
Definition: CallGraph.cpp:246
clang::CallGraphNode::print
void print(raw_ostream &os) const
Definition: CallGraph.cpp:250
clang::IdentifierInfo::getName
StringRef getName() const
Return the actual identifier string.
Definition: IdentifierTable.h:155
Node
ast_type_traits::DynTypedNode Node
Definition: ASTMatchFinder.cpp:60
Decl.h
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1271
clang::CallGraph::getNode
CallGraphNode * getNode(const Decl *) const
Lookup the node for the given declaration.
Definition: CallGraph.cpp:193
clang::CallGraph::getRoot
CallGraphNode * getRoot() const
\ brief Get the virtual root of the graph, all the functions available externally are represented as ...
Definition: CallGraph.h:90
clang::CXXDefaultArgExpr::getExpr
const Expr * getExpr() const
Definition: ExprCXX.h:1241
clang::CallGraphNode::begin
iterator begin()
Iterators through all the callees/children of the node.
Definition: CallGraph.h:162
clang::Expr::IgnoreParenImpCasts
Expr * IgnoreParenImpCasts() LLVM_READONLY
Skip past any parentheses and implicit casts which might surround this expression until reaching a fi...
Definition: Expr.cpp:2990
clang::CXXCtorInitializer
Represents a C++ base or member initializer.
Definition: DeclCXX.h:2155
clang::CXXDefaultInitExpr
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1279
clang::interp::LE
bool LE(InterpState &S, CodePtr OpPC)
Definition: Interp.h:237
Expr.h
clang::CallExpr
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2546
clang::NamedDecl
This represents a decl that may have a name.
Definition: Decl.h:223
clang::ObjCMessageExpr::isInstanceMessage
bool isInstanceMessage() const
Determine whether this is an instance message to either a computed object or to super.
Definition: ExprObjC.h:1248
clang::FunctionTemplateDecl
Declaration of a template function.
Definition: DeclTemplate.h:977