blob: 73eb142f97377e253268b7a0a5e3bc49c82674ba [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
George Karpenkov50657f62017-09-06 21:45:03 +000015#include "clang/Analysis/AnalysisDeclContext.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#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,
Matthias Gehre351c2182017-07-12 07:04:19 +000070 bool addLifetime,
Peter Szecsi999a25f2017-08-19 11:19:16 +000071 bool addLoopExit,
Ted Kremenek233c1b02013-03-29 00:09:22 +000072 bool synthesizeBodies,
Jordan Rosec9176072014-01-13 17:59:19 +000073 bool addStaticInitBranch,
Ted Kremenekeeccb302014-08-27 15:14:15 +000074 bool addCXXNewAllocator,
75 CodeInjector *injector)
76 : Injector(injector), SynthesizeBodies(synthesizeBodies)
Ted Kremenek14f779c2012-09-21 00:09:11 +000077{
Ted Kremenek189ecec2011-07-21 05:22:47 +000078 cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG;
Ted Kremenekf9d82902011-03-10 01:14:05 +000079 cfgBuildOptions.AddImplicitDtors = addImplicitDtors;
80 cfgBuildOptions.AddInitializers = addInitializers;
Jordan Rose6d671cc2012-09-05 22:55:23 +000081 cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors;
Matthias Gehre351c2182017-07-12 07:04:19 +000082 cfgBuildOptions.AddLifetime = addLifetime;
Peter Szecsi999a25f2017-08-19 11:19:16 +000083 cfgBuildOptions.AddLoopExit = addLoopExit;
Ted Kremenek233c1b02013-03-29 00:09:22 +000084 cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch;
Jordan Rosec9176072014-01-13 17:59:19 +000085 cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator;
Ted Kremenekf9d82902011-03-10 01:14:05 +000086}
87
Justin Lebar5cb35e12016-10-10 16:26:44 +000088void AnalysisDeclContextManager::clear() { Contexts.clear(); }
Ted Kremenekd45ff6c2009-10-20 21:39:41 +000089
Ted Kremenekeeccb302014-08-27 15:14:15 +000090static BodyFarm &getBodyFarm(ASTContext &C, CodeInjector *injector = nullptr) {
91 static BodyFarm *BF = new BodyFarm(C, injector);
Ted Kremenek14f779c2012-09-21 00:09:11 +000092 return *BF;
93}
94
Anna Zaks00c69a52013-02-02 00:30:04 +000095Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const {
NAKAMURA Takumicc4aaef2013-02-04 05:06:21 +000096 IsAutosynthesized = false;
Ted Kremenek14f779c2012-09-21 00:09:11 +000097 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
98 Stmt *Body = FD->getBody();
Eric Fiselierda8f9b52017-05-25 02:16:53 +000099 if (auto *CoroBody = dyn_cast_or_null<CoroutineBodyStmt>(Body))
100 Body = CoroBody->getBody();
Devin Coughlin7646ebe2016-03-28 23:55:58 +0000101 if (Manager && Manager->synthesizeBodies()) {
102 Stmt *SynthesizedBody =
103 getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(FD);
104 if (SynthesizedBody) {
105 Body = SynthesizedBody;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000106 IsAutosynthesized = true;
Devin Coughlin7646ebe2016-03-28 23:55:58 +0000107 }
Anna Zaks00c69a52013-02-02 00:30:04 +0000108 }
Ted Kremenek14f779c2012-09-21 00:09:11 +0000109 return Body;
110 }
Jordan Rose1a866cd2014-01-10 20:06:06 +0000111 else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) {
112 Stmt *Body = MD->getBody();
Devin Coughlin7646ebe2016-03-28 23:55:58 +0000113 if (Manager && Manager->synthesizeBodies()) {
114 Stmt *SynthesizedBody =
115 getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(MD);
116 if (SynthesizedBody) {
117 Body = SynthesizedBody;
Jordan Rose1a866cd2014-01-10 20:06:06 +0000118 IsAutosynthesized = true;
Devin Coughlin7646ebe2016-03-28 23:55:58 +0000119 }
Jordan Rose1a866cd2014-01-10 20:06:06 +0000120 }
121 return Body;
122 } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D))
Ted Kremenek45805b92009-12-04 20:34:55 +0000123 return BD->getBody();
Mike Stump1bacb812010-01-13 02:59:54 +0000124 else if (const FunctionTemplateDecl *FunTmpl
125 = dyn_cast_or_null<FunctionTemplateDecl>(D))
126 return FunTmpl->getTemplatedDecl()->getBody();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000127
Jeffrey Yasskin1615d452009-12-12 05:05:38 +0000128 llvm_unreachable("unknown code decl");
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000129}
130
Anna Zaks00c69a52013-02-02 00:30:04 +0000131Stmt *AnalysisDeclContext::getBody() const {
132 bool Tmp;
133 return getBody(Tmp);
134}
135
136bool AnalysisDeclContext::isBodyAutosynthesized() const {
137 bool Tmp;
138 getBody(Tmp);
139 return Tmp;
140}
141
Ted Kremenekeeccb302014-08-27 15:14:15 +0000142bool AnalysisDeclContext::isBodyAutosynthesizedFromModelFile() const {
143 bool Tmp;
144 Stmt *Body = getBody(Tmp);
145 return Tmp && Body->getLocStart().isValid();
146}
147
Devin Coughlinc289b742016-02-23 22:26:04 +0000148/// Returns true if \param VD is an Objective-C implicit 'self' parameter.
149static bool isSelfDecl(const VarDecl *VD) {
150 return isa<ImplicitParamDecl>(VD) && VD->getName() == "self";
151}
Ted Kremenekeeccb302014-08-27 15:14:15 +0000152
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000153const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const {
Ted Kremenek608677a2009-08-21 23:25:54 +0000154 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D))
155 return MD->getSelfDecl();
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000156 if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) {
157 // See if 'self' was captured by the block.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000158 for (const auto &I : BD->captures()) {
159 const VarDecl *VD = I.getVariable();
Devin Coughlinc289b742016-02-23 22:26:04 +0000160 if (isSelfDecl(VD))
Ted Kremenekb39fcfa2011-11-14 19:36:08 +0000161 return dyn_cast<ImplicitParamDecl>(VD);
162 }
163 }
Mike Stump11289f42009-09-09 15:08:12 +0000164
Devin Coughlin1d405832015-11-15 17:48:22 +0000165 auto *CXXMethod = dyn_cast<CXXMethodDecl>(D);
166 if (!CXXMethod)
167 return nullptr;
168
169 const CXXRecordDecl *parent = CXXMethod->getParent();
170 if (!parent->isLambda())
171 return nullptr;
172
173 for (const LambdaCapture &LC : parent->captures()) {
174 if (!LC.capturesVariable())
175 continue;
176
177 VarDecl *VD = LC.getCapturedVar();
Devin Coughlinc289b742016-02-23 22:26:04 +0000178 if (isSelfDecl(VD))
Devin Coughlin1d405832015-11-15 17:48:22 +0000179 return dyn_cast<ImplicitParamDecl>(VD);
180 }
181
Craig Topper25542942014-05-20 04:30:07 +0000182 return nullptr;
Ted Kremenek608677a2009-08-21 23:25:54 +0000183}
184
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000185void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000186 if (!forcedBlkExprs)
187 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
188 // Default construct an entry for 'stmt'.
Jordy Rose17347372011-06-10 08:49:37 +0000189 if (const Expr *e = dyn_cast<Expr>(stmt))
190 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000191 (void) (*forcedBlkExprs)[stmt];
192}
193
194const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000195AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000196 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000197 if (const Expr *e = dyn_cast<Expr>(stmt))
198 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000199 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
200 forcedBlkExprs->find(stmt);
201 assert(itr != forcedBlkExprs->end());
202 return itr->second;
203}
204
Jordan Rosecf10ea82013-06-06 21:53:45 +0000205/// Add each synthetic statement in the CFG to the parent map, using the
206/// source statement's parent.
207static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
208 if (!TheCFG)
209 return;
210
211 for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
212 E = TheCFG->synthetic_stmt_end();
213 I != E; ++I) {
214 PM.setParent(I->first, PM.getParent(I->second));
215 }
216}
217
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000218CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000219 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000220 return getUnoptimizedCFG();
221
Ted Kremenek0b405322010-03-23 00:13:23 +0000222 if (!builtCFG) {
David Blaikiee90195c2014-08-29 18:53:26 +0000223 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
Ted Kremenek0b405322010-03-23 00:13:23 +0000224 // Even when the cfg is not successfully built, we don't
225 // want to try building it again.
226 builtCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000227
228 if (PM)
229 addParentsForSyntheticStmts(cfg.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000230
Richard Trieue729d9b2014-04-15 01:06:38 +0000231 // The Observer should only observe one build of the CFG.
Craig Topper25542942014-05-20 04:30:07 +0000232 getCFGBuildOptions().Observer = nullptr;
Ted Kremenek0b405322010-03-23 00:13:23 +0000233 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000234 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000235}
236
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000237CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000238 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000239 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
240 false);
David Blaikiee90195c2014-08-29 18:53:26 +0000241 completeCFG =
242 CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000243 // Even when the cfg is not successfully built, we don't
244 // want to try building it again.
245 builtCompleteCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000246
247 if (PM)
248 addParentsForSyntheticStmts(completeCFG.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000249
Richard Trieue729d9b2014-04-15 01:06:38 +0000250 // The Observer should only observe one build of the CFG.
Craig Topper25542942014-05-20 04:30:07 +0000251 getCFGBuildOptions().Observer = nullptr;
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000252 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000253 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000254}
255
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000256CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000257 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000258 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000259
260 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000261 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
262 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000263 }
Craig Topper25542942014-05-20 04:30:07 +0000264
265 return nullptr;
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000266}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000267
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000268CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000269 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000270 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000271
Ted Kremenek80861ca2011-02-23 01:51:59 +0000272 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000273 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000274 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000275 }
Craig Topper25542942014-05-20 04:30:07 +0000276
277 return nullptr;
Ted Kremenek80861ca2011-02-23 01:51:59 +0000278}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000279
Ted Kremenek72be32a2011-12-22 23:33:52 +0000280void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000281 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000282}
283
Ted Kremenek35de1452013-05-17 09:41:40 +0000284ParentMap &AnalysisDeclContext::getParentMap() {
285 if (!PM) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000286 PM.reset(new ParentMap(getBody()));
287 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
Aaron Ballman0ad78302014-03-13 17:34:31 +0000288 for (const auto *I : C->inits()) {
289 PM->addStmt(I->getInit());
Jordan Rose433b0f52013-05-18 02:26:50 +0000290 }
291 }
Jordan Rosecf10ea82013-06-06 21:53:45 +0000292 if (builtCFG)
293 addParentsForSyntheticStmts(getCFG(), *PM);
294 if (builtCompleteCFG)
295 addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
Ted Kremenek35de1452013-05-17 09:41:40 +0000296 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000297 return *PM;
298}
299
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000300PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000301 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000302 PCA.reset(new PseudoConstantAnalysis(getBody()));
303 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000304}
305
Jordy Rose4f8198e2012-04-28 01:58:08 +0000306AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000307 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000308 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
309 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000310 FD->hasBody(FD);
311 D = FD;
312 }
313
Justin Lebar5cb35e12016-10-10 16:26:44 +0000314 std::unique_ptr<AnalysisDeclContext> &AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000315 if (!AC)
Justin Lebar5cb35e12016-10-10 16:26:44 +0000316 AC = llvm::make_unique<AnalysisDeclContext>(this, D, cfgBuildOptions);
317 return AC.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000318}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000319
Ted Kremenek142adc42011-10-23 02:31:52 +0000320const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000321AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000322 const CFGBlock *Blk, unsigned Idx) {
323 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
324}
325
Ted Kremenekc3da3762012-06-01 20:04:04 +0000326const BlockInvocationContext *
327AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
328 const clang::BlockDecl *BD,
329 const void *ContextData) {
330 return getLocationContextManager().getBlockInvocationContext(this, parent,
331 BD, ContextData);
332}
333
Devin Coughlin9165df12016-02-07 16:55:44 +0000334bool AnalysisDeclContext::isInStdNamespace(const Decl *D) {
335 const DeclContext *DC = D->getDeclContext()->getEnclosingNamespaceContext();
336 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
337 if (!ND)
338 return false;
339
340 while (const DeclContext *Parent = ND->getParent()) {
341 if (!isa<NamespaceDecl>(Parent))
342 break;
343 ND = cast<NamespaceDecl>(Parent);
344 }
345
346 return ND->isStdNamespace();
347}
348
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000349LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000350 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000351 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000352 return Manager->getLocationContextManager();
353}
354
Ted Kremenek25388242009-12-04 00:50:10 +0000355//===----------------------------------------------------------------------===//
356// FoldingSet profiling.
357//===----------------------------------------------------------------------===//
358
Ted Kremenek25388242009-12-04 00:50:10 +0000359void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
360 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000361 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000362 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000363 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000364 ID.AddInteger(ck);
365 ID.AddPointer(ctx);
366 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000367 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000368}
369
Ted Kremenek25388242009-12-04 00:50:10 +0000370void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000371 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000372}
373
Ted Kremenek25388242009-12-04 00:50:10 +0000374void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000375 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000376}
377
378void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000379 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000380}
381
382//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000383// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000384//===----------------------------------------------------------------------===//
385
Ted Kremenek43d4a892009-12-04 01:28:56 +0000386template <typename LOC, typename DATA>
387const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000388LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000389 const LocationContext *parent,
390 const DATA *d) {
391 llvm::FoldingSetNodeID ID;
392 LOC::Profile(ID, ctx, parent, d);
393 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000394
Ted Kremenek43d4a892009-12-04 01:28:56 +0000395 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000396
Ted Kremenek43d4a892009-12-04 01:28:56 +0000397 if (!L) {
398 L = new LOC(ctx, parent, d);
399 Contexts.InsertNode(L, InsertPos);
400 }
401 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000402}
403
Ted Kremenek43d4a892009-12-04 01:28:56 +0000404const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000405LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000406 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000407 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000408 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000409 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000410 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000411 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000412 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000413 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
414 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000415 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000416 Contexts.InsertNode(L, InsertPos);
417 }
418 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000419}
420
Ted Kremenek43d4a892009-12-04 01:28:56 +0000421const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000422LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000423 const LocationContext *parent,
424 const Stmt *s) {
425 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
426}
Mike Stump11289f42009-09-09 15:08:12 +0000427
Ted Kremenekc3da3762012-06-01 20:04:04 +0000428const BlockInvocationContext *
429LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
430 const LocationContext *parent,
431 const BlockDecl *BD,
432 const void *ContextData) {
433 llvm::FoldingSetNodeID ID;
434 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
435 void *InsertPos;
436 BlockInvocationContext *L =
437 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
438 InsertPos));
439 if (!L) {
440 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
441 Contexts.InsertNode(L, InsertPos);
442 }
443 return L;
444}
445
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000446//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000447// LocationContext methods.
448//===----------------------------------------------------------------------===//
449
450const StackFrameContext *LocationContext::getCurrentStackFrame() const {
451 const LocationContext *LC = this;
452 while (LC) {
453 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
454 return SFC;
455 LC = LC->getParent();
456 }
Craig Topper25542942014-05-20 04:30:07 +0000457 return nullptr;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000458}
459
Anna Zaks44dc91b2012-11-03 02:54:16 +0000460bool LocationContext::inTopFrame() const {
461 return getCurrentStackFrame()->inTopFrame();
462}
463
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000464bool LocationContext::isParentOf(const LocationContext *LC) const {
465 do {
466 const LocationContext *Parent = LC->getParent();
467 if (Parent == this)
468 return true;
469 else
470 LC = Parent;
471 } while (LC);
472
473 return false;
474}
475
Jordan Rosee9c57222013-07-19 00:59:08 +0000476void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
Jordan Rose6fdef112013-03-30 01:31:35 +0000477 ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
478 PrintingPolicy PP(Ctx.getLangOpts());
479 PP.TerseOutput = 1;
480
481 unsigned Frame = 0;
482 for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
483 switch (LCtx->getKind()) {
484 case StackFrame:
Jordan Rosee9c57222013-07-19 00:59:08 +0000485 OS << Indent << '#' << Frame++ << ' ';
486 cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
487 OS << '\n';
Jordan Rose6fdef112013-03-30 01:31:35 +0000488 break;
489 case Scope:
Jordan Rosee9c57222013-07-19 00:59:08 +0000490 OS << Indent << " (scope)\n";
Jordan Rose6fdef112013-03-30 01:31:35 +0000491 break;
492 case Block:
Jordan Rosee9c57222013-07-19 00:59:08 +0000493 OS << Indent << " (block context: "
Jordan Rose6fdef112013-03-30 01:31:35 +0000494 << cast<BlockInvocationContext>(LCtx)->getContextData()
495 << ")\n";
496 break;
497 }
498 }
499}
500
Alp Tokeref6b0072014-01-04 13:47:14 +0000501LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
Jordan Rosee9c57222013-07-19 00:59:08 +0000502 dumpStack(llvm::errs());
503}
504
Ted Kremenek04af9f22009-12-07 22:05:27 +0000505//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000506// Lazily generated map to query the external variables referenced by a Block.
507//===----------------------------------------------------------------------===//
508
509namespace {
510class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
511 BumpVector<const VarDecl*> &BEVals;
512 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000513 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
514 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000515public:
516 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
517 BumpVectorContext &bc)
518 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000519
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000520 void VisitStmt(Stmt *S) {
Benjamin Kramer642f1732015-07-02 21:03:14 +0000521 for (Stmt *Child : S->children())
522 if (Child)
523 Visit(Child);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000524 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000525
Ted Kremenek299cfb72011-12-22 01:30:46 +0000526 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000527 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000528 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000529 if (!VD->hasLocalStorage()) {
David Blaikie82e95a32014-11-19 07:49:47 +0000530 if (Visited.insert(VD).second)
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000531 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000532 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000533 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000534 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000535
Ted Kremenek575398e2010-03-10 00:18:11 +0000536 void VisitBlockExpr(BlockExpr *BR) {
537 // Blocks containing blocks can transitively capture more variables.
538 IgnoredContexts.insert(BR->getBlockDecl());
539 Visit(BR->getBlockDecl()->getBody());
540 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000541
542 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
543 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
544 et = PE->semantics_end(); it != et; ++it) {
545 Expr *Semantic = *it;
546 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
547 Semantic = OVE->getSourceExpr();
548 Visit(Semantic);
549 }
550 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000551};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000552} // end anonymous namespace
553
554typedef BumpVector<const VarDecl*> DeclVec;
555
556static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
557 void *&Vec,
558 llvm::BumpPtrAllocator &A) {
559 if (Vec)
560 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000561
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000562 BumpVectorContext BC(A);
563 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
564 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000565
Ted Kremenek3e871d82012-12-06 07:17:26 +0000566 // Go through the capture list.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000567 for (const auto &CI : BD->captures()) {
568 BV->push_back(CI.getVariable(), BC);
Ted Kremenek3e871d82012-12-06 07:17:26 +0000569 }
570
571 // Find the referenced global/static variables.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000572 FindBlockDeclRefExprsVals F(*BV, BC);
573 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000574
575 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000576 return BV;
577}
578
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000579llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000580AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000581 if (!ReferencedBlockVars)
582 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000583
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000584 const DeclVec *V =
585 LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
586 return llvm::make_range(V->begin(), V->end());
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000587}
588
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000589ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000590 if (!ManagedAnalyses)
591 ManagedAnalyses = new ManagedAnalysisMap();
592 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
593 return (*M)[tag];
594}
595
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000596//===----------------------------------------------------------------------===//
597// Cleanup.
598//===----------------------------------------------------------------------===//
599
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000600ManagedAnalysis::~ManagedAnalysis() {}
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000601
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000602AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000603 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000604 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000605 // Release the managed analyses.
606 if (ManagedAnalyses) {
607 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
Reid Kleckner588c9372014-02-19 23:44:52 +0000608 llvm::DeleteContainerSeconds(*M);
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000609 delete M;
610 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000611}
612
Justin Lebar5cb35e12016-10-10 16:26:44 +0000613AnalysisDeclContextManager::~AnalysisDeclContextManager() {}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000614
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000615LocationContext::~LocationContext() {}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000616
617LocationContextManager::~LocationContextManager() {
618 clear();
619}
620
621void LocationContextManager::clear() {
622 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000623 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000624 LocationContext *LC = &*I;
625 ++I;
626 delete LC;
627 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000628
Ted Kremenek43d4a892009-12-04 01:28:56 +0000629 Contexts.clear();
630}
631