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 | |
| 14 | #include "clang/Analysis/CallGraph.h" |
| 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) { |
| 43 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I != E;++I) |
| 44 | if (*I) |
| 45 | static_cast<CGBuilder*>(this)->Visit(*I); |
| 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); |
Zhongxing Xu | 7f66bd2 | 2009-07-17 07:36:20 +0000 | [diff] [blame] | 54 | |
| 55 | Decl *Parent = ASTLocation::FindImmediateParent(FD, CE); |
| 56 | |
| 57 | CallerNode->addCallee(ASTLocation(Parent, CE), CalleeNode); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Zhongxing Xu | 56a5d80 | 2009-07-23 13:39:38 +0000 | [diff] [blame] | 61 | CallGraph::CallGraph() : Root(0) { |
| 62 | ExternalCallingNode = getOrInsertFunction(Entity()); |
| 63 | } |
| 64 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 65 | CallGraph::~CallGraph() { |
| 66 | if (!FunctionMap.empty()) { |
| 67 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
| 68 | I != E; ++I) |
| 69 | delete I->second; |
| 70 | FunctionMap.clear(); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void CallGraph::addTU(ASTUnit &AST) { |
| 75 | ASTContext &Ctx = AST.getASTContext(); |
| 76 | DeclContext *DC = Ctx.getTranslationUnitDecl(); |
| 77 | |
| 78 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 79 | I != E; ++I) { |
| 80 | |
| 81 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 82 | if (FD->isThisDeclarationADefinition()) { |
| 83 | // Set caller's ASTContext. |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 84 | Entity Ent = Entity::get(FD, Prog); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 85 | CallGraphNode *Node = getOrInsertFunction(Ent); |
| 86 | CallerCtx[Node] = &Ctx; |
Zhongxing Xu | 56a5d80 | 2009-07-23 13:39:38 +0000 | [diff] [blame] | 87 | |
| 88 | // If this function has external linkage, anything could call it. |
| 89 | if (FD->isGlobal()) |
| 90 | ExternalCallingNode->addCallee(idx::ASTLocation(), Node); |
| 91 | |
| 92 | // Set root node to 'main' function. |
| 93 | if (FD->getNameAsString() == "main") |
| 94 | Root = Node; |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 95 | |
| 96 | CGBuilder builder(*this, FD, Ent, Node); |
| 97 | builder.Visit(FD->getBody()); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Argyrios Kyrtzidis | f7cf15c | 2009-07-21 00:07:06 +0000 | [diff] [blame] | 103 | CallGraphNode *CallGraph::getOrInsertFunction(Entity F) { |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 104 | CallGraphNode *&Node = FunctionMap[F]; |
| 105 | if (Node) |
| 106 | return Node; |
| 107 | |
| 108 | return Node = new CallGraphNode(F); |
| 109 | } |
| 110 | |
Zhongxing Xu | e3e643f | 2009-07-24 03:41:11 +0000 | [diff] [blame] | 111 | Decl *CallGraph::getDecl(CallGraphNode *Node) { |
| 112 | // Get the function's context. |
| 113 | ASTContext *Ctx = CallerCtx[Node]; |
| 114 | |
| 115 | return Node->getDecl(*Ctx); |
| 116 | } |
| 117 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 118 | void CallGraph::print(llvm::raw_ostream &os) { |
| 119 | for (iterator I = begin(), E = end(); I != E; ++I) { |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 120 | if (I->second->hasCallee()) { |
Argyrios Kyrtzidis | 4c7c5a1 | 2009-07-21 07:52:21 +0000 | [diff] [blame] | 121 | os << "function: " << I->first.getPrintableName() |
Zhongxing Xu | 56aac3f | 2009-07-17 07:49:44 +0000 | [diff] [blame] | 122 | << " calls:\n"; |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 123 | for (CallGraphNode::iterator CI = I->second->begin(), |
| 124 | CE = I->second->end(); CI != CE; ++CI) { |
Argyrios Kyrtzidis | 4c7c5a1 | 2009-07-21 07:52:21 +0000 | [diff] [blame] | 125 | os << " " << CI->second->getName().c_str(); |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 126 | } |
| 127 | os << '\n'; |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 128 | } |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | void CallGraph::dump() { |
| 133 | print(llvm::errs()); |
| 134 | } |
Zhongxing Xu | 16a705f | 2009-07-23 09:04:23 +0000 | [diff] [blame] | 135 | |
| 136 | void CallGraph::ViewCallGraph() const { |
| 137 | llvm::ViewGraph(*this, "CallGraph"); |
| 138 | } |
| 139 | |
| 140 | namespace llvm { |
| 141 | |
| 142 | template <> |
| 143 | struct DOTGraphTraits<CallGraph> : public DefaultDOTGraphTraits { |
| 144 | |
| 145 | static std::string getNodeLabel(const CallGraphNode *Node, |
| 146 | const CallGraph &CG, bool ShortNames) { |
| 147 | return Node->getName(); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | }; |
| 152 | |
| 153 | } |