blob: d79b4e7fe59bb13a49aaf3d78dedcfd73608f76e [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
Ted Kremenekdc0d9092009-12-04 00:50:10 +000083//===----------------------------------------------------------------------===//
84// FoldingSet profiling.
85//===----------------------------------------------------------------------===//
86
Zhongxing Xu18c7c062009-08-03 07:23:22 +000087void LocationContext::Profile(llvm::FoldingSetNodeID &ID, ContextKind k,
Ted Kremenek54c809b2009-08-21 23:39:58 +000088 AnalysisContext *ctx,
89 const LocationContext *parent) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +000090 ID.AddInteger(k);
91 ID.AddPointer(ctx);
92 ID.AddPointer(parent);
93}
94
Ted Kremenekdc0d9092009-12-04 00:50:10 +000095void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
96 ContextKind ck,
97 AnalysisContext *ctx,
98 const LocationContext *parent,
99 const void* data) {
100 LocationContext::Profile(ID, ck, ctx, parent);
101 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000102}
103
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000104void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
105 Profile(ID, getAnalysisContext(), getParent(), CallSite);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000106}
107
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000108void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
109 Profile(ID, getAnalysisContext(), getParent(), Enter);
110}
111
112void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
113 Profile(ID, getAnalysisContext(), getParent(), BD);
114}
115
116//===----------------------------------------------------------------------===//
117// Cleanup.
118//===----------------------------------------------------------------------===//
119
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000120LocationContextManager::~LocationContextManager() {
121 clear();
122}
123
124void LocationContextManager::clear() {
125 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
126 E = Contexts.end(); I != E; ) {
127 LocationContext *LC = &*I;
128 ++I;
129 delete LC;
130 }
131
132 Contexts.clear();
133}
134
Ted Kremenek54c809b2009-08-21 23:39:58 +0000135StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000136LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000137 const LocationContext *parent,
138 const Stmt *s) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000139 llvm::FoldingSetNodeID ID;
140 StackFrameContext::Profile(ID, ctx, parent, s);
141 void *InsertPos;
142
Mike Stump1eb44332009-09-09 15:08:12 +0000143 StackFrameContext *f =
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000144 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
145 if (!f) {
146 f = new StackFrameContext(ctx, parent, s);
147 Contexts.InsertNode(f, InsertPos);
148 }
149 return f;
150}
151
152ScopeContext *LocationContextManager::getScope(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000153 const LocationContext *parent,
154 const Stmt *s) {
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000155 llvm::FoldingSetNodeID ID;
156 ScopeContext::Profile(ID, ctx, parent, s);
157 void *InsertPos;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000159 ScopeContext *scope =
160 cast_or_null<ScopeContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
161
162 if (!scope) {
163 scope = new ScopeContext(ctx, parent, s);
164 Contexts.InsertNode(scope, InsertPos);
165 }
166 return scope;
167}
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000168
169//===----------------------------------------------------------------------===//
170// Lazily generated map to query the external variables referenced by a Block.
171//===----------------------------------------------------------------------===//
172
173namespace {
174class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
175 BumpVector<const VarDecl*> &BEVals;
176 BumpVectorContext &BC;
177public:
178 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
179 BumpVectorContext &bc)
180 : BEVals(bevals), BC(bc) {}
181
182 void VisitStmt(Stmt *S) {
183 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
184 if (Stmt *child = *I)
185 Visit(child);
186 }
187
188 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
189 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
190 BEVals.push_back(VD, BC);
191 }
192};
193} // end anonymous namespace
194
195typedef BumpVector<const VarDecl*> DeclVec;
196
197static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
198 void *&Vec,
199 llvm::BumpPtrAllocator &A) {
200 if (Vec)
201 return (DeclVec*) Vec;
202
203 BumpVectorContext BC(A);
204 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
205 new (BV) DeclVec(BC, 10);
206
207 // Find the referenced variables.
208 FindBlockDeclRefExprsVals F(*BV, BC);
209 F.Visit(BD->getBody());
210
211 Vec = BV;
212 return BV;
213}
214
215std::pair<AnalysisContext::referenced_decls_iterator,
216 AnalysisContext::referenced_decls_iterator>
217AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
218 if (!ReferencedBlockVars)
219 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
220
221 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
222 return std::make_pair(V->begin(), V->end());
223}
224
225//===----------------------------------------------------------------------===//
226// Cleanup.
227//===----------------------------------------------------------------------===//
228
229AnalysisContext::~AnalysisContext() {
230 delete cfg;
231 delete liveness;
232 delete PM;
233 delete ReferencedBlockVars;
234}
235
236AnalysisContextManager::~AnalysisContextManager() {
237 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
238 delete I->second;
239}