blob: 884cbc65f4b89664a660a0375b241c491e7edef7 [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
Tom Careec49bf42010-08-27 22:30:10 +0000107LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
108 if (!relaxedLiveness) {
109 CFG *c = getCFG();
110 if (!c)
111 return 0;
112
113 relaxedLiveness = new LiveVariables(*this, false);
114 relaxedLiveness->runOnCFG(*c);
115 relaxedLiveness->runOnAllBlocks(*c, 0, true);
116 }
117
118 return relaxedLiveness;
119}
120
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000121AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000122 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000123 AnalysisContext *&AC = Contexts[D];
124 if (!AC)
Ted Kremenek9b823e82010-08-03 00:09:51 +0000125 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG);
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Ted Kremenek23760022009-08-21 23:58:43 +0000127 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000128}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000129
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000130//===----------------------------------------------------------------------===//
131// FoldingSet profiling.
132//===----------------------------------------------------------------------===//
133
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000134void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
135 ContextKind ck,
136 AnalysisContext *ctx,
137 const LocationContext *parent,
138 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000139 ID.AddInteger(ck);
140 ID.AddPointer(ctx);
141 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000142 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000143}
144
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000145void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000146 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000147}
148
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000149void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
150 Profile(ID, getAnalysisContext(), getParent(), Enter);
151}
152
153void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000154 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000155}
156
157//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000158// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000159//===----------------------------------------------------------------------===//
160
Ted Kremenek0ee41242009-12-04 01:28:56 +0000161template <typename LOC, typename DATA>
162const LOC*
163LocationContextManager::getLocationContext(AnalysisContext *ctx,
164 const LocationContext *parent,
165 const DATA *d) {
166 llvm::FoldingSetNodeID ID;
167 LOC::Profile(ID, ctx, parent, d);
168 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000169
Ted Kremenek0ee41242009-12-04 01:28:56 +0000170 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000171
Ted Kremenek0ee41242009-12-04 01:28:56 +0000172 if (!L) {
173 L = new LOC(ctx, parent, d);
174 Contexts.InsertNode(L, InsertPos);
175 }
176 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000177}
178
Ted Kremenek0ee41242009-12-04 01:28:56 +0000179const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000180LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000181 const LocationContext *parent,
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000182 const Stmt *s, const CFGBlock *blk,
183 unsigned idx) {
184 llvm::FoldingSetNodeID ID;
185 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
186 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000187 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000188 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
189 if (!L) {
190 L = new StackFrameContext(ctx, parent, s, blk, idx);
191 Contexts.InsertNode(L, InsertPos);
192 }
193 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000194}
195
Ted Kremenek0ee41242009-12-04 01:28:56 +0000196const ScopeContext *
197LocationContextManager::getScope(AnalysisContext *ctx,
198 const LocationContext *parent,
199 const Stmt *s) {
200 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
201}
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000203//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000204// LocationContext methods.
205//===----------------------------------------------------------------------===//
206
207const StackFrameContext *LocationContext::getCurrentStackFrame() const {
208 const LocationContext *LC = this;
209 while (LC) {
210 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
211 return SFC;
212 LC = LC->getParent();
213 }
214 return NULL;
215}
216
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000217const StackFrameContext *
218LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
219 const LocationContext *LC = this;
220 while (LC) {
221 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
222 if (cast<DeclContext>(SFC->getDecl()) == DC)
223 return SFC;
224 }
225 LC = LC->getParent();
226 }
227 return NULL;
228}
229
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000230bool LocationContext::isParentOf(const LocationContext *LC) const {
231 do {
232 const LocationContext *Parent = LC->getParent();
233 if (Parent == this)
234 return true;
235 else
236 LC = Parent;
237 } while (LC);
238
239 return false;
240}
241
Ted Kremenek67d12872009-12-07 22:05:27 +0000242//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000243// Lazily generated map to query the external variables referenced by a Block.
244//===----------------------------------------------------------------------===//
245
246namespace {
247class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
248 BumpVector<const VarDecl*> &BEVals;
249 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000250 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000251 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000252public:
253 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
254 BumpVectorContext &bc)
255 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000256
257 bool IsTrackedDecl(const VarDecl *VD) {
258 const DeclContext *DC = VD->getDeclContext();
259 return IgnoredContexts.count(DC) == 0;
260 }
261
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000262 void VisitStmt(Stmt *S) {
263 for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();I!=E;++I)
264 if (Stmt *child = *I)
265 Visit(child);
266 }
Ted Kremenek85248732010-02-06 00:30:00 +0000267
268 void VisitDeclRefExpr(const DeclRefExpr *DR) {
269 // Non-local variables are also directly modified.
270 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
271 if (!VD->hasLocalStorage()) {
272 unsigned &flag = Visited[VD];
273 if (!flag) {
274 flag = 1;
275 BEVals.push_back(VD, BC);
276 }
277 }
278 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000279
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000280 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000281 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
282 unsigned &flag = Visited[VD];
283 if (!flag) {
284 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000285 if (IsTrackedDecl(VD))
286 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000287 }
288 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000289 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000290
291 void VisitBlockExpr(BlockExpr *BR) {
292 // Blocks containing blocks can transitively capture more variables.
293 IgnoredContexts.insert(BR->getBlockDecl());
294 Visit(BR->getBlockDecl()->getBody());
295 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000296};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000297} // end anonymous namespace
298
299typedef BumpVector<const VarDecl*> DeclVec;
300
301static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
302 void *&Vec,
303 llvm::BumpPtrAllocator &A) {
304 if (Vec)
305 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000306
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000307 BumpVectorContext BC(A);
308 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
309 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000310
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000311 // Find the referenced variables.
312 FindBlockDeclRefExprsVals F(*BV, BC);
313 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000314
315 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000316 return BV;
317}
318
319std::pair<AnalysisContext::referenced_decls_iterator,
320 AnalysisContext::referenced_decls_iterator>
321AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
322 if (!ReferencedBlockVars)
323 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000324
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000325 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
326 return std::make_pair(V->begin(), V->end());
327}
328
329//===----------------------------------------------------------------------===//
330// Cleanup.
331//===----------------------------------------------------------------------===//
332
333AnalysisContext::~AnalysisContext() {
334 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000335 delete completeCFG;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000336 delete liveness;
337 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000338 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000339 delete ReferencedBlockVars;
340}
341
342AnalysisContextManager::~AnalysisContextManager() {
343 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
344 delete I->second;
345}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000346
347LocationContext::~LocationContext() {}
348
349LocationContextManager::~LocationContextManager() {
350 clear();
351}
352
353void LocationContextManager::clear() {
354 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000355 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000356 LocationContext *LC = &*I;
357 ++I;
358 delete LC;
359 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000360
Ted Kremenek0ee41242009-12-04 01:28:56 +0000361 Contexts.clear();
362}
363