Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 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" |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTContext.h" |
| 15 | #include "clang/AST/Decl.h" |
| 16 | #include "clang/AST/StmtVisitor.h" |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/PostOrderIterator.h" |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/Statistic.h" |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/GraphWriter.h" |
| 20 | |
| 21 | using namespace clang; |
| 22 | |
Chandler Carruth | 1034666 | 2014-04-22 03:17:02 +0000 | [diff] [blame] | 23 | #define DEBUG_TYPE "CallGraph" |
| 24 | |
Anna Zaks | 4127750 | 2012-12-21 17:27:04 +0000 | [diff] [blame] | 25 | STATISTIC(NumObjCCallEdges, "Number of Objective-C method call edges"); |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 26 | STATISTIC(NumBlockCallEdges, "Number of block call edges"); |
| 27 | |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 28 | namespace { |
| 29 | /// A helper class, which walks the AST and locates all the call sites in the |
| 30 | /// given function body. |
| 31 | class CGBuilder : public StmtVisitor<CGBuilder> { |
| 32 | CallGraph *G; |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 33 | CallGraphNode *CallerNode; |
| 34 | |
| 35 | public: |
Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 36 | CGBuilder(CallGraph *g, CallGraphNode *N) |
| 37 | : G(g), CallerNode(N) {} |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 38 | |
| 39 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
| 40 | |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 41 | Decl *getDeclFromCall(CallExpr *CE) { |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 42 | if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 43 | return CalleeDecl; |
| 44 | |
| 45 | // Simple detection of a call through a block. |
| 46 | Expr *CEE = CE->getCallee()->IgnoreParenImpCasts(); |
| 47 | if (BlockExpr *Block = dyn_cast<BlockExpr>(CEE)) { |
| 48 | NumBlockCallEdges++; |
| 49 | return Block->getBlockDecl(); |
| 50 | } |
| 51 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 52 | return nullptr; |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void addCalledDecl(Decl *D) { |
| 56 | if (G->includeInGraph(D)) { |
| 57 | CallGraphNode *CalleeNode = G->getOrInsertNode(D); |
| 58 | CallerNode->addCallee(CalleeNode, G); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void VisitCallExpr(CallExpr *CE) { |
| 63 | if (Decl *D = getDeclFromCall(CE)) |
| 64 | addCalledDecl(D); |
| 65 | } |
| 66 | |
| 67 | // Adds may-call edges for the ObjC message sends. |
| 68 | void VisitObjCMessageExpr(ObjCMessageExpr *ME) { |
| 69 | if (ObjCInterfaceDecl *IDecl = ME->getReceiverInterface()) { |
| 70 | Selector Sel = ME->getSelector(); |
| 71 | |
Anna Zaks | 4127750 | 2012-12-21 17:27:04 +0000 | [diff] [blame] | 72 | // Find the callee definition within the same translation unit. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 73 | Decl *D = nullptr; |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 74 | if (ME->isInstanceMessage()) |
| 75 | D = IDecl->lookupPrivateMethod(Sel); |
| 76 | else |
| 77 | D = IDecl->lookupPrivateClassMethod(Sel); |
| 78 | if (D) { |
| 79 | addCalledDecl(D); |
| 80 | NumObjCCallEdges++; |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 81 | } |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 82 | } |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void VisitChildren(Stmt *S) { |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 86 | for (Stmt *SubStmt : S->children()) |
| 87 | if (SubStmt) |
| 88 | this->Visit(SubStmt); |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 89 | } |
| 90 | }; |
| 91 | |
| 92 | } // end anonymous namespace |
| 93 | |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 94 | void CallGraph::addNodesForBlocks(DeclContext *D) { |
| 95 | if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 96 | addNodeForDecl(BD, true); |
| 97 | |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 98 | for (auto *I : D->decls()) |
| 99 | if (auto *DC = dyn_cast<DeclContext>(I)) |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 100 | addNodesForBlocks(DC); |
| 101 | } |
| 102 | |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 103 | CallGraph::CallGraph() { |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 104 | Root = getOrInsertNode(nullptr); |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Justin Lebar | 5f3d1dc | 2016-10-10 16:26:48 +0000 | [diff] [blame^] | 107 | CallGraph::~CallGraph() {} |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 108 | |
| 109 | bool CallGraph::includeInGraph(const Decl *D) { |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 110 | assert(D); |
Anna Zaks | 87d404d | 2014-12-17 00:34:07 +0000 | [diff] [blame] | 111 | if (!D->hasBody()) |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 112 | return false; |
| 113 | |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 114 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 115 | // We skip function template definitions, as their semantics is |
| 116 | // only determined when they are instantiated. |
Anna Zaks | 87d404d | 2014-12-17 00:34:07 +0000 | [diff] [blame] | 117 | if (FD->isDependentContext()) |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 118 | return false; |
| 119 | |
| 120 | IdentifierInfo *II = FD->getIdentifier(); |
| 121 | if (II && II->getName().startswith("__inline")) |
| 122 | return false; |
| 123 | } |
| 124 | |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 125 | return true; |
| 126 | } |
| 127 | |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 128 | void CallGraph::addNodeForDecl(Decl* D, bool IsGlobal) { |
Anna Zaks | 9a008bb | 2012-03-08 23:16:26 +0000 | [diff] [blame] | 129 | assert(D); |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 130 | |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 131 | // Allocate a new node, mark it as root, and process it's calls. |
| 132 | CallGraphNode *Node = getOrInsertNode(D); |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 133 | |
| 134 | // Process all the calls by this function as well. |
Benjamin Kramer | d1d76b2 | 2012-06-06 17:32:50 +0000 | [diff] [blame] | 135 | CGBuilder builder(this, Node); |
Ted Kremenek | 504957f | 2012-04-05 04:03:23 +0000 | [diff] [blame] | 136 | if (Stmt *Body = D->getBody()) |
| 137 | builder.Visit(Body); |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Anna Zaks | c255577 | 2012-03-09 21:13:53 +0000 | [diff] [blame] | 140 | CallGraphNode *CallGraph::getNode(const Decl *F) const { |
Matt Beaumont-Gay | dcc425e | 2012-03-14 20:21:25 +0000 | [diff] [blame] | 141 | FunctionMapTy::const_iterator I = FunctionMap.find(F); |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 142 | if (I == FunctionMap.end()) return nullptr; |
Justin Lebar | 5f3d1dc | 2016-10-10 16:26:48 +0000 | [diff] [blame^] | 143 | return I->second.get(); |
Anna Zaks | c255577 | 2012-03-09 21:13:53 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Anna Zaks | 8e07852 | 2012-04-12 22:36:48 +0000 | [diff] [blame] | 146 | CallGraphNode *CallGraph::getOrInsertNode(Decl *F) { |
Anna Zaks | 87d404d | 2014-12-17 00:34:07 +0000 | [diff] [blame] | 147 | if (F && !isa<ObjCMethodDecl>(F)) |
| 148 | F = F->getCanonicalDecl(); |
| 149 | |
Justin Lebar | 5f3d1dc | 2016-10-10 16:26:48 +0000 | [diff] [blame^] | 150 | std::unique_ptr<CallGraphNode> &Node = FunctionMap[F]; |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 151 | if (Node) |
Justin Lebar | 5f3d1dc | 2016-10-10 16:26:48 +0000 | [diff] [blame^] | 152 | return Node.get(); |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 153 | |
Justin Lebar | 5f3d1dc | 2016-10-10 16:26:48 +0000 | [diff] [blame^] | 154 | Node = llvm::make_unique<CallGraphNode>(F); |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 155 | // Make Root node a parent of all functions to make sure all are reachable. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 156 | if (F) |
Justin Lebar | 5f3d1dc | 2016-10-10 16:26:48 +0000 | [diff] [blame^] | 157 | Root->addCallee(Node.get(), this); |
| 158 | return Node.get(); |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void CallGraph::print(raw_ostream &OS) const { |
| 162 | OS << " --- Call graph Dump --- \n"; |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 163 | |
| 164 | // We are going to print the graph in reverse post order, partially, to make |
| 165 | // sure the output is deterministic. |
| 166 | llvm::ReversePostOrderTraversal<const clang::CallGraph*> RPOT(this); |
| 167 | for (llvm::ReversePostOrderTraversal<const clang::CallGraph*>::rpo_iterator |
| 168 | I = RPOT.begin(), E = RPOT.end(); I != E; ++I) { |
| 169 | const CallGraphNode *N = *I; |
| 170 | |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 171 | OS << " Function: "; |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 172 | if (N == Root) |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 173 | OS << "< root >"; |
| 174 | else |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 175 | N->print(OS); |
| 176 | |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 177 | OS << " calls: "; |
Anna Zaks | 1ee76c1 | 2012-12-21 17:27:01 +0000 | [diff] [blame] | 178 | for (CallGraphNode::const_iterator CI = N->begin(), |
| 179 | CE = N->end(); CI != CE; ++CI) { |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 180 | assert(*CI != Root && "No one can call the root node."); |
| 181 | (*CI)->print(OS); |
| 182 | OS << " "; |
| 183 | } |
| 184 | OS << '\n'; |
| 185 | } |
| 186 | OS.flush(); |
| 187 | } |
| 188 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 189 | LLVM_DUMP_METHOD void CallGraph::dump() const { |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 190 | print(llvm::errs()); |
| 191 | } |
| 192 | |
| 193 | void CallGraph::viewGraph() const { |
| 194 | llvm::ViewGraph(this, "CallGraph"); |
| 195 | } |
| 196 | |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 197 | void CallGraphNode::print(raw_ostream &os) const { |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 198 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(FD)) |
| 199 | return ND->printName(os); |
| 200 | os << "< >"; |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Yaron Keren | cdae941 | 2016-01-29 19:38:18 +0000 | [diff] [blame] | 203 | LLVM_DUMP_METHOD void CallGraphNode::dump() const { |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 204 | print(llvm::errs()); |
| 205 | } |
| 206 | |
| 207 | namespace llvm { |
| 208 | |
| 209 | template <> |
| 210 | struct DOTGraphTraits<const CallGraph*> : public DefaultDOTGraphTraits { |
| 211 | |
| 212 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 213 | |
| 214 | static std::string getNodeLabel(const CallGraphNode *Node, |
| 215 | const CallGraph *CG) { |
| 216 | if (CG->getRoot() == Node) { |
| 217 | return "< root >"; |
| 218 | } |
Anna Zaks | 5c32dfc | 2012-12-21 01:19:15 +0000 | [diff] [blame] | 219 | if (const NamedDecl *ND = dyn_cast_or_null<NamedDecl>(Node->getDecl())) |
| 220 | return ND->getNameAsString(); |
| 221 | else |
| 222 | return "< >"; |
Anna Zaks | c000e7e | 2012-03-08 00:42:23 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 226 | } |