blob: 5233d3b8f9f89e02ae94cc2cc684b997d6307ba0 [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
Anders Carlsson04eeba42011-01-16 22:05:23 +000089void AnalysisContext::dumpCFG() {
90 getCFG()->dump(getASTContext().getLangOptions());
91}
92
Zhongxing Xu97ab3942009-07-30 01:17:21 +000093ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000094 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000095 PM = new ParentMap(getBody());
96 return *PM;
97}
98
Tom Caredb34ab72010-08-23 19:51:57 +000099PseudoConstantAnalysis *AnalysisContext::getPseudoConstantAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +0000100 if (!PCA)
Tom Caredb34ab72010-08-23 19:51:57 +0000101 PCA = new PseudoConstantAnalysis(getBody());
Tom Care245adab2010-08-18 21:17:24 +0000102 return PCA;
103}
104
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000105LiveVariables *AnalysisContext::getLiveVariables() {
106 if (!liveness) {
107 CFG *c = getCFG();
108 if (!c)
109 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000110
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000111 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000112 liveness->runOnCFG(*c);
113 liveness->runOnAllBlocks(*c, 0, true);
114 }
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000116 return liveness;
117}
118
Tom Careec49bf42010-08-27 22:30:10 +0000119LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
120 if (!relaxedLiveness) {
121 CFG *c = getCFG();
122 if (!c)
123 return 0;
124
125 relaxedLiveness = new LiveVariables(*this, false);
126 relaxedLiveness->runOnCFG(*c);
127 relaxedLiveness->runOnAllBlocks(*c, 0, true);
128 }
129
130 return relaxedLiveness;
131}
132
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000133AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000134 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000135 AnalysisContext *&AC = Contexts[D];
136 if (!AC)
Marcin Swiderski9121ba22010-09-30 07:41:24 +0000137 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG, false,
138 AddImplicitDtors, AddInitializers);
Mike Stump1eb44332009-09-09 15:08:12 +0000139
Ted Kremenek23760022009-08-21 23:58:43 +0000140 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000141}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000142
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000143//===----------------------------------------------------------------------===//
144// FoldingSet profiling.
145//===----------------------------------------------------------------------===//
146
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000147void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
148 ContextKind ck,
149 AnalysisContext *ctx,
150 const LocationContext *parent,
151 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000152 ID.AddInteger(ck);
153 ID.AddPointer(ctx);
154 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000155 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000156}
157
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000158void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000159 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000160}
161
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000162void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
163 Profile(ID, getAnalysisContext(), getParent(), Enter);
164}
165
166void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000167 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000168}
169
170//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000171// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000172//===----------------------------------------------------------------------===//
173
Ted Kremenek0ee41242009-12-04 01:28:56 +0000174template <typename LOC, typename DATA>
175const LOC*
176LocationContextManager::getLocationContext(AnalysisContext *ctx,
177 const LocationContext *parent,
178 const DATA *d) {
179 llvm::FoldingSetNodeID ID;
180 LOC::Profile(ID, ctx, parent, d);
181 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000182
Ted Kremenek0ee41242009-12-04 01:28:56 +0000183 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000184
Ted Kremenek0ee41242009-12-04 01:28:56 +0000185 if (!L) {
186 L = new LOC(ctx, parent, d);
187 Contexts.InsertNode(L, InsertPos);
188 }
189 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000190}
191
Ted Kremenek0ee41242009-12-04 01:28:56 +0000192const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000193LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000194 const LocationContext *parent,
Ted Kremenek892697d2010-12-16 07:46:53 +0000195 const Stmt *s,
Zhongxing Xud7064342010-11-24 13:08:51 +0000196 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000197 llvm::FoldingSetNodeID ID;
Ted Kremenek892697d2010-12-16 07:46:53 +0000198 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000199 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000200 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000201 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
202 if (!L) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000203 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000204 Contexts.InsertNode(L, InsertPos);
205 }
206 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000207}
208
Ted Kremenek0ee41242009-12-04 01:28:56 +0000209const ScopeContext *
210LocationContextManager::getScope(AnalysisContext *ctx,
211 const LocationContext *parent,
212 const Stmt *s) {
213 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
214}
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000216//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000217// LocationContext methods.
218//===----------------------------------------------------------------------===//
219
220const StackFrameContext *LocationContext::getCurrentStackFrame() const {
221 const LocationContext *LC = this;
222 while (LC) {
223 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
224 return SFC;
225 LC = LC->getParent();
226 }
227 return NULL;
228}
229
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000230const StackFrameContext *
231LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
232 const LocationContext *LC = this;
233 while (LC) {
234 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
235 if (cast<DeclContext>(SFC->getDecl()) == DC)
236 return SFC;
237 }
238 LC = LC->getParent();
239 }
240 return NULL;
241}
242
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000243bool LocationContext::isParentOf(const LocationContext *LC) const {
244 do {
245 const LocationContext *Parent = LC->getParent();
246 if (Parent == this)
247 return true;
248 else
249 LC = Parent;
250 } while (LC);
251
252 return false;
253}
254
Ted Kremenek67d12872009-12-07 22:05:27 +0000255//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000256// Lazily generated map to query the external variables referenced by a Block.
257//===----------------------------------------------------------------------===//
258
259namespace {
260class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
261 BumpVector<const VarDecl*> &BEVals;
262 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000263 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000264 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000265public:
266 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
267 BumpVectorContext &bc)
268 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000269
270 bool IsTrackedDecl(const VarDecl *VD) {
271 const DeclContext *DC = VD->getDeclContext();
272 return IgnoredContexts.count(DC) == 0;
273 }
274
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000275 void VisitStmt(Stmt *S) {
John McCall7502c1d2011-02-13 04:07:26 +0000276 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000277 if (Stmt *child = *I)
278 Visit(child);
279 }
Ted Kremenek85248732010-02-06 00:30:00 +0000280
281 void VisitDeclRefExpr(const DeclRefExpr *DR) {
282 // Non-local variables are also directly modified.
283 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
284 if (!VD->hasLocalStorage()) {
285 unsigned &flag = Visited[VD];
286 if (!flag) {
287 flag = 1;
288 BEVals.push_back(VD, BC);
289 }
290 }
291 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000292
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000293 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000294 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
295 unsigned &flag = Visited[VD];
296 if (!flag) {
297 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000298 if (IsTrackedDecl(VD))
299 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000300 }
301 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000302 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000303
304 void VisitBlockExpr(BlockExpr *BR) {
305 // Blocks containing blocks can transitively capture more variables.
306 IgnoredContexts.insert(BR->getBlockDecl());
307 Visit(BR->getBlockDecl()->getBody());
308 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000309};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000310} // end anonymous namespace
311
312typedef BumpVector<const VarDecl*> DeclVec;
313
314static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
315 void *&Vec,
316 llvm::BumpPtrAllocator &A) {
317 if (Vec)
318 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000319
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000320 BumpVectorContext BC(A);
321 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
322 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000323
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000324 // Find the referenced variables.
325 FindBlockDeclRefExprsVals F(*BV, BC);
326 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000327
328 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000329 return BV;
330}
331
332std::pair<AnalysisContext::referenced_decls_iterator,
333 AnalysisContext::referenced_decls_iterator>
334AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
335 if (!ReferencedBlockVars)
336 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000337
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000338 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
339 return std::make_pair(V->begin(), V->end());
340}
341
342//===----------------------------------------------------------------------===//
343// Cleanup.
344//===----------------------------------------------------------------------===//
345
346AnalysisContext::~AnalysisContext() {
347 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000348 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000349 delete liveness;
Ted Kremenek91c83e72010-08-28 18:59:04 +0000350 delete relaxedLiveness;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000351 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000352 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000353 delete ReferencedBlockVars;
354}
355
356AnalysisContextManager::~AnalysisContextManager() {
357 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
358 delete I->second;
359}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000360
361LocationContext::~LocationContext() {}
362
363LocationContextManager::~LocationContextManager() {
364 clear();
365}
366
367void LocationContextManager::clear() {
368 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000369 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000370 LocationContext *LC = &*I;
371 ++I;
372 delete LC;
373 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000374
Ted Kremenek0ee41242009-12-04 01:28:56 +0000375 Contexts.clear();
376}
377