blob: 13f74aef69946b19b8f3aef7bf7975fc60dd56f1 [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.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000136 for (const auto &I : BD->captures()) {
137 const VarDecl *VD = I.getVariable();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000138 if (VD->getName() == "self")
139 return dyn_cast<ImplicitParamDecl>(VD);
140 }
141 }
Mike Stump11289f42009-09-09 15:08:12 +0000142
Ted Kremenek608677a2009-08-21 23:25:54 +0000143 return NULL;
144}
145
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000146void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000147 if (!forcedBlkExprs)
148 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
149 // Default construct an entry for 'stmt'.
Jordy Rose17347372011-06-10 08:49:37 +0000150 if (const Expr *e = dyn_cast<Expr>(stmt))
151 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000152 (void) (*forcedBlkExprs)[stmt];
153}
154
155const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000156AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000157 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000158 if (const Expr *e = dyn_cast<Expr>(stmt))
159 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000160 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
161 forcedBlkExprs->find(stmt);
162 assert(itr != forcedBlkExprs->end());
163 return itr->second;
164}
165
Jordan Rosecf10ea82013-06-06 21:53:45 +0000166/// Add each synthetic statement in the CFG to the parent map, using the
167/// source statement's parent.
168static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
169 if (!TheCFG)
170 return;
171
172 for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
173 E = TheCFG->synthetic_stmt_end();
174 I != E; ++I) {
175 PM.setParent(I->first, PM.getParent(I->second));
176 }
177}
178
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000179CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000180 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000181 return getUnoptimizedCFG();
182
Ted Kremenek0b405322010-03-23 00:13:23 +0000183 if (!builtCFG) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000184 cfg.reset(CFG::buildCFG(D, getBody(),
185 &D->getASTContext(), cfgBuildOptions));
Ted Kremenek0b405322010-03-23 00:13:23 +0000186 // Even when the cfg is not successfully built, we don't
187 // want to try building it again.
188 builtCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000189
190 if (PM)
191 addParentsForSyntheticStmts(cfg.get(), *PM);
Ted Kremenek0b405322010-03-23 00:13:23 +0000192 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000193 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000194}
195
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000196CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000197 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000198 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
199 false);
200 completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
201 cfgBuildOptions));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000202 // Even when the cfg is not successfully built, we don't
203 // want to try building it again.
204 builtCompleteCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000205
206 if (PM)
207 addParentsForSyntheticStmts(completeCFG.get(), *PM);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000208 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000209 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000210}
211
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000212CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000213 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000214 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000215
216 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000217 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
218 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000219 }
220
221 return 0;
222}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000223
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000224CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000225 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000226 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000227
Ted Kremenek80861ca2011-02-23 01:51:59 +0000228 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000229 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000230 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000231 }
232
233 return 0;
234}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000235
Ted Kremenek72be32a2011-12-22 23:33:52 +0000236void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000237 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000238}
239
Ted Kremenek35de1452013-05-17 09:41:40 +0000240ParentMap &AnalysisDeclContext::getParentMap() {
241 if (!PM) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000242 PM.reset(new ParentMap(getBody()));
243 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
Aaron Ballman0ad78302014-03-13 17:34:31 +0000244 for (const auto *I : C->inits()) {
245 PM->addStmt(I->getInit());
Jordan Rose433b0f52013-05-18 02:26:50 +0000246 }
247 }
Jordan Rosecf10ea82013-06-06 21:53:45 +0000248 if (builtCFG)
249 addParentsForSyntheticStmts(getCFG(), *PM);
250 if (builtCompleteCFG)
251 addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
Ted Kremenek35de1452013-05-17 09:41:40 +0000252 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000253 return *PM;
254}
255
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000256PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000257 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000258 PCA.reset(new PseudoConstantAnalysis(getBody()));
259 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000260}
261
Jordy Rose4f8198e2012-04-28 01:58:08 +0000262AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000263 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000264 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
265 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000266 FD->hasBody(FD);
267 D = FD;
268 }
269
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000270 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000271 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000272 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000273 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000274}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000275
Ted Kremenek142adc42011-10-23 02:31:52 +0000276const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000277AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000278 const CFGBlock *Blk, unsigned Idx) {
279 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
280}
281
Ted Kremenekc3da3762012-06-01 20:04:04 +0000282const BlockInvocationContext *
283AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
284 const clang::BlockDecl *BD,
285 const void *ContextData) {
286 return getLocationContextManager().getBlockInvocationContext(this, parent,
287 BD, ContextData);
288}
289
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000290LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000291 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000292 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000293 return Manager->getLocationContextManager();
294}
295
Ted Kremenek25388242009-12-04 00:50:10 +0000296//===----------------------------------------------------------------------===//
297// FoldingSet profiling.
298//===----------------------------------------------------------------------===//
299
Ted Kremenek25388242009-12-04 00:50:10 +0000300void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
301 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000302 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000303 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000304 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000305 ID.AddInteger(ck);
306 ID.AddPointer(ctx);
307 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000308 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000309}
310
Ted Kremenek25388242009-12-04 00:50:10 +0000311void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000312 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000313}
314
Ted Kremenek25388242009-12-04 00:50:10 +0000315void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000316 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000317}
318
319void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000320 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000321}
322
323//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000324// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000325//===----------------------------------------------------------------------===//
326
Ted Kremenek43d4a892009-12-04 01:28:56 +0000327template <typename LOC, typename DATA>
328const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000329LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000330 const LocationContext *parent,
331 const DATA *d) {
332 llvm::FoldingSetNodeID ID;
333 LOC::Profile(ID, ctx, parent, d);
334 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000335
Ted Kremenek43d4a892009-12-04 01:28:56 +0000336 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000337
Ted Kremenek43d4a892009-12-04 01:28:56 +0000338 if (!L) {
339 L = new LOC(ctx, parent, d);
340 Contexts.InsertNode(L, InsertPos);
341 }
342 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000343}
344
Ted Kremenek43d4a892009-12-04 01:28:56 +0000345const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000346LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000347 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000348 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000349 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000350 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000351 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000352 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000353 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000354 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
355 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000356 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000357 Contexts.InsertNode(L, InsertPos);
358 }
359 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000360}
361
Ted Kremenek43d4a892009-12-04 01:28:56 +0000362const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000363LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000364 const LocationContext *parent,
365 const Stmt *s) {
366 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
367}
Mike Stump11289f42009-09-09 15:08:12 +0000368
Ted Kremenekc3da3762012-06-01 20:04:04 +0000369const BlockInvocationContext *
370LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
371 const LocationContext *parent,
372 const BlockDecl *BD,
373 const void *ContextData) {
374 llvm::FoldingSetNodeID ID;
375 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
376 void *InsertPos;
377 BlockInvocationContext *L =
378 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
379 InsertPos));
380 if (!L) {
381 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
382 Contexts.InsertNode(L, InsertPos);
383 }
384 return L;
385}
386
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000387//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000388// LocationContext methods.
389//===----------------------------------------------------------------------===//
390
391const StackFrameContext *LocationContext::getCurrentStackFrame() const {
392 const LocationContext *LC = this;
393 while (LC) {
394 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
395 return SFC;
396 LC = LC->getParent();
397 }
398 return NULL;
399}
400
Anna Zaks44dc91b2012-11-03 02:54:16 +0000401bool LocationContext::inTopFrame() const {
402 return getCurrentStackFrame()->inTopFrame();
403}
404
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000405bool LocationContext::isParentOf(const LocationContext *LC) const {
406 do {
407 const LocationContext *Parent = LC->getParent();
408 if (Parent == this)
409 return true;
410 else
411 LC = Parent;
412 } while (LC);
413
414 return false;
415}
416
Jordan Rosee9c57222013-07-19 00:59:08 +0000417void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
Jordan Rose6fdef112013-03-30 01:31:35 +0000418 ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
419 PrintingPolicy PP(Ctx.getLangOpts());
420 PP.TerseOutput = 1;
421
422 unsigned Frame = 0;
423 for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
424 switch (LCtx->getKind()) {
425 case StackFrame:
Jordan Rosee9c57222013-07-19 00:59:08 +0000426 OS << Indent << '#' << Frame++ << ' ';
427 cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
428 OS << '\n';
Jordan Rose6fdef112013-03-30 01:31:35 +0000429 break;
430 case Scope:
Jordan Rosee9c57222013-07-19 00:59:08 +0000431 OS << Indent << " (scope)\n";
Jordan Rose6fdef112013-03-30 01:31:35 +0000432 break;
433 case Block:
Jordan Rosee9c57222013-07-19 00:59:08 +0000434 OS << Indent << " (block context: "
Jordan Rose6fdef112013-03-30 01:31:35 +0000435 << cast<BlockInvocationContext>(LCtx)->getContextData()
436 << ")\n";
437 break;
438 }
439 }
440}
441
Alp Tokeref6b0072014-01-04 13:47:14 +0000442LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
Jordan Rosee9c57222013-07-19 00:59:08 +0000443 dumpStack(llvm::errs());
444}
445
Ted Kremenek04af9f22009-12-07 22:05:27 +0000446//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000447// Lazily generated map to query the external variables referenced by a Block.
448//===----------------------------------------------------------------------===//
449
450namespace {
451class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
452 BumpVector<const VarDecl*> &BEVals;
453 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000454 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
455 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000456public:
457 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
458 BumpVectorContext &bc)
459 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000460
461 bool IsTrackedDecl(const VarDecl *VD) {
462 const DeclContext *DC = VD->getDeclContext();
463 return IgnoredContexts.count(DC) == 0;
464 }
465
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000466 void VisitStmt(Stmt *S) {
John McCall8322c3a2011-02-13 04:07:26 +0000467 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000468 if (Stmt *child = *I)
469 Visit(child);
470 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000471
Ted Kremenek299cfb72011-12-22 01:30:46 +0000472 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000473 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000474 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000475 if (!VD->hasLocalStorage()) {
Benjamin Kramer616f8022012-03-10 15:08:09 +0000476 if (Visited.insert(VD))
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000477 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000478 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000479 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000480 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000481
Ted Kremenek575398e2010-03-10 00:18:11 +0000482 void VisitBlockExpr(BlockExpr *BR) {
483 // Blocks containing blocks can transitively capture more variables.
484 IgnoredContexts.insert(BR->getBlockDecl());
485 Visit(BR->getBlockDecl()->getBody());
486 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000487
488 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
489 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
490 et = PE->semantics_end(); it != et; ++it) {
491 Expr *Semantic = *it;
492 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
493 Semantic = OVE->getSourceExpr();
494 Visit(Semantic);
495 }
496 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000497};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000498} // end anonymous namespace
499
500typedef BumpVector<const VarDecl*> DeclVec;
501
502static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
503 void *&Vec,
504 llvm::BumpPtrAllocator &A) {
505 if (Vec)
506 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000507
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000508 BumpVectorContext BC(A);
509 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
510 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000511
Ted Kremenek3e871d82012-12-06 07:17:26 +0000512 // Go through the capture list.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000513 for (const auto &CI : BD->captures()) {
514 BV->push_back(CI.getVariable(), BC);
Ted Kremenek3e871d82012-12-06 07:17:26 +0000515 }
516
517 // Find the referenced global/static variables.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000518 FindBlockDeclRefExprsVals F(*BV, BC);
519 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000520
521 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000522 return BV;
523}
524
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000525std::pair<AnalysisDeclContext::referenced_decls_iterator,
526 AnalysisDeclContext::referenced_decls_iterator>
527AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000528 if (!ReferencedBlockVars)
529 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000530
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000531 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
532 return std::make_pair(V->begin(), V->end());
533}
534
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000535ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000536 if (!ManagedAnalyses)
537 ManagedAnalyses = new ManagedAnalysisMap();
538 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
539 return (*M)[tag];
540}
541
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000542//===----------------------------------------------------------------------===//
543// Cleanup.
544//===----------------------------------------------------------------------===//
545
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000546ManagedAnalysis::~ManagedAnalysis() {}
547
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000548AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000549 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000550 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000551 // Release the managed analyses.
552 if (ManagedAnalyses) {
553 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
Reid Kleckner588c9372014-02-19 23:44:52 +0000554 llvm::DeleteContainerSeconds(*M);
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000555 delete M;
556 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000557}
558
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000559AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Reid Kleckner588c9372014-02-19 23:44:52 +0000560 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000561}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000562
563LocationContext::~LocationContext() {}
564
565LocationContextManager::~LocationContextManager() {
566 clear();
567}
568
569void LocationContextManager::clear() {
570 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000571 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000572 LocationContext *LC = &*I;
573 ++I;
574 delete LC;
575 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000576
Ted Kremenek43d4a892009-12-04 01:28:56 +0000577 Contexts.clear();
578}
579