blob: 422c5013cc8d3922b2f41c69c8e343d58a29f686 [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
36 void VisitCompoundStmt(CompoundStmt *S) {
37 VisitChildren(S);
38 }
39
Zhongxing Xu24ff0302009-07-17 05:49:16 +000040 void VisitIfStmt(IfStmt *S) {
41 VisitChildren(S);
42 }
43
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +000044 void VisitCallExpr(CallExpr *CE);
45
46 void VisitChildren(Stmt *S) {
47 for (Stmt::child_iterator I=S->child_begin(), E=S->child_end(); I != E;++I)
48 if (*I)
49 static_cast<CGBuilder*>(this)->Visit(*I);
50 }
51};
52}
53
54void CGBuilder::VisitCallExpr(CallExpr *CE) {
55 Expr *Callee = CE->getCallee();
56
57 if (CastExpr *CE = dyn_cast<CastExpr>(Callee))
58 Callee = CE->getSubExpr();
59
60 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Callee)) {
61 Decl *D = DRE->getDecl();
62 if (FunctionDecl *CalleeDecl = dyn_cast<FunctionDecl>(D)) {
63
64 Entity *Ent = Entity::get(CalleeDecl, G.getProgram());
65
66 CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent);
67
68 CallerNode->addCallee(ASTLocation(FD, CE), CalleeNode);
69 }
70 }
71}
72
73CallGraph::~CallGraph() {
74 if (!FunctionMap.empty()) {
75 for (FunctionMapTy::iterator I = FunctionMap.begin(), E = FunctionMap.end();
76 I != E; ++I)
77 delete I->second;
78 FunctionMap.clear();
79 }
80}
81
82void CallGraph::addTU(ASTUnit &AST) {
83 ASTContext &Ctx = AST.getASTContext();
84 DeclContext *DC = Ctx.getTranslationUnitDecl();
85
86 for (DeclContext::decl_iterator I = DC->decls_begin(), E = DC->decls_end();
87 I != E; ++I) {
88
89 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) {
90 if (FD->isThisDeclarationADefinition()) {
91 // Set caller's ASTContext.
92 Entity *Ent = Entity::get(FD, Prog);
93 CallGraphNode *Node = getOrInsertFunction(Ent);
94 CallerCtx[Node] = &Ctx;
95
96 CGBuilder builder(*this, FD, Ent, Node);
97 builder.Visit(FD->getBody());
98 }
99 }
100 }
101}
102
103CallGraphNode *CallGraph::getOrInsertFunction(Entity *F) {
104 CallGraphNode *&Node = FunctionMap[F];
105 if (Node)
106 return Node;
107
108 return Node = new CallGraphNode(F);
109}
110
111void CallGraph::print(llvm::raw_ostream &os) {
112 for (iterator I = begin(), E = end(); I != E; ++I) {
Zhongxing Xu24ff0302009-07-17 05:49:16 +0000113 if (I->second->hasCallee()) {
114 ASTContext &Ctx = *CallerCtx[I->second];
115 os << "function: " << I->first->getName(Ctx) << " calls:\n";
116 for (CallGraphNode::iterator CI = I->second->begin(),
117 CE = I->second->end(); CI != CE; ++CI) {
118 os << " " << CI->second->getName(Ctx);
119 }
120 os << '\n';
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +0000121 }
Zhongxing Xu6bd8fb52009-07-16 00:54:12 +0000122 }
123}
124
125void CallGraph::dump() {
126 print(llvm::errs());
127}