Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Analysis/CallGraph.h
Line
Count
Source (jump to first uncovered line)
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/RecursiveASTVisitor.h"
22
#include "llvm/ADT/DenseMap.h"
23
#include "llvm/ADT/GraphTraits.h"
24
#include "llvm/ADT/STLExtras.h"
25
#include "llvm/ADT/SetVector.h"
26
#include "llvm/ADT/SmallVector.h"
27
#include "llvm/ADT/iterator_range.h"
28
#include <memory>
29
30
namespace clang {
31
32
class CallGraphNode;
33
class Decl;
34
class DeclContext;
35
class Stmt;
36
37
/// The AST-based call graph.
38
///
39
/// The call graph extends itself with the given declarations by implementing
40
/// the recursive AST visitor, which constructs the graph by visiting the given
41
/// declarations.
42
class CallGraph : public RecursiveASTVisitor<CallGraph> {
43
  friend class CallGraphNode;
44
45
  using FunctionMapTy =
46
      llvm::DenseMap<const Decl *, std::unique_ptr<CallGraphNode>>;
47
48
  /// FunctionMap owns all CallGraphNodes.
49
  FunctionMapTy FunctionMap;
50
51
  /// This is a virtual root node that has edges to all the functions.
52
  CallGraphNode *Root;
53
54
public:
55
  CallGraph();
56
  ~CallGraph();
57
58
  /// Populate the call graph with the functions in the given
59
  /// declaration.
60
  ///
61
  /// Recursively walks the declaration to find all the dependent Decls as well.
62
29.9k
  void addToCallGraph(Decl *D) {
63
29.9k
    TraverseDecl(D);
64
29.9k
  }
65
66
  /// Determine if a declaration should be included in the graph.
67
  static bool includeInGraph(const Decl *D);
68
69
  /// Lookup the node for the given declaration.
70
  CallGraphNode *getNode(const Decl *) const;
71
72
  /// Lookup the node for the given declaration. If none found, insert
73
  /// one into the graph.
74
  CallGraphNode *getOrInsertNode(Decl *);
75
76
  using iterator = FunctionMapTy::iterator;
77
  using const_iterator = FunctionMapTy::const_iterator;
78
79
  /// Iterators through all the elements in the graph. Note, this gives
80
  /// non-deterministic order.
81
0
  iterator begin() { return FunctionMap.begin(); }
82
0
  iterator end()   { return FunctionMap.end();   }
83
0
  const_iterator begin() const { return FunctionMap.begin(); }
84
0
  const_iterator end()   const { return FunctionMap.end();   }
85
86
  /// Get the number of nodes in the graph.
87
0
  unsigned size() const { return FunctionMap.size(); }
88
89
  /// \ brief Get the virtual root of the graph, all the functions available
90
  /// externally are represented as callees of the node.
91
1.22k
  CallGraphNode *getRoot() const { return Root; }
92
93
  /// Iterators through all the nodes of the graph that have no parent. These
94
  /// are the unreachable nodes, which are either unused or are due to us
95
  /// failing to add a call edge due to the analysis imprecision.
96
  using nodes_iterator = llvm::SetVector<CallGraphNode *>::iterator;
97
  using const_nodes_iterator = llvm::SetVector<CallGraphNode *>::const_iterator;
98
99
  void print(raw_ostream &os) const;
100
  void dump() const;
101
  void viewGraph() const;
102
103
  void addNodesForBlocks(DeclContext *D);
104
105
  /// Part of recursive declaration visitation. We recursively visit all the
106
  /// declarations to collect the root functions.
107
72.1k
  bool VisitFunctionDecl(FunctionDecl *FD) {
108
72.1k
    // We skip function template definitions, as their semantics is
109
72.1k
    // only determined when they are instantiated.
110
72.1k
    if (includeInGraph(FD) && 
FD->isThisDeclarationADefinition()18.7k
) {
111
18.5k
      // Add all blocks declared inside this function to the graph.
112
18.5k
      addNodesForBlocks(FD);
113
18.5k
      // If this function has external linkage, anything could call it.
114
18.5k
      // Note, we are not precise here. For example, the function could have
115
18.5k
      // its address taken.
116
18.5k
      addNodeForDecl(FD, FD->isGlobal());
117
18.5k
    }
118
72.1k
    return true;
119
72.1k
  }
120
121
  /// Part of recursive declaration visitation.
122
5.67k
  bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
123
5.67k
    if (includeInGraph(MD)) {
124
1.12k
      addNodesForBlocks(MD);
125
1.12k
      addNodeForDecl(MD, true);
126
1.12k
    }
127
5.67k
    return true;
128
5.67k
  }
129
130
  // We are only collecting the declarations, so do not step into the bodies.
131
55.1k
  bool TraverseStmt(Stmt *S) { return true; }
132
133
319k
  bool shouldWalkTypesOfTypeLocs() const { return false; }
134
14.3k
  bool shouldVisitTemplateInstantiations() const { return true; }
135
264k
  bool shouldVisitImplicitCode() const { return true; }
136
137
private:
138
  /// Add the given declaration to the call graph.
139
  void addNodeForDecl(Decl *D, bool IsGlobal);
140
};
141
142
class CallGraphNode {
143
public:
144
  struct CallRecord {
145
    CallGraphNode *Callee;
146
    Expr *CallExpr;
147
148
    CallRecord() = default;
149
150
    CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
151
35.6k
        : Callee(Callee_), CallExpr(CallExpr_) {}
152
153
    // The call destination is the only important data here,
154
    // allow to transparently unwrap into it.
155
35.6k
    operator CallGraphNode *() const { return Callee; }
156
  };
157
158
private:
159
  /// The function/method declaration.
160
  Decl *FD;
161
162
  /// The list of functions called from this node.
163
  SmallVector<CallRecord, 5> CalledFunctions;
164
165
public:
166
20.5k
  CallGraphNode(Decl *D) : FD(D) {}
167
168
  using iterator = SmallVectorImpl<CallRecord>::iterator;
169
  using const_iterator = SmallVectorImpl<CallRecord>::const_iterator;
170
171
  /// Iterators through all the callees/children of the node.
172
20.5k
  iterator begin() { return CalledFunctions.begin(); }
173
56.1k
  iterator end() { return CalledFunctions.end(); }
174
54
  const_iterator begin() const { return CalledFunctions.begin(); }
175
101
  const_iterator end() const { return CalledFunctions.end(); }
176
177
  /// Iterator access to callees/children of the node.
178
0
  llvm::iterator_range<iterator> callees() {
179
0
    return llvm::make_range(begin(), end());
180
0
  }
181
0
  llvm::iterator_range<const_iterator> callees() const {
182
0
    return llvm::make_range(begin(), end());
183
0
  }
184
185
0
  bool empty() const { return CalledFunctions.empty(); }
186
0
  unsigned size() const { return CalledFunctions.size(); }
187
188
35.6k
  void addCallee(CallRecord Call) { CalledFunctions.push_back(Call); }
189
190
20.5k
  Decl *getDecl() const { return FD; }
191
192
  void print(raw_ostream &os) const;
193
  void dump() const;
194
};
195
196
// NOTE: we are comparing based on the callee only. So different call records
197
// (with different call expressions) to the same callee will compare equal!
198
inline bool operator==(const CallGraphNode::CallRecord &LHS,
199
0
                       const CallGraphNode::CallRecord &RHS) {
200
0
  return LHS.Callee == RHS.Callee;
201
0
}
202
203
} // namespace clang
204
205
namespace llvm {
206
207
// Specialize DenseMapInfo for clang::CallGraphNode::CallRecord.
208
template <> struct DenseMapInfo<clang::CallGraphNode::CallRecord> {
209
0
  static inline clang::CallGraphNode::CallRecord getEmptyKey() {
210
0
    return clang::CallGraphNode::CallRecord(
211
0
        DenseMapInfo<clang::CallGraphNode *>::getEmptyKey(),
212
0
        DenseMapInfo<clang::Expr *>::getEmptyKey());
213
0
  }
214
215
0
  static inline clang::CallGraphNode::CallRecord getTombstoneKey() {
216
0
    return clang::CallGraphNode::CallRecord(
217
0
        DenseMapInfo<clang::CallGraphNode *>::getTombstoneKey(),
218
0
        DenseMapInfo<clang::Expr *>::getTombstoneKey());
219
0
  }
220
221
0
  static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val) {
222
0
    // NOTE: we are comparing based on the callee only.
223
0
    // Different call records with the same callee will compare equal!
224
0
    return DenseMapInfo<clang::CallGraphNode *>::getHashValue(Val.Callee);
225
0
  }
226
227
  static bool isEqual(const clang::CallGraphNode::CallRecord &LHS,
228
0
                      const clang::CallGraphNode::CallRecord &RHS) {
229
0
    return LHS == RHS;
230
0
  }
231
};
232
233
// Graph traits for iteration, viewing.
234
template <> struct GraphTraits<clang::CallGraphNode*> {
235
  using NodeType = clang::CallGraphNode;
236
  using NodeRef = clang::CallGraphNode *;
237
  using ChildIteratorType = NodeType::iterator;
238
239
1.21k
  static NodeType *getEntryNode(clang::CallGraphNode *CGN) { return CGN; }
240
20.5k
  static ChildIteratorType child_begin(NodeType *N) { return N->begin();  }
241
56.1k
  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
242
};
243
244
template <> struct GraphTraits<const clang::CallGraphNode*> {
245
  using NodeType = const clang::CallGraphNode;
246
  using NodeRef = const clang::CallGraphNode *;
247
  using ChildIteratorType = NodeType::const_iterator;
248
249
2
  static NodeType *getEntryNode(const clang::CallGraphNode *CGN) { return CGN; }
250
27
  static ChildIteratorType child_begin(NodeType *N) { return N->begin();}
251
74
  static ChildIteratorType child_end(NodeType *N) { return N->end(); }
252
};
253
254
template <> struct GraphTraits<clang::CallGraph*>
255
  : public GraphTraits<clang::CallGraphNode*> {
256
1.21k
  static NodeType *getEntryNode(clang::CallGraph *CGN) {
257
1.21k
    return CGN->getRoot();  // Start at the external node!
258
1.21k
  }
259
260
  static clang::CallGraphNode *
261
0
  CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
262
0
    return P.second.get();
263
0
  }
264
265
  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
266
  using nodes_iterator =
267
      mapped_iterator<clang::CallGraph::iterator, decltype(&CGGetValue)>;
268
269
0
  static nodes_iterator nodes_begin(clang::CallGraph *CG) {
270
0
    return nodes_iterator(CG->begin(), &CGGetValue);
271
0
  }
272
273
0
  static nodes_iterator nodes_end  (clang::CallGraph *CG) {
274
0
    return nodes_iterator(CG->end(), &CGGetValue);
275
0
  }
276
277
0
  static unsigned size(clang::CallGraph *CG) { return CG->size(); }
278
};
279
280
template <> struct GraphTraits<const clang::CallGraph*> :
281
  public GraphTraits<const clang::CallGraphNode*> {
282
2
  static NodeType *getEntryNode(const clang::CallGraph *CGN) {
283
2
    return CGN->getRoot();
284
2
  }
285
286
  static clang::CallGraphNode *
287
0
  CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
288
0
    return P.second.get();
289
0
  }
290
291
  // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
292
  using nodes_iterator =
293
      mapped_iterator<clang::CallGraph::const_iterator, decltype(&CGGetValue)>;
294
295
0
  static nodes_iterator nodes_begin(const clang::CallGraph *CG) {
296
0
    return nodes_iterator(CG->begin(), &CGGetValue);
297
0
  }
298
299
0
  static nodes_iterator nodes_end(const clang::CallGraph *CG) {
300
0
    return nodes_iterator(CG->end(), &CGGetValue);
301
0
  }
302
303
0
  static unsigned size(const clang::CallGraph *CG) { return CG->size(); }
304
};
305
306
} // namespace llvm
307
308
#endif // LLVM_CLANG_ANALYSIS_CALLGRAPH_H