Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 1 | //== CallGraph.cpp - Call graph building ------------------------*- 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 defined the CallGraph and CGBuilder classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 8fd57fe | 2009-12-03 07:20:04 +0000 | [diff] [blame] | 14 | #include "clang/Index/CallGraph.h" |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 15 | |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/StmtVisitor.h" |
| 18 | |
Zhongxing Xu | 16a705f | 2009-07-23 09:04:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/GraphWriter.h" |
| 20 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace idx; |
| 23 | |
| 24 | namespace { |
| 25 | class CGBuilder : public StmtVisitor<CGBuilder> { |
| 26 | |
| 27 | CallGraph &G; |
| 28 | FunctionDecl *FD; |
| 29 | |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 30 | Entity CallerEnt; |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 31 | |
| 32 | CallGraphNode *CallerNode; |
| 33 | |
| 34 | public: |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 35 | CGBuilder(CallGraph &g, FunctionDecl *fd, Entity E, CallGraphNode *N) |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 36 | : G(g), FD(fd), CallerEnt(E), CallerNode(N) {} |
| 37 | |
Zhongxing Xu | 93cbae3 | 2009-07-18 08:49:07 +0000 | [diff] [blame] | 38 | void VisitStmt(Stmt *S) { VisitChildren(S); } |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 39 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 40 | void VisitCallExpr(CallExpr *CE); |
| 41 | |
| 42 | void VisitChildren(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 43 | for (Stmt::child_range I = S->children(); I; ++I) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | if (*I) |
| 45 | static_cast<CGBuilder*>(this)->Visit(*I); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 46 | } |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | void CGBuilder::VisitCallExpr(CallExpr *CE) { |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 51 | if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) { |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 52 | Entity Ent = Entity::get(CalleeDecl, G.getProgram()); |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 53 | CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent); |
Argyrios Kyrtzidis | f4526e3 | 2009-09-29 19:44:27 +0000 | [diff] [blame] | 54 | CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
Zhongxing Xu | 6d956df | 2010-07-02 06:39:46 +0000 | [diff] [blame] | 58 | CallGraph::CallGraph(Program &P) : Prog(P), Root(0) { |
Zhongxing Xu | 56a5d80 | 2009-07-23 13:39:38 +0000 | [diff] [blame] | 59 | ExternalCallingNode = getOrInsertFunction(Entity()); |
| 60 | } |
| 61 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 62 | CallGraph::~CallGraph() { |
| 63 | if (!FunctionMap.empty()) { |
| 64 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
| 65 | I != E; ++I) |
| 66 | delete I->second; |
| 67 | FunctionMap.clear(); |
| 68 | } |
| 69 | } |
| 70 | |
Zhongxing Xu | f20288c | 2009-10-28 12:23:03 +0000 | [diff] [blame] | 71 | void CallGraph::addTU(ASTContext& Ctx) { |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 72 | DeclContext *DC = Ctx.getTranslationUnitDecl(); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 73 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 74 | I != E; ++I) { |
| 75 | |
| 76 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
Sean Hunt | 10620eb | 2011-05-06 20:44:56 +0000 | [diff] [blame] | 77 | if (FD->doesThisDeclarationHaveABody()) { |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 78 | // Set caller's ASTContext. |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 79 | Entity Ent = Entity::get(FD, Prog); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 80 | CallGraphNode *Node = getOrInsertFunction(Ent); |
| 81 | CallerCtx[Node] = &Ctx; |
Zhongxing Xu | 56a5d80 | 2009-07-23 13:39:38 +0000 | [diff] [blame] | 82 | |
| 83 | // If this function has external linkage, anything could call it. |
| 84 | if (FD->isGlobal()) |
| 85 | ExternalCallingNode->addCallee(idx::ASTLocation(), Node); |
| 86 | |
| 87 | // Set root node to 'main' function. |
| 88 | if (FD->getNameAsString() == "main") |
| 89 | Root = Node; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 90 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 91 | CGBuilder builder(*this, FD, Ent, Node); |
| 92 | builder.Visit(FD->getBody()); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 98 | CallGraphNode *CallGraph::getOrInsertFunction(Entity F) { |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 99 | CallGraphNode *&Node = FunctionMap[F]; |
| 100 | if (Node) |
| 101 | return Node; |
| 102 | |
| 103 | return Node = new CallGraphNode(F); |
| 104 | } |
| 105 | |
Zhongxing Xu | e3e643f | 2009-07-24 03:41:11 +0000 | [diff] [blame] | 106 | Decl *CallGraph::getDecl(CallGraphNode *Node) { |
| 107 | // Get the function's context. |
| 108 | ASTContext *Ctx = CallerCtx[Node]; |
| 109 | |
| 110 | return Node->getDecl(*Ctx); |
| 111 | } |
| 112 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 113 | void CallGraph::print(raw_ostream &os) { |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 114 | for (iterator I = begin(), E = end(); I != E; ++I) { |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 115 | if (I->second->hasCallee()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | os << "function: " << I->first.getPrintableName() |
Zhongxing Xu | 56aac3f | 2009-07-17 07:49:44 +0000 | [diff] [blame] | 117 | << " calls:\n"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | for (CallGraphNode::iterator CI = I->second->begin(), |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 119 | CE = I->second->end(); CI != CE; ++CI) { |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 120 | os << " " << CI->second->getName(); |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 121 | } |
| 122 | os << '\n'; |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 123 | } |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | void CallGraph::dump() { |
| 128 | print(llvm::errs()); |
| 129 | } |
Zhongxing Xu | 16a705f | 2009-07-23 09:04:23 +0000 | [diff] [blame] | 130 | |
| 131 | void CallGraph::ViewCallGraph() const { |
| 132 | llvm::ViewGraph(*this, "CallGraph"); |
| 133 | } |
| 134 | |
| 135 | namespace llvm { |
| 136 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | template <> |
Zhongxing Xu | 16a705f | 2009-07-23 09:04:23 +0000 | [diff] [blame] | 138 | struct DOTGraphTraits<CallGraph> : public DefaultDOTGraphTraits { |
| 139 | |
Tobias Grosser | 006b0eb | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 140 | DOTGraphTraits (bool isSimple=false) : DefaultDOTGraphTraits(isSimple) {} |
| 141 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | static std::string getNodeLabel(const CallGraphNode *Node, |
Tobias Grosser | 006b0eb | 2009-11-30 14:16:05 +0000 | [diff] [blame] | 143 | const CallGraph &CG) { |
Zhongxing Xu | 16a705f | 2009-07-23 09:04:23 +0000 | [diff] [blame] | 144 | return Node->getName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Zhongxing Xu | 16a705f | 2009-07-23 09:04:23 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | }; |
| 149 | |
| 150 | } |