blob: a982a5c5be93e0b1bea4c0f807be4f739046050f [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 Kremenek283a3582011-02-23 01:51:53 +000024#include "clang/Analysis/CFGStmtMap.h"
Ted Kremenekb1a7b652009-11-26 02:31:33 +000025#include "clang/Analysis/Support/BumpVector.h"
Ted Kremenek2cfe28b2010-03-10 00:18:11 +000026#include "llvm/ADT/SmallSet.h"
Mike Stump87a05f12009-07-31 01:10:29 +000027#include "llvm/Support/ErrorHandling.h"
Zhongxing Xu97ab3942009-07-30 01:17:21 +000028
29using namespace clang;
30
Ted Kremenek58f5ec72009-10-20 21:39:41 +000031void AnalysisContextManager::clear() {
32 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
33 delete I->second;
34 Contexts.clear();
35}
36
Zhongxing Xu97ab3942009-07-30 01:17:21 +000037Stmt *AnalysisContext::getBody() {
Ted Kremenek23760022009-08-21 23:58:43 +000038 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000039 return FD->getBody();
Ted Kremenek23760022009-08-21 23:58:43 +000040 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu97ab3942009-07-30 01:17:21 +000041 return MD->getBody();
Ted Kremenek30a45342009-12-04 20:34:55 +000042 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
43 return BD->getBody();
Mike Stumpfa6ef182010-01-13 02:59:54 +000044 else if (const FunctionTemplateDecl *FunTmpl
45 = dyn_cast_or_null<FunctionTemplateDecl>(D))
46 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu97ab3942009-07-30 01:17:21 +000047
Jeffrey Yasskin9f61aa92009-12-12 05:05:38 +000048 llvm_unreachable("unknown code decl");
Zhongxing Xu97ab3942009-07-30 01:17:21 +000049}
50
Ted Kremenek82cd37c2009-08-21 23:25:54 +000051const ImplicitParamDecl *AnalysisContext::getSelfDecl() const {
52 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
53 return MD->getSelfDecl();
Mike Stump1eb44332009-09-09 15:08:12 +000054
Ted Kremenek82cd37c2009-08-21 23:25:54 +000055 return NULL;
56}
57
Zhongxing Xu97ab3942009-07-30 01:17:21 +000058CFG *AnalysisContext::getCFG() {
Ted Kremenek9b823e82010-08-03 00:09:51 +000059 if (UseUnoptimizedCFG)
60 return getUnoptimizedCFG();
61
Ted Kremenekd064fdc2010-03-23 00:13:23 +000062 if (!builtCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000063 CFG::BuildOptions B;
64 B.AddEHEdges = AddEHEdges;
Marcin Swiderski9121ba22010-09-30 07:41:24 +000065 B.AddImplicitDtors = AddImplicitDtors;
66 B.AddInitializers = AddInitializers;
Ted Kremenek6c52c782010-09-14 23:41:16 +000067 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekd064fdc2010-03-23 00:13:23 +000068 // Even when the cfg is not successfully built, we don't
69 // want to try building it again.
70 builtCFG = true;
71 }
Zhongxing Xu97ab3942009-07-30 01:17:21 +000072 return cfg;
73}
74
Ted Kremenekad5a8942010-08-02 23:46:59 +000075CFG *AnalysisContext::getUnoptimizedCFG() {
76 if (!builtCompleteCFG) {
Ted Kremenek6c52c782010-09-14 23:41:16 +000077 CFG::BuildOptions B;
78 B.PruneTriviallyFalseEdges = false;
79 B.AddEHEdges = AddEHEdges;
Marcin Swiderski9121ba22010-09-30 07:41:24 +000080 B.AddImplicitDtors = AddImplicitDtors;
81 B.AddInitializers = AddInitializers;
Ted Kremenek6c52c782010-09-14 23:41:16 +000082 completeCFG = CFG::buildCFG(D, getBody(), &D->getASTContext(), B);
Ted Kremenekad5a8942010-08-02 23:46:59 +000083 // Even when the cfg is not successfully built, we don't
84 // want to try building it again.
85 builtCompleteCFG = true;
86 }
87 return completeCFG;
88}
89
Ted Kremenek283a3582011-02-23 01:51:53 +000090CFGStmtMap *AnalysisContext::getCFGStmtMap() {
91 if (cfgStmtMap)
92 return cfgStmtMap;
93
94 if (CFG *c = getCFG()) {
95 cfgStmtMap = CFGStmtMap::Build(c, &getParentMap());
96 return cfgStmtMap;
97 }
98
99 return 0;
100}
101
102
Anders Carlsson04eeba42011-01-16 22:05:23 +0000103void AnalysisContext::dumpCFG() {
104 getCFG()->dump(getASTContext().getLangOptions());
105}
106
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000107ParentMap &AnalysisContext::getParentMap() {
Mike Stump1eb44332009-09-09 15:08:12 +0000108 if (!PM)
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000109 PM = new ParentMap(getBody());
110 return *PM;
111}
112
Tom Caredb34ab72010-08-23 19:51:57 +0000113PseudoConstantAnalysis *AnalysisContext::getPseudoConstantAnalysis() {
Tom Care245adab2010-08-18 21:17:24 +0000114 if (!PCA)
Tom Caredb34ab72010-08-23 19:51:57 +0000115 PCA = new PseudoConstantAnalysis(getBody());
Tom Care245adab2010-08-18 21:17:24 +0000116 return PCA;
117}
118
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000119LiveVariables *AnalysisContext::getLiveVariables() {
120 if (!liveness) {
121 CFG *c = getCFG();
122 if (!c)
123 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000125 liveness = new LiveVariables(*this);
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000126 liveness->runOnCFG(*c);
127 liveness->runOnAllBlocks(*c, 0, true);
128 }
Mike Stump1eb44332009-09-09 15:08:12 +0000129
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000130 return liveness;
131}
132
Tom Careec49bf42010-08-27 22:30:10 +0000133LiveVariables *AnalysisContext::getRelaxedLiveVariables() {
134 if (!relaxedLiveness) {
135 CFG *c = getCFG();
136 if (!c)
137 return 0;
138
139 relaxedLiveness = new LiveVariables(*this, false);
140 relaxedLiveness->runOnCFG(*c);
141 relaxedLiveness->runOnAllBlocks(*c, 0, true);
142 }
143
144 return relaxedLiveness;
145}
146
Zhongxing Xuc6238d22010-07-19 01:31:21 +0000147AnalysisContext *AnalysisContextManager::getContext(const Decl *D,
Zhongxing Xu2ce43c82010-07-22 13:52:13 +0000148 idx::TranslationUnit *TU) {
Ted Kremenek23760022009-08-21 23:58:43 +0000149 AnalysisContext *&AC = Contexts[D];
150 if (!AC)
Marcin Swiderski9121ba22010-09-30 07:41:24 +0000151 AC = new AnalysisContext(D, TU, UseUnoptimizedCFG, false,
152 AddImplicitDtors, AddInitializers);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Ted Kremenek23760022009-08-21 23:58:43 +0000154 return AC;
Zhongxing Xu97ab3942009-07-30 01:17:21 +0000155}
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000156
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000157//===----------------------------------------------------------------------===//
158// FoldingSet profiling.
159//===----------------------------------------------------------------------===//
160
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000161void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
162 ContextKind ck,
163 AnalysisContext *ctx,
164 const LocationContext *parent,
165 const void* data) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000166 ID.AddInteger(ck);
167 ID.AddPointer(ctx);
168 ID.AddPointer(parent);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000169 ID.AddPointer(data);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000170}
171
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000172void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000173 Profile(ID, getAnalysisContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000174}
175
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000176void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
177 Profile(ID, getAnalysisContext(), getParent(), Enter);
178}
179
180void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek1309f9a2010-01-25 04:41:41 +0000181 Profile(ID, getAnalysisContext(), getParent(), BD);
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000182}
183
184//===----------------------------------------------------------------------===//
Ted Kremenek0ee41242009-12-04 01:28:56 +0000185// LocationContext creation.
Ted Kremenekdc0d9092009-12-04 00:50:10 +0000186//===----------------------------------------------------------------------===//
187
Ted Kremenek0ee41242009-12-04 01:28:56 +0000188template <typename LOC, typename DATA>
189const LOC*
190LocationContextManager::getLocationContext(AnalysisContext *ctx,
191 const LocationContext *parent,
192 const DATA *d) {
193 llvm::FoldingSetNodeID ID;
194 LOC::Profile(ID, ctx, parent, d);
195 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000196
Ted Kremenek0ee41242009-12-04 01:28:56 +0000197 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000198
Ted Kremenek0ee41242009-12-04 01:28:56 +0000199 if (!L) {
200 L = new LOC(ctx, parent, d);
201 Contexts.InsertNode(L, InsertPos);
202 }
203 return L;
Ted Kremenek58f5ec72009-10-20 21:39:41 +0000204}
205
Ted Kremenek0ee41242009-12-04 01:28:56 +0000206const StackFrameContext*
Mike Stump1eb44332009-09-09 15:08:12 +0000207LocationContextManager::getStackFrame(AnalysisContext *ctx,
Ted Kremenek54c809b2009-08-21 23:39:58 +0000208 const LocationContext *parent,
Ted Kremenek892697d2010-12-16 07:46:53 +0000209 const Stmt *s,
Zhongxing Xud7064342010-11-24 13:08:51 +0000210 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000211 llvm::FoldingSetNodeID ID;
Ted Kremenek892697d2010-12-16 07:46:53 +0000212 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000213 void *InsertPos;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000214 StackFrameContext *L =
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000215 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
216 if (!L) {
Ted Kremenek892697d2010-12-16 07:46:53 +0000217 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu62d399e2009-12-24 03:34:38 +0000218 Contexts.InsertNode(L, InsertPos);
219 }
220 return L;
Zhongxing Xu18c7c062009-08-03 07:23:22 +0000221}
222
Ted Kremenek0ee41242009-12-04 01:28:56 +0000223const ScopeContext *
224LocationContextManager::getScope(AnalysisContext *ctx,
225 const LocationContext *parent,
226 const Stmt *s) {
227 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
228}
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000230//===----------------------------------------------------------------------===//
Ted Kremenek67d12872009-12-07 22:05:27 +0000231// LocationContext methods.
232//===----------------------------------------------------------------------===//
233
234const StackFrameContext *LocationContext::getCurrentStackFrame() const {
235 const LocationContext *LC = this;
236 while (LC) {
237 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
238 return SFC;
239 LC = LC->getParent();
240 }
241 return NULL;
242}
243
Ted Kremenek2b87ae42009-12-11 06:43:27 +0000244const StackFrameContext *
245LocationContext::getStackFrameForDeclContext(const DeclContext *DC) const {
246 const LocationContext *LC = this;
247 while (LC) {
248 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) {
249 if (cast<DeclContext>(SFC->getDecl()) == DC)
250 return SFC;
251 }
252 LC = LC->getParent();
253 }
254 return NULL;
255}
256
Zhongxing Xu8ddf7ce2010-02-17 08:45:06 +0000257bool LocationContext::isParentOf(const LocationContext *LC) const {
258 do {
259 const LocationContext *Parent = LC->getParent();
260 if (Parent == this)
261 return true;
262 else
263 LC = Parent;
264 } while (LC);
265
266 return false;
267}
268
Ted Kremenek67d12872009-12-07 22:05:27 +0000269//===----------------------------------------------------------------------===//
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000270// Lazily generated map to query the external variables referenced by a Block.
271//===----------------------------------------------------------------------===//
272
273namespace {
274class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
275 BumpVector<const VarDecl*> &BEVals;
276 BumpVectorContext &BC;
Ted Kremenek85248732010-02-06 00:30:00 +0000277 llvm::DenseMap<const VarDecl*, unsigned> Visited;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000278 llvm::SmallSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000279public:
280 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
281 BumpVectorContext &bc)
282 : BEVals(bevals), BC(bc) {}
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000283
284 bool IsTrackedDecl(const VarDecl *VD) {
285 const DeclContext *DC = VD->getDeclContext();
286 return IgnoredContexts.count(DC) == 0;
287 }
288
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000289 void VisitStmt(Stmt *S) {
John McCall7502c1d2011-02-13 04:07:26 +0000290 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000291 if (Stmt *child = *I)
292 Visit(child);
293 }
Ted Kremenek85248732010-02-06 00:30:00 +0000294
295 void VisitDeclRefExpr(const DeclRefExpr *DR) {
296 // Non-local variables are also directly modified.
297 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl()))
298 if (!VD->hasLocalStorage()) {
299 unsigned &flag = Visited[VD];
300 if (!flag) {
301 flag = 1;
302 BEVals.push_back(VD, BC);
303 }
304 }
305 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000306
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000307 void VisitBlockDeclRefExpr(BlockDeclRefExpr *DR) {
Ted Kremenek85248732010-02-06 00:30:00 +0000308 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
309 unsigned &flag = Visited[VD];
310 if (!flag) {
311 flag = 1;
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000312 if (IsTrackedDecl(VD))
313 BEVals.push_back(VD, BC);
Ted Kremenek85248732010-02-06 00:30:00 +0000314 }
315 }
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000316 }
Ted Kremenek2cfe28b2010-03-10 00:18:11 +0000317
318 void VisitBlockExpr(BlockExpr *BR) {
319 // Blocks containing blocks can transitively capture more variables.
320 IgnoredContexts.insert(BR->getBlockDecl());
321 Visit(BR->getBlockDecl()->getBody());
322 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000323};
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000324} // end anonymous namespace
325
326typedef BumpVector<const VarDecl*> DeclVec;
327
328static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
329 void *&Vec,
330 llvm::BumpPtrAllocator &A) {
331 if (Vec)
332 return (DeclVec*) Vec;
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000333
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000334 BumpVectorContext BC(A);
335 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
336 new (BV) DeclVec(BC, 10);
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000337
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000338 // Find the referenced variables.
339 FindBlockDeclRefExprsVals F(*BV, BC);
340 F.Visit(BD->getBody());
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000341
342 Vec = BV;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000343 return BV;
344}
345
346std::pair<AnalysisContext::referenced_decls_iterator,
347 AnalysisContext::referenced_decls_iterator>
348AnalysisContext::getReferencedBlockVars(const BlockDecl *BD) {
349 if (!ReferencedBlockVars)
350 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000351
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000352 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
353 return std::make_pair(V->begin(), V->end());
354}
355
356//===----------------------------------------------------------------------===//
357// Cleanup.
358//===----------------------------------------------------------------------===//
359
360AnalysisContext::~AnalysisContext() {
361 delete cfg;
Ted Kremenekad5a8942010-08-02 23:46:59 +0000362 delete completeCFG;
Ted Kremenek283a3582011-02-23 01:51:53 +0000363 delete cfgStmtMap;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000364 delete liveness;
Ted Kremenek91c83e72010-08-28 18:59:04 +0000365 delete relaxedLiveness;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000366 delete PM;
Tom Care245adab2010-08-18 21:17:24 +0000367 delete PCA;
Ted Kremenekb1a7b652009-11-26 02:31:33 +0000368 delete ReferencedBlockVars;
369}
370
371AnalysisContextManager::~AnalysisContextManager() {
372 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
373 delete I->second;
374}
Ted Kremenek0ee41242009-12-04 01:28:56 +0000375
376LocationContext::~LocationContext() {}
377
378LocationContextManager::~LocationContextManager() {
379 clear();
380}
381
382void LocationContextManager::clear() {
383 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000384 E = Contexts.end(); I != E; ) {
Ted Kremenek0ee41242009-12-04 01:28:56 +0000385 LocationContext *LC = &*I;
386 ++I;
387 delete LC;
388 }
Ted Kremenekd064fdc2010-03-23 00:13:23 +0000389
Ted Kremenek0ee41242009-12-04 01:28:56 +0000390 Contexts.clear();
391}
392