blob: ced4f1dd2eeabf911d8ddce1d4c5d3a6b01f351b [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
Zhongxing Xu97ab3942009-07-30 01:17:21 +000015#include "clang/AST/Decl.h"
16#include "clang/AST/DeclObjC.h"
Mike Stumpfa6ef182010-01-13 02:59:54 +000017#include "clang/AST/DeclTemplate.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000018#include "clang/AST/ParentMap.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000019#include "clang/AST/StmtVisitor.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000020#include "clang/Analysis/Analyses/LiveVariables.h"
21#include "clang/Analysis/AnalysisContext.h"
22#include "clang/Analysis/CFG.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000023#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000024#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000025#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000026
27using namespace clang;
28
Ted Kremenek58f5ec72009-10-20 21:39:41 +000029void AnalysisContextManager::clear() {
30 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
31 delete I->second;
32 Contexts.clear();
33}
34
Zhongxing Xu97ab3942009-07-30 01:17:21 +000035Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000036 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000037 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000038 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000039 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000040 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
41 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000042 else if (const FunctionTemplateDecl *FunTmpl
43 = dyn_cast_or_null<FunctionTemplateDecl>(D))
44 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000045
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000046 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000047}
48
Ted Kremenek82cd37c2009-08-21 23:25:54 +000049const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
50 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
51 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000052
Ted Kremenek82cd37c2009-08-21 23:25:54 +000053 return NULL;
54}
55
Zhongxing Xu97ab3942009-07-30 01:17:21 +000056CFG *AnalysisContext::getCFG() {
Ted Kremenek9b823e82010-08-03 00:09:51 +000057 if (UseUnoptimizedCFG)
58 return getUnoptimizedCFG();
59
Ted Kremenekd064fdc2010-03-23 00:13:23 +000060 if (!builtCFG) {
Ted Kremenekad5a8942010-08-02 23:46:59 +000061 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), true, AddEHEdges);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000062 // Even when the cfg is not successfully built, we don't
63 // want to try building it again.
64 builtCFG = true;
65 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000066 return cfg;
67}
68
Ted Kremenekad5a8942010-08-02 23:46:59 +000069CFG *AnalysisContext::getUnoptimizedCFG() {
70 if (!builtCompleteCFG) {
71 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(),
72 false, AddEHEdges);
73 // Even when the cfg is not successfully built, we don't
74 // want to try building it again.
75 builtCompleteCFG = true;
76 }
77 return completeCFG;
78}
79
Zhongxing Xu97ab3942009-07-30 01:17:21 +000080ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000081 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000082 PM = new ParentMap(getBody());
83 return *PM;
84}
85
86LiveVariables *AnalysisContext::getLiveVariables() {
87 if (!liveness) {
88 CFG *c = getCFG();
89 if (!c)
90 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000091
Ted Kremenekb1a7b652009-11-26 02:31:33 +000092 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +000093 liveness->runOnCFG(*c);
94 liveness->runOnAllBlocks(*c, 0, true);
95 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Zhongxing Xu97ab3942009-07-30 01:17:21 +000097 return liveness;
98}
99
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000100AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000101 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000102 AnalysisContext *&AC = Contexts[D];
103 if (!AC)
Ted Kremenek9b823e82010-08-03 00:09:51 +0000104 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG);
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Ted Kremenek23760022009-08-21 23:58:43 +0000106 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000107}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000108
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000109//===----------------------------------------------------------------------===//
110// FoldingSet profiling.
111//===----------------------------------------------------------------------===//
112
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000113void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
114 ContextKind ck,
115 AnalysisContext *ctx,
116 const LocationContext *parent,
117 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000118 ID.AddInteger(ck);
119 ID.AddPointer(ctx);
120 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000121 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000122}
123
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000124void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000125 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000126}
127
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000128void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
129 Profile(ID, getAnalysisContext(), getParent(), Enter);
130}
131
132void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000133 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000134}
135
136//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000137// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000138//===----------------------------------------------------------------------===//
139
Ted Kremenek0ee41242009-12-04 01:28:56 +0000140template <typename LOC, typename DATA>
141const LOC*
142LocationContextManager::getLocationContext(AnalysisContext *ctx,
143 const LocationContext *parent,
144 const DATA *d) {
145 llvm::FoldingSetNodeID ID;
146 LOC::Profile(ID, ctx, parent, d);
147 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000148
Ted Kremenek0ee41242009-12-04 01:28:56 +0000149 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000150
Ted Kremenek0ee41242009-12-04 01:28:56 +0000151 if (!L) {
152 L = new LOC(ctx, parent, d);
153 Contexts.InsertNode(L, InsertPos);
154 }
155 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000156}
157
Ted Kremenek0ee41242009-12-04 01:28:56 +0000158const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000159LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000160 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000161 const Stmt *s, const CFGBlock *blk,
162 unsigned idx) {
163 llvm::FoldingSetNodeID ID;
164 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
165 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000166 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000167 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
168 if (!L) {
169 L = new StackFrameContext(ctx, parent, s, blk, idx);
170 Contexts.InsertNode(L, InsertPos);
171 }
172 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000173}
174
Ted Kremenek0ee41242009-12-04 01:28:56 +0000175const ScopeContext *
176LocationContextManager::getScope(AnalysisContext *ctx,
177 const LocationContext *parent,
178 const Stmt *s) {
179 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
180}
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000182//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000183// LocationContext methods.
184//===----------------------------------------------------------------------===//
185
186const StackFrameContext *LocationContext::getCurrentStackFrame() const {
187 const LocationContext *LC = this;
188 while (LC) {
189 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
190 return SFC;
191 LC = LC->getParent();
192 }
193 return NULL;
194}
195
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000196const StackFrameContext *
197LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
198 const LocationContext *LC = this;
199 while (LC) {
200 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
201 if (cast<DeclContext>(SFC->getDecl()) == DC)
202 return SFC;
203 }
204 LC = LC->getParent();
205 }
206 return NULL;
207}
208
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000209bool LocationContext::isParentOf(const LocationContext *LC) const {
210 do {
211 const LocationContext *Parent = LC->getParent();
212 if (Parent == this)
213 return true;
214 else
215 LC = Parent;
216 } while (LC);
217
218 return false;
219}
220
Ted Kremenek67d12872009-12-07 22:05:27 +0000221//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000222// Lazily generated map to query the external variables referenced by a Block.
223//===----------------------------------------------------------------------===//
224
225namespace {
226class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
227 BumpVector<const VarDecl*> &BEVals;
228 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000229 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000230 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000231public:
232 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
233 BumpVectorContext &bc)
234 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000235
236 bool IsTrackedDecl(const VarDecl *VD) {
237 const DeclContext *DC = VD->getDeclContext();
238 return IgnoredContexts.count(DC) == 0;
239 }
240
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000241 void VisitStmt(Stmt *S) {
242 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
243 if (Stmt *child = *I)
244 Visit(child);
245 }
Ted Kremenek85248732010-02-06 00:30:00 +0000246
247 void VisitDeclRefExpr(const DeclRefExpr *DR) {
248 // Non-local variables are also directly modified.
249 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
250 if (!VD->hasLocalStorage()) {
251 unsigned &flag = Visited[VD];
252 if (!flag) {
253 flag = 1;
254 BEVals.push_back(VD, BC);
255 }
256 }
257 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000258
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000259 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000260 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
261 unsigned &flag = Visited[VD];
262 if (!flag) {
263 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000264 if (IsTrackedDecl(VD))
265 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000266 }
267 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000268 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000269
270 void VisitBlockExpr(BlockExpr *BR) {
271 // Blocks containing blocks can transitively capture more variables.
272 IgnoredContexts.insert(BR->getBlockDecl());
273 Visit(BR->getBlockDecl()->getBody());
274 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000275};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000276} // end anonymous namespace
277
278typedef BumpVector<const VarDecl*> DeclVec;
279
280static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
281 void *&Vec,
282 llvm::BumpPtrAllocator &A) {
283 if (Vec)
284 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000285
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000286 BumpVectorContext BC(A);
287 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
288 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000289
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000290 // Find the referenced variables.
291 FindBlockDeclRefExprsVals F(*BV, BC);
292 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000293
294 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000295 return BV;
296}
297
298std::pair<AnalysisContext::referenced_decls_iterator,
299 AnalysisContext::referenced_decls_iterator>
300AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
301 if (!ReferencedBlockVars)
302 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000303
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000304 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
305 return std::make_pair(V->begin(), V->end());
306}
307
308//===----------------------------------------------------------------------===//
309// Cleanup.
310//===----------------------------------------------------------------------===//
311
312AnalysisContext::~AnalysisContext() {
313 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000314 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000315 delete liveness;
316 delete PM;
317 delete ReferencedBlockVars;
318}
319
320AnalysisContextManager::~AnalysisContextManager() {
321 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
322 delete I->second;
323}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000324
325LocationContext::~LocationContext() {}
326
327LocationContextManager::~LocationContextManager() {
328 clear();
329}
330
331void LocationContextManager::clear() {
332 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000333 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000334 LocationContext *LC = &*I;
335 ++I;
336 delete LC;
337 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000338
Ted Kremenek0ee41242009-12-04 01:28:56 +0000339 Contexts.clear();
340}
341