blob: 94f753ed912809815300969448f7caf3859694f0 [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
Devin Coughlin1d405832015-11-15 17:48:22 +0000151 auto *CXXMethod = dyn_cast<CXXMethodDecl>(D);
152 if (!CXXMethod)
153 return nullptr;
154
155 const CXXRecordDecl *parent = CXXMethod->getParent();
156 if (!parent->isLambda())
157 return nullptr;
158
159 for (const LambdaCapture &LC : parent->captures()) {
160 if (!LC.capturesVariable())
161 continue;
162
163 VarDecl *VD = LC.getCapturedVar();
164 if (VD->getName() == "self")
165 return dyn_cast<ImplicitParamDecl>(VD);
166 }
167
Craig Topper25542942014-05-20 04:30:07 +0000168 return nullptr;
Ted Kremenek608677a2009-08-21 23:25:54 +0000169}
170
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000171void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000172 if (!forcedBlkExprs)
173 forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs();
174 // Default construct an entry for 'stmt'.
Jordy Rose17347372011-06-10 08:49:37 +0000175 if (const Expr *e = dyn_cast<Expr>(stmt))
176 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000177 (void) (*forcedBlkExprs)[stmt];
178}
179
180const CFGBlock *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000181AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) {
Ted Kremeneka099c592011-03-10 03:50:34 +0000182 assert(forcedBlkExprs);
Jordy Rose17347372011-06-10 08:49:37 +0000183 if (const Expr *e = dyn_cast<Expr>(stmt))
184 stmt = e->IgnoreParens();
Ted Kremeneka099c592011-03-10 03:50:34 +0000185 CFG::BuildOptions::ForcedBlkExprs::const_iterator itr =
186 forcedBlkExprs->find(stmt);
187 assert(itr != forcedBlkExprs->end());
188 return itr->second;
189}
190
Jordan Rosecf10ea82013-06-06 21:53:45 +0000191/// Add each synthetic statement in the CFG to the parent map, using the
192/// source statement's parent.
193static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) {
194 if (!TheCFG)
195 return;
196
197 for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(),
198 E = TheCFG->synthetic_stmt_end();
199 I != E; ++I) {
200 PM.setParent(I->first, PM.getParent(I->second));
201 }
202}
203
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000204CFG *AnalysisDeclContext::getCFG() {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000205 if (!cfgBuildOptions.PruneTriviallyFalseEdges)
Ted Kremenek4a2b2372010-08-03 00:09:51 +0000206 return getUnoptimizedCFG();
207
Ted Kremenek0b405322010-03-23 00:13:23 +0000208 if (!builtCFG) {
David Blaikiee90195c2014-08-29 18:53:26 +0000209 cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
Ted Kremenek0b405322010-03-23 00:13:23 +0000210 // Even when the cfg is not successfully built, we don't
211 // want to try building it again.
212 builtCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000213
214 if (PM)
215 addParentsForSyntheticStmts(cfg.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000216
Richard Trieue729d9b2014-04-15 01:06:38 +0000217 // The Observer should only observe one build of the CFG.
Craig Topper25542942014-05-20 04:30:07 +0000218 getCFGBuildOptions().Observer = nullptr;
Ted Kremenek0b405322010-03-23 00:13:23 +0000219 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000220 return cfg.get();
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000221}
222
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000223CFG *AnalysisDeclContext::getUnoptimizedCFG() {
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000224 if (!builtCompleteCFG) {
Ted Kremenek189ecec2011-07-21 05:22:47 +0000225 SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges,
226 false);
David Blaikiee90195c2014-08-29 18:53:26 +0000227 completeCFG =
228 CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions);
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000229 // Even when the cfg is not successfully built, we don't
230 // want to try building it again.
231 builtCompleteCFG = true;
Jordan Rosecf10ea82013-06-06 21:53:45 +0000232
233 if (PM)
234 addParentsForSyntheticStmts(completeCFG.get(), *PM);
Richard Trieue9fa2662014-04-15 00:57:50 +0000235
Richard Trieue729d9b2014-04-15 01:06:38 +0000236 // The Observer should only observe one build of the CFG.
Craig Topper25542942014-05-20 04:30:07 +0000237 getCFGBuildOptions().Observer = nullptr;
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000238 }
Ted Kremenekf9d82902011-03-10 01:14:05 +0000239 return completeCFG.get();
Ted Kremenekdc03bd02010-08-02 23:46:59 +0000240}
241
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000242CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() {
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000243 if (cfgStmtMap)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000244 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000245
246 if (CFG *c = getCFG()) {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000247 cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap()));
248 return cfgStmtMap.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000249 }
Craig Topper25542942014-05-20 04:30:07 +0000250
251 return nullptr;
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000252}
Ted Kremenek80861ca2011-02-23 01:51:59 +0000253
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000254CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() {
Ted Kremenek80861ca2011-02-23 01:51:59 +0000255 if (CFA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000256 return CFA.get();
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000257
Ted Kremenek80861ca2011-02-23 01:51:59 +0000258 if (CFG *c = getCFG()) {
Ted Kremenekddc06d02011-03-19 01:00:33 +0000259 CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c));
Ted Kremenekf9d82902011-03-10 01:14:05 +0000260 return CFA.get();
Ted Kremenek80861ca2011-02-23 01:51:59 +0000261 }
Craig Topper25542942014-05-20 04:30:07 +0000262
263 return nullptr;
Ted Kremenek80861ca2011-02-23 01:51:59 +0000264}
Ted Kremenekcc7f1f82011-02-23 01:51:53 +0000265
Ted Kremenek72be32a2011-12-22 23:33:52 +0000266void AnalysisDeclContext::dumpCFG(bool ShowColors) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000267 getCFG()->dump(getASTContext().getLangOpts(), ShowColors);
Anders Carlsson36ecb1f2011-01-16 22:05:23 +0000268}
269
Ted Kremenek35de1452013-05-17 09:41:40 +0000270ParentMap &AnalysisDeclContext::getParentMap() {
271 if (!PM) {
Jordan Rose433b0f52013-05-18 02:26:50 +0000272 PM.reset(new ParentMap(getBody()));
273 if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) {
Aaron Ballman0ad78302014-03-13 17:34:31 +0000274 for (const auto *I : C->inits()) {
275 PM->addStmt(I->getInit());
Jordan Rose433b0f52013-05-18 02:26:50 +0000276 }
277 }
Jordan Rosecf10ea82013-06-06 21:53:45 +0000278 if (builtCFG)
279 addParentsForSyntheticStmts(getCFG(), *PM);
280 if (builtCompleteCFG)
281 addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM);
Ted Kremenek35de1452013-05-17 09:41:40 +0000282 }
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000283 return *PM;
284}
285
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000286PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() {
Tom Careb9933f32010-08-18 21:17:24 +0000287 if (!PCA)
Ted Kremenekf9d82902011-03-10 01:14:05 +0000288 PCA.reset(new PseudoConstantAnalysis(getBody()));
289 return PCA.get();
Tom Careb9933f32010-08-18 21:17:24 +0000290}
291
Jordy Rose4f8198e2012-04-28 01:58:08 +0000292AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) {
Ted Kremenek14f779c2012-09-21 00:09:11 +0000293 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Ted Kremenek3d0ec382012-09-24 21:17:14 +0000294 // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl
295 // that has the body.
Ted Kremenek14f779c2012-09-21 00:09:11 +0000296 FD->hasBody(FD);
297 D = FD;
298 }
299
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000300 AnalysisDeclContext *&AC = Contexts[D];
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000301 if (!AC)
Jordy Rose4f8198e2012-04-28 01:58:08 +0000302 AC = new AnalysisDeclContext(this, D, cfgBuildOptions);
Ted Kremenekcdf5f4a2009-08-21 23:58:43 +0000303 return AC;
Zhongxing Xu14407bf2009-07-30 01:17:21 +0000304}
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000305
Ted Kremenek142adc42011-10-23 02:31:52 +0000306const StackFrameContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000307AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S,
Ted Kremenek142adc42011-10-23 02:31:52 +0000308 const CFGBlock *Blk, unsigned Idx) {
309 return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx);
310}
311
Ted Kremenekc3da3762012-06-01 20:04:04 +0000312const BlockInvocationContext *
313AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent,
314 const clang::BlockDecl *BD,
315 const void *ContextData) {
316 return getLocationContextManager().getBlockInvocationContext(this, parent,
317 BD, ContextData);
318}
319
Devin Coughlin9165df12016-02-07 16:55:44 +0000320bool AnalysisDeclContext::isInStdNamespace(const Decl *D) {
321 const DeclContext *DC = D->getDeclContext()->getEnclosingNamespaceContext();
322 const NamespaceDecl *ND = dyn_cast<NamespaceDecl>(DC);
323 if (!ND)
324 return false;
325
326 while (const DeclContext *Parent = ND->getParent()) {
327 if (!isa<NamespaceDecl>(Parent))
328 break;
329 ND = cast<NamespaceDecl>(Parent);
330 }
331
332 return ND->isStdNamespace();
333}
334
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000335LocationContextManager & AnalysisDeclContext::getLocationContextManager() {
Ted Kremenek142adc42011-10-23 02:31:52 +0000336 assert(Manager &&
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000337 "Cannot create LocationContexts without an AnalysisDeclContextManager!");
Ted Kremenek142adc42011-10-23 02:31:52 +0000338 return Manager->getLocationContextManager();
339}
340
Ted Kremenek25388242009-12-04 00:50:10 +0000341//===----------------------------------------------------------------------===//
342// FoldingSet profiling.
343//===----------------------------------------------------------------------===//
344
Ted Kremenek25388242009-12-04 00:50:10 +0000345void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID,
346 ContextKind ck,
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000347 AnalysisDeclContext *ctx,
Ted Kremenek25388242009-12-04 00:50:10 +0000348 const LocationContext *parent,
Ted Kremenek5ef32db2011-08-12 23:37:29 +0000349 const void *data) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000350 ID.AddInteger(ck);
351 ID.AddPointer(ctx);
352 ID.AddPointer(parent);
Ted Kremenek25388242009-12-04 00:50:10 +0000353 ID.AddPointer(data);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000354}
355
Ted Kremenek25388242009-12-04 00:50:10 +0000356void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000357 Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index);
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000358}
359
Ted Kremenek25388242009-12-04 00:50:10 +0000360void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000361 Profile(ID, getAnalysisDeclContext(), getParent(), Enter);
Ted Kremenek25388242009-12-04 00:50:10 +0000362}
363
364void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) {
Ted Kremenekc3da3762012-06-01 20:04:04 +0000365 Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData);
Ted Kremenek25388242009-12-04 00:50:10 +0000366}
367
368//===----------------------------------------------------------------------===//
Ted Kremenek43d4a892009-12-04 01:28:56 +0000369// LocationContext creation.
Ted Kremenek25388242009-12-04 00:50:10 +0000370//===----------------------------------------------------------------------===//
371
Ted Kremenek43d4a892009-12-04 01:28:56 +0000372template <typename LOC, typename DATA>
373const LOC*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000374LocationContextManager::getLocationContext(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000375 const LocationContext *parent,
376 const DATA *d) {
377 llvm::FoldingSetNodeID ID;
378 LOC::Profile(ID, ctx, parent, d);
379 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000380
Ted Kremenek43d4a892009-12-04 01:28:56 +0000381 LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
Ted Kremenek0b405322010-03-23 00:13:23 +0000382
Ted Kremenek43d4a892009-12-04 01:28:56 +0000383 if (!L) {
384 L = new LOC(ctx, parent, d);
385 Contexts.InsertNode(L, InsertPos);
386 }
387 return L;
Ted Kremenekd45ff6c2009-10-20 21:39:41 +0000388}
389
Ted Kremenek43d4a892009-12-04 01:28:56 +0000390const StackFrameContext*
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000391LocationContextManager::getStackFrame(AnalysisDeclContext *ctx,
Ted Kremenek00aeae92009-08-21 23:39:58 +0000392 const LocationContext *parent,
Ted Kremenek8219b822010-12-16 07:46:53 +0000393 const Stmt *s,
Zhongxing Xua1a9ba12010-11-24 13:08:51 +0000394 const CFGBlock *blk, unsigned idx) {
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000395 llvm::FoldingSetNodeID ID;
Ted Kremenek8219b822010-12-16 07:46:53 +0000396 StackFrameContext::Profile(ID, ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000397 void *InsertPos;
Ted Kremenek0b405322010-03-23 00:13:23 +0000398 StackFrameContext *L =
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000399 cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos));
400 if (!L) {
Ted Kremenek8219b822010-12-16 07:46:53 +0000401 L = new StackFrameContext(ctx, parent, s, blk, idx);
Zhongxing Xu51f1ca82009-12-24 03:34:38 +0000402 Contexts.InsertNode(L, InsertPos);
403 }
404 return L;
Zhongxing Xu9ad0b462009-08-03 07:23:22 +0000405}
406
Ted Kremenek43d4a892009-12-04 01:28:56 +0000407const ScopeContext *
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000408LocationContextManager::getScope(AnalysisDeclContext *ctx,
Ted Kremenek43d4a892009-12-04 01:28:56 +0000409 const LocationContext *parent,
410 const Stmt *s) {
411 return getLocationContext<ScopeContext, Stmt>(ctx, parent, s);
412}
Mike Stump11289f42009-09-09 15:08:12 +0000413
Ted Kremenekc3da3762012-06-01 20:04:04 +0000414const BlockInvocationContext *
415LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx,
416 const LocationContext *parent,
417 const BlockDecl *BD,
418 const void *ContextData) {
419 llvm::FoldingSetNodeID ID;
420 BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData);
421 void *InsertPos;
422 BlockInvocationContext *L =
423 cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID,
424 InsertPos));
425 if (!L) {
426 L = new BlockInvocationContext(ctx, parent, BD, ContextData);
427 Contexts.InsertNode(L, InsertPos);
428 }
429 return L;
430}
431
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000432//===----------------------------------------------------------------------===//
Ted Kremenek04af9f22009-12-07 22:05:27 +0000433// LocationContext methods.
434//===----------------------------------------------------------------------===//
435
436const StackFrameContext *LocationContext::getCurrentStackFrame() const {
437 const LocationContext *LC = this;
438 while (LC) {
439 if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC))
440 return SFC;
441 LC = LC->getParent();
442 }
Craig Topper25542942014-05-20 04:30:07 +0000443 return nullptr;
Ted Kremenek04af9f22009-12-07 22:05:27 +0000444}
445
Anna Zaks44dc91b2012-11-03 02:54:16 +0000446bool LocationContext::inTopFrame() const {
447 return getCurrentStackFrame()->inTopFrame();
448}
449
Zhongxing Xu86bab2c2010-02-17 08:45:06 +0000450bool LocationContext::isParentOf(const LocationContext *LC) const {
451 do {
452 const LocationContext *Parent = LC->getParent();
453 if (Parent == this)
454 return true;
455 else
456 LC = Parent;
457 } while (LC);
458
459 return false;
460}
461
Jordan Rosee9c57222013-07-19 00:59:08 +0000462void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const {
Jordan Rose6fdef112013-03-30 01:31:35 +0000463 ASTContext &Ctx = getAnalysisDeclContext()->getASTContext();
464 PrintingPolicy PP(Ctx.getLangOpts());
465 PP.TerseOutput = 1;
466
467 unsigned Frame = 0;
468 for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) {
469 switch (LCtx->getKind()) {
470 case StackFrame:
Jordan Rosee9c57222013-07-19 00:59:08 +0000471 OS << Indent << '#' << Frame++ << ' ';
472 cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP);
473 OS << '\n';
Jordan Rose6fdef112013-03-30 01:31:35 +0000474 break;
475 case Scope:
Jordan Rosee9c57222013-07-19 00:59:08 +0000476 OS << Indent << " (scope)\n";
Jordan Rose6fdef112013-03-30 01:31:35 +0000477 break;
478 case Block:
Jordan Rosee9c57222013-07-19 00:59:08 +0000479 OS << Indent << " (block context: "
Jordan Rose6fdef112013-03-30 01:31:35 +0000480 << cast<BlockInvocationContext>(LCtx)->getContextData()
481 << ")\n";
482 break;
483 }
484 }
485}
486
Alp Tokeref6b0072014-01-04 13:47:14 +0000487LLVM_DUMP_METHOD void LocationContext::dumpStack() const {
Jordan Rosee9c57222013-07-19 00:59:08 +0000488 dumpStack(llvm::errs());
489}
490
Ted Kremenek04af9f22009-12-07 22:05:27 +0000491//===----------------------------------------------------------------------===//
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000492// Lazily generated map to query the external variables referenced by a Block.
493//===----------------------------------------------------------------------===//
494
495namespace {
496class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{
497 BumpVector<const VarDecl*> &BEVals;
498 BumpVectorContext &BC;
Benjamin Kramer616f8022012-03-10 15:08:09 +0000499 llvm::SmallPtrSet<const VarDecl*, 4> Visited;
500 llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000501public:
502 FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals,
503 BumpVectorContext &bc)
504 : BEVals(bevals), BC(bc) {}
Ted Kremenek575398e2010-03-10 00:18:11 +0000505
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000506 void VisitStmt(Stmt *S) {
Benjamin Kramer642f1732015-07-02 21:03:14 +0000507 for (Stmt *Child : S->children())
508 if (Child)
509 Visit(Child);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000510 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000511
Ted Kremenek299cfb72011-12-22 01:30:46 +0000512 void VisitDeclRefExpr(DeclRefExpr *DR) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000513 // Non-local variables are also directly modified.
Benjamin Kramer616f8022012-03-10 15:08:09 +0000514 if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) {
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000515 if (!VD->hasLocalStorage()) {
David Blaikie82e95a32014-11-19 07:49:47 +0000516 if (Visited.insert(VD).second)
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000517 BEVals.push_back(VD, BC);
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000518 }
Benjamin Kramer616f8022012-03-10 15:08:09 +0000519 }
Ted Kremenek5abd69d2010-02-06 00:30:00 +0000520 }
Ted Kremenek575398e2010-03-10 00:18:11 +0000521
Ted Kremenek575398e2010-03-10 00:18:11 +0000522 void VisitBlockExpr(BlockExpr *BR) {
523 // Blocks containing blocks can transitively capture more variables.
524 IgnoredContexts.insert(BR->getBlockDecl());
525 Visit(BR->getBlockDecl()->getBody());
526 }
Ted Kremenek299cfb72011-12-22 01:30:46 +0000527
528 void VisitPseudoObjectExpr(PseudoObjectExpr *PE) {
529 for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(),
530 et = PE->semantics_end(); it != et; ++it) {
531 Expr *Semantic = *it;
532 if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic))
533 Semantic = OVE->getSourceExpr();
534 Visit(Semantic);
535 }
536 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000537};
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000538} // end anonymous namespace
539
540typedef BumpVector<const VarDecl*> DeclVec;
541
542static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD,
543 void *&Vec,
544 llvm::BumpPtrAllocator &A) {
545 if (Vec)
546 return (DeclVec*) Vec;
Ted Kremenek0b405322010-03-23 00:13:23 +0000547
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000548 BumpVectorContext BC(A);
549 DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>();
550 new (BV) DeclVec(BC, 10);
Ted Kremenek0b405322010-03-23 00:13:23 +0000551
Ted Kremenek3e871d82012-12-06 07:17:26 +0000552 // Go through the capture list.
Aaron Ballman9371dd22014-03-14 18:34:04 +0000553 for (const auto &CI : BD->captures()) {
554 BV->push_back(CI.getVariable(), BC);
Ted Kremenek3e871d82012-12-06 07:17:26 +0000555 }
556
557 // Find the referenced global/static variables.
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000558 FindBlockDeclRefExprsVals F(*BV, BC);
559 F.Visit(BD->getBody());
Ted Kremenek0b405322010-03-23 00:13:23 +0000560
561 Vec = BV;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000562 return BV;
563}
564
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000565llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator>
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000566AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) {
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000567 if (!ReferencedBlockVars)
568 ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>();
Ted Kremenek0b405322010-03-23 00:13:23 +0000569
Benjamin Kramerb4ef6682015-02-06 17:25:10 +0000570 const DeclVec *V =
571 LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A);
572 return llvm::make_range(V->begin(), V->end());
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000573}
574
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000575ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) {
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000576 if (!ManagedAnalyses)
577 ManagedAnalyses = new ManagedAnalysisMap();
578 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
579 return (*M)[tag];
580}
581
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000582//===----------------------------------------------------------------------===//
583// Cleanup.
584//===----------------------------------------------------------------------===//
585
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000586ManagedAnalysis::~ManagedAnalysis() {}
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000587
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000588AnalysisDeclContext::~AnalysisDeclContext() {
Ted Kremenekf9d82902011-03-10 01:14:05 +0000589 delete forcedBlkExprs;
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000590 delete ReferencedBlockVars;
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000591 // Release the managed analyses.
592 if (ManagedAnalyses) {
593 ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses;
Reid Kleckner588c9372014-02-19 23:44:52 +0000594 llvm::DeleteContainerSeconds(*M);
Ted Kremenekdccc2b22011-10-07 22:21:02 +0000595 delete M;
596 }
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000597}
598
Ted Kremenek81ce1c82011-10-24 01:32:45 +0000599AnalysisDeclContextManager::~AnalysisDeclContextManager() {
Reid Kleckner588c9372014-02-19 23:44:52 +0000600 llvm::DeleteContainerSeconds(Contexts);
Ted Kremenek0f5e6f882009-11-26 02:31:33 +0000601}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000602
Angel Garcia Gomez637d1e62015-10-20 13:23:58 +0000603LocationContext::~LocationContext() {}
Ted Kremenek43d4a892009-12-04 01:28:56 +0000604
605LocationContextManager::~LocationContextManager() {
606 clear();
607}
608
609void LocationContextManager::clear() {
610 for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(),
Ted Kremenek0b405322010-03-23 00:13:23 +0000611 E = Contexts.end(); I != E; ) {
Ted Kremenek43d4a892009-12-04 01:28:56 +0000612 LocationContext *LC = &*I;
613 ++I;
614 delete LC;
615 }
Ted Kremenek0b405322010-03-23 00:13:23 +0000616
Ted Kremenek43d4a892009-12-04 01:28:56 +0000617 Contexts.clear();
618}
619