CallGraph.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //== CallGraph.cpp - AST-based Call graph ----------------------*- C++ -*--==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the AST-based CallGraph.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Analysis/CallGraph.h"
  14. #include "clang/AST/ASTContext.h"
  15. #include "clang/AST/Decl.h"
  16. #include "clang/AST/StmtVisitor.h"
  17. #include "llvm/ADT/PostOrderIterator.h"
  18. #include "llvm/ADT/Statistic.h"
  19. #include "llvm/Support/GraphWriter.h"
  20. // //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. using namespace clang;
  23. #define DEBUG_TYPE "CallGraph"
  24. STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges");
  25. STATISTIC(NumBlockCallEdges, "Number of block call edges");
  26. namespace {
  27. /// A helper class, which walks the AST and locates all the call sites in the
  28. /// given function body.
  29. class CGBuilder : public StmtVisitor<CGBuilder> {
  30. CallGraph *G;
  31. CallGraphNode *CallerNode;
  32. public:
  33. CGBuilder(CallGraph *g, CallGraphNode *N)
  34. : G(g), CallerNode(N) {}
  35. void VisitStmt(Stmt *S) { VisitChildren(S); }
  36. Decl *getDeclFromCall(CallExpr *CE) {
  37. if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
  38. return CalleeDecl;
  39. // Simple detection of a call through a block.
  40. Expr *CEE = CE->getCallee()->IgnoreParenImpCasts();
  41. if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) {
  42. NumBlockCallEdges++;
  43. return Block->getBlockDecl();
  44. }
  45. return nullptr;
  46. }
  47. void addCalledDecl(Decl *D) {
  48. if (G->includeInGraph(D)) {
  49. CallGraphNode *CalleeNode = G->getOrInsertNode(D);
  50. CallerNode->addCallee(CalleeNode, G);
  51. }
  52. }
  53. void VisitCallExpr(CallExpr *CE) {
  54. if (Decl *D = getDeclFromCall(CE))
  55. addCalledDecl(D);
  56. }
  57. // Adds may-call edges for the ObjC message sends.
  58. void VisitObjCMessageExpr(ObjCMessageExpr *ME) {
  59. if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) {
  60. Selector Sel = ME->getSelector();
  61. // Find the callee definition within the same translation unit.
  62. Decl *D = nullptr;
  63. if (ME->isInstanceMessage())
  64. D = IDecl->lookupPrivateMethod(Sel);
  65. else
  66. D = IDecl->lookupPrivateClassMethod(Sel);
  67. if (D) {
  68. addCalledDecl(D);
  69. NumObjCCallEdges++;
  70. }
  71. }
  72. }
  73. void VisitChildren(Stmt *S) {
  74. for (Stmt *SubStmt : S->children())
  75. if (SubStmt)
  76. this->Visit(SubStmt);
  77. }
  78. };
  79. } // end anonymous namespace
  80. void CallGraph::addNodesForBlocks(DeclContext *D) {
  81. if (BlockDecl *BD = dyn_cast<BlockDecl>(D))
  82. addNodeForDecl(BD, true);
  83. for (auto *I : D->decls())
  84. if (auto *DC = dyn_cast<DeclContext>(I))
  85. addNodesForBlocks(DC);
  86. }
  87. CallGraph::CallGraph() {
  88. Root = getOrInsertNode(nullptr);
  89. }
  90. CallGraph::~CallGraph() {
  91. llvm::DeleteContainerSeconds(FunctionMap);
  92. }
  93. bool CallGraph::includeInGraph(const Decl *D) {
  94. assert(D);
  95. if (!D->hasBody())
  96. return false;
  97. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  98. // We skip function template definitions, as their semantics is
  99. // only determined when they are instantiated.
  100. if (FD->isDependentContext())
  101. return false;
  102. IdentifierInfo *II = FD->getIdentifier();
  103. if (II && II->getName().startswith("__inline"))
  104. return false;
  105. }
  106. return true;
  107. }
  108. void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
  109. assert(D);
  110. // Allocate a new node, mark it as root, and process it's calls.
  111. CallGraphNode *Node = getOrInsertNode(D);
  112. // Process all the calls by this function as well.
  113. CGBuilder builder(this, Node);
  114. if (Stmt *Body = D->getBody())
  115. builder.Visit(Body);
  116. }
  117. CallGraphNode *CallGraph::getNode(const Decl *F) const {
  118. FunctionMapTy::const_iterator I = FunctionMap.find(F);
  119. if (I == FunctionMap.end()) return nullptr;
  120. return I->second;
  121. }
  122. CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
  123. if (F && !isa<ObjCMethodDecl>(F))
  124. F = F->getCanonicalDecl();
  125. CallGraphNode *&Node = FunctionMap[F];
  126. if (Node)
  127. return Node;
  128. Node = new CallGraphNode(F);
  129. // Make Root node a parent of all functions to make sure all are reachable.
  130. if (F)
  131. Root->addCallee(Node, this);
  132. return Node;
  133. }
  134. void CallGraph::print(raw_ostream &OS) const {
  135. OS << " --- Call graph Dump --- \n";
  136. // We are going to print the graph in reverse post order, partially, to make
  137. // sure the output is deterministic.
  138. llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this);
  139. for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator
  140. I = RPOT.begin(), E = RPOT.end(); I != E; ++I) {
  141. const CallGraphNode *N = *I;
  142. OS << " Function: ";
  143. if (N == Root)
  144. OS << "< root >";
  145. else
  146. N->print(OS);
  147. OS << " calls: ";
  148. for (CallGraphNode::const_iterator CI = N->begin(),
  149. CE = N->end(); CI != CE; ++CI) {
  150. assert(*CI != Root && "No one can call the root node.");
  151. (*CI)->print(OS);
  152. OS << " ";
  153. }
  154. OS << '\n';
  155. }
  156. OS.flush();
  157. }
  158. void CallGraph::dump() const {
  159. print(llvm::errs());
  160. }
  161. void CallGraph::viewGraph() const {
  162. llvm::ViewGraph(this, "CallGraph");
  163. }
  164. void CallGraphNode::print(raw_ostream &os) const {
  165. if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD))
  166. return ND->printName(os);
  167. os << "< >";
  168. }
  169. void CallGraphNode::dump() const {
  170. print(llvm::errs());
  171. }
  172. namespace llvm {
  173. template <>
  174. struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
  175. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  176. static std::string getNodeLabel(const CallGraphNode *Node,
  177. const CallGraph *CG) {
  178. if (CG->getRoot() == Node) {
  179. return "< root >";
  180. }
  181. if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl()))
  182. return ND->getNameAsString();
  183. else
  184. return "< >";
  185. }
  186. };
  187. }