blob: 83d441e812bbdf5ce57e7c339426a4bce6dc0ac9 [file] [log] [blame]
Ted Kremenek81ce1c82011-10-24 01:32:45 +00001//== AnalysisDeclContext.cpp - Analysis context for Path Sens analysis -*- C++ -*-//
Zhongxing Xu14407bf2009-07-30 01:17:21 +00002//
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//
Ted Kremenek81ce1c82011-10-24 01:32:45 +000010// This file defines AnalysisDeclContext, a class that manages the analysis context
Zhongxing Xu14407bf2009-07-30 01:17:21 +000011// data for path sensitive analysis.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "clang/Analysis/AnalysisContext.h"
16#include "BodyFarm.h"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000017#include "clang/AST/ASTContext.h"
Zhongxing Xu14407bf2009-07-30 01:17:21 +000018#include "clang/AST/Decl.h"
19#include "clang/AST/DeclObjC.h"
Mike Stump1bacb812010-01-13 02:59:54 +000020#include "clang/AST/DeclTemplate.h"
Zhongxing Xu14407bf2009-07-30 01:17:21 +000021#include "clang/AST/ParentMap.h"
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000022#include "clang/AST/StmtVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
Ted Kremenek575398e2010-03-10 00:18:11 +000024#include "clang/Analysis/Analyses/LiveVariables.h"
Tom Caree332c3b2010-08-23 19:51:57 +000025#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
Ted Kremenek575398e2010-03-10 00:18:11 +000026#include "clang/Analysis/CFG.h"
Ted Kremenekcc7f1f82011-02-23 01:51:53 +000027#include "clang/Analysis/CFGStmtMap.h"
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000028#include "clang/Analysis/Support/BumpVector.h"
Benjamin Kramer616f8022012-03-10 15:08:09 +000029#include "llvm/ADT/SmallPtrSet.h"
Mike Stump5b78af92009-07-31 01:10:29 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000031#include "llvm/Support/SaveAndRestore.h"
Ted Kremenek14f779c2012-09-21 00:09:11 +000032
Zhongxing Xu14407bf2009-07-30 01:17:21 +000033using namespace clang;
34
Ted Kremenekdccc2b22011-10-07 22:21:02 +000035typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap;
36
Ted Kremenek81ce1c82011-10-24 01:32:45 +000037AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
Ted Kremenek14f779c2012-09-21 00:09:11 +000038 const Decl *d,
39 const CFG::BuildOptions &buildOptions)
Ted Kremenek142adc42011-10-23 02:31:52 +000040 : Manager(Mgr),
41 D(d),
Ted Kremenek189ecec2011-07-21 05:22:47 +000042 cfgBuildOptions(buildOptions),
Ted Kremenekf9d82902011-03-10 01:14:05 +000043 forcedBlkExprs(0),
Ted Kremenek189ecec2011-07-21 05:22:47 +000044 builtCFG(false),
45 builtCompleteCFG(false),
Ted Kremenekdccc2b22011-10-07 22:21:02 +000046 ReferencedBlockVars(0),
47 ManagedAnalyses(0)
Ted Kremenek189ecec2011-07-21 05:22:47 +000048{
Ted Kremenekf9d82902011-03-10 01:14:05 +000049 cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
Ted Kremenek189ecec2011-07-21 05:22:47 +000050}
51
Ted Kremenek81ce1c82011-10-24 01:32:45 +000052AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
Ted Kremenek14f779c2012-09-21 00:09:11 +000053 const Decl *d)
Ted Kremenek142adc42011-10-23 02:31:52 +000054: Manager(Mgr),
55 D(d),
Ted Kremenek189ecec2011-07-21 05:22:47 +000056 forcedBlkExprs(0),
57 builtCFG(false),
58 builtCompleteCFG(false),
Ted Kremenekdccc2b22011-10-07 22:21:02 +000059 ReferencedBlockVars(0),
60 ManagedAnalyses(0)
Ted Kremenek189ecec2011-07-21 05:22:47 +000061{
62 cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
63}
64
Ted Kremenek81ce1c82011-10-24 01:32:45 +000065AnalysisDeclContextManager::AnalysisDeclContextManager(bool useUnoptimizedCFG,
Jordan Rose6d671cc2012-09-05 22:55:23 +000066 bool addImplicitDtors,
67 bool addInitializers,
Ted Kremenek14f779c2012-09-21 00:09:11 +000068 bool addTemporaryDtors,
69 bool synthesizeBodies)
70 : SynthesizeBodies(synthesizeBodies)
71{
Ted Kremenek189ecec2011-07-21 05:22:47 +000072 cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +000073 cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
74 cfgBuildOptions.AddInitializers = addInitializers;
Jordan Rose6d671cc2012-09-05 22:55:23 +000075 cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
Ted Kremenekf9d82902011-03-10 01:14:05 +000076}
77
Ted Kremenek81ce1c82011-10-24 01:32:45 +000078void AnalysisDeclContextManager::clear() {
Ted Kremenekd45ff6c2009-10-20 21:39:41 +000079 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
80 delete I->second;
81 Contexts.clear();
82}
83
Ted Kremenek14f779c2012-09-21 00:09:11 +000084static BodyFarm &getBodyFarm(ASTContext &C) {
85 static BodyFarm *BF = new BodyFarm(C);
86 return *BF;
87}
88
Ted Kremenek81ce1c82011-10-24 01:32:45 +000089Stmt *AnalysisDeclContext::getBody() const {
Ted Kremenek14f779c2012-09-21 00:09:11 +000090 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
91 Stmt *Body = FD->getBody();
92 if (!Body && Manager && Manager->synthesizeBodies())
93 return getBodyFarm(getASTContext()).getBody(FD);
94 return Body;
95 }
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +000096 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu14407bf2009-07-30 01:17:21 +000097 return MD->getBody();
Ted Kremenek45805b92009-12-04 20:34:55 +000098 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
99 return BD->getBody();
Mike Stump1bacb812010-01-13 02:59:54 +0000100 else if (const FunctionTemplateDecl *FunTmpl
101 = dyn_cast_or_null<FunctionTemplateDecl>(D))
102 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000103
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000104 llvm_unreachable("unknown code decl");
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000105}
106
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000107const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
Ted Kremenek608677a2009-08-21 23:25:54 +0000108 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
109 return MD->getSelfDecl();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000110 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
111 // See if 'self' was captured by the block.
112 for (BlockDecl::capture_const_iterator it = BD->capture_begin(),
113 et = BD->capture_end(); it != et; ++it) {
114 const VarDecl *VD = it->getVariable();
115 if (VD->getName() == "self")
116 return dyn_cast<ImplicitParamDecl>(VD);
117 }
118 }
Mike Stump11289f42009-09-09 15:08:12 +0000119
Ted Kremenek608677a2009-08-21 23:25:54 +0000120 return NULL;
121}
122
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000123void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000124 if (!forcedBlkExprs)
125 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
126 // Default construct an entry for 'stmt'.
Jordy Rose17347372011-06-10 08:49:37 +0000127 if (const Expr *e = dyn_cast<Expr>(stmt))
128 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000129 (void) (*forcedBlkExprs)[stmt];
130}
131
132const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000133AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000134 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000135 if (const Expr *e = dyn_cast<Expr>(stmt))
136 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000137 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
138 forcedBlkExprs->find(stmt);
139 assert(itr != forcedBlkExprs->end());
140 return itr->second;
141}
142
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000143CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000144 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000145 return getUnoptimizedCFG();
146
Ted Kremenek0b405322010-03-23 00:13:23 +0000147 if (!builtCFG) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000148 cfg.reset(CFG::buildCFG(D, getBody(),
149 &D->getASTContext(), cfgBuildOptions));
Ted Kremenek0b405322010-03-23 00:13:23 +0000150 // Even when the cfg is not successfully built, we don't
151 // want to try building it again.
152 builtCFG = true;
153 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000154 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000155}
156
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000157CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000158 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000159 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
160 false);
161 completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
162 cfgBuildOptions));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000163 // Even when the cfg is not successfully built, we don't
164 // want to try building it again.
165 builtCompleteCFG = true;
166 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000167 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000168}
169
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000170CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000171 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000172 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000173
174 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000175 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
176 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000177 }
178
179 return 0;
180}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000181
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000182CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000183 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000184 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000185
Ted Kremenek80861ca2011-02-23 01:51:59 +0000186 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000187 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000188 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000189 }
190
191 return 0;
192}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000193
Ted Kremenek72be32a2011-12-22 23:33:52 +0000194void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000195 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000196}
197
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000198ParentMap &AnalysisDeclContext::getParentMap() {
Jordan Rose25bc20f2012-07-26 20:04:30 +0000199 if (!PM) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000200 PM.reset(new ParentMap(getBody()));
Jordan Rose25bc20f2012-07-26 20:04:30 +0000201 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
202 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
203 E = C->init_end();
204 I != E; ++I) {
205 PM->addStmt((*I)->getInit());
206 }
207 }
208 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000209 return *PM;
210}
211
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000212PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000213 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000214 PCA.reset(new PseudoConstantAnalysis(getBody()));
215 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000216}
217
Jordy Rose4f8198e2012-04-28 01:58:08 +0000218AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000219 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000220 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
221 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000222 FD->hasBody(FD);
223 D = FD;
224 }
225
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000226 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000227 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000228 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000229 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000230}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000231
Ted Kremenek142adc42011-10-23 02:31:52 +0000232const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000233AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000234 const CFGBlock *Blk, unsigned Idx) {
235 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
236}
237
Ted Kremenekc3da3762012-06-01 20:04:04 +0000238const BlockInvocationContext *
239AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
240 const clang::BlockDecl *BD,
241 const void *ContextData) {
242 return getLocationContextManager().getBlockInvocationContext(this, parent,
243 BD, ContextData);
244}
245
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000246LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000247 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000248 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000249 return Manager->getLocationContextManager();
250}
251
Ted Kremenek25388242009-12-04 00:50:10 +0000252//===----------------------------------------------------------------------===//
253// FoldingSet profiling.
254//===----------------------------------------------------------------------===//
255
Ted Kremenek25388242009-12-04 00:50:10 +0000256void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
257 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000258 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000259 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000260 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000261 ID.AddInteger(ck);
262 ID.AddPointer(ctx);
263 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000264 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000265}
266
Ted Kremenek25388242009-12-04 00:50:10 +0000267void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000268 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000269}
270
Ted Kremenek25388242009-12-04 00:50:10 +0000271void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000272 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000273}
274
275void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000276 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000277}
278
279//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000280// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000281//===----------------------------------------------------------------------===//
282
Ted Kremenek43d4a892009-12-04 01:28:56 +0000283template <typename LOC, typename DATA>
284const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000285LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000286 const LocationContext *parent,
287 const DATA *d) {
288 llvm::FoldingSetNodeID ID;
289 LOC::Profile(ID, ctx, parent, d);
290 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000291
Ted Kremenek43d4a892009-12-04 01:28:56 +0000292 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000293
Ted Kremenek43d4a892009-12-04 01:28:56 +0000294 if (!L) {
295 L = new LOC(ctx, parent, d);
296 Contexts.InsertNode(L, InsertPos);
297 }
298 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000299}
300
Ted Kremenek43d4a892009-12-04 01:28:56 +0000301const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000302LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000303 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000304 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000305 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000306 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000307 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000308 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000309 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000310 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
311 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000312 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000313 Contexts.InsertNode(L, InsertPos);
314 }
315 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000316}
317
Ted Kremenek43d4a892009-12-04 01:28:56 +0000318const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000319LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000320 const LocationContext *parent,
321 const Stmt *s) {
322 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
323}
Mike Stump11289f42009-09-09 15:08:12 +0000324
Ted Kremenekc3da3762012-06-01 20:04:04 +0000325const BlockInvocationContext *
326LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
327 const LocationContext *parent,
328 const BlockDecl *BD,
329 const void *ContextData) {
330 llvm::FoldingSetNodeID ID;
331 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
332 void *InsertPos;
333 BlockInvocationContext *L =
334 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
335 InsertPos));
336 if (!L) {
337 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
338 Contexts.InsertNode(L, InsertPos);
339 }
340 return L;
341}
342
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000343//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000344// LocationContext methods.
345//===----------------------------------------------------------------------===//
346
347const StackFrameContext *LocationContext::getCurrentStackFrame() const {
348 const LocationContext *LC = this;
349 while (LC) {
350 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
351 return SFC;
352 LC = LC->getParent();
353 }
354 return NULL;
355}
356
Anna Zaks44dc91b2012-11-03 02:54:16 +0000357bool LocationContext::inTopFrame() const {
358 return getCurrentStackFrame()->inTopFrame();
359}
360
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000361bool LocationContext::isParentOf(const LocationContext *LC) const {
362 do {
363 const LocationContext *Parent = LC->getParent();
364 if (Parent == this)
365 return true;
366 else
367 LC = Parent;
368 } while (LC);
369
370 return false;
371}
372
Ted Kremenek04af9f22009-12-07 22:05:27 +0000373//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000374// Lazily generated map to query the external variables referenced by a Block.
375//===----------------------------------------------------------------------===//
376
377namespace {
378class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
379 BumpVector<const VarDecl*> &BEVals;
380 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000381 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
382 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000383public:
384 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
385 BumpVectorContext &bc)
386 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000387
388 bool IsTrackedDecl(const VarDecl *VD) {
389 const DeclContext *DC = VD->getDeclContext();
390 return IgnoredContexts.count(DC) == 0;
391 }
392
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000393 void VisitStmt(Stmt *S) {
John McCall8322c3a2011-02-13 04:07:26 +0000394 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000395 if (Stmt *child = *I)
396 Visit(child);
397 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000398
Ted Kremenek299cfb72011-12-22 01:30:46 +0000399 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000400 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000401 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000402 if (!VD->hasLocalStorage()) {
Benjamin Kramer616f8022012-03-10 15:08:09 +0000403 if (Visited.insert(VD))
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000404 BEVals.push_back(VD, BC);
John McCall113bee02012-03-10 09:33:50 +0000405 } else if (DR->refersToEnclosingLocal()) {
Benjamin Kramer616f8022012-03-10 15:08:09 +0000406 if (Visited.insert(VD) && IsTrackedDecl(VD))
407 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000408 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000409 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000410 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000411
Ted Kremenek575398e2010-03-10 00:18:11 +0000412 void VisitBlockExpr(BlockExpr *BR) {
413 // Blocks containing blocks can transitively capture more variables.
414 IgnoredContexts.insert(BR->getBlockDecl());
415 Visit(BR->getBlockDecl()->getBody());
416 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000417
418 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
419 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
420 et = PE->semantics_end(); it != et; ++it) {
421 Expr *Semantic = *it;
422 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
423 Semantic = OVE->getSourceExpr();
424 Visit(Semantic);
425 }
426 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000427};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000428} // end anonymous namespace
429
430typedef BumpVector<const VarDecl*> DeclVec;
431
432static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
433 void *&Vec,
434 llvm::BumpPtrAllocator &A) {
435 if (Vec)
436 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000437
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000438 BumpVectorContext BC(A);
439 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
440 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000441
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000442 // Find the referenced variables.
443 FindBlockDeclRefExprsVals F(*BV, BC);
444 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000445
446 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000447 return BV;
448}
449
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000450std::pair<AnalysisDeclContext::referenced_decls_iterator,
451 AnalysisDeclContext::referenced_decls_iterator>
452AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000453 if (!ReferencedBlockVars)
454 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000455
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000456 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
457 return std::make_pair(V->begin(), V->end());
458}
459
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000460ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000461 if (!ManagedAnalyses)
462 ManagedAnalyses = new ManagedAnalysisMap();
463 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
464 return (*M)[tag];
465}
466
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000467//===----------------------------------------------------------------------===//
468// Cleanup.
469//===----------------------------------------------------------------------===//
470
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000471ManagedAnalysis::~ManagedAnalysis() {}
472
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000473AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000474 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000475 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000476 // Release the managed analyses.
477 if (ManagedAnalyses) {
478 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
479 for (ManagedAnalysisMap::iterator I = M->begin(), E = M->end(); I!=E; ++I)
480 delete I->second;
481 delete M;
482 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000483}
484
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000485AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000486 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
487 delete I->second;
488}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000489
490LocationContext::~LocationContext() {}
491
492LocationContextManager::~LocationContextManager() {
493 clear();
494}
495
496void LocationContextManager::clear() {
497 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000498 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000499 LocationContext *LC = &*I;
500 ++I;
501 delete LC;
502 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000503
Ted Kremenek43d4a892009-12-04 01:28:56 +0000504 Contexts.clear();
505}
506