blob: 5307074dce6889d10d8a4336dcf706acee0cf45c [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"
Tom Caredb34ab72010-08-23 19:51:57 +000021#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000022#include "clang/Analysis/AnalysisContext.h"
23#include "clang/Analysis/CFG.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000024#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000025#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000026#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000027
28using namespace clang;
29
Ted Kremenek58f5ec72009-10-20 21:39:41 +000030void AnalysisContextManager::clear() {
31 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
32 delete I->second;
33 Contexts.clear();
34}
35
Zhongxing Xu97ab3942009-07-30 01:17:21 +000036Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000037 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000038 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000039 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000040 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000041 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
42 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000043 else if (const FunctionTemplateDecl *FunTmpl
44 = dyn_cast_or_null<FunctionTemplateDecl>(D))
45 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000046
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000047 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000048}
49
Ted Kremenek82cd37c2009-08-21 23:25:54 +000050const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
51 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
52 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000053
Ted Kremenek82cd37c2009-08-21 23:25:54 +000054 return NULL;
55}
56
Zhongxing Xu97ab3942009-07-30 01:17:21 +000057CFG *AnalysisContext::getCFG() {
Ted Kremenek9b823e82010-08-03 00:09:51 +000058 if (UseUnoptimizedCFG)
59 return getUnoptimizedCFG();
60
Ted Kremenekd064fdc2010-03-23 00:13:23 +000061 if (!builtCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000062 CFG::BuildOptions B;
63 B.AddEHEdges = AddEHEdges;
Marcin Swiderski9121ba22010-09-30 07:41:24 +000064 B.AddImplicitDtors = AddImplicitDtors;
65 B.AddInitializers = AddInitializers;
Ted Kremenek6c52c782010-09-14 23:41:16 +000066 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000067 // Even when the cfg is not successfully built, we don't
68 // want to try building it again.
69 builtCFG = true;
70 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000071 return cfg;
72}
73
Ted Kremenekad5a8942010-08-02 23:46:59 +000074CFG *AnalysisContext::getUnoptimizedCFG() {
75 if (!builtCompleteCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000076 CFG::BuildOptions B;
77 B.PruneTriviallyFalseEdges = false;
78 B.AddEHEdges = AddEHEdges;
Marcin Swiderski9121ba22010-09-30 07:41:24 +000079 B.AddImplicitDtors = AddImplicitDtors;
80 B.AddInitializers = AddInitializers;
Ted Kremenek6c52c782010-09-14 23:41:16 +000081 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekad5a8942010-08-02 23:46:59 +000082 // Even when the cfg is not successfully built, we don't
83 // want to try building it again.
84 builtCompleteCFG = true;
85 }
86 return completeCFG;
87}
88
Zhongxing Xu97ab3942009-07-30 01:17:21 +000089ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000090 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000091 PM = new ParentMap(getBody());
92 return *PM;
93}
94
Tom Caredb34ab72010-08-23 19:51:57 +000095PseudoConstantAnalysis *AnalysisContext::getPseudoConstantAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +000096 if (!PCA)
Tom Caredb34ab72010-08-23 19:51:57 +000097 PCA = new PseudoConstantAnalysis(getBody());
Tom Care245adab2010-08-18 21:17:24 +000098 return PCA;
99}
100
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000101LiveVariables *AnalysisContext::getLiveVariables() {
102 if (!liveness) {
103 CFG *c = getCFG();
104 if (!c)
105 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000107 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000108 liveness->runOnCFG(*c);
109 liveness->runOnAllBlocks(*c, 0, true);
110 }
Mike Stump1eb44332009-09-09 15:08:12 +0000111
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000112 return liveness;
113}
114
Tom Careec49bf42010-08-27 22:30:10 +0000115LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
116 if (!relaxedLiveness) {
117 CFG *c = getCFG();
118 if (!c)
119 return 0;
120
121 relaxedLiveness = new LiveVariables(*this, false);
122 relaxedLiveness->runOnCFG(*c);
123 relaxedLiveness->runOnAllBlocks(*c, 0, true);
124 }
125
126 return relaxedLiveness;
127}
128
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000129AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000130 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000131 AnalysisContext *&AC = Contexts[D];
132 if (!AC)
Marcin Swiderski9121ba22010-09-30 07:41:24 +0000133 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG, false,
134 AddImplicitDtors, AddInitializers);
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Ted Kremenek23760022009-08-21 23:58:43 +0000136 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000137}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000138
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000139//===----------------------------------------------------------------------===//
140// FoldingSet profiling.
141//===----------------------------------------------------------------------===//
142
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000143void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
144 ContextKind ck,
145 AnalysisContext *ctx,
146 const LocationContext *parent,
147 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000148 ID.AddInteger(ck);
149 ID.AddPointer(ctx);
150 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000151 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000152}
153
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000154void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xud7064342010-11-24 13:08:51 +0000155 Profile(ID, getAnalysisContext(), getParent(), CallSite.getPointer(),
156 CallSite.getInt(), Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000157}
158
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000159void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
160 Profile(ID, getAnalysisContext(), getParent(), Enter);
161}
162
163void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000164 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000165}
166
167//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000168// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000169//===----------------------------------------------------------------------===//
170
Ted Kremenek0ee41242009-12-04 01:28:56 +0000171template <typename LOC, typename DATA>
172const LOC*
173LocationContextManager::getLocationContext(AnalysisContext *ctx,
174 const LocationContext *parent,
175 const DATA *d) {
176 llvm::FoldingSetNodeID ID;
177 LOC::Profile(ID, ctx, parent, d);
178 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000179
Ted Kremenek0ee41242009-12-04 01:28:56 +0000180 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000181
Ted Kremenek0ee41242009-12-04 01:28:56 +0000182 if (!L) {
183 L = new LOC(ctx, parent, d);
184 Contexts.InsertNode(L, InsertPos);
185 }
186 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000187}
188
Ted Kremenek0ee41242009-12-04 01:28:56 +0000189const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000190LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000191 const LocationContext *parent,
Zhongxing Xud7064342010-11-24 13:08:51 +0000192 const Stmt *s, bool asLValue,
193 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000194 llvm::FoldingSetNodeID ID;
Zhongxing Xud7064342010-11-24 13:08:51 +0000195 StackFrameContext::Profile(ID, ctx, parent, s, asLValue, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000196 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000197 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000198 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
199 if (!L) {
Zhongxing Xud7064342010-11-24 13:08:51 +0000200 L = new StackFrameContext(ctx, parent, s, asLValue, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000201 Contexts.InsertNode(L, InsertPos);
202 }
203 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000204}
205
Ted Kremenek0ee41242009-12-04 01:28:56 +0000206const ScopeContext *
207LocationContextManager::getScope(AnalysisContext *ctx,
208 const LocationContext *parent,
209 const Stmt *s) {
210 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
211}
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000213//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000214// LocationContext methods.
215//===----------------------------------------------------------------------===//
216
217const StackFrameContext *LocationContext::getCurrentStackFrame() const {
218 const LocationContext *LC = this;
219 while (LC) {
220 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
221 return SFC;
222 LC = LC->getParent();
223 }
224 return NULL;
225}
226
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000227const StackFrameContext *
228LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
229 const LocationContext *LC = this;
230 while (LC) {
231 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
232 if (cast<DeclContext>(SFC->getDecl()) == DC)
233 return SFC;
234 }
235 LC = LC->getParent();
236 }
237 return NULL;
238}
239
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000240bool LocationContext::isParentOf(const LocationContext *LC) const {
241 do {
242 const LocationContext *Parent = LC->getParent();
243 if (Parent == this)
244 return true;
245 else
246 LC = Parent;
247 } while (LC);
248
249 return false;
250}
251
Ted Kremenek67d12872009-12-07 22:05:27 +0000252//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000253// Lazily generated map to query the external variables referenced by a Block.
254//===----------------------------------------------------------------------===//
255
256namespace {
257class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
258 BumpVector<const VarDecl*> &BEVals;
259 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000260 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000261 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000262public:
263 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
264 BumpVectorContext &bc)
265 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000266
267 bool IsTrackedDecl(const VarDecl *VD) {
268 const DeclContext *DC = VD->getDeclContext();
269 return IgnoredContexts.count(DC) == 0;
270 }
271
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000272 void VisitStmt(Stmt *S) {
273 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
274 if (Stmt *child = *I)
275 Visit(child);
276 }
Ted Kremenek85248732010-02-06 00:30:00 +0000277
278 void VisitDeclRefExpr(const DeclRefExpr *DR) {
279 // Non-local variables are also directly modified.
280 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
281 if (!VD->hasLocalStorage()) {
282 unsigned &flag = Visited[VD];
283 if (!flag) {
284 flag = 1;
285 BEVals.push_back(VD, BC);
286 }
287 }
288 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000289
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000290 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000291 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
292 unsigned &flag = Visited[VD];
293 if (!flag) {
294 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000295 if (IsTrackedDecl(VD))
296 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000297 }
298 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000299 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000300
301 void VisitBlockExpr(BlockExpr *BR) {
302 // Blocks containing blocks can transitively capture more variables.
303 IgnoredContexts.insert(BR->getBlockDecl());
304 Visit(BR->getBlockDecl()->getBody());
305 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000306};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000307} // end anonymous namespace
308
309typedef BumpVector<const VarDecl*> DeclVec;
310
311static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
312 void *&Vec,
313 llvm::BumpPtrAllocator &A) {
314 if (Vec)
315 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000316
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000317 BumpVectorContext BC(A);
318 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
319 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000320
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000321 // Find the referenced variables.
322 FindBlockDeclRefExprsVals F(*BV, BC);
323 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000324
325 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000326 return BV;
327}
328
329std::pair<AnalysisContext::referenced_decls_iterator,
330 AnalysisContext::referenced_decls_iterator>
331AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
332 if (!ReferencedBlockVars)
333 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000334
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000335 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
336 return std::make_pair(V->begin(), V->end());
337}
338
339//===----------------------------------------------------------------------===//
340// Cleanup.
341//===----------------------------------------------------------------------===//
342
343AnalysisContext::~AnalysisContext() {
344 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000345 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000346 delete liveness;
Ted Kremenek91c83e72010-08-28 18:59:04 +0000347 delete relaxedLiveness;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000348 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000349 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000350 delete ReferencedBlockVars;
351}
352
353AnalysisContextManager::~AnalysisContextManager() {
354 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
355 delete I->second;
356}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000357
358LocationContext::~LocationContext() {}
359
360LocationContextManager::~LocationContextManager() {
361 clear();
362}
363
364void LocationContextManager::clear() {
365 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000366 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000367 LocationContext *LC = &*I;
368 ++I;
369 delete LC;
370 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000371
Ted Kremenek0ee41242009-12-04 01:28:56 +0000372 Contexts.clear();
373}
374