blob: 07c2b35349f966c8b84e30753e1f37de4c72e7b5 [file] [log] [blame]
Zhongxing Xua62ad242009-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
Zhongxing Xue0c993f2009-07-23 09:04:23 +000019#include "llvm/Support/GraphWriter.h"
20
Zhongxing Xua62ad242009-07-16 00:54:12 +000021using namespace clang;
22using namespace idx;
23
24namespace {
25class CGBuilder : public StmtVisitor<CGBuilder> {
26
27 CallGraph &G;
28 FunctionDecl *FD;
29
Argiris Kirtzidisff088262009-07-21 00:07:06 +000030 Entity CallerEnt;
Zhongxing Xua62ad242009-07-16 00:54:12 +000031
32 CallGraphNode *CallerNode;
33
34public:
Argiris Kirtzidisff088262009-07-21 00:07:06 +000035 CGBuilder(CallGraph &g, FunctionDecl *fd, Entity E, CallGraphNode *N)
Zhongxing Xua62ad242009-07-16 00:54:12 +000036 : G(g), FD(fd), CallerEnt(E), CallerNode(N) {}
37
Zhongxing Xue288fe22009-07-18 08:49:07 +000038 void VisitStmt(Stmt *S) { VisitChildren(S); }
Zhongxing Xuba64f382009-07-17 05:49:16 +000039
Zhongxing Xua62ad242009-07-16 00:54:12 +000040 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
50void CGBuilder::VisitCallExpr(CallExpr *CE) {
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +000051 if (FunctionDecl *CalleeDecl = CE->getDirectCallee()) {
Argiris Kirtzidisff088262009-07-21 00:07:06 +000052 Entity Ent = Entity::get(CalleeDecl, G.getProgram());
Zhongxing Xuc2aab6f2009-07-17 07:29:51 +000053 CallGraphNode *CalleeNode = G.getOrInsertFunction(Ent);
Zhongxing Xu95ed5d02009-07-17 07:36:20 +000054
55 Decl *Parent = ASTLocation::FindImmediateParent(FD, CE);
56
57 CallerNode->addCallee(ASTLocation(Parent, CE), CalleeNode);
Zhongxing Xua62ad242009-07-16 00:54:12 +000058 }
59}
60
Zhongxing Xua754eb22009-07-23 13:39:38 +000061CallGraph::CallGraph() : Root(0) {
62 ExternalCallingNode = getOrInsertFunction(Entity());
63}
64
Zhongxing Xua62ad242009-07-16 00:54:12 +000065CallGraph::~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
74void 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.
Argiris Kirtzidisff088262009-07-21 00:07:06 +000084 Entity Ent = Entity::get(FD, Prog);
Zhongxing Xua62ad242009-07-16 00:54:12 +000085 CallGraphNode *Node = getOrInsertFunction(Ent);
86 CallerCtx[Node] = &Ctx;
Zhongxing Xua754eb22009-07-23 13:39:38 +000087
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 Xua62ad242009-07-16 00:54:12 +000095
96 CGBuilder builder(*this, FD, Ent, Node);
97 builder.Visit(FD->getBody());
98 }
99 }
100 }
101}
102
Argiris Kirtzidisff088262009-07-21 00:07:06 +0000103CallGraphNode *CallGraph::getOrInsertFunction(Entity F) {
Zhongxing Xua62ad242009-07-16 00:54:12 +0000104 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 Xuba64f382009-07-17 05:49:16 +0000113 if (I->second->hasCallee()) {
Argiris Kirtzidis0d0b5652009-07-21 07:52:21 +0000114 os << "function: " << I->first.getPrintableName()
Zhongxing Xuc2bc1472009-07-17 07:49:44 +0000115 << " calls:\n";
Zhongxing Xuba64f382009-07-17 05:49:16 +0000116 for (CallGraphNode::iterator CI = I->second->begin(),
117 CE = I->second->end(); CI != CE; ++CI) {
Argiris Kirtzidis0d0b5652009-07-21 07:52:21 +0000118 os << " " << CI->second->getName().c_str();
Zhongxing Xuba64f382009-07-17 05:49:16 +0000119 }
120 os << '\n';
Zhongxing Xua62ad242009-07-16 00:54:12 +0000121 }
Zhongxing Xua62ad242009-07-16 00:54:12 +0000122 }
123}
124
125void CallGraph::dump() {
126 print(llvm::errs());
127}
Zhongxing Xue0c993f2009-07-23 09:04:23 +0000128
129void CallGraph::ViewCallGraph() const {
130 llvm::ViewGraph(*this, "CallGraph");
131}
132
133namespace llvm {
134
135template <>
136struct DOTGraphTraits<CallGraph> : public DefaultDOTGraphTraits {
137
138 static std::string getNodeLabel(const CallGraphNode *Node,
139 const CallGraph &CG, bool ShortNames) {
140 return Node->getName();
141
142 }
143
144};
145
146}