blob: 2c337f07c3c9c4786318693d545158ff28d9aee4 [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 Kremenekad5a8942010-08-02 23:46:59 +000062 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), true, AddEHEdges);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000063 // Even when the cfg is not successfully built, we don't
64 // want to try building it again.
65 builtCFG = true;
66 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000067 return cfg;
68}
69
Ted Kremenekad5a8942010-08-02 23:46:59 +000070CFG *AnalysisContext::getUnoptimizedCFG() {
71 if (!builtCompleteCFG) {
72 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(),
73 false, AddEHEdges);
74 // Even when the cfg is not successfully built, we don't
75 // want to try building it again.
76 builtCompleteCFG = true;
77 }
78 return completeCFG;
79}
80
Zhongxing Xu97ab3942009-07-30 01:17:21 +000081ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +000082 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +000083 PM = new ParentMap(getBody());
84 return *PM;
85}
86
Tom Caredb34ab72010-08-23 19:51:57 +000087PseudoConstantAnalysis *AnalysisContext::getPseudoConstantAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +000088 if (!PCA)
Tom Caredb34ab72010-08-23 19:51:57 +000089 PCA = new PseudoConstantAnalysis(getBody());
Tom Care245adab2010-08-18 21:17:24 +000090 return PCA;
91}
92
Zhongxing Xu97ab3942009-07-30 01:17:21 +000093LiveVariables *AnalysisContext::getLiveVariables() {
94 if (!liveness) {
95 CFG *c = getCFG();
96 if (!c)
97 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000098
Ted Kremenekb1a7b652009-11-26 02:31:33 +000099 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000100 liveness->runOnCFG(*c);
101 liveness->runOnAllBlocks(*c, 0, true);
102 }
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000104 return liveness;
105}
106
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000107AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000108 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000109 AnalysisContext *&AC = Contexts[D];
110 if (!AC)
Ted Kremenek9b823e82010-08-03 00:09:51 +0000111 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG);
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek23760022009-08-21 23:58:43 +0000113 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000114}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000115
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000116//===----------------------------------------------------------------------===//
117// FoldingSet profiling.
118//===----------------------------------------------------------------------===//
119
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000120void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
121 ContextKind ck,
122 AnalysisContext *ctx,
123 const LocationContext *parent,
124 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000125 ID.AddInteger(ck);
126 ID.AddPointer(ctx);
127 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000128 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000129}
130
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000131void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000132 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000133}
134
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000135void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
136 Profile(ID, getAnalysisContext(), getParent(), Enter);
137}
138
139void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000140 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000141}
142
143//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000144// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000145//===----------------------------------------------------------------------===//
146
Ted Kremenek0ee41242009-12-04 01:28:56 +0000147template <typename LOC, typename DATA>
148const LOC*
149LocationContextManager::getLocationContext(AnalysisContext *ctx,
150 const LocationContext *parent,
151 const DATA *d) {
152 llvm::FoldingSetNodeID ID;
153 LOC::Profile(ID, ctx, parent, d);
154 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000155
Ted Kremenek0ee41242009-12-04 01:28:56 +0000156 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000157
Ted Kremenek0ee41242009-12-04 01:28:56 +0000158 if (!L) {
159 L = new LOC(ctx, parent, d);
160 Contexts.InsertNode(L, InsertPos);
161 }
162 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000163}
164
Ted Kremenek0ee41242009-12-04 01:28:56 +0000165const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000166LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000167 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000168 const Stmt *s, const CFGBlock *blk,
169 unsigned idx) {
170 llvm::FoldingSetNodeID ID;
171 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
172 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000173 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000174 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
175 if (!L) {
176 L = new StackFrameContext(ctx, parent, s, blk, idx);
177 Contexts.InsertNode(L, InsertPos);
178 }
179 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000180}
181
Ted Kremenek0ee41242009-12-04 01:28:56 +0000182const ScopeContext *
183LocationContextManager::getScope(AnalysisContext *ctx,
184 const LocationContext *parent,
185 const Stmt *s) {
186 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
187}
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000189//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000190// LocationContext methods.
191//===----------------------------------------------------------------------===//
192
193const StackFrameContext *LocationContext::getCurrentStackFrame() const {
194 const LocationContext *LC = this;
195 while (LC) {
196 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
197 return SFC;
198 LC = LC->getParent();
199 }
200 return NULL;
201}
202
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000203const StackFrameContext *
204LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
205 const LocationContext *LC = this;
206 while (LC) {
207 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
208 if (cast<DeclContext>(SFC->getDecl()) == DC)
209 return SFC;
210 }
211 LC = LC->getParent();
212 }
213 return NULL;
214}
215
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000216bool LocationContext::isParentOf(const LocationContext *LC) const {
217 do {
218 const LocationContext *Parent = LC->getParent();
219 if (Parent == this)
220 return true;
221 else
222 LC = Parent;
223 } while (LC);
224
225 return false;
226}
227
Ted Kremenek67d12872009-12-07 22:05:27 +0000228//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000229// Lazily generated map to query the external variables referenced by a Block.
230//===----------------------------------------------------------------------===//
231
232namespace {
233class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
234 BumpVector<const VarDecl*> &BEVals;
235 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000236 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000237 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000238public:
239 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
240 BumpVectorContext &bc)
241 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000242
243 bool IsTrackedDecl(const VarDecl *VD) {
244 const DeclContext *DC = VD->getDeclContext();
245 return IgnoredContexts.count(DC) == 0;
246 }
247
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000248 void VisitStmt(Stmt *S) {
249 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
250 if (Stmt *child = *I)
251 Visit(child);
252 }
Ted Kremenek85248732010-02-06 00:30:00 +0000253
254 void VisitDeclRefExpr(const DeclRefExpr *DR) {
255 // Non-local variables are also directly modified.
256 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
257 if (!VD->hasLocalStorage()) {
258 unsigned &flag = Visited[VD];
259 if (!flag) {
260 flag = 1;
261 BEVals.push_back(VD, BC);
262 }
263 }
264 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000265
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000266 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000267 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
268 unsigned &flag = Visited[VD];
269 if (!flag) {
270 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000271 if (IsTrackedDecl(VD))
272 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000273 }
274 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000275 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000276
277 void VisitBlockExpr(BlockExpr *BR) {
278 // Blocks containing blocks can transitively capture more variables.
279 IgnoredContexts.insert(BR->getBlockDecl());
280 Visit(BR->getBlockDecl()->getBody());
281 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000282};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000283} // end anonymous namespace
284
285typedef BumpVector<const VarDecl*> DeclVec;
286
287static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
288 void *&Vec,
289 llvm::BumpPtrAllocator &A) {
290 if (Vec)
291 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000292
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000293 BumpVectorContext BC(A);
294 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
295 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000296
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000297 // Find the referenced variables.
298 FindBlockDeclRefExprsVals F(*BV, BC);
299 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000300
301 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000302 return BV;
303}
304
305std::pair<AnalysisContext::referenced_decls_iterator,
306 AnalysisContext::referenced_decls_iterator>
307AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
308 if (!ReferencedBlockVars)
309 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000310
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000311 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
312 return std::make_pair(V->begin(), V->end());
313}
314
315//===----------------------------------------------------------------------===//
316// Cleanup.
317//===----------------------------------------------------------------------===//
318
319AnalysisContext::~AnalysisContext() {
320 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000321 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000322 delete liveness;
323 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000324 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000325 delete ReferencedBlockVars;
326}
327
328AnalysisContextManager::~AnalysisContextManager() {
329 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
330 delete I->second;
331}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000332
333LocationContext::~LocationContext() {}
334
335LocationContextManager::~LocationContextManager() {
336 clear();
337}
338
339void LocationContextManager::clear() {
340 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000341 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000342 LocationContext *LC = &*I;
343 ++I;
344 delete LC;
345 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000346
Ted Kremenek0ee41242009-12-04 01:28:56 +0000347 Contexts.clear();
348}
349