blob: fa4bd33fa17c50ae6c81ed1d34530eaa6915ff74 [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);
Richard Trieue9fa2662014-04-15 00:57:50 +0000192
Richard Trieue729d9b2014-04-15 01:06:38 +0000193 // The Observer should only observe one build of the CFG.
Richard Trieue9fa2662014-04-15 00:57:50 +0000194 getCFGBuildOptions().Observer = 0;
Ted Kremenek0b405322010-03-23 00:13:23 +0000195 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000196 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000197}
198
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000199CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000200 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000201 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
202 false);
203 completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(),
204 cfgBuildOptions));
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000205 // Even when the cfg is not successfully built, we don't
206 // want to try building it again.
207 builtCompleteCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000208
209 if (PM)
210 addParentsForSyntheticStmts(completeCFG.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000211
Richard Trieue729d9b2014-04-15 01:06:38 +0000212 // The Observer should only observe one build of the CFG.
Richard Trieue9fa2662014-04-15 00:57:50 +0000213 getCFGBuildOptions().Observer = 0;
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000214 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000215 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000216}
217
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000218CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000219 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000220 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000221
222 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000223 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
224 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000225 }
226
227 return 0;
228}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000229
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000230CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000231 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000232 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000233
Ted Kremenek80861ca2011-02-23 01:51:59 +0000234 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000235 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000236 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000237 }
238
239 return 0;
240}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000241
Ted Kremenek72be32a2011-12-22 23:33:52 +0000242void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000243 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000244}
245
Ted Kremenek35de1452013-05-17 09:41:40 +0000246ParentMap &AnalysisDeclContext::getParentMap() {
247 if (!PM) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000248 PM.reset(new ParentMap(getBody()));
249 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
Aaron Ballman0ad78302014-03-13 17:34:31 +0000250 for (const auto *I : C->inits()) {
251 PM->addStmt(I->getInit());
Jordan Rose433b0f52013-05-18 02:26:50 +0000252 }
253 }
Jordan Rosecf10ea82013-06-06 21:53:45 +0000254 if (builtCFG)
255 addParentsForSyntheticStmts(getCFG(), *PM);
256 if (builtCompleteCFG)
257 addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
Ted Kremenek35de1452013-05-17 09:41:40 +0000258 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000259 return *PM;
260}
261
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000262PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000263 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000264 PCA.reset(new PseudoConstantAnalysis(getBody()));
265 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000266}
267
Jordy Rose4f8198e2012-04-28 01:58:08 +0000268AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000269 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000270 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
271 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000272 FD->hasBody(FD);
273 D = FD;
274 }
275
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000276 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000277 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000278 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000279 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000280}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000281
Ted Kremenek142adc42011-10-23 02:31:52 +0000282const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000283AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000284 const CFGBlock *Blk, unsigned Idx) {
285 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
286}
287
Ted Kremenekc3da3762012-06-01 20:04:04 +0000288const BlockInvocationContext *
289AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
290 const clang::BlockDecl *BD,
291 const void *ContextData) {
292 return getLocationContextManager().getBlockInvocationContext(this, parent,
293 BD, ContextData);
294}
295
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000296LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000297 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000298 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000299 return Manager->getLocationContextManager();
300}
301
Ted Kremenek25388242009-12-04 00:50:10 +0000302//===----------------------------------------------------------------------===//
303// FoldingSet profiling.
304//===----------------------------------------------------------------------===//
305
Ted Kremenek25388242009-12-04 00:50:10 +0000306void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
307 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000308 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000309 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000310 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000311 ID.AddInteger(ck);
312 ID.AddPointer(ctx);
313 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000314 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000315}
316
Ted Kremenek25388242009-12-04 00:50:10 +0000317void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000318 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000319}
320
Ted Kremenek25388242009-12-04 00:50:10 +0000321void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000322 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000323}
324
325void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000326 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000327}
328
329//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000330// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000331//===----------------------------------------------------------------------===//
332
Ted Kremenek43d4a892009-12-04 01:28:56 +0000333template <typename LOC, typename DATA>
334const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000335LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000336 const LocationContext *parent,
337 const DATA *d) {
338 llvm::FoldingSetNodeID ID;
339 LOC::Profile(ID, ctx, parent, d);
340 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000341
Ted Kremenek43d4a892009-12-04 01:28:56 +0000342 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000343
Ted Kremenek43d4a892009-12-04 01:28:56 +0000344 if (!L) {
345 L = new LOC(ctx, parent, d);
346 Contexts.InsertNode(L, InsertPos);
347 }
348 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000349}
350
Ted Kremenek43d4a892009-12-04 01:28:56 +0000351const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000352LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000353 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000354 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000355 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000356 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000357 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000358 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000359 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000360 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
361 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000362 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000363 Contexts.InsertNode(L, InsertPos);
364 }
365 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000366}
367
Ted Kremenek43d4a892009-12-04 01:28:56 +0000368const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000369LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000370 const LocationContext *parent,
371 const Stmt *s) {
372 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
373}
Mike Stump11289f42009-09-09 15:08:12 +0000374
Ted Kremenekc3da3762012-06-01 20:04:04 +0000375const BlockInvocationContext *
376LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
377 const LocationContext *parent,
378 const BlockDecl *BD,
379 const void *ContextData) {
380 llvm::FoldingSetNodeID ID;
381 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
382 void *InsertPos;
383 BlockInvocationContext *L =
384 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
385 InsertPos));
386 if (!L) {
387 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
388 Contexts.InsertNode(L, InsertPos);
389 }
390 return L;
391}
392
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000393//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000394// LocationContext methods.
395//===----------------------------------------------------------------------===//
396
397const StackFrameContext *LocationContext::getCurrentStackFrame() const {
398 const LocationContext *LC = this;
399 while (LC) {
400 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
401 return SFC;
402 LC = LC->getParent();
403 }
404 return NULL;
405}
406
Anna Zaks44dc91b2012-11-03 02:54:16 +0000407bool LocationContext::inTopFrame() const {
408 return getCurrentStackFrame()->inTopFrame();
409}
410
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000411bool LocationContext::isParentOf(const LocationContext *LC) const {
412 do {
413 const LocationContext *Parent = LC->getParent();
414 if (Parent == this)
415 return true;
416 else
417 LC = Parent;
418 } while (LC);
419
420 return false;
421}
422
Jordan Rosee9c57222013-07-19 00:59:08 +0000423void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
Jordan Rose6fdef112013-03-30 01:31:35 +0000424 ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
425 PrintingPolicy PP(Ctx.getLangOpts());
426 PP.TerseOutput = 1;
427
428 unsigned Frame = 0;
429 for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
430 switch (LCtx->getKind()) {
431 case StackFrame:
Jordan Rosee9c57222013-07-19 00:59:08 +0000432 OS << Indent << '#' << Frame++ << ' ';
433 cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
434 OS << '\n';
Jordan Rose6fdef112013-03-30 01:31:35 +0000435 break;
436 case Scope:
Jordan Rosee9c57222013-07-19 00:59:08 +0000437 OS << Indent << " (scope)\n";
Jordan Rose6fdef112013-03-30 01:31:35 +0000438 break;
439 case Block:
Jordan Rosee9c57222013-07-19 00:59:08 +0000440 OS << Indent << " (block context: "
Jordan Rose6fdef112013-03-30 01:31:35 +0000441 << cast<BlockInvocationContext>(LCtx)->getContextData()
442 << ")\n";
443 break;
444 }
445 }
446}
447
Alp Tokeref6b0072014-01-04 13:47:14 +0000448LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
Jordan Rosee9c57222013-07-19 00:59:08 +0000449 dumpStack(llvm::errs());
450}
451
Ted Kremenek04af9f22009-12-07 22:05:27 +0000452//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000453// Lazily generated map to query the external variables referenced by a Block.
454//===----------------------------------------------------------------------===//
455
456namespace {
457class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
458 BumpVector<const VarDecl*> &BEVals;
459 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000460 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
461 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000462public:
463 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
464 BumpVectorContext &bc)
465 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000466
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.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000514 for (const auto &CI : BD->captures()) {
515 BV->push_back(CI.getVariable(), BC);
Ted Kremenek3e871d82012-12-06 07:17:26 +0000516 }
517
518 // Find the referenced global/static variables.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000519 FindBlockDeclRefExprsVals F(*BV, BC);
520 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000521
522 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000523 return BV;
524}
525
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000526std::pair<AnalysisDeclContext::referenced_decls_iterator,
527 AnalysisDeclContext::referenced_decls_iterator>
528AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000529 if (!ReferencedBlockVars)
530 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000531
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000532 DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
533 return std::make_pair(V->begin(), V->end());
534}
535
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000536ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000537 if (!ManagedAnalyses)
538 ManagedAnalyses = new ManagedAnalysisMap();
539 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
540 return (*M)[tag];
541}
542
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000543//===----------------------------------------------------------------------===//
544// Cleanup.
545//===----------------------------------------------------------------------===//
546
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000547ManagedAnalysis::~ManagedAnalysis() {}
548
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000549AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000550 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000551 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000552 // Release the managed analyses.
553 if (ManagedAnalyses) {
554 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
Reid Kleckner588c9372014-02-19 23:44:52 +0000555 llvm::DeleteContainerSeconds(*M);
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000556 delete M;
557 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000558}
559
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000560AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Reid Kleckner588c9372014-02-19 23:44:52 +0000561 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000562}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000563
564LocationContext::~LocationContext() {}
565
566LocationContextManager::~LocationContextManager() {
567 clear();
568}
569
570void LocationContextManager::clear() {
571 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000572 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000573 LocationContext *LC = &*I;
574 ++I;
575 delete LC;
576 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000577
Ted Kremenek43d4a892009-12-04 01:28:56 +0000578 Contexts.clear();
579}
580