Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
16.7k
  CGBuilder(CallGraph *g, CallGraphNode *N) : G(g), CallerNode(N) {}
52
53
269k
  void VisitStmt(Stmt *S) { VisitChildren(S); }
54
55
22.6k
  Decl *getDeclFromCall(CallExpr *CE) {
56
22.6k
    if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
57
22.4k
      return CalleeDecl;
58
217
59
217
    // Simple detection of a call through a block.
60
217
    Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
61
217
    if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
62
76
      NumBlockCallEdges++;
63
76
      return Block->getBlockDecl();
64
76
    }
65
141
66
141
    return nullptr;
67
141
  }
68
69
22.8k
  void addCalledDecl(Decl *D) {
70
22.8k
    if (G->includeInGraph(D)) {
71
6.21k
      CallGraphNode *CalleeNode = G->getOrInsertNode(D);
72
6.21k
      CallerNode->addCallee(CalleeNode);
73
6.21k
    }
74
22.8k
  }
75
76
22.6k
  void VisitCallExpr(CallExpr *CE) {
77
22.6k
    if (Decl *D = getDeclFromCall(CE))
78
22.5k
      addCalledDecl(D);
79
22.6k
    VisitChildren(CE);
80
22.6k
  }
81
82
  // Adds may-call edges for the ObjC message sends.
83
2.82k
  void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
84
2.82k
    if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
85
2.40k
      Selector Sel = ME->getSelector();
86
2.40k
87
2.40k
      // Find the callee definition within the same translation unit.
88
2.40k
      Decl *D = nullptr;
89
2.40k
      if (ME->isInstanceMessage())
90
2.17k
        D = IDecl->lookupPrivateMethod(Sel);
91
232
      else
92
232
        D = IDecl->lookupPrivateClassMethod(Sel);
93
2.40k
      if (D) {
94
342
        addCalledDecl(D);
95
342
        NumObjCCallEdges++;
96
342
      }
97
2.40k
    }
98
2.82k
  }
99
100
292k
  void VisitChildren(Stmt *S) {
101
292k
    for (Stmt *SubStmt : S->children())
102
279k
      if (SubStmt)
103
278k
        this->Visit(SubStmt);
104
292k
  }
105
};
106
107
} // namespace
108
109
18.1k
void CallGraph::addNodesForBlocks(DeclContext *D) {
110
18.1k
  if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
111
349
    addNodeForDecl(BD, true);
112
18.1k
113
18.1k
  for (auto *I : D->decls())
114
27.9k
    if (auto *DC = dyn_cast<DeclContext>(I))
115
1.77k
      addNodesForBlocks(DC);
116
18.1k
}
117
118
883
CallGraph::CallGraph() {
119
883
  Root = getOrInsertNode(nullptr);
120
883
}
121
122
883
CallGraph::~CallGraph() = default;
123
124
78.5k
bool CallGraph::includeInGraph(const Decl *D) {
125
78.5k
  assert(D);
126
78.5k
  if (!D->hasBody())
127
41.5k
    return false;
128
37.0k
129
37.0k
  if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
130
35.5k
    // We skip function template definitions, as their semantics is
131
35.5k
    // only determined when they are instantiated.
132
35.5k
    if (FD->isDependentContext())
133
14.3k
      return false;
134
21.2k
135
21.2k
    IdentifierInfo *II = FD->getIdentifier();
136
21.2k
    if (II && 
II->getName().startswith("__inline")16.8k
)
137
6
      return false;
138
22.7k
  }
139
22.7k
140
22.7k
  return true;
141
22.7k
}
142
143
16.7k
void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
144
16.7k
  assert(D);
145
16.7k
146
16.7k
  // Allocate a new node, mark it as root, and process it's calls.
147
16.7k
  CallGraphNode *Node = getOrInsertNode(D);
148
16.7k
149
16.7k
  // Process all the calls by this function as well.
150
16.7k
  CGBuilder builder(this, Node);
151
16.7k
  if (Stmt *Body = D->getBody())
152
16.7k
    builder.Visit(Body);
153
16.7k
}
154
155
0
CallGraphNode *CallGraph::getNode(const Decl *F) const {
156
0
  FunctionMapTy::const_iterator I = FunctionMap.find(F);
157
0
  if (I == FunctionMap.end()) return nullptr;
158
0
  return I->second.get();
159
0
}
160
161
23.8k
CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
162
23.8k
  if (F && 
!isa<ObjCMethodDecl>(F)22.9k
)
163
21.5k
    F = F->getCanonicalDecl();
164
23.8k
165
23.8k
  std::unique_ptr<CallGraphNode> &Node = FunctionMap[F];
166
23.8k
  if (Node)
167
6.77k
    return Node.get();
168
17.0k
169
17.0k
  Node = llvm::make_unique<CallGraphNode>(F);
170
17.0k
  // Make Root node a parent of all functions to make sure all are reachable.
171
17.0k
  if (F)
172
16.1k
    Root->addCallee(Node.get());
173
17.0k
  return Node.get();
174
17.0k
}
175
176
1
void CallGraph::print(raw_ostream &OS) const {
177
1
  OS << " --- Call graph Dump --- \n";
178
1
179
1
  // We are going to print the graph in reverse post order, partially, to make
180
1
  // sure the output is deterministic.
181
1
  llvm::ReversePostOrderTraversal<const CallGraph *> RPOT(this);
182
1
  for (llvm::ReversePostOrderTraversal<const CallGraph *>::rpo_iterator
183
19
         I = RPOT.begin(), E = RPOT.end(); I != E; 
++I18
) {
184
18
    const CallGraphNode *N = *I;
185
18
186
18
    OS << "  Function: ";
187
18
    if (N == Root)
188
1
      OS << "< root >";
189
17
    else
190
17
      N->print(OS);
191
18
192
18
    OS << " calls: ";
193
18
    for (CallGraphNode::const_iterator CI = N->begin(),
194
48
                                       CE = N->end(); CI != CE; 
++CI30
) {
195
30
      assert(*CI != Root && "No one can call the root node.");
196
30
      (*CI)->print(OS);
197
30
      OS << " ";
198
30
    }
199
18
    OS << '\n';
200
18
  }
201
1
  OS.flush();
202
1
}
203
204
1
LLVM_DUMP_METHOD void CallGraph::dump() const {
205
1
  print(llvm::errs());
206
1
}
207
208
0
void CallGraph::viewGraph() const {
209
0
  llvm::ViewGraph(this, "CallGraph");
210
0
}
211
212
47
void CallGraphNode::print(raw_ostream &os) const {
213
47
  if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
214
44
      return ND->printQualifiedName(os);
215
3
  os << "< >";
216
3
}
217
218
0
LLVM_DUMP_METHOD void CallGraphNode::dump() const {
219
0
  print(llvm::errs());
220
0
}
221
222
namespace llvm {
223
224
template <>
225
struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
226
0
  DOTGraphTraits (bool isSimple = false) : DefaultDOTGraphTraits(isSimple) {}
227
228
  static std::string getNodeLabel(const CallGraphNode *Node,
229
0
                                  const CallGraph *CG) {
230
0
    if (CG->getRoot() == Node) {
231
0
      return "< root >";
232
0
    }
233
0
    if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
234
0
      return ND->getNameAsString();
235
0
    else
236
0
      return "< >";
237
0
  }
238
};
239
240
} // namespace llvm