blob: d49e8ec11be00b5fecca08ceb4a78dceb494f254 [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);
72 CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode);
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +000073 }
74}
75
76CallGraph::~CallGraph() {
77 if (!FunctionMap.empty()) {
78 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
79 I != E; ++I)
80 delete I->second;
81 FunctionMap.clear();
82 }
83}
84
85void CallGraph::addTU(ASTUnit &AST) {
86 ASTContext &Ctx = AST.getASTContext();
87 DeclContext *DC = Ctx.getTranslationUnitDecl();
88
89 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
90 I != E; ++I) {
91
92 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
93 if (FD->isThisDeclarationADefinition()) {
94 // Set caller's ASTContext.
95 Entity *Ent = Entity::get(FD, Prog);
96 CallGraphNode *Node = getOrInsertFunction(Ent);
97 CallerCtx[Node] = &Ctx;
98
99 CGBuilder builder(*this, FD, Ent, Node);
100 builder.Visit(FD->getBody());
101 }
102 }
103 }
104}
105
106CallGraphNode *CallGraph::getOrInsertFunction(Entity *F) {
107 CallGraphNode *&Node = FunctionMap[F];
108 if (Node)
109 return Node;
110
111 return Node = new CallGraphNode(F);
112}
113
114void CallGraph::print(llvm::raw_ostream &os) {
115 for (iterator I = begin(), E = end(); I != E; ++I) {
Zhongxing Xu24ff0302009-07-17 05:49:16 +0000116 if (I->second->hasCallee()) {
117 ASTContext &Ctx = *CallerCtx[I->second];
118 os << "function: " << I->first->getName(Ctx) << " calls:\n";
119 for (CallGraphNode::iterator CI = I->second->begin(),
120 CE = I->second->end(); CI != CE; ++CI) {
121 os << " " << CI->second->getName(Ctx);
122 }
123 os << '\n';
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +0000124 }
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +0000125 }
126}
127
128void CallGraph::dump() {
129 print(llvm::errs());
130}