CallGraph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- CallGraph.h - AST-based Call graph -----------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file declares the AST-based CallGraph.
  15. //
  16. // A call graph for functions whose definitions/bodies are available in the
  17. // current translation unit. The graph has a "virtual" root node that contains
  18. // edges to all externally available functions.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_CLANG_ANALYSIS_CALLGRAPH_H
  22. #define LLVM_CLANG_ANALYSIS_CALLGRAPH_H
  23. #include "clang/AST/Decl.h"
  24. #include "clang/AST/RecursiveASTVisitor.h"
  25. #include "llvm/ADT/DenseMap.h"
  26. #include "llvm/ADT/GraphTraits.h"
  27. #include "llvm/ADT/STLExtras.h"
  28. #include "llvm/ADT/SetVector.h"
  29. #include "llvm/ADT/SmallVector.h"
  30. #include "llvm/ADT/iterator_range.h"
  31. #include <memory>
  32. namespace clang {
  33. class CallGraphNode;
  34. class Decl;
  35. class DeclContext;
  36. class Stmt;
  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. using FunctionMapTy =
  45. llvm::DenseMap<const Decl *, std::unique_ptr<CallGraphNode>>;
  46. /// FunctionMap owns all CallGraphNodes.
  47. FunctionMapTy FunctionMap;
  48. /// This is a virtual root node that has edges to all the functions.
  49. CallGraphNode *Root;
  50. public:
  51. CallGraph();
  52. ~CallGraph();
  53. /// Populate the call graph with the functions in the given
  54. /// declaration.
  55. ///
  56. /// Recursively walks the declaration to find all the dependent Decls as well.
  57. void addToCallGraph(Decl *D) {
  58. TraverseDecl(D);
  59. }
  60. /// Determine if a declaration should be included in the graph.
  61. static bool includeInGraph(const Decl *D);
  62. /// Determine if a declaration should be included in the graph for the
  63. /// purposes of being a callee. This is similar to includeInGraph except
  64. /// it permits declarations, not just definitions.
  65. static bool includeCalleeInGraph(const Decl *D);
  66. /// Lookup the node for the given declaration.
  67. CallGraphNode *getNode(const Decl *) const;
  68. /// Lookup the node for the given declaration. If none found, insert
  69. /// one into the graph.
  70. CallGraphNode *getOrInsertNode(Decl *);
  71. using iterator = FunctionMapTy::iterator;
  72. using const_iterator = FunctionMapTy::const_iterator;
  73. /// Iterators through all the elements in the graph. Note, this gives
  74. /// non-deterministic order.
  75. iterator begin() { return FunctionMap.begin(); }
  76. iterator end() { return FunctionMap.end(); }
  77. const_iterator begin() const { return FunctionMap.begin(); }
  78. const_iterator end() const { return FunctionMap.end(); }
  79. /// Get the number of nodes in the graph.
  80. unsigned size() const { return FunctionMap.size(); }
  81. /// Get the virtual root of the graph, all the functions available externally
  82. /// are represented as callees of the node.
  83. CallGraphNode *getRoot() const { return Root; }
  84. /// Iterators through all the nodes of the graph that have no parent. These
  85. /// are the unreachable nodes, which are either unused or are due to us
  86. /// failing to add a call edge due to the analysis imprecision.
  87. using nodes_iterator = llvm::SetVector<CallGraphNode *>::iterator;
  88. using const_nodes_iterator = llvm::SetVector<CallGraphNode *>::const_iterator;
  89. void print(raw_ostream &os) const;
  90. void dump() const;
  91. void viewGraph() const;
  92. void addNodesForBlocks(DeclContext *D);
  93. /// Part of recursive declaration visitation. We recursively visit all the
  94. /// declarations to collect the root functions.
  95. bool VisitFunctionDecl(FunctionDecl *FD) {
  96. // We skip function template definitions, as their semantics is
  97. // only determined when they are instantiated.
  98. if (includeInGraph(FD) && FD->isThisDeclarationADefinition()) {
  99. // Add all blocks declared inside this function to the graph.
  100. addNodesForBlocks(FD);
  101. // If this function has external linkage, anything could call it.
  102. // Note, we are not precise here. For example, the function could have
  103. // its address taken.
  104. addNodeForDecl(FD, FD->isGlobal());
  105. }
  106. return true;
  107. }
  108. /// Part of recursive declaration visitation.
  109. bool VisitObjCMethodDecl(ObjCMethodDecl *MD) {
  110. if (includeInGraph(MD)) {
  111. addNodesForBlocks(MD);
  112. addNodeForDecl(MD, true);
  113. }
  114. return true;
  115. }
  116. // We are only collecting the declarations, so do not step into the bodies.
  117. bool TraverseStmt(Stmt *S) { return true; }
  118. bool shouldWalkTypesOfTypeLocs() const { return false; }
  119. bool shouldVisitTemplateInstantiations() const { return true; }
  120. bool shouldVisitImplicitCode() const { return true; }
  121. private:
  122. /// Add the given declaration to the call graph.
  123. void addNodeForDecl(Decl *D, bool IsGlobal);
  124. };
  125. class CallGraphNode {
  126. public:
  127. struct CallRecord {
  128. CallGraphNode *Callee;
  129. Expr *CallExpr;
  130. CallRecord() = default;
  131. CallRecord(CallGraphNode *Callee_, Expr *CallExpr_)
  132. : Callee(Callee_), CallExpr(CallExpr_) {}
  133. // The call destination is the only important data here,
  134. // allow to transparently unwrap into it.
  135. operator CallGraphNode *() const { return Callee; }
  136. };
  137. private:
  138. /// The function/method declaration.
  139. Decl *FD;
  140. /// The list of functions called from this node.
  141. SmallVector<CallRecord, 5> CalledFunctions;
  142. public:
  143. CallGraphNode(Decl *D) : FD(D) {}
  144. using iterator = SmallVectorImpl<CallRecord>::iterator;
  145. using const_iterator = SmallVectorImpl<CallRecord>::const_iterator;
  146. /// Iterators through all the callees/children of the node.
  147. iterator begin() { return CalledFunctions.begin(); }
  148. iterator end() { return CalledFunctions.end(); }
  149. const_iterator begin() const { return CalledFunctions.begin(); }
  150. const_iterator end() const { return CalledFunctions.end(); }
  151. /// Iterator access to callees/children of the node.
  152. llvm::iterator_range<iterator> callees() {
  153. return llvm::make_range(begin(), end());
  154. }
  155. llvm::iterator_range<const_iterator> callees() const {
  156. return llvm::make_range(begin(), end());
  157. }
  158. bool empty() const { return CalledFunctions.empty(); }
  159. unsigned size() const { return CalledFunctions.size(); }
  160. void addCallee(CallRecord Call) { CalledFunctions.push_back(Call); }
  161. Decl *getDecl() const { return FD; }
  162. FunctionDecl *getDefinition() const {
  163. return getDecl()->getAsFunction()->getDefinition();
  164. }
  165. void print(raw_ostream &os) const;
  166. void dump() const;
  167. };
  168. // NOTE: we are comparing based on the callee only. So different call records
  169. // (with different call expressions) to the same callee will compare equal!
  170. inline bool operator==(const CallGraphNode::CallRecord &LHS,
  171. const CallGraphNode::CallRecord &RHS) {
  172. return LHS.Callee == RHS.Callee;
  173. }
  174. } // namespace clang
  175. namespace llvm {
  176. // Specialize DenseMapInfo for clang::CallGraphNode::CallRecord.
  177. template <> struct DenseMapInfo<clang::CallGraphNode::CallRecord> {
  178. static inline clang::CallGraphNode::CallRecord getEmptyKey() {
  179. return clang::CallGraphNode::CallRecord(
  180. DenseMapInfo<clang::CallGraphNode *>::getEmptyKey(),
  181. DenseMapInfo<clang::Expr *>::getEmptyKey());
  182. }
  183. static inline clang::CallGraphNode::CallRecord getTombstoneKey() {
  184. return clang::CallGraphNode::CallRecord(
  185. DenseMapInfo<clang::CallGraphNode *>::getTombstoneKey(),
  186. DenseMapInfo<clang::Expr *>::getTombstoneKey());
  187. }
  188. static unsigned getHashValue(const clang::CallGraphNode::CallRecord &Val) {
  189. // NOTE: we are comparing based on the callee only.
  190. // Different call records with the same callee will compare equal!
  191. return DenseMapInfo<clang::CallGraphNode *>::getHashValue(Val.Callee);
  192. }
  193. static bool isEqual(const clang::CallGraphNode::CallRecord &LHS,
  194. const clang::CallGraphNode::CallRecord &RHS) {
  195. return LHS == RHS;
  196. }
  197. };
  198. // Graph traits for iteration, viewing.
  199. template <> struct GraphTraits<clang::CallGraphNode*> {
  200. using NodeType = clang::CallGraphNode;
  201. using NodeRef = clang::CallGraphNode *;
  202. using ChildIteratorType = NodeType::iterator;
  203. static NodeType *getEntryNode(clang::CallGraphNode *CGN) { return CGN; }
  204. static ChildIteratorType child_begin(NodeType *N) { return N->begin(); }
  205. static ChildIteratorType child_end(NodeType *N) { return N->end(); }
  206. };
  207. template <> struct GraphTraits<const clang::CallGraphNode*> {
  208. using NodeType = const clang::CallGraphNode;
  209. using NodeRef = const clang::CallGraphNode *;
  210. using ChildIteratorType = NodeType::const_iterator;
  211. static NodeType *getEntryNode(const clang::CallGraphNode *CGN) { return CGN; }
  212. static ChildIteratorType child_begin(NodeType *N) { return N->begin();}
  213. static ChildIteratorType child_end(NodeType *N) { return N->end(); }
  214. };
  215. template <> struct GraphTraits<clang::CallGraph*>
  216. : public GraphTraits<clang::CallGraphNode*> {
  217. static NodeType *getEntryNode(clang::CallGraph *CGN) {
  218. return CGN->getRoot(); // Start at the external node!
  219. }
  220. static clang::CallGraphNode *
  221. CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
  222. return P.second.get();
  223. }
  224. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  225. using nodes_iterator =
  226. mapped_iterator<clang::CallGraph::iterator, decltype(&CGGetValue)>;
  227. static nodes_iterator nodes_begin(clang::CallGraph *CG) {
  228. return nodes_iterator(CG->begin(), &CGGetValue);
  229. }
  230. static nodes_iterator nodes_end (clang::CallGraph *CG) {
  231. return nodes_iterator(CG->end(), &CGGetValue);
  232. }
  233. static unsigned size(clang::CallGraph *CG) { return CG->size(); }
  234. };
  235. template <> struct GraphTraits<const clang::CallGraph*> :
  236. public GraphTraits<const clang::CallGraphNode*> {
  237. static NodeType *getEntryNode(const clang::CallGraph *CGN) {
  238. return CGN->getRoot();
  239. }
  240. static clang::CallGraphNode *
  241. CGGetValue(clang::CallGraph::const_iterator::value_type &P) {
  242. return P.second.get();
  243. }
  244. // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
  245. using nodes_iterator =
  246. mapped_iterator<clang::CallGraph::const_iterator, decltype(&CGGetValue)>;
  247. static nodes_iterator nodes_begin(const clang::CallGraph *CG) {
  248. return nodes_iterator(CG->begin(), &CGGetValue);
  249. }
  250. static nodes_iterator nodes_end(const clang::CallGraph *CG) {
  251. return nodes_iterator(CG->end(), &CGGetValue);
  252. }
  253. static unsigned size(const clang::CallGraph *CG) { return CG->size(); }
  254. };
  255. } // namespace llvm
  256. #endif // LLVM_CLANG_ANALYSIS_CALLGRAPH_H
  257. #ifdef __GNUC__
  258. #pragma GCC diagnostic pop
  259. #endif