Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Analysis/CallGraph.cpp
Line
Count
Source (jump to first uncovered line)
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
20.1k
  CGBuilder(CallGraph *g, CallGraphNode *N) : G(g), CallerNode(N) {}
52
53
306k
  void VisitStmt(Stmt *S) { VisitChildren(S); }
54
55
29.7k
  Decl *getDeclFromCall(CallExpr *CE) {
56
29.7k
    if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
57
29.5k
      return CalleeDecl;
58
222
59
222
    // Simple detection of a call through a block.
60
222
    Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
61
222
    if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
62
77
      NumBlockCallEdges++;
63
77
      return Block->getBlockDecl();
64
77
    }
65
145
66
145
    return nullptr;
67
145
  }
68
69
40.0k
  void addCalledDecl(Decl *D, Expr *CallExpr) {
70
40.0k
    if (G->includeInGraph(D)) {
71
16.3k
      CallGraphNode *CalleeNode = G->getOrInsertNode(D);
72
16.3k
      CallerNode->addCallee({CalleeNode, CallExpr});
73
16.3k
    }
74
40.0k
  }
75
76
29.7k
  void VisitCallExpr(CallExpr *CE) {
77
29.7k
    if (Decl *D = getDeclFromCall(CE))
78
29.6k
      addCalledDecl(D, CE);
79
29.7k
    VisitChildren(CE);
80
29.7k
  }
81
82
205
  void VisitLambdaExpr(LambdaExpr *LE) {
83
205
    if (FunctionTemplateDecl *FTD = LE->getDependentCallOperator())
84
2
      for (FunctionDecl *FD : FTD->specializations())
85
2
        G->VisitFunctionDecl(FD);
86
203
    else if (CXXMethodDecl *MD = LE->getCallOperator())
87
203
      G->VisitFunctionDecl(MD);
88
205
  }
89
90
867
  void VisitCXXNewExpr(CXXNewExpr *E) {
91
867
    if (FunctionDecl *FD = E->getOperatorNew())
92
867
      addCalledDecl(FD, E);
93
867
    VisitChildren(E);
94
867
  }
95
96
9.86k
  void VisitCXXConstructExpr(CXXConstructExpr *E) {
97
9.86k
    CXXConstructorDecl *Ctor = E->getConstructor();
98
9.86k
    if (FunctionDecl *Def = Ctor->getDefinition())
99
8.98k
      addCalledDecl(Def, E);
100
9.86k
    VisitChildren(E);
101
9.86k
  }
102
103
  // Include the evaluation of the default argument.
104
1.69k
  void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) {
105
1.69k
    Visit(E->getExpr());
106
1.69k
  }
107
108
  // Include the evaluation of the default initializers in a class.
109
113
  void VisitCXXDefaultInitExpr(CXXDefaultInitExpr *E) {
110
113
    Visit(E->getExpr());
111
113
  }
112
113
  // Adds may-call edges for the ObjC message sends.
114
2.86k
  void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
115
2.86k
    if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
116
2.44k
      Selector Sel = ME->getSelector();
117
2.44k
118
2.44k
      // Find the callee definition within the same translation unit.
119
2.44k
      Decl *D = nullptr;
120
2.44k
      if (ME->isInstanceMessage())
121
2.21k
        D = IDecl->lookupPrivateMethod(Sel);
122
232
      else
123
232
        D = IDecl->lookupPrivateClassMethod(Sel);
124
2.44k
      if (D) {
125
579
        addCalledDecl(D, ME);
126
579
        NumObjCCallEdges++;
127
579
      }
128
2.44k
    }
129
2.86k
  }
130
131
347k
  void VisitChildren(Stmt *S) {
132
347k
    for (Stmt *SubStmt : S->children())
133
328k
      if (SubStmt)
134
327k
        this->Visit(SubStmt);
135
347k
  }
136
};
137
138
} // namespace
139
140
21.6k
void CallGraph::addNodesForBlocks(DeclContext *D) {
141
21.6k
  if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
142
353
    addNodeForDecl(BD, true);
143
21.6k
144
21.6k
  for (auto *I : D->decls())
145
31.2k
    if (auto *DC = dyn_cast<DeclContext>(I))
146
1.92k
      addNodesForBlocks(DC);
147
21.6k
}
148
149
1.22k
CallGraph::CallGraph() {
150
1.22k
  Root = getOrInsertNode(nullptr);
151
1.22k
}
152
153
1.22k
CallGraph::~CallGraph() = default;
154
155
118k
bool CallGraph::includeInGraph(const Decl *D) {
156
118k
  assert(D);
157
118k
  if (!D->hasBody())
158
64.7k
    return false;
159
53.2k
160
53.2k
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
161
51.7k
    // We skip function template definitions, as their semantics is
162
51.7k
    // only determined when they are instantiated.
163
51.7k
    if (FD->isDependentContext())
164
16.9k
      return false;
165
34.7k
166
34.7k
    IdentifierInfo *II = FD->getIdentifier();
167
34.7k
    if (II && 
II->getName().startswith("__inline")19.2k
)
168
6
      return false;
169
36.2k
  }
170
36.2k
171
36.2k
  return true;
172
36.2k
}
173
174
20.1k
void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
175
20.1k
  assert(D);
176
20.1k
177
20.1k
  // Allocate a new node, mark it as root, and process its calls.
178
20.1k
  CallGraphNode *Node = getOrInsertNode(D);
179
20.1k
180
20.1k
  // Process all the calls by this function as well.
181
20.1k
  CGBuilder builder(this, Node);
182
20.1k
  if (Stmt *Body = D->getBody())
183
20.1k
    builder.Visit(Body);
184
20.1k
185
20.1k
  // Include C++ constructor member initializers.
186
20.1k
  if (auto constructor = dyn_cast<CXXConstructorDecl>(D)) {
187
2.94k
    for (CXXCtorInitializer *init : constructor->inits()) {
188
2.45k
      builder.Visit(init->getInit());
189
2.45k
    }
190
2.94k
  }
191
20.1k
}
192
193
0
CallGraphNode *CallGraph::getNode(const Decl *F) const {
194
0
  FunctionMapTy::const_iterator I = FunctionMap.find(F);
195
0
  if (I == FunctionMap.end()) return nullptr;
196
0
  return I->second.get();
197
0
}
198
199
37.6k
CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
200
37.6k
  if (F && 
!isa<ObjCMethodDecl>(F)36.4k
)
201
35.0k
    F = F->getCanonicalDecl();
202
37.6k
203
37.6k
  std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
204
37.6k
  if (Node)
205
17.0k
    return Node.get();
206
20.6k
207
20.6k
  Node = std::make_unique<CallGraphNode>(F);
208
20.6k
  // Make Root node a parent of all functions to make sure all are reachable.
209
20.6k
  if (F)
210
19.3k
    Root->addCallee({Node.get(), /*Call=*/nullptr});
211
20.6k
  return Node.get();
212
20.6k
}
213
214
2
void CallGraph::print(raw_ostream &OS) const {
215
2
  OS << " --- Call graph Dump --- \n";
216
2
217
2
  // We are going to print the graph in reverse post order, partially, to make
218
2
  // sure the output is deterministic.
219
2
  llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(this);
220
2
  for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
221
29
         I = RPOT.begin(), E = RPOT.end(); I != E; 
++I27
) {
222
27
    const CallGraphNode *N = *I;
223
27
224
27
    OS << "  Function: ";
225
27
    if (N == Root)
226
2
      OS << "< root >";
227
25
    else
228
25
      N->print(OS);
229
27
230
27
    OS << " calls: ";
231
27
    for (CallGraphNode::const_iterator CI = N->begin(),
232
74
                                       CE = N->end(); CI != CE; 
++CI47
) {
233
47
      assert(CI->Callee != Root && "No one can call the root node.");
234
47
      CI->Callee->print(OS);
235
47
      OS << " ";
236
47
    }
237
27
    OS << '\n';
238
27
  }
239
2
  OS.flush();
240
2
}
241
242
2
LLVM_DUMP_METHOD void CallGraph::dump() const {
243
2
  print(llvm::errs());
244
2
}
245
246
0
void CallGraph::viewGraph() const {
247
0
  llvm::ViewGraph(this, "CallGraph");
248
0
}
249
250
72
void CallGraphNode::print(raw_ostream &os) const {
251
72
  if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
252
69
      return ND->printQualifiedName(os);
253
3
  os << "< >";
254
3
}
255
256
0
LLVM_DUMP_METHOD void CallGraphNode::dump() const {
257
0
  print(llvm::errs());
258
0
}
259
260
namespace llvm {
261
262
template <>
263
struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
264
0
  DOTGraphTraits (bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
265
266
  static std::string getNodeLabel(const CallGraphNode *Node,
267
0
                                  const CallGraph *CG) {
268
0
    if (CG->getRoot() == Node) {
269
0
      return "< root >";
270
0
    }
271
0
    if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
272
0
      return ND->getNameAsString();
273
0
    else
274
0
      return "< >";
275
0
  }
276
};
277
278
} // namespace llvm