blob: 339e2c93cea90bcebf5238e373cfdacba2f8fdc6 [file] [log] [blame]
Zhongxing Xu97ab3942009-07-30 01:17:21 +00001//== AnalysisContext.cpp - Analysis context for Path Sens analysis -*- 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 defines AnalysisContext, a class that manages the analysis context
11// data for path sensitive analysis.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/AnalysisContext.h"
16#include "clang/Analysis/Analyses/LiveVariables.h"
17#include "clang/Analysis/CFG.h"
18#include "clang/AST/Decl.h"
19#include "clang/AST/DeclObjC.h"
20#include "clang/AST/ParentMap.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000021#include "clang/AST/StmtVisitor.h"
22#include "clang/Analysis/Support/BumpVector.h"
Mike Stump87a05f12009-07-31 01:10:29 +000023#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000024
25using namespace clang;
26
Ted Kremenek58f5ec72009-10-20 21:39:41 +000027void AnalysisContextManager::clear() {
28 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
29 delete I->second;
30 Contexts.clear();
31}
32
Zhongxing Xu97ab3942009-07-30 01:17:21 +000033Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000034 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000035 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000036 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000037 return MD->getBody();
38
Mike Stump87a05f12009-07-31 01:10:29 +000039 llvm::llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000040}
41
Ted Kremenek82cd37c2009-08-21 23:25:54 +000042const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
43 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
44 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000045
Ted Kremenek82cd37c2009-08-21 23:25:54 +000046 return NULL;
47}
48
Zhongxing Xu97ab3942009-07-30 01:17:21 +000049CFG *AnalysisContext::getCFG() {
Mike Stump1eb44332009-09-09 15:08:12 +000050 if (!cfg)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000051 cfg = CFG::buildCFG(getBody(), &D->getASTContext());
52 return cfg;
53}
54
55ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000056 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000057 PM = new ParentMap(getBody());
58 return *PM;
59}
60
61LiveVariables *AnalysisContext::getLiveVariables() {
62 if (!liveness) {
63 CFG *c = getCFG();
64 if (!c)
65 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000066
Ted Kremenekb1a7b652009-11-26 02:31:33 +000067 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000068 liveness->runOnCFG(*c);
69 liveness->runOnAllBlocks(*c, 0, true);
70 }
Mike Stump1eb44332009-09-09 15:08:12 +000071
Zhongxing Xu97ab3942009-07-30 01:17:21 +000072 return liveness;
73}
74
Ted Kremenek23760022009-08-21 23:58:43 +000075AnalysisContext *AnalysisContextManager::getContext(const Decl *D) {
76 AnalysisContext *&AC = Contexts[D];
77 if (!AC)
78 AC = new AnalysisContext(D);
Mike Stump1eb44332009-09-09 15:08:12 +000079
Ted Kremenek23760022009-08-21 23:58:43 +000080 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +000081}
Zhongxing Xu18c7c062009-08-03 07:23:22 +000082
83void LocationContext::Profile(llvm::FoldingSetNodeID &ID, ContextKind k,
Ted Kremenek54c809b2009-08-21 23:39:58 +000084 AnalysisContext *ctx,
85 const LocationContext *parent) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +000086 ID.AddInteger(k);
87 ID.AddPointer(ctx);
88 ID.AddPointer(parent);
89}
90
91void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID,AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +000092 const LocationContext *parent, const Stmt *s) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +000093 LocationContext::Profile(ID, StackFrame, ctx, parent);
94 ID.AddPointer(s);
95}
96
97void ScopeContext::Profile(llvm::FoldingSetNodeID &ID, AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +000098 const LocationContext *parent, const Stmt *s) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +000099 LocationContext::Profile(ID, Scope, ctx, parent);
100 ID.AddPointer(s);
101}
102
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000103LocationContextManager::~LocationContextManager() {
104 clear();
105}
106
107void LocationContextManager::clear() {
108 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
109 E = Contexts.end(); I != E; ) {
110 LocationContext *LC = &*I;
111 ++I;
112 delete LC;
113 }
114
115 Contexts.clear();
116}
117
Ted Kremenek54c809b2009-08-21 23:39:58 +0000118StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000119LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000120 const LocationContext *parent,
121 const Stmt *s) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000122 llvm::FoldingSetNodeID ID;
123 StackFrameContext::Profile(ID, ctx, parent, s);
124 void *InsertPos;
125
Mike Stump1eb44332009-09-09 15:08:12 +0000126 StackFrameContext *f =
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000127 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
128 if (!f) {
129 f = new StackFrameContext(ctx, parent, s);
130 Contexts.InsertNode(f, InsertPos);
131 }
132 return f;
133}
134
135ScopeContext *LocationContextManager::getScope(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000136 const LocationContext *parent,
137 const Stmt *s) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000138 llvm::FoldingSetNodeID ID;
139 ScopeContext::Profile(ID, ctx, parent, s);
140 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000142 ScopeContext *scope =
143 cast_or_null<ScopeContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
144
145 if (!scope) {
146 scope = new ScopeContext(ctx, parent, s);
147 Contexts.InsertNode(scope, InsertPos);
148 }
149 return scope;
150}
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000151
152//===----------------------------------------------------------------------===//
153// Lazily generated map to query the external variables referenced by a Block.
154//===----------------------------------------------------------------------===//
155
156namespace {
157class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
158 BumpVector<const VarDecl*> &BEVals;
159 BumpVectorContext &BC;
160public:
161 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
162 BumpVectorContext &bc)
163 : BEVals(bevals), BC(bc) {}
164
165 void VisitStmt(Stmt *S) {
166 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
167 if (Stmt *child = *I)
168 Visit(child);
169 }
170
171 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
172 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
173 BEVals.push_back(VD, BC);
174 }
175};
176} // end anonymous namespace
177
178typedef BumpVector<const VarDecl*> DeclVec;
179
180static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
181 void *&Vec,
182 llvm::BumpPtrAllocator &A) {
183 if (Vec)
184 return (DeclVec*) Vec;
185
186 BumpVectorContext BC(A);
187 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
188 new (BV) DeclVec(BC, 10);
189
190 // Find the referenced variables.
191 FindBlockDeclRefExprsVals F(*BV, BC);
192 F.Visit(BD->getBody());
193
194 Vec = BV;
195 return BV;
196}
197
198std::pair<AnalysisContext::referenced_decls_iterator,
199 AnalysisContext::referenced_decls_iterator>
200AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
201 if (!ReferencedBlockVars)
202 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
203
204 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
205 return std::make_pair(V->begin(), V->end());
206}
207
208//===----------------------------------------------------------------------===//
209// Cleanup.
210//===----------------------------------------------------------------------===//
211
212AnalysisContext::~AnalysisContext() {
213 delete cfg;
214 delete liveness;
215 delete PM;
216 delete ReferencedBlockVars;
217}
218
219AnalysisContextManager::~AnalysisContextManager() {
220 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
221 delete I->second;
222}