blob: 1cc87d1a6904b842ae9f2014f82bf57879140d74 [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"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000032#include "llvm/Support/raw_ostream.h"
Ted Kremenek14f779c2012-09-21 00:09:11 +000033
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,
Ted Kremenek233c1b02013-03-29 00:09:22 +000070 bool synthesizeBodies,
Jordan Rosec9176072014-01-13 17:59:19 +000071 bool addStaticInitBranch,
72 bool addCXXNewAllocator)
Ted Kremenek14f779c2012-09-21 00:09:11 +000073 : SynthesizeBodies(synthesizeBodies)
74{
Ted Kremenek189ecec2011-07-21 05:22:47 +000075 cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +000076 cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
77 cfgBuildOptions.AddInitializers = addInitializers;
Jordan Rose6d671cc2012-09-05 22:55:23 +000078 cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
Ted Kremenek233c1b02013-03-29 00:09:22 +000079 cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
Jordan Rosec9176072014-01-13 17:59:19 +000080 cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
Ted Kremenekf9d82902011-03-10 01:14:05 +000081}
82
Ted Kremenek81ce1c82011-10-24 01:32:45 +000083void AnalysisDeclContextManager::clear() {
Reid Kleckner588c9372014-02-19 23:44:52 +000084 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenekd45ff6c2009-10-20 21:39:41 +000085}
86
Ted Kremenek14f779c2012-09-21 00:09:11 +000087static BodyFarm &getBodyFarm(ASTContext &C) {
88 static BodyFarm *BF = new BodyFarm(C);
89 return *BF;
90}
91
Anna Zaks00c69a52013-02-02 00:30:04 +000092Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const {
NAKAMURA Takumicc4aaef2013-02-04 05:06:21 +000093 IsAutosynthesized = false;
Ted Kremenek14f779c2012-09-21 00:09:11 +000094 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
95 Stmt *Body = FD->getBody();
Anna Zaks00c69a52013-02-02 00:30:04 +000096 if (!Body && Manager && Manager->synthesizeBodies()) {
Jordan Rose1a866cd2014-01-10 20:06:06 +000097 Body = getBodyFarm(getASTContext()).getBody(FD);
98 if (Body)
99 IsAutosynthesized = true;
Anna Zaks00c69a52013-02-02 00:30:04 +0000100 }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000101 return Body;
102 }
Jordan Rose1a866cd2014-01-10 20:06:06 +0000103 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
104 Stmt *Body = MD->getBody();
105 if (!Body && Manager && Manager->synthesizeBodies()) {
106 Body = getBodyFarm(getASTContext()).getBody(MD);
107 if (Body)
108 IsAutosynthesized = true;
109 }
110 return Body;
111 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek45805b92009-12-04 20:34:55 +0000112 return BD->getBody();
Mike Stump1bacb812010-01-13 02:59:54 +0000113 else if (const FunctionTemplateDecl *FunTmpl
114 = dyn_cast_or_null<FunctionTemplateDecl>(D))
115 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000116
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000117 llvm_unreachable("unknown code decl");
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000118}
119
Anna Zaks00c69a52013-02-02 00:30:04 +0000120Stmt *AnalysisDeclContext::getBody() const {
121 bool Tmp;
122 return getBody(Tmp);
123}
124
125bool AnalysisDeclContext::isBodyAutosynthesized() const {
126 bool Tmp;
127 getBody(Tmp);
128 return Tmp;
129}
130
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000131const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
Ted Kremenek608677a2009-08-21 23:25:54 +0000132 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
133 return MD->getSelfDecl();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000134 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
135 // See if 'self' was captured by the block.
136 for (BlockDecl::capture_const_iterator it = BD->capture_begin(),
137 et = BD->capture_end(); it != et; ++it) {
138 const VarDecl *VD = it->getVariable();
139 if (VD->getName() == "self")
140 return dyn_cast<ImplicitParamDecl>(VD);
141 }
142 }
Mike Stump11289f42009-09-09 15:08:12 +0000143
Ted Kremenek608677a2009-08-21 23:25:54 +0000144 return NULL;
145}
146
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000147void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000148 if (!forcedBlkExprs)
149 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
150 // Default construct an entry for 'stmt'.
Jordy Rose17347372011-06-10 08:49:37 +0000151 if (const Expr *e = dyn_cast<Expr>(stmt))
152 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000153 (void) (*forcedBlkExprs)[stmt];
154}
155
156const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000157AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000158 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000159 if (const Expr *e = dyn_cast<Expr>(stmt))
160 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000161 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
162 forcedBlkExprs->find(stmt);
163 assert(itr != forcedBlkExprs->end());
164 return itr->second;
165}
166
Jordan Rosecf10ea82013-06-06 21:53:45 +0000167/// Add each synthetic statement in the CFG to the parent map, using the
168/// source statement's parent.
169static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
170 if (!TheCFG)
171 return;
172
173 for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
174 E = TheCFG->synthetic_stmt_end();
175 I != E; ++I) {
176 PM.setParent(I->first, PM.getParent(I->second));
177 }
178}
179
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000180CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000181 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000182 return getUnoptimizedCFG();
183
Ted Kremenek0b405322010-03-23 00:13:23 +0000184 if (!builtCFG) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000185 cfg.reset(CFG::buildCFG(D, getBody(),
186 &D->getASTContext(), cfgBuildOptions));
Ted Kremenek0b405322010-03-23 00:13:23 +0000187 // Even when the cfg is not successfully built, we don't
188 // want to try building it again.
189 builtCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000190
191 if (PM)
192 addParentsForSyntheticStmts(cfg.get(), *PM);
Ted Kremenek0b405322010-03-23 00:13:23 +0000193 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000194 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000195}
196
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000197CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000198 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000199 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
200 false);
201 completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
202 cfgBuildOptions));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000203 // Even when the cfg is not successfully built, we don't
204 // want to try building it again.
205 builtCompleteCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000206
207 if (PM)
208 addParentsForSyntheticStmts(completeCFG.get(), *PM);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000209 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000210 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000211}
212
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000213CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000214 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000215 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000216
217 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000218 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
219 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000220 }
221
222 return 0;
223}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000224
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000225CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000226 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000227 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000228
Ted Kremenek80861ca2011-02-23 01:51:59 +0000229 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000230 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000231 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000232 }
233
234 return 0;
235}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000236
Ted Kremenek72be32a2011-12-22 23:33:52 +0000237void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000238 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000239}
240
Ted Kremenek35de1452013-05-17 09:41:40 +0000241ParentMap &AnalysisDeclContext::getParentMap() {
242 if (!PM) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000243 PM.reset(new ParentMap(getBody()));
244 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
Aaron Ballman0ad78302014-03-13 17:34:31 +0000245 for (const auto *I : C->inits()) {
246 PM->addStmt(I->getInit());
Jordan Rose433b0f52013-05-18 02:26:50 +0000247 }
248 }
Jordan Rosecf10ea82013-06-06 21:53:45 +0000249 if (builtCFG)
250 addParentsForSyntheticStmts(getCFG(), *PM);
251 if (builtCompleteCFG)
252 addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
Ted Kremenek35de1452013-05-17 09:41:40 +0000253 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000254 return *PM;
255}
256
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000257PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000258 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000259 PCA.reset(new PseudoConstantAnalysis(getBody()));
260 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000261}
262
Jordy Rose4f8198e2012-04-28 01:58:08 +0000263AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000264 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000265 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
266 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000267 FD->hasBody(FD);
268 D = FD;
269 }
270
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000271 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000272 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000273 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000274 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000275}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000276
Ted Kremenek142adc42011-10-23 02:31:52 +0000277const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000278AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000279 const CFGBlock *Blk, unsigned Idx) {
280 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
281}
282
Ted Kremenekc3da3762012-06-01 20:04:04 +0000283const BlockInvocationContext *
284AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
285 const clang::BlockDecl *BD,
286 const void *ContextData) {
287 return getLocationContextManager().getBlockInvocationContext(this, parent,
288 BD, ContextData);
289}
290
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000291LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000292 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000293 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000294 return Manager->getLocationContextManager();
295}
296
Ted Kremenek25388242009-12-04 00:50:10 +0000297//===----------------------------------------------------------------------===//
298// FoldingSet profiling.
299//===----------------------------------------------------------------------===//
300
Ted Kremenek25388242009-12-04 00:50:10 +0000301void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
302 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000303 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000304 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000305 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000306 ID.AddInteger(ck);
307 ID.AddPointer(ctx);
308 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000309 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000310}
311
Ted Kremenek25388242009-12-04 00:50:10 +0000312void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000313 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000314}
315
Ted Kremenek25388242009-12-04 00:50:10 +0000316void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000317 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000318}
319
320void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000321 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000322}
323
324//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000325// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000326//===----------------------------------------------------------------------===//
327
Ted Kremenek43d4a892009-12-04 01:28:56 +0000328template <typename LOC, typename DATA>
329const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000330LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000331 const LocationContext *parent,
332 const DATA *d) {
333 llvm::FoldingSetNodeID ID;
334 LOC::Profile(ID, ctx, parent, d);
335 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000336
Ted Kremenek43d4a892009-12-04 01:28:56 +0000337 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000338
Ted Kremenek43d4a892009-12-04 01:28:56 +0000339 if (!L) {
340 L = new LOC(ctx, parent, d);
341 Contexts.InsertNode(L, InsertPos);
342 }
343 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000344}
345
Ted Kremenek43d4a892009-12-04 01:28:56 +0000346const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000347LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000348 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000349 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000350 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000351 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000352 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000353 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000354 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000355 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
356 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000357 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000358 Contexts.InsertNode(L, InsertPos);
359 }
360 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000361}
362
Ted Kremenek43d4a892009-12-04 01:28:56 +0000363const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000364LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000365 const LocationContext *parent,
366 const Stmt *s) {
367 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
368}
Mike Stump11289f42009-09-09 15:08:12 +0000369
Ted Kremenekc3da3762012-06-01 20:04:04 +0000370const BlockInvocationContext *
371LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
372 const LocationContext *parent,
373 const BlockDecl *BD,
374 const void *ContextData) {
375 llvm::FoldingSetNodeID ID;
376 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
377 void *InsertPos;
378 BlockInvocationContext *L =
379 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
380 InsertPos));
381 if (!L) {
382 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
383 Contexts.InsertNode(L, InsertPos);
384 }
385 return L;
386}
387
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000388//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000389// LocationContext methods.
390//===----------------------------------------------------------------------===//
391
392const StackFrameContext *LocationContext::getCurrentStackFrame() const {
393 const LocationContext *LC = this;
394 while (LC) {
395 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
396 return SFC;
397 LC = LC->getParent();
398 }
399 return NULL;
400}
401
Anna Zaks44dc91b2012-11-03 02:54:16 +0000402bool LocationContext::inTopFrame() const {
403 return getCurrentStackFrame()->inTopFrame();
404}
405
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000406bool LocationContext::isParentOf(const LocationContext *LC) const {
407 do {
408 const LocationContext *Parent = LC->getParent();
409 if (Parent == this)
410 return true;
411 else
412 LC = Parent;
413 } while (LC);
414
415 return false;
416}
417
Jordan Rosee9c57222013-07-19 00:59:08 +0000418void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
Jordan Rose6fdef112013-03-30 01:31:35 +0000419 ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
420 PrintingPolicy PP(Ctx.getLangOpts());
421 PP.TerseOutput = 1;
422
423 unsigned Frame = 0;
424 for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
425 switch (LCtx->getKind()) {
426 case StackFrame:
Jordan Rosee9c57222013-07-19 00:59:08 +0000427 OS << Indent << '#' << Frame++ << ' ';
428 cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
429 OS << '\n';
Jordan Rose6fdef112013-03-30 01:31:35 +0000430 break;
431 case Scope:
Jordan Rosee9c57222013-07-19 00:59:08 +0000432 OS << Indent << " (scope)\n";
Jordan Rose6fdef112013-03-30 01:31:35 +0000433 break;
434 case Block:
Jordan Rosee9c57222013-07-19 00:59:08 +0000435 OS << Indent << " (block context: "
Jordan Rose6fdef112013-03-30 01:31:35 +0000436 << cast<BlockInvocationContext>(LCtx)->getContextData()
437 << ")\n";
438 break;
439 }
440 }
441}
442
Alp Tokeref6b0072014-01-04 13:47:14 +0000443LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
Jordan Rosee9c57222013-07-19 00:59:08 +0000444 dumpStack(llvm::errs());
445}
446
Ted Kremenek04af9f22009-12-07 22:05:27 +0000447//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000448// Lazily generated map to query the external variables referenced by a Block.
449//===----------------------------------------------------------------------===//
450
451namespace {
452class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
453 BumpVector<const VarDecl*> &BEVals;
454 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000455 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
456 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000457public:
458 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
459 BumpVectorContext &bc)
460 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000461
462 bool IsTrackedDecl(const VarDecl *VD) {
463 const DeclContext *DC = VD->getDeclContext();
464 return IgnoredContexts.count(DC) == 0;
465 }
466
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000467 void VisitStmt(Stmt *S) {
John McCall8322c3a2011-02-13 04:07:26 +0000468 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000469 if (Stmt *child = *I)
470 Visit(child);
471 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000472
Ted Kremenek299cfb72011-12-22 01:30:46 +0000473 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000474 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000475 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000476 if (!VD->hasLocalStorage()) {
Benjamin Kramer616f8022012-03-10 15:08:09 +0000477 if (Visited.insert(VD))
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000478 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000479 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000480 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000481 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000482
Ted Kremenek575398e2010-03-10 00:18:11 +0000483 void VisitBlockExpr(BlockExpr *BR) {
484 // Blocks containing blocks can transitively capture more variables.
485 IgnoredContexts.insert(BR->getBlockDecl());
486 Visit(BR->getBlockDecl()->getBody());
487 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000488
489 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
490 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
491 et = PE->semantics_end(); it != et; ++it) {
492 Expr *Semantic = *it;
493 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
494 Semantic = OVE->getSourceExpr();
495 Visit(Semantic);
496 }
497 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000498};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000499} // end anonymous namespace
500
501typedef BumpVector<const VarDecl*> DeclVec;
502
503static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
504 void *&Vec,
505 llvm::BumpPtrAllocator &A) {
506 if (Vec)
507 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000508
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000509 BumpVectorContext BC(A);
510 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
511 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000512
Ted Kremenek3e871d82012-12-06 07:17:26 +0000513 // Go through the capture list.
514 for (BlockDecl::capture_const_iterator CI = BD->capture_begin(),
515 CE = BD->capture_end(); CI != CE; ++CI) {
516 BV->push_back(CI->getVariable(), BC);
517 }
518
519 // Find the referenced global/static variables.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000520 FindBlockDeclRefExprsVals F(*BV, BC);
521 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000522
523 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000524 return BV;
525}
526
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000527std::pair<AnalysisDeclContext::referenced_decls_iterator,
528 AnalysisDeclContext::referenced_decls_iterator>
529AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000530 if (!ReferencedBlockVars)
531 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000532
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000533 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
534 return std::make_pair(V->begin(), V->end());
535}
536
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000537ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000538 if (!ManagedAnalyses)
539 ManagedAnalyses = new ManagedAnalysisMap();
540 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
541 return (*M)[tag];
542}
543
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000544//===----------------------------------------------------------------------===//
545// Cleanup.
546//===----------------------------------------------------------------------===//
547
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000548ManagedAnalysis::~ManagedAnalysis() {}
549
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000550AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000551 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000552 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000553 // Release the managed analyses.
554 if (ManagedAnalyses) {
555 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
Reid Kleckner588c9372014-02-19 23:44:52 +0000556 llvm::DeleteContainerSeconds(*M);
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000557 delete M;
558 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000559}
560
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000561AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Reid Kleckner588c9372014-02-19 23:44:52 +0000562 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000563}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000564
565LocationContext::~LocationContext() {}
566
567LocationContextManager::~LocationContextManager() {
568 clear();
569}
570
571void LocationContextManager::clear() {
572 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000573 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000574 LocationContext *LC = &*I;
575 ++I;
576 delete LC;
577 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000578
Ted Kremenek43d4a892009-12-04 01:28:56 +0000579 Contexts.clear();
580}
581