blob: 4e623c8d6c399093af674ee42d516d459c640075 [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),
Craig Topper25542942014-05-20 04:30:07 +000044 forcedBlkExprs(nullptr),
Ted Kremenek189ecec2011-07-21 05:22:47 +000045 builtCFG(false),
46 builtCompleteCFG(false),
Craig Topper25542942014-05-20 04:30:07 +000047 ReferencedBlockVars(nullptr),
48 ManagedAnalyses(nullptr)
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),
Craig Topper25542942014-05-20 04:30:07 +000057 forcedBlkExprs(nullptr),
Ted Kremenek189ecec2011-07-21 05:22:47 +000058 builtCFG(false),
59 builtCompleteCFG(false),
Craig Topper25542942014-05-20 04:30:07 +000060 ReferencedBlockVars(nullptr),
61 ManagedAnalyses(nullptr)
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,
Ted Kremenekeeccb302014-08-27 15:14:15 +000072 bool addCXXNewAllocator,
73 CodeInjector *injector)
74 : Injector(injector), SynthesizeBodies(synthesizeBodies)
Ted Kremenek14f779c2012-09-21 00:09:11 +000075{
Ted Kremenek189ecec2011-07-21 05:22:47 +000076 cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +000077 cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
78 cfgBuildOptions.AddInitializers = addInitializers;
Jordan Rose6d671cc2012-09-05 22:55:23 +000079 cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
Ted Kremenek233c1b02013-03-29 00:09:22 +000080 cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
Jordan Rosec9176072014-01-13 17:59:19 +000081 cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
Ted Kremenekf9d82902011-03-10 01:14:05 +000082}
83
Ted Kremenek81ce1c82011-10-24 01:32:45 +000084void AnalysisDeclContextManager::clear() {
Reid Kleckner588c9372014-02-19 23:44:52 +000085 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenekd45ff6c2009-10-20 21:39:41 +000086}
87
Ted Kremenekeeccb302014-08-27 15:14:15 +000088static BodyFarm &getBodyFarm(ASTContext &C, CodeInjector *injector = nullptr) {
89 static BodyFarm *BF = new BodyFarm(C, injector);
Ted Kremenek14f779c2012-09-21 00:09:11 +000090 return *BF;
91}
92
Anna Zaks00c69a52013-02-02 00:30:04 +000093Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const {
NAKAMURA Takumicc4aaef2013-02-04 05:06:21 +000094 IsAutosynthesized = false;
Ted Kremenek14f779c2012-09-21 00:09:11 +000095 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
96 Stmt *Body = FD->getBody();
Anna Zaks00c69a52013-02-02 00:30:04 +000097 if (!Body && Manager && Manager->synthesizeBodies()) {
Ted Kremenekeeccb302014-08-27 15:14:15 +000098 Body = getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(FD);
Jordan Rose1a866cd2014-01-10 20:06:06 +000099 if (Body)
100 IsAutosynthesized = true;
Anna Zaks00c69a52013-02-02 00:30:04 +0000101 }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000102 return Body;
103 }
Jordan Rose1a866cd2014-01-10 20:06:06 +0000104 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
105 Stmt *Body = MD->getBody();
106 if (!Body && Manager && Manager->synthesizeBodies()) {
Ted Kremenekeeccb302014-08-27 15:14:15 +0000107 Body = getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(MD);
Jordan Rose1a866cd2014-01-10 20:06:06 +0000108 if (Body)
109 IsAutosynthesized = true;
110 }
111 return Body;
112 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek45805b92009-12-04 20:34:55 +0000113 return BD->getBody();
Mike Stump1bacb812010-01-13 02:59:54 +0000114 else if (const FunctionTemplateDecl *FunTmpl
115 = dyn_cast_or_null<FunctionTemplateDecl>(D))
116 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000117
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000118 llvm_unreachable("unknown code decl");
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000119}
120
Anna Zaks00c69a52013-02-02 00:30:04 +0000121Stmt *AnalysisDeclContext::getBody() const {
122 bool Tmp;
123 return getBody(Tmp);
124}
125
126bool AnalysisDeclContext::isBodyAutosynthesized() const {
127 bool Tmp;
128 getBody(Tmp);
129 return Tmp;
130}
131
Ted Kremenekeeccb302014-08-27 15:14:15 +0000132bool AnalysisDeclContext::isBodyAutosynthesizedFromModelFile() const {
133 bool Tmp;
134 Stmt *Body = getBody(Tmp);
135 return Tmp && Body->getLocStart().isValid();
136}
137
138
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000139const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
Ted Kremenek608677a2009-08-21 23:25:54 +0000140 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
141 return MD->getSelfDecl();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000142 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
143 // See if 'self' was captured by the block.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000144 for (const auto &I : BD->captures()) {
145 const VarDecl *VD = I.getVariable();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000146 if (VD->getName() == "self")
147 return dyn_cast<ImplicitParamDecl>(VD);
148 }
149 }
Mike Stump11289f42009-09-09 15:08:12 +0000150
Craig Topper25542942014-05-20 04:30:07 +0000151 return nullptr;
Ted Kremenek608677a2009-08-21 23:25:54 +0000152}
153
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000154void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000155 if (!forcedBlkExprs)
156 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
157 // Default construct an entry for 'stmt'.
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 (void) (*forcedBlkExprs)[stmt];
161}
162
163const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000164AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000165 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000166 if (const Expr *e = dyn_cast<Expr>(stmt))
167 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000168 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
169 forcedBlkExprs->find(stmt);
170 assert(itr != forcedBlkExprs->end());
171 return itr->second;
172}
173
Jordan Rosecf10ea82013-06-06 21:53:45 +0000174/// Add each synthetic statement in the CFG to the parent map, using the
175/// source statement's parent.
176static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
177 if (!TheCFG)
178 return;
179
180 for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
181 E = TheCFG->synthetic_stmt_end();
182 I != E; ++I) {
183 PM.setParent(I->first, PM.getParent(I->second));
184 }
185}
186
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000187CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000188 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000189 return getUnoptimizedCFG();
190
Ted Kremenek0b405322010-03-23 00:13:23 +0000191 if (!builtCFG) {
David Blaikiee90195c2014-08-29 18:53:26 +0000192 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
Ted Kremenek0b405322010-03-23 00:13:23 +0000193 // Even when the cfg is not successfully built, we don't
194 // want to try building it again.
195 builtCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000196
197 if (PM)
198 addParentsForSyntheticStmts(cfg.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000199
Richard Trieue729d9b2014-04-15 01:06:38 +0000200 // The Observer should only observe one build of the CFG.
Craig Topper25542942014-05-20 04:30:07 +0000201 getCFGBuildOptions().Observer = nullptr;
Ted Kremenek0b405322010-03-23 00:13:23 +0000202 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000203 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000204}
205
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000206CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000207 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000208 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
209 false);
David Blaikiee90195c2014-08-29 18:53:26 +0000210 completeCFG =
211 CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000212 // Even when the cfg is not successfully built, we don't
213 // want to try building it again.
214 builtCompleteCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000215
216 if (PM)
217 addParentsForSyntheticStmts(completeCFG.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000218
Richard Trieue729d9b2014-04-15 01:06:38 +0000219 // The Observer should only observe one build of the CFG.
Craig Topper25542942014-05-20 04:30:07 +0000220 getCFGBuildOptions().Observer = nullptr;
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000221 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000222 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000223}
224
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000225CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000226 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000227 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000228
229 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000230 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
231 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000232 }
Craig Topper25542942014-05-20 04:30:07 +0000233
234 return nullptr;
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000235}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000236
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000237CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000238 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000239 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000240
Ted Kremenek80861ca2011-02-23 01:51:59 +0000241 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000242 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000243 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000244 }
Craig Topper25542942014-05-20 04:30:07 +0000245
246 return nullptr;
Ted Kremenek80861ca2011-02-23 01:51:59 +0000247}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000248
Ted Kremenek72be32a2011-12-22 23:33:52 +0000249void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000250 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000251}
252
Ted Kremenek35de1452013-05-17 09:41:40 +0000253ParentMap &AnalysisDeclContext::getParentMap() {
254 if (!PM) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000255 PM.reset(new ParentMap(getBody()));
256 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
Aaron Ballman0ad78302014-03-13 17:34:31 +0000257 for (const auto *I : C->inits()) {
258 PM->addStmt(I->getInit());
Jordan Rose433b0f52013-05-18 02:26:50 +0000259 }
260 }
Jordan Rosecf10ea82013-06-06 21:53:45 +0000261 if (builtCFG)
262 addParentsForSyntheticStmts(getCFG(), *PM);
263 if (builtCompleteCFG)
264 addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
Ted Kremenek35de1452013-05-17 09:41:40 +0000265 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000266 return *PM;
267}
268
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000269PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000270 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000271 PCA.reset(new PseudoConstantAnalysis(getBody()));
272 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000273}
274
Jordy Rose4f8198e2012-04-28 01:58:08 +0000275AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000276 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000277 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
278 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000279 FD->hasBody(FD);
280 D = FD;
281 }
282
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000283 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000284 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000285 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000286 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000287}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000288
Ted Kremenek142adc42011-10-23 02:31:52 +0000289const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000290AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000291 const CFGBlock *Blk, unsigned Idx) {
292 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
293}
294
Ted Kremenekc3da3762012-06-01 20:04:04 +0000295const BlockInvocationContext *
296AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
297 const clang::BlockDecl *BD,
298 const void *ContextData) {
299 return getLocationContextManager().getBlockInvocationContext(this, parent,
300 BD, ContextData);
301}
302
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000303LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000304 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000305 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000306 return Manager->getLocationContextManager();
307}
308
Ted Kremenek25388242009-12-04 00:50:10 +0000309//===----------------------------------------------------------------------===//
310// FoldingSet profiling.
311//===----------------------------------------------------------------------===//
312
Ted Kremenek25388242009-12-04 00:50:10 +0000313void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
314 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000315 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000316 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000317 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000318 ID.AddInteger(ck);
319 ID.AddPointer(ctx);
320 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000321 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000322}
323
Ted Kremenek25388242009-12-04 00:50:10 +0000324void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000325 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000326}
327
Ted Kremenek25388242009-12-04 00:50:10 +0000328void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000329 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000330}
331
332void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000333 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000334}
335
336//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000337// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000338//===----------------------------------------------------------------------===//
339
Ted Kremenek43d4a892009-12-04 01:28:56 +0000340template <typename LOC, typename DATA>
341const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000342LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000343 const LocationContext *parent,
344 const DATA *d) {
345 llvm::FoldingSetNodeID ID;
346 LOC::Profile(ID, ctx, parent, d);
347 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000348
Ted Kremenek43d4a892009-12-04 01:28:56 +0000349 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000350
Ted Kremenek43d4a892009-12-04 01:28:56 +0000351 if (!L) {
352 L = new LOC(ctx, parent, d);
353 Contexts.InsertNode(L, InsertPos);
354 }
355 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000356}
357
Ted Kremenek43d4a892009-12-04 01:28:56 +0000358const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000359LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000360 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000361 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000362 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000363 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000364 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000365 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000366 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000367 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
368 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000369 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000370 Contexts.InsertNode(L, InsertPos);
371 }
372 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000373}
374
Ted Kremenek43d4a892009-12-04 01:28:56 +0000375const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000376LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000377 const LocationContext *parent,
378 const Stmt *s) {
379 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
380}
Mike Stump11289f42009-09-09 15:08:12 +0000381
Ted Kremenekc3da3762012-06-01 20:04:04 +0000382const BlockInvocationContext *
383LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
384 const LocationContext *parent,
385 const BlockDecl *BD,
386 const void *ContextData) {
387 llvm::FoldingSetNodeID ID;
388 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
389 void *InsertPos;
390 BlockInvocationContext *L =
391 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
392 InsertPos));
393 if (!L) {
394 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
395 Contexts.InsertNode(L, InsertPos);
396 }
397 return L;
398}
399
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000400//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000401// LocationContext methods.
402//===----------------------------------------------------------------------===//
403
404const StackFrameContext *LocationContext::getCurrentStackFrame() const {
405 const LocationContext *LC = this;
406 while (LC) {
407 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
408 return SFC;
409 LC = LC->getParent();
410 }
Craig Topper25542942014-05-20 04:30:07 +0000411 return nullptr;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000412}
413
Anna Zaks44dc91b2012-11-03 02:54:16 +0000414bool LocationContext::inTopFrame() const {
415 return getCurrentStackFrame()->inTopFrame();
416}
417
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000418bool LocationContext::isParentOf(const LocationContext *LC) const {
419 do {
420 const LocationContext *Parent = LC->getParent();
421 if (Parent == this)
422 return true;
423 else
424 LC = Parent;
425 } while (LC);
426
427 return false;
428}
429
Jordan Rosee9c57222013-07-19 00:59:08 +0000430void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
Jordan Rose6fdef112013-03-30 01:31:35 +0000431 ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
432 PrintingPolicy PP(Ctx.getLangOpts());
433 PP.TerseOutput = 1;
434
435 unsigned Frame = 0;
436 for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
437 switch (LCtx->getKind()) {
438 case StackFrame:
Jordan Rosee9c57222013-07-19 00:59:08 +0000439 OS << Indent << '#' << Frame++ << ' ';
440 cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
441 OS << '\n';
Jordan Rose6fdef112013-03-30 01:31:35 +0000442 break;
443 case Scope:
Jordan Rosee9c57222013-07-19 00:59:08 +0000444 OS << Indent << " (scope)\n";
Jordan Rose6fdef112013-03-30 01:31:35 +0000445 break;
446 case Block:
Jordan Rosee9c57222013-07-19 00:59:08 +0000447 OS << Indent << " (block context: "
Jordan Rose6fdef112013-03-30 01:31:35 +0000448 << cast<BlockInvocationContext>(LCtx)->getContextData()
449 << ")\n";
450 break;
451 }
452 }
453}
454
Alp Tokeref6b0072014-01-04 13:47:14 +0000455LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
Jordan Rosee9c57222013-07-19 00:59:08 +0000456 dumpStack(llvm::errs());
457}
458
Ted Kremenek04af9f22009-12-07 22:05:27 +0000459//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000460// Lazily generated map to query the external variables referenced by a Block.
461//===----------------------------------------------------------------------===//
462
463namespace {
464class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
465 BumpVector<const VarDecl*> &BEVals;
466 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000467 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
468 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000469public:
470 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
471 BumpVectorContext &bc)
472 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000473
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000474 void VisitStmt(Stmt *S) {
John McCall8322c3a2011-02-13 04:07:26 +0000475 for (Stmt::child_range I = S->children(); I; ++I)
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000476 if (Stmt *child = *I)
477 Visit(child);
478 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000479
Ted Kremenek299cfb72011-12-22 01:30:46 +0000480 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000481 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000482 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000483 if (!VD->hasLocalStorage()) {
David Blaikie82e95a32014-11-19 07:49:47 +0000484 if (Visited.insert(VD).second)
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000485 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000486 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000487 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000488 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000489
Ted Kremenek575398e2010-03-10 00:18:11 +0000490 void VisitBlockExpr(BlockExpr *BR) {
491 // Blocks containing blocks can transitively capture more variables.
492 IgnoredContexts.insert(BR->getBlockDecl());
493 Visit(BR->getBlockDecl()->getBody());
494 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000495
496 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
497 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
498 et = PE->semantics_end(); it != et; ++it) {
499 Expr *Semantic = *it;
500 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
501 Semantic = OVE->getSourceExpr();
502 Visit(Semantic);
503 }
504 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000505};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000506} // end anonymous namespace
507
508typedef BumpVector<const VarDecl*> DeclVec;
509
510static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
511 void *&Vec,
512 llvm::BumpPtrAllocator &A) {
513 if (Vec)
514 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000515
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000516 BumpVectorContext BC(A);
517 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
518 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000519
Ted Kremenek3e871d82012-12-06 07:17:26 +0000520 // Go through the capture list.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000521 for (const auto &CI : BD->captures()) {
522 BV->push_back(CI.getVariable(), BC);
Ted Kremenek3e871d82012-12-06 07:17:26 +0000523 }
524
525 // Find the referenced global/static variables.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000526 FindBlockDeclRefExprsVals F(*BV, BC);
527 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000528
529 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000530 return BV;
531}
532
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000533llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000534AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000535 if (!ReferencedBlockVars)
536 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000537
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000538 const DeclVec *V =
539 LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
540 return llvm::make_range(V->begin(), V->end());
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000541}
542
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000543ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000544 if (!ManagedAnalyses)
545 ManagedAnalyses = new ManagedAnalysisMap();
546 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
547 return (*M)[tag];
548}
549
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000550//===----------------------------------------------------------------------===//
551// Cleanup.
552//===----------------------------------------------------------------------===//
553
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000554ManagedAnalysis::~ManagedAnalysis() {}
555
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000556AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000557 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000558 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000559 // Release the managed analyses.
560 if (ManagedAnalyses) {
561 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
Reid Kleckner588c9372014-02-19 23:44:52 +0000562 llvm::DeleteContainerSeconds(*M);
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000563 delete M;
564 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000565}
566
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000567AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Reid Kleckner588c9372014-02-19 23:44:52 +0000568 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000569}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000570
571LocationContext::~LocationContext() {}
572
573LocationContextManager::~LocationContextManager() {
574 clear();
575}
576
577void LocationContextManager::clear() {
578 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000579 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000580 LocationContext *LC = &*I;
581 ++I;
582 delete LC;
583 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000584
Ted Kremenek43d4a892009-12-04 01:28:56 +0000585 Contexts.clear();
586}
587