blob: b4f57b8fde47be17c9a7302ac5f941d9296f7226 [file] [log] [blame]
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +00001//== 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
19using namespace clang;
20using namespace idx;
21
22namespace {
23class CGBuilder : public StmtVisitor<CGBuilder> {
24
25 CallGraph &G;
26 FunctionDecl *FD;
27
28 Entity *CallerEnt;
29
30 CallGraphNode *CallerNode;
31
32public:
33 CGBuilder(CallGraph &g, FunctionDecl *fd, Entity *E, CallGraphNode *N)
34 : G(g), FD(fd), CallerEnt(E), CallerNode(N) {}
35
Zhongxing Xu6fc45052009-07-17 07:05:19 +000036 void VisitCompoundStmt(CompoundStmt *S) { VisitChildren(S); }
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +000037
Zhongxing Xu6fc45052009-07-17 07:05:19 +000038 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 Xu24ff0302009-07-17 05:49:16 +000057
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +000058 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
68void CGBuilder::VisitCallExpr(CallExpr *CE) {
Zhongxing Xua0042542009-07-17 07:29:51 +000069 if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) {
70 Entity *Ent = Entity::get(CalleeDecl, G.getProgram());
71 CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent);
Zhongxing Xu7f66bd22009-07-17 07:36:20 +000072
73 Decl *Parent = ASTLocation::FindImmediateParent(FD, CE);
74
75 CallerNode->addCallee(ASTLocation(Parent, CE), CalleeNode);
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +000076 }
77}
78
79CallGraph::~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
88void 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
109CallGraphNode *CallGraph::getOrInsertFunction(Entity *F) {
110 CallGraphNode *&Node = FunctionMap[F];
111 if (Node)
112 return Node;
113
114 return Node = new CallGraphNode(F);
115}
116
117void CallGraph::print(llvm::raw_ostream &os) {
118 for (iterator I = begin(), E = end(); I != E; ++I) {
Zhongxing Xu24ff0302009-07-17 05:49:16 +0000119 if (I->second->hasCallee()) {
120 ASTContext &Ctx = *CallerCtx[I->second];
Zhongxing Xu56aac3f2009-07-17 07:49:44 +0000121 os << "function: " << I->first->getPrintableName(Ctx).c_str()
122 << " calls:\n";
Zhongxing Xu24ff0302009-07-17 05:49:16 +0000123 for (CallGraphNode::iterator CI = I->second->begin(),
124 CE = I->second->end(); CI != CE; ++CI) {
Zhongxing Xu56aac3f2009-07-17 07:49:44 +0000125 os << " " << CI->second->getName(Ctx).c_str();
Zhongxing Xu24ff0302009-07-17 05:49:16 +0000126 }
127 os << '\n';
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +0000128 }
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +0000129 }
130}
131
132void CallGraph::dump() {
133 print(llvm::errs());
134}