CallGraph.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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/Support/GraphWriter.h"
  18. using namespace clang;
  19. namespace {
  20. /// A helper class, which walks the AST and locates all the call sites in the
  21. /// given function body.
  22. class CGBuilder : public StmtVisitor<CGBuilder> {
  23. CallGraph *G;
  24. CallGraphNode *CallerNode;
  25. public:
  26. CGBuilder(CallGraph *g, CallGraphNode *N)
  27. : G(g), CallerNode(N) {}
  28. void VisitStmt(Stmt *S) { VisitChildren(S); }
  29. void VisitCallExpr(CallExpr *CE) {
  30. // TODO: We need to handle ObjC method calls as well.
  31. if (FunctionDecl *CalleeDecl = CE->getDirectCallee())
  32. if (G->includeInGraph(CalleeDecl)) {
  33. CallGraphNode *CalleeNode = G->getOrInsertNode(CalleeDecl);
  34. CallerNode->addCallee(CalleeNode, G);
  35. }
  36. }
  37. void VisitChildren(Stmt *S) {
  38. for (Stmt::child_range I = S->children(); I; ++I)
  39. if (*I)
  40. static_cast<CGBuilder*>(this)->Visit(*I);
  41. }
  42. };
  43. } // end anonymous namespace
  44. CallGraph::CallGraph() {
  45. Root = getOrInsertNode(0);
  46. }
  47. CallGraph::~CallGraph() {
  48. if (!FunctionMap.empty()) {
  49. for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
  50. I != E; ++I)
  51. delete I->second;
  52. FunctionMap.clear();
  53. }
  54. }
  55. bool CallGraph::includeInGraph(const Decl *D) {
  56. if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
  57. // We skip function template definitions, as their semantics is
  58. // only determined when they are instantiated.
  59. if (!FD->isThisDeclarationADefinition() ||
  60. FD->isDependentContext())
  61. return false;
  62. IdentifierInfo *II = FD->getIdentifier();
  63. if (II && II->getName().startswith("__inline"))
  64. return false;
  65. }
  66. if (const ObjCMethodDecl *ID = dyn_cast<ObjCMethodDecl>(D)) {
  67. if (!ID->isThisDeclarationADefinition())
  68. return false;
  69. }
  70. return true;
  71. }
  72. void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) {
  73. assert(D);
  74. // Do nothing if the node already exists.
  75. if (FunctionMap.find(D) != FunctionMap.end())
  76. return;
  77. // Allocate a new node, mark it as root, and process it's calls.
  78. CallGraphNode *Node = getOrInsertNode(D);
  79. if (IsGlobal)
  80. Root->addCallee(Node, this);
  81. // Process all the calls by this function as well.
  82. CGBuilder builder(this, Node);
  83. if (Stmt *Body = D->getBody())
  84. builder.Visit(Body);
  85. }
  86. CallGraphNode *CallGraph::getNode(const Decl *F) const {
  87. FunctionMapTy::const_iterator I = FunctionMap.find(F);
  88. if (I == FunctionMap.end()) return 0;
  89. return I->second;
  90. }
  91. CallGraphNode *CallGraph::getOrInsertNode(Decl *F) {
  92. CallGraphNode *&Node = FunctionMap[F];
  93. if (Node)
  94. return Node;
  95. Node = new CallGraphNode(F);
  96. // If not root, add to the parentless list.
  97. if (F != 0)
  98. ParentlessNodes.insert(Node);
  99. return Node;
  100. }
  101. void CallGraph::print(raw_ostream &OS) const {
  102. OS << " --- Call graph Dump --- \n";
  103. for (const_iterator I = begin(), E = end(); I != E; ++I) {
  104. OS << " Function: ";
  105. if (I->second == Root)
  106. OS << "< root >";
  107. else
  108. I->second->print(OS);
  109. OS << " calls: ";
  110. for (CallGraphNode::iterator CI = I->second->begin(),
  111. CE = I->second->end(); CI != CE; ++CI) {
  112. assert(*CI != Root && "No one can call the root node.");
  113. (*CI)->print(OS);
  114. OS << " ";
  115. }
  116. OS << '\n';
  117. }
  118. OS.flush();
  119. }
  120. void CallGraph::dump() const {
  121. print(llvm::errs());
  122. }
  123. void CallGraph::viewGraph() const {
  124. llvm::ViewGraph(this, "CallGraph");
  125. }
  126. StringRef CallGraphNode::getName() const {
  127. if (const FunctionDecl *D = dyn_cast_or_null<FunctionDecl>(FD))
  128. if (const IdentifierInfo *II = D->getIdentifier())
  129. return II->getName();
  130. return "< >";
  131. }
  132. void CallGraphNode::print(raw_ostream &os) const {
  133. os << getName();
  134. }
  135. void CallGraphNode::dump() const {
  136. print(llvm::errs());
  137. }
  138. namespace llvm {
  139. template <>
  140. struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits {
  141. DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {}
  142. static std::string getNodeLabel(const CallGraphNode *Node,
  143. const CallGraph *CG) {
  144. if (CG->getRoot() == Node) {
  145. return "< root >";
  146. }
  147. return Node->getName();
  148. }
  149. };
  150. }