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 | |
| 19 | using namespace clang; |
| 20 | using namespace idx; |
| 21 | |
| 22 | namespace { |
| 23 | class CGBuilder : public StmtVisitor<CGBuilder> { |
| 24 | |
| 25 | CallGraph &G; |
| 26 | FunctionDecl *FD; |
| 27 | |
| 28 | Entity *CallerEnt; |
| 29 | |
| 30 | CallGraphNode *CallerNode; |
| 31 | |
| 32 | public: |
| 33 | CGBuilder(CallGraph &g, FunctionDecl *fd, Entity *E, CallGraphNode *N) |
| 34 | : G(g), FD(fd), CallerEnt(E), CallerNode(N) {} |
| 35 | |
Zhongxing Xu | 6fc4505 | 2009-07-17 07:05:19 +0000 | [diff] [blame] | 36 | void VisitCompoundStmt(CompoundStmt *S) { VisitChildren(S); } |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 37 | |
Zhongxing Xu | 6fc4505 | 2009-07-17 07:05:19 +0000 | [diff] [blame] | 38 | void VisitCastStmt(CaseStmt *S) { VisitChildren(S); } |
| 39 | |
| 40 | void VisitDefaultStmt(DefaultStmt *S) { VisitChildren(S); } |
| 41 | |
| 42 | void VisitLabelStmt(LabelStmt *S) { VisitChildren(S); } |
| 43 | |
| 44 | void VisitIfStmt(IfStmt *S) { VisitChildren(S); } |
| 45 | |
| 46 | void VisitSwitchStmt(SwitchStmt *S) { VisitChildren(S); } |
| 47 | |
| 48 | void VisitDoStmt(DoStmt *S) { VisitChildren(S); } |
| 49 | |
| 50 | void VisitForStmt(ForStmt *S) { VisitChildren(S); } |
| 51 | |
| 52 | void VisitIndirectGotoStmt(IndirectGotoStmt *S) { VisitChildren(S); } |
| 53 | |
| 54 | void VisitReturnStmt(ReturnStmt *S) { VisitChildren(S); } |
| 55 | |
| 56 | void VisitDeclStmt(DeclStmt *S) { VisitChildren(S); } |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 57 | |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 58 | void VisitCallExpr(CallExpr *CE); |
| 59 | |
| 60 | void VisitChildren(Stmt *S) { |
| 61 | for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I != E;++I) |
| 62 | if (*I) |
| 63 | static_cast<CGBuilder*>(this)->Visit(*I); |
| 64 | } |
| 65 | }; |
| 66 | } |
| 67 | |
| 68 | void CGBuilder::VisitCallExpr(CallExpr *CE) { |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 69 | if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) { |
| 70 | Entity *Ent = Entity::get(CalleeDecl, G.getProgram()); |
| 71 | CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent); |
Zhongxing Xu | 7f66bd2 | 2009-07-17 07:36:20 +0000 | [diff] [blame] | 72 | |
| 73 | Decl *Parent = ASTLocation::FindImmediateParent(FD, CE); |
| 74 | |
| 75 | CallerNode->addCallee(ASTLocation(Parent, CE), CalleeNode); |
Zhongxing Xu | 6bd8fb5 | 2009-07-16 00:54:12 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
| 79 | CallGraph::~CallGraph() { |
| 80 | if (!FunctionMap.empty()) { |
| 81 | for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end(); |
| 82 | I != E; ++I) |
| 83 | delete I->second; |
| 84 | FunctionMap.clear(); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void CallGraph::addTU(ASTUnit &AST) { |
| 89 | ASTContext &Ctx = AST.getASTContext(); |
| 90 | DeclContext *DC = Ctx.getTranslationUnitDecl(); |
| 91 | |
| 92 | for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end(); |
| 93 | I != E; ++I) { |
| 94 | |
| 95 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) { |
| 96 | if (FD->isThisDeclarationADefinition()) { |
| 97 | // Set caller's ASTContext. |
| 98 | Entity *Ent = Entity::get(FD, Prog); |
| 99 | CallGraphNode *Node = getOrInsertFunction(Ent); |
| 100 | CallerCtx[Node] = &Ctx; |
| 101 | |
| 102 | CGBuilder builder(*this, FD, Ent, Node); |
| 103 | builder.Visit(FD->getBody()); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | CallGraphNode *CallGraph::getOrInsertFunction(Entity *F) { |
| 110 | CallGraphNode *&Node = FunctionMap[F]; |
| 111 | if (Node) |
| 112 | return Node; |
| 113 | |
| 114 | return Node = new CallGraphNode(F); |
| 115 | } |
| 116 | |
| 117 | void CallGraph::print(llvm::raw_ostream &os) { |
| 118 | for (iterator I = begin(), E = end(); I != E; ++I) { |
Zhongxing Xu | 24ff030 | 2009-07-17 05:49:16 +0000 | [diff] [blame] | 119 | if (I->second->hasCallee()) { |
| 120 | ASTContext &Ctx = *CallerCtx[I->second]; |
Zhongxing Xu | 56aac3f | 2009-07-17 07:49:44 +0000 | [diff] [blame] | 121 | os << "function: " << I->first->getPrintableName(Ctx).c_str() |
| 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) { |
Zhongxing Xu | 56aac3f | 2009-07-17 07:49:44 +0000 | [diff] [blame] | 125 | os << " " << CI->second->getName(Ctx).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 | } |