blob: e7df0a813b37f0dd41098258e6ef9e8eeac0a01b [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
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Zhongxing Xu14407bf2009-07-30 01:17:21 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclObjC.h"
Mike Stump1bacb812010-01-13 02:59:54 +000018#include "clang/AST/DeclTemplate.h"
Zhongxing Xu14407bf2009-07-30 01:17:21 +000019#include "clang/AST/ParentMap.h"
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000020#include "clang/AST/StmtVisitor.h"
Ted Kremenek575398e2010-03-10 00:18:11 +000021#include "clang/Analysis/Analyses/LiveVariables.h"
Tom Caree332c3b2010-08-23 19:51:57 +000022#include "clang/Analysis/Analyses/PseudoConstantAnalysis.h"
Ted Kremenek80861ca2011-02-23 01:51:59 +000023#include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h"
Ted Kremenek575398e2010-03-10 00:18:11 +000024#include "clang/Analysis/AnalysisContext.h"
25#include "clang/Analysis/CFG.h"
Ted Kremenekcc7f1f82011-02-23 01:51:53 +000026#include "clang/Analysis/CFGStmtMap.h"
Ted Kremenek0f5e6f882009-11-26 02:31:33 +000027#include "clang/Analysis/Support/BumpVector.h"
Argyrios Kyrtzidis981a9612012-03-01 19:45:56 +000028#include "llvm/Support/SaveAndRestore.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"
Zhongxing Xu14407bf2009-07-30 01:17:21 +000031
Ted Kremenek14f779c2012-09-21 00:09:11 +000032#include "BodyFarm.h"
33
Zhongxing Xu14407bf2009-07-30 01:17:21 +000034using namespace clang;
35
Ted Kremenekdccc2b22011-10-07 22:21:02 +000036typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap;
37
Ted Kremenek81ce1c82011-10-24 01:32:45 +000038AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
Ted Kremenek14f779c2012-09-21 00:09:11 +000039 const Decl *d,
40 const CFG::BuildOptions &buildOptions)
Ted Kremenek142adc42011-10-23 02:31:52 +000041 : Manager(Mgr),
42 D(d),
Ted Kremenek189ecec2011-07-21 05:22:47 +000043 cfgBuildOptions(buildOptions),
Ted Kremenekf9d82902011-03-10 01:14:05 +000044 forcedBlkExprs(0),
Ted Kremenek189ecec2011-07-21 05:22:47 +000045 builtCFG(false),
46 builtCompleteCFG(false),
Ted Kremenekdccc2b22011-10-07 22:21:02 +000047 ReferencedBlockVars(0),
48 ManagedAnalyses(0)
Ted Kremenek189ecec2011-07-21 05:22:47 +000049{
Ted Kremenekf9d82902011-03-10 01:14:05 +000050 cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
Ted Kremenek189ecec2011-07-21 05:22:47 +000051}
52
Ted Kremenek81ce1c82011-10-24 01:32:45 +000053AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr,
Ted Kremenek14f779c2012-09-21 00:09:11 +000054 const Decl *d)
Ted Kremenek142adc42011-10-23 02:31:52 +000055: Manager(Mgr),
56 D(d),
Ted Kremenek189ecec2011-07-21 05:22:47 +000057 forcedBlkExprs(0),
58 builtCFG(false),
59 builtCompleteCFG(false),
Ted Kremenekdccc2b22011-10-07 22:21:02 +000060 ReferencedBlockVars(0),
61 ManagedAnalyses(0)
Ted Kremenek189ecec2011-07-21 05:22:47 +000062{
63 cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs;
64}
65
Ted Kremenek81ce1c82011-10-24 01:32:45 +000066AnalysisDeclContextManager::AnalysisDeclContextManager(bool useUnoptimizedCFG,
Jordan Rose6d671cc2012-09-05 22:55:23 +000067 bool addImplicitDtors,
68 bool addInitializers,
Ted Kremenek14f779c2012-09-21 00:09:11 +000069 bool addTemporaryDtors,
70 bool synthesizeBodies)
71 : SynthesizeBodies(synthesizeBodies)
72{
Ted Kremenek189ecec2011-07-21 05:22:47 +000073 cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +000074 cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
75 cfgBuildOptions.AddInitializers = addInitializers;
Jordan Rose6d671cc2012-09-05 22:55:23 +000076 cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
Ted Kremenekf9d82902011-03-10 01:14:05 +000077}
78
Ted Kremenek81ce1c82011-10-24 01:32:45 +000079void AnalysisDeclContextManager::clear() {
Ted Kremenekd45ff6c2009-10-20 21:39:41 +000080 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
81 delete I->second;
82 Contexts.clear();
83}
84
Ted Kremenek14f779c2012-09-21 00:09:11 +000085static BodyFarm &getBodyFarm(ASTContext &C) {
86 static BodyFarm *BF = new BodyFarm(C);
87 return *BF;
88}
89
Ted Kremenek81ce1c82011-10-24 01:32:45 +000090Stmt *AnalysisDeclContext::getBody() const {
Ted Kremenek14f779c2012-09-21 00:09:11 +000091 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
92 Stmt *Body = FD->getBody();
93 if (!Body && Manager && Manager->synthesizeBodies())
94 return getBodyFarm(getASTContext()).getBody(FD);
95 return Body;
96 }
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +000097 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
Zhongxing Xu14407bf2009-07-30 01:17:21 +000098 return MD->getBody();
Ted Kremenek45805b92009-12-04 20:34:55 +000099 else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
100 return BD->getBody();
Mike Stump1bacb812010-01-13 02:59:54 +0000101 else if (const FunctionTemplateDecl *FunTmpl
102 = dyn_cast_or_null<FunctionTemplateDecl>(D))
103 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000104
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000105 llvm_unreachable("unknown code decl");
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000106}
107
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000108const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
Ted Kremenek608677a2009-08-21 23:25:54 +0000109 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
110 return MD->getSelfDecl();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000111 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
112 // See if 'self' was captured by the block.
113 for (BlockDecl::capture_const_iterator it = BD->capture_begin(),
114 et = BD->capture_end(); it != et; ++it) {
115 const VarDecl *VD = it->getVariable();
116 if (VD->getName() == "self")
117 return dyn_cast<ImplicitParamDecl>(VD);
118 }
119 }
Mike Stump11289f42009-09-09 15:08:12 +0000120
Ted Kremenek608677a2009-08-21 23:25:54 +0000121 return NULL;
122}
123
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000124void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000125 if (!forcedBlkExprs)
126 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
127 // Default construct an entry for 'stmt'.
Jordy Rose17347372011-06-10 08:49:37 +0000128 if (const Expr *e = dyn_cast<Expr>(stmt))
129 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000130 (void) (*forcedBlkExprs)[stmt];
131}
132
133const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000134AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000135 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000136 if (const Expr *e = dyn_cast<Expr>(stmt))
137 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000138 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
139 forcedBlkExprs->find(stmt);
140 assert(itr != forcedBlkExprs->end());
141 return itr->second;
142}
143
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000144CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000145 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000146 return getUnoptimizedCFG();
147
Ted Kremenek0b405322010-03-23 00:13:23 +0000148 if (!builtCFG) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000149 cfg.reset(CFG::buildCFG(D, getBody(),
150 &D->getASTContext(), cfgBuildOptions));
Ted Kremenek0b405322010-03-23 00:13:23 +0000151 // Even when the cfg is not successfully built, we don't
152 // want to try building it again.
153 builtCFG = true;
154 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000155 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000156}
157
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000158CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000159 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000160 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
161 false);
162 completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
163 cfgBuildOptions));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000164 // Even when the cfg is not successfully built, we don't
165 // want to try building it again.
166 builtCompleteCFG = true;
167 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000168 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000169}
170
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000171CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000172 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000173 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000174
175 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000176 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
177 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000178 }
179
180 return 0;
181}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000182
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000183CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000184 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000185 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000186
Ted Kremenek80861ca2011-02-23 01:51:59 +0000187 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000188 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000189 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000190 }
191
192 return 0;
193}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000194
Ted Kremenek72be32a2011-12-22 23:33:52 +0000195void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000196 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000197}
198
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000199ParentMap &AnalysisDeclContext::getParentMap() {
Jordan Rose25bc20f2012-07-26 20:04:30 +0000200 if (!PM) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000201 PM.reset(new ParentMap(getBody()));
Jordan Rose25bc20f2012-07-26 20:04:30 +0000202 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
203 for (CXXConstructorDecl::init_const_iterator I = C->init_begin(),
204 E = C->init_end();
205 I != E; ++I) {
206 PM->addStmt((*I)->getInit());
207 }
208 }
209 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000210 return *PM;
211}
212
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000213PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000214 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000215 PCA.reset(new PseudoConstantAnalysis(getBody()));
216 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000217}
218
Jordy Rose4f8198e2012-04-28 01:58:08 +0000219AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000220 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000221 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
222 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000223 FD->hasBody(FD);
224 D = FD;
225 }
226
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000227 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000228 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000229 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000230 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000231}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000232
Ted Kremenek142adc42011-10-23 02:31:52 +0000233const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000234AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000235 const CFGBlock *Blk, unsigned Idx) {
236 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
237}
238
Ted Kremenekc3da3762012-06-01 20:04:04 +0000239const BlockInvocationContext *
240AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
241 const clang::BlockDecl *BD,
242 const void *ContextData) {
243 return getLocationContextManager().getBlockInvocationContext(this, parent,
244 BD, ContextData);
245}
246
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000247LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000248 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000249 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000250 return Manager->getLocationContextManager();
251}
252
Ted Kremenek25388242009-12-04 00:50:10 +0000253//===----------------------------------------------------------------------===//
254// FoldingSet profiling.
255//===----------------------------------------------------------------------===//
256
Ted Kremenek25388242009-12-04 00:50:10 +0000257void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
258 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000259 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000260 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000261 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000262 ID.AddInteger(ck);
263 ID.AddPointer(ctx);
264 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000265 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000266}
267
Ted Kremenek25388242009-12-04 00:50:10 +0000268void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000269 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000270}
271
Ted Kremenek25388242009-12-04 00:50:10 +0000272void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000273 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000274}
275
276void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000277 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000278}
279
280//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000281// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000282//===----------------------------------------------------------------------===//
283
Ted Kremenek43d4a892009-12-04 01:28:56 +0000284template <typename LOC, typename DATA>
285const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000286LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000287 const LocationContext *parent,
288 const DATA *d) {
289 llvm::FoldingSetNodeID ID;
290 LOC::Profile(ID, ctx, parent, d);
291 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000292
Ted Kremenek43d4a892009-12-04 01:28:56 +0000293 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000294
Ted Kremenek43d4a892009-12-04 01:28:56 +0000295 if (!L) {
296 L = new LOC(ctx, parent, d);
297 Contexts.InsertNode(L, InsertPos);
298 }
299 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000300}
301
Ted Kremenek43d4a892009-12-04 01:28:56 +0000302const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000303LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000304 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000305 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000306 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000307 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000308 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000309 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000310 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000311 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
312 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000313 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000314 Contexts.InsertNode(L, InsertPos);
315 }
316 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000317}
318
Ted Kremenek43d4a892009-12-04 01:28:56 +0000319const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000320LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000321 const LocationContext *parent,
322 const Stmt *s) {
323 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
324}
Mike Stump11289f42009-09-09 15:08:12 +0000325
Ted Kremenekc3da3762012-06-01 20:04:04 +0000326const BlockInvocationContext *
327LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
328 const LocationContext *parent,
329 const BlockDecl *BD,
330 const void *ContextData) {
331 llvm::FoldingSetNodeID ID;
332 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
333 void *InsertPos;
334 BlockInvocationContext *L =
335 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
336 InsertPos));
337 if (!L) {
338 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
339 Contexts.InsertNode(L, InsertPos);
340 }
341 return L;
342}
343
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000344//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000345// LocationContext methods.
346//===----------------------------------------------------------------------===//
347
348const StackFrameContext *LocationContext::getCurrentStackFrame() const {
349 const LocationContext *LC = this;
350 while (LC) {
351 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
352 return SFC;
353 LC = LC->getParent();
354 }
355 return NULL;
356}
357
Anna Zaks44dc91b2012-11-03 02:54:16 +0000358bool LocationContext::inTopFrame() const {
359 return getCurrentStackFrame()->inTopFrame();
360}
361
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000362bool LocationContext::isParentOf(const LocationContext *LC) const {
363 do {
364 const LocationContext *Parent = LC->getParent();
365 if (Parent == this)
366 return true;
367 else
368 LC = Parent;
369 } while (LC);
370
371 return false;
372}
373
Ted Kremenek04af9f22009-12-07 22:05:27 +0000374//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000375// Lazily generated map to query the external variables referenced by a Block.
376//===----------------------------------------------------------------------===//
377
378namespace {
379class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
380 BumpVector<const VarDecl*> &BEVals;
381 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000382 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
383 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000384public:
385 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
386 BumpVectorContext &bc)
387 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000388
389 bool IsTrackedDecl(const VarDecl *VD) {
390 const DeclContext *DC = VD->getDeclContext();
391 return IgnoredContexts.count(DC) == 0;
392 }
393
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000394 void VisitStmt(Stmt *S) {
John McCall8322c3a2011-02-13 04:07:26 +0000395 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000396 if (Stmt *child = *I)
397 Visit(child);
398 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000399
Ted Kremenek299cfb72011-12-22 01:30:46 +0000400 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000401 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000402 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000403 if (!VD->hasLocalStorage()) {
Benjamin Kramer616f8022012-03-10 15:08:09 +0000404 if (Visited.insert(VD))
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000405 BEVals.push_back(VD, BC);
John McCall113bee02012-03-10 09:33:50 +0000406 } else if (DR->refersToEnclosingLocal()) {
Benjamin Kramer616f8022012-03-10 15:08:09 +0000407 if (Visited.insert(VD) && IsTrackedDecl(VD))
408 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000409 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000410 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000411 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000412
Ted Kremenek575398e2010-03-10 00:18:11 +0000413 void VisitBlockExpr(BlockExpr *BR) {
414 // Blocks containing blocks can transitively capture more variables.
415 IgnoredContexts.insert(BR->getBlockDecl());
416 Visit(BR->getBlockDecl()->getBody());
417 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000418
419 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
420 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
421 et = PE->semantics_end(); it != et; ++it) {
422 Expr *Semantic = *it;
423 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
424 Semantic = OVE->getSourceExpr();
425 Visit(Semantic);
426 }
427 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000428};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000429} // end anonymous namespace
430
431typedef BumpVector<const VarDecl*> DeclVec;
432
433static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
434 void *&Vec,
435 llvm::BumpPtrAllocator &A) {
436 if (Vec)
437 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000438
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000439 BumpVectorContext BC(A);
440 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
441 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000442
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000443 // Find the referenced variables.
444 FindBlockDeclRefExprsVals F(*BV, BC);
445 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000446
447 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000448 return BV;
449}
450
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000451std::pair<AnalysisDeclContext::referenced_decls_iterator,
452 AnalysisDeclContext::referenced_decls_iterator>
453AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000454 if (!ReferencedBlockVars)
455 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000456
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000457 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
458 return std::make_pair(V->begin(), V->end());
459}
460
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000461ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000462 if (!ManagedAnalyses)
463 ManagedAnalyses = new ManagedAnalysisMap();
464 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
465 return (*M)[tag];
466}
467
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000468//===----------------------------------------------------------------------===//
469// Cleanup.
470//===----------------------------------------------------------------------===//
471
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000472ManagedAnalysis::~ManagedAnalysis() {}
473
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000474AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000475 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000476 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000477 // Release the managed analyses.
478 if (ManagedAnalyses) {
479 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
480 for (ManagedAnalysisMap::iterator I = M->begin(), E = M->end(); I!=E; ++I)
481 delete I->second;
482 delete M;
483 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000484}
485
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000486AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000487 for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I)
488 delete I->second;
489}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000490
491LocationContext::~LocationContext() {}
492
493LocationContextManager::~LocationContextManager() {
494 clear();
495}
496
497void LocationContextManager::clear() {
498 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000499 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000500 LocationContext *LC = &*I;
501 ++I;
502 delete LC;
503 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000504
Ted Kremenek43d4a892009-12-04 01:28:56 +0000505 Contexts.clear();
506}
507