blob: 4305507c9cbce9d1d67ea0478dd56a2ef617aa4d [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) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000155 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000156}
157
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000158void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
159 Profile(ID, getAnalysisContext(), getParent(), Enter);
160}
161
162void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000163 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000164}
165
166//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000167// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000168//===----------------------------------------------------------------------===//
169
Ted Kremenek0ee41242009-12-04 01:28:56 +0000170template <typename LOC, typename DATA>
171const LOC*
172LocationContextManager::getLocationContext(AnalysisContext *ctx,
173 const LocationContext *parent,
174 const DATA *d) {
175 llvm::FoldingSetNodeID ID;
176 LOC::Profile(ID, ctx, parent, d);
177 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000178
Ted Kremenek0ee41242009-12-04 01:28:56 +0000179 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000180
Ted Kremenek0ee41242009-12-04 01:28:56 +0000181 if (!L) {
182 L = new LOC(ctx, parent, d);
183 Contexts.InsertNode(L, InsertPos);
184 }
185 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000186}
187
Ted Kremenek0ee41242009-12-04 01:28:56 +0000188const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000189LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000190 const LocationContext *parent,
Ted Kremenek892697d2010-12-16 07:46:53 +0000191 const Stmt *s,
Zhongxing Xud7064342010-11-24 13:08:51 +0000192 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000193 llvm::FoldingSetNodeID ID;
Ted Kremenek892697d2010-12-16 07:46:53 +0000194 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000195 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000196 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000197 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
198 if (!L) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000199 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000200 Contexts.InsertNode(L, InsertPos);
201 }
202 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000203}
204
Ted Kremenek0ee41242009-12-04 01:28:56 +0000205const ScopeContext *
206LocationContextManager::getScope(AnalysisContext *ctx,
207 const LocationContext *parent,
208 const Stmt *s) {
209 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
210}
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000212//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000213// LocationContext methods.
214//===----------------------------------------------------------------------===//
215
216const StackFrameContext *LocationContext::getCurrentStackFrame() const {
217 const LocationContext *LC = this;
218 while (LC) {
219 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
220 return SFC;
221 LC = LC->getParent();
222 }
223 return NULL;
224}
225
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000226const StackFrameContext *
227LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
228 const LocationContext *LC = this;
229 while (LC) {
230 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
231 if (cast<DeclContext>(SFC->getDecl()) == DC)
232 return SFC;
233 }
234 LC = LC->getParent();
235 }
236 return NULL;
237}
238
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000239bool LocationContext::isParentOf(const LocationContext *LC) const {
240 do {
241 const LocationContext *Parent = LC->getParent();
242 if (Parent == this)
243 return true;
244 else
245 LC = Parent;
246 } while (LC);
247
248 return false;
249}
250
Ted Kremenek67d12872009-12-07 22:05:27 +0000251//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000252// Lazily generated map to query the external variables referenced by a Block.
253//===----------------------------------------------------------------------===//
254
255namespace {
256class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
257 BumpVector<const VarDecl*> &BEVals;
258 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000259 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000260 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000261public:
262 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
263 BumpVectorContext &bc)
264 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000265
266 bool IsTrackedDecl(const VarDecl *VD) {
267 const DeclContext *DC = VD->getDeclContext();
268 return IgnoredContexts.count(DC) == 0;
269 }
270
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000271 void VisitStmt(Stmt *S) {
272 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
273 if (Stmt *child = *I)
274 Visit(child);
275 }
Ted Kremenek85248732010-02-06 00:30:00 +0000276
277 void VisitDeclRefExpr(const DeclRefExpr *DR) {
278 // Non-local variables are also directly modified.
279 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
280 if (!VD->hasLocalStorage()) {
281 unsigned &flag = Visited[VD];
282 if (!flag) {
283 flag = 1;
284 BEVals.push_back(VD, BC);
285 }
286 }
287 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000288
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000289 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000290 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
291 unsigned &flag = Visited[VD];
292 if (!flag) {
293 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000294 if (IsTrackedDecl(VD))
295 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000296 }
297 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000298 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000299
300 void VisitBlockExpr(BlockExpr *BR) {
301 // Blocks containing blocks can transitively capture more variables.
302 IgnoredContexts.insert(BR->getBlockDecl());
303 Visit(BR->getBlockDecl()->getBody());
304 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000305};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000306} // end anonymous namespace
307
308typedef BumpVector<const VarDecl*> DeclVec;
309
310static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
311 void *&Vec,
312 llvm::BumpPtrAllocator &A) {
313 if (Vec)
314 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000315
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000316 BumpVectorContext BC(A);
317 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
318 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000319
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000320 // Find the referenced variables.
321 FindBlockDeclRefExprsVals F(*BV, BC);
322 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000323
324 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000325 return BV;
326}
327
328std::pair<AnalysisContext::referenced_decls_iterator,
329 AnalysisContext::referenced_decls_iterator>
330AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
331 if (!ReferencedBlockVars)
332 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000333
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000334 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
335 return std::make_pair(V->begin(), V->end());
336}
337
338//===----------------------------------------------------------------------===//
339// Cleanup.
340//===----------------------------------------------------------------------===//
341
342AnalysisContext::~AnalysisContext() {
343 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000344 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000345 delete liveness;
Ted Kremenek91c83e72010-08-28 18:59:04 +0000346 delete relaxedLiveness;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000347 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000348 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000349 delete ReferencedBlockVars;
350}
351
352AnalysisContextManager::~AnalysisContextManager() {
353 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
354 delete I->second;
355}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000356
357LocationContext::~LocationContext() {}
358
359LocationContextManager::~LocationContextManager() {
360 clear();
361}
362
363void LocationContextManager::clear() {
364 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000365 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000366 LocationContext *LC = &*I;
367 ++I;
368 delete LC;
369 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000370
Ted Kremenek0ee41242009-12-04 01:28:56 +0000371 Contexts.clear();
372}
373