Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 1 | //== AnalysisDeclContext.cpp - Analysis context for Path Sens analysis -*- C++ -*-// |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 2 | // |
| 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 Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 10 | // This file defines AnalysisDeclContext, a class that manages the analysis context |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 11 | // data for path sensitive analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
George Karpenkov | 50657f6 | 2017-09-06 21:45:03 +0000 | [diff] [blame^] | 15 | #include "clang/Analysis/AnalysisDeclContext.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "BodyFarm.h" |
Benjamin Kramer | 4ab984e | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 18 | #include "clang/AST/Decl.h" |
| 19 | #include "clang/AST/DeclObjC.h" |
Mike Stump | 1bacb81 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 21 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Tom Care | e332c3b | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 25 | #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h" |
Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 26 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/CFGStmtMap.h" |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 28 | #include "clang/Analysis/Support/BumpVector.h" |
Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" |
Mike Stump | 5b78af9 | 2009-07-31 01:10:29 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "llvm/Support/SaveAndRestore.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 32 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 33 | |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 36 | typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap; |
| 37 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 38 | AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 39 | const Decl *d, |
| 40 | const CFG::BuildOptions &buildOptions) |
Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 41 | : Manager(Mgr), |
| 42 | D(d), |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 43 | cfgBuildOptions(buildOptions), |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 44 | forcedBlkExprs(nullptr), |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 45 | builtCFG(false), |
| 46 | builtCompleteCFG(false), |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 47 | ReferencedBlockVars(nullptr), |
| 48 | ManagedAnalyses(nullptr) |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 49 | { |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 50 | cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs; |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 53 | AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 54 | const Decl *d) |
Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 55 | : Manager(Mgr), |
| 56 | D(d), |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 57 | forcedBlkExprs(nullptr), |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 58 | builtCFG(false), |
| 59 | builtCompleteCFG(false), |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 60 | ReferencedBlockVars(nullptr), |
| 61 | ManagedAnalyses(nullptr) |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 62 | { |
| 63 | cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs; |
| 64 | } |
| 65 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 66 | AnalysisDeclContextManager::AnalysisDeclContextManager(bool useUnoptimizedCFG, |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 67 | bool addImplicitDtors, |
| 68 | bool addInitializers, |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 69 | bool addTemporaryDtors, |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 70 | bool addLifetime, |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 71 | bool addLoopExit, |
Ted Kremenek | 233c1b0 | 2013-03-29 00:09:22 +0000 | [diff] [blame] | 72 | bool synthesizeBodies, |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 73 | bool addStaticInitBranch, |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 74 | bool addCXXNewAllocator, |
| 75 | CodeInjector *injector) |
| 76 | : Injector(injector), SynthesizeBodies(synthesizeBodies) |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 77 | { |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 78 | cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG; |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 79 | cfgBuildOptions.AddImplicitDtors = addImplicitDtors; |
| 80 | cfgBuildOptions.AddInitializers = addInitializers; |
Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 81 | cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors; |
Matthias Gehre | 351c218 | 2017-07-12 07:04:19 +0000 | [diff] [blame] | 82 | cfgBuildOptions.AddLifetime = addLifetime; |
Peter Szecsi | 999a25f | 2017-08-19 11:19:16 +0000 | [diff] [blame] | 83 | cfgBuildOptions.AddLoopExit = addLoopExit; |
Ted Kremenek | 233c1b0 | 2013-03-29 00:09:22 +0000 | [diff] [blame] | 84 | cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch; |
Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 85 | cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator; |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Justin Lebar | 5cb35e1 | 2016-10-10 16:26:44 +0000 | [diff] [blame] | 88 | void AnalysisDeclContextManager::clear() { Contexts.clear(); } |
Ted Kremenek | d45ff6c | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 90 | static BodyFarm &getBodyFarm(ASTContext &C, CodeInjector *injector = nullptr) { |
| 91 | static BodyFarm *BF = new BodyFarm(C, injector); |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 92 | return *BF; |
| 93 | } |
| 94 | |
Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 95 | Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const { |
NAKAMURA Takumi | cc4aaef | 2013-02-04 05:06:21 +0000 | [diff] [blame] | 96 | IsAutosynthesized = false; |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 97 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 98 | Stmt *Body = FD->getBody(); |
Eric Fiselier | da8f9b5 | 2017-05-25 02:16:53 +0000 | [diff] [blame] | 99 | if (auto *CoroBody = dyn_cast_or_null<CoroutineBodyStmt>(Body)) |
| 100 | Body = CoroBody->getBody(); |
Devin Coughlin | 7646ebe | 2016-03-28 23:55:58 +0000 | [diff] [blame] | 101 | if (Manager && Manager->synthesizeBodies()) { |
| 102 | Stmt *SynthesizedBody = |
| 103 | getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(FD); |
| 104 | if (SynthesizedBody) { |
| 105 | Body = SynthesizedBody; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 106 | IsAutosynthesized = true; |
Devin Coughlin | 7646ebe | 2016-03-28 23:55:58 +0000 | [diff] [blame] | 107 | } |
Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 108 | } |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 109 | return Body; |
| 110 | } |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 111 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 112 | Stmt *Body = MD->getBody(); |
Devin Coughlin | 7646ebe | 2016-03-28 23:55:58 +0000 | [diff] [blame] | 113 | if (Manager && Manager->synthesizeBodies()) { |
| 114 | Stmt *SynthesizedBody = |
| 115 | getBodyFarm(getASTContext(), Manager->Injector.get()).getBody(MD); |
| 116 | if (SynthesizedBody) { |
| 117 | Body = SynthesizedBody; |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 118 | IsAutosynthesized = true; |
Devin Coughlin | 7646ebe | 2016-03-28 23:55:58 +0000 | [diff] [blame] | 119 | } |
Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 120 | } |
| 121 | return Body; |
| 122 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
Ted Kremenek | 45805b9 | 2009-12-04 20:34:55 +0000 | [diff] [blame] | 123 | return BD->getBody(); |
Mike Stump | 1bacb81 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 124 | else if (const FunctionTemplateDecl *FunTmpl |
| 125 | = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 126 | return FunTmpl->getTemplatedDecl()->getBody(); |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 127 | |
Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 128 | llvm_unreachable("unknown code decl"); |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 131 | Stmt *AnalysisDeclContext::getBody() const { |
| 132 | bool Tmp; |
| 133 | return getBody(Tmp); |
| 134 | } |
| 135 | |
| 136 | bool AnalysisDeclContext::isBodyAutosynthesized() const { |
| 137 | bool Tmp; |
| 138 | getBody(Tmp); |
| 139 | return Tmp; |
| 140 | } |
| 141 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 142 | bool AnalysisDeclContext::isBodyAutosynthesizedFromModelFile() const { |
| 143 | bool Tmp; |
| 144 | Stmt *Body = getBody(Tmp); |
| 145 | return Tmp && Body->getLocStart().isValid(); |
| 146 | } |
| 147 | |
Devin Coughlin | c289b74 | 2016-02-23 22:26:04 +0000 | [diff] [blame] | 148 | /// Returns true if \param VD is an Objective-C implicit 'self' parameter. |
| 149 | static bool isSelfDecl(const VarDecl *VD) { |
| 150 | return isa<ImplicitParamDecl>(VD) && VD->getName() == "self"; |
| 151 | } |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 153 | const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const { |
Ted Kremenek | 608677a | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 154 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 155 | return MD->getSelfDecl(); |
Ted Kremenek | b39fcfa | 2011-11-14 19:36:08 +0000 | [diff] [blame] | 156 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 157 | // See if 'self' was captured by the block. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 158 | for (const auto &I : BD->captures()) { |
| 159 | const VarDecl *VD = I.getVariable(); |
Devin Coughlin | c289b74 | 2016-02-23 22:26:04 +0000 | [diff] [blame] | 160 | if (isSelfDecl(VD)) |
Ted Kremenek | b39fcfa | 2011-11-14 19:36:08 +0000 | [diff] [blame] | 161 | return dyn_cast<ImplicitParamDecl>(VD); |
| 162 | } |
| 163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Devin Coughlin | 1d40583 | 2015-11-15 17:48:22 +0000 | [diff] [blame] | 165 | 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 Coughlin | c289b74 | 2016-02-23 22:26:04 +0000 | [diff] [blame] | 178 | if (isSelfDecl(VD)) |
Devin Coughlin | 1d40583 | 2015-11-15 17:48:22 +0000 | [diff] [blame] | 179 | return dyn_cast<ImplicitParamDecl>(VD); |
| 180 | } |
| 181 | |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 182 | return nullptr; |
Ted Kremenek | 608677a | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 185 | void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) { |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 186 | if (!forcedBlkExprs) |
| 187 | forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs(); |
| 188 | // Default construct an entry for 'stmt'. |
Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 189 | if (const Expr *e = dyn_cast<Expr>(stmt)) |
| 190 | stmt = e->IgnoreParens(); |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 191 | (void) (*forcedBlkExprs)[stmt]; |
| 192 | } |
| 193 | |
| 194 | const CFGBlock * |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 195 | AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) { |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 196 | assert(forcedBlkExprs); |
Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 197 | if (const Expr *e = dyn_cast<Expr>(stmt)) |
| 198 | stmt = e->IgnoreParens(); |
Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 199 | CFG::BuildOptions::ForcedBlkExprs::const_iterator itr = |
| 200 | forcedBlkExprs->find(stmt); |
| 201 | assert(itr != forcedBlkExprs->end()); |
| 202 | return itr->second; |
| 203 | } |
| 204 | |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 205 | /// Add each synthetic statement in the CFG to the parent map, using the |
| 206 | /// source statement's parent. |
| 207 | static 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 Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 218 | CFG *AnalysisDeclContext::getCFG() { |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 219 | if (!cfgBuildOptions.PruneTriviallyFalseEdges) |
Ted Kremenek | 4a2b237 | 2010-08-03 00:09:51 +0000 | [diff] [blame] | 220 | return getUnoptimizedCFG(); |
| 221 | |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 222 | if (!builtCFG) { |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 223 | cfg = CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions); |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 224 | // Even when the cfg is not successfully built, we don't |
| 225 | // want to try building it again. |
| 226 | builtCFG = true; |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 227 | |
| 228 | if (PM) |
| 229 | addParentsForSyntheticStmts(cfg.get(), *PM); |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 230 | |
Richard Trieu | e729d9b | 2014-04-15 01:06:38 +0000 | [diff] [blame] | 231 | // The Observer should only observe one build of the CFG. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 232 | getCFGBuildOptions().Observer = nullptr; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 233 | } |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 234 | return cfg.get(); |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 237 | CFG *AnalysisDeclContext::getUnoptimizedCFG() { |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 238 | if (!builtCompleteCFG) { |
Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 239 | SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges, |
| 240 | false); |
David Blaikie | e90195c | 2014-08-29 18:53:26 +0000 | [diff] [blame] | 241 | completeCFG = |
| 242 | CFG::buildCFG(D, getBody(), &D->getASTContext(), cfgBuildOptions); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 243 | // Even when the cfg is not successfully built, we don't |
| 244 | // want to try building it again. |
| 245 | builtCompleteCFG = true; |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 246 | |
| 247 | if (PM) |
| 248 | addParentsForSyntheticStmts(completeCFG.get(), *PM); |
Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 249 | |
Richard Trieu | e729d9b | 2014-04-15 01:06:38 +0000 | [diff] [blame] | 250 | // The Observer should only observe one build of the CFG. |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 251 | getCFGBuildOptions().Observer = nullptr; |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 252 | } |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 253 | return completeCFG.get(); |
Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 256 | CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() { |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 257 | if (cfgStmtMap) |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 258 | return cfgStmtMap.get(); |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 259 | |
| 260 | if (CFG *c = getCFG()) { |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 261 | cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap())); |
| 262 | return cfgStmtMap.get(); |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 263 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 264 | |
| 265 | return nullptr; |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 266 | } |
Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 267 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 268 | CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() { |
Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 269 | if (CFA) |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 270 | return CFA.get(); |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 272 | if (CFG *c = getCFG()) { |
Ted Kremenek | ddc06d0 | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 273 | CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c)); |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 274 | return CFA.get(); |
Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 275 | } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 276 | |
| 277 | return nullptr; |
Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 278 | } |
Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 279 | |
Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 280 | void AnalysisDeclContext::dumpCFG(bool ShowColors) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 281 | getCFG()->dump(getASTContext().getLangOpts(), ShowColors); |
Anders Carlsson | 36ecb1f | 2011-01-16 22:05:23 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Ted Kremenek | 35de145 | 2013-05-17 09:41:40 +0000 | [diff] [blame] | 284 | ParentMap &AnalysisDeclContext::getParentMap() { |
| 285 | if (!PM) { |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 286 | PM.reset(new ParentMap(getBody())); |
| 287 | if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) { |
Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 288 | for (const auto *I : C->inits()) { |
| 289 | PM->addStmt(I->getInit()); |
Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 292 | if (builtCFG) |
| 293 | addParentsForSyntheticStmts(getCFG(), *PM); |
| 294 | if (builtCompleteCFG) |
| 295 | addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM); |
Ted Kremenek | 35de145 | 2013-05-17 09:41:40 +0000 | [diff] [blame] | 296 | } |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 297 | return *PM; |
| 298 | } |
| 299 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 300 | PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() { |
Tom Care | b9933f3 | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 301 | if (!PCA) |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 302 | PCA.reset(new PseudoConstantAnalysis(getBody())); |
| 303 | return PCA.get(); |
Tom Care | b9933f3 | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Jordy Rose | 4f8198e | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 306 | AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) { |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 307 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Ted Kremenek | 3d0ec38 | 2012-09-24 21:17:14 +0000 | [diff] [blame] | 308 | // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl |
| 309 | // that has the body. |
Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 310 | FD->hasBody(FD); |
| 311 | D = FD; |
| 312 | } |
| 313 | |
Justin Lebar | 5cb35e1 | 2016-10-10 16:26:44 +0000 | [diff] [blame] | 314 | std::unique_ptr<AnalysisDeclContext> &AC = Contexts[D]; |
Ted Kremenek | cdf5f4a | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 315 | if (!AC) |
Justin Lebar | 5cb35e1 | 2016-10-10 16:26:44 +0000 | [diff] [blame] | 316 | AC = llvm::make_unique<AnalysisDeclContext>(this, D, cfgBuildOptions); |
| 317 | return AC.get(); |
Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 318 | } |
Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 320 | const StackFrameContext * |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 321 | AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S, |
Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 322 | const CFGBlock *Blk, unsigned Idx) { |
| 323 | return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx); |
| 324 | } |
| 325 | |
Ted Kremenek | c3da376 | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 326 | const BlockInvocationContext * |
| 327 | AnalysisDeclContext::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 Coughlin | 9165df1 | 2016-02-07 16:55:44 +0000 | [diff] [blame] | 334 | bool 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 Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 349 | LocationContextManager & AnalysisDeclContext::getLocationContextManager() { |
Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 350 | assert(Manager && |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 351 | "Cannot create LocationContexts without an AnalysisDeclContextManager!"); |
Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 352 | return Manager->getLocationContextManager(); |
| 353 | } |
| 354 | |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 355 | //===----------------------------------------------------------------------===// |
| 356 | // FoldingSet profiling. |
| 357 | //===----------------------------------------------------------------------===// |
| 358 | |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 359 | void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID, |
| 360 | ContextKind ck, |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 361 | AnalysisDeclContext *ctx, |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 362 | const LocationContext *parent, |
Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 363 | const void *data) { |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 364 | ID.AddInteger(ck); |
| 365 | ID.AddPointer(ctx); |
| 366 | ID.AddPointer(parent); |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 367 | ID.AddPointer(data); |
Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 370 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) { |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 371 | Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index); |
Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 374 | void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) { |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 375 | Profile(ID, getAnalysisDeclContext(), getParent(), Enter); |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) { |
Ted Kremenek | c3da376 | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 379 | Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData); |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 383 | // LocationContext creation. |
Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 384 | //===----------------------------------------------------------------------===// |
| 385 | |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 386 | template <typename LOC, typename DATA> |
| 387 | const LOC* |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 388 | LocationContextManager::getLocationContext(AnalysisDeclContext *ctx, |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 389 | const LocationContext *parent, |
| 390 | const DATA *d) { |
| 391 | llvm::FoldingSetNodeID ID; |
| 392 | LOC::Profile(ID, ctx, parent, d); |
| 393 | void *InsertPos; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 394 | |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 395 | LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 396 | |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 397 | if (!L) { |
| 398 | L = new LOC(ctx, parent, d); |
| 399 | Contexts.InsertNode(L, InsertPos); |
| 400 | } |
| 401 | return L; |
Ted Kremenek | d45ff6c | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 404 | const StackFrameContext* |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 405 | LocationContextManager::getStackFrame(AnalysisDeclContext *ctx, |
Ted Kremenek | 00aeae9 | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 406 | const LocationContext *parent, |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 407 | const Stmt *s, |
Zhongxing Xu | a1a9ba1 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 408 | const CFGBlock *blk, unsigned idx) { |
Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 409 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 410 | StackFrameContext::Profile(ID, ctx, parent, s, blk, idx); |
Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 411 | void *InsertPos; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 412 | StackFrameContext *L = |
Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 413 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 414 | if (!L) { |
Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 415 | L = new StackFrameContext(ctx, parent, s, blk, idx); |
Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 416 | Contexts.InsertNode(L, InsertPos); |
| 417 | } |
| 418 | return L; |
Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 421 | const ScopeContext * |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 422 | LocationContextManager::getScope(AnalysisDeclContext *ctx, |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 423 | const LocationContext *parent, |
| 424 | const Stmt *s) { |
| 425 | return getLocationContext<ScopeContext, Stmt>(ctx, parent, s); |
| 426 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Ted Kremenek | c3da376 | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 428 | const BlockInvocationContext * |
| 429 | LocationContextManager::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 Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 446 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 04af9f2 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 447 | // LocationContext methods. |
| 448 | //===----------------------------------------------------------------------===// |
| 449 | |
| 450 | const 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 Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 457 | return nullptr; |
Ted Kremenek | 04af9f2 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Anna Zaks | 44dc91b | 2012-11-03 02:54:16 +0000 | [diff] [blame] | 460 | bool LocationContext::inTopFrame() const { |
| 461 | return getCurrentStackFrame()->inTopFrame(); |
| 462 | } |
| 463 | |
Zhongxing Xu | 86bab2c | 2010-02-17 08:45:06 +0000 | [diff] [blame] | 464 | bool 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 Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 476 | void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const { |
Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 477 | 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 Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 485 | OS << Indent << '#' << Frame++ << ' '; |
| 486 | cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP); |
| 487 | OS << '\n'; |
Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 488 | break; |
| 489 | case Scope: |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 490 | OS << Indent << " (scope)\n"; |
Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 491 | break; |
| 492 | case Block: |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 493 | OS << Indent << " (block context: " |
Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 494 | << cast<BlockInvocationContext>(LCtx)->getContextData() |
| 495 | << ")\n"; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | |
Alp Toker | ef6b007 | 2014-01-04 13:47:14 +0000 | [diff] [blame] | 501 | LLVM_DUMP_METHOD void LocationContext::dumpStack() const { |
Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 502 | dumpStack(llvm::errs()); |
| 503 | } |
| 504 | |
Ted Kremenek | 04af9f2 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 505 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 506 | // Lazily generated map to query the external variables referenced by a Block. |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | |
| 509 | namespace { |
| 510 | class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{ |
| 511 | BumpVector<const VarDecl*> &BEVals; |
| 512 | BumpVectorContext &BC; |
Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 513 | llvm::SmallPtrSet<const VarDecl*, 4> Visited; |
| 514 | llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts; |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 515 | public: |
| 516 | FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals, |
| 517 | BumpVectorContext &bc) |
| 518 | : BEVals(bevals), BC(bc) {} |
Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 519 | |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 520 | void VisitStmt(Stmt *S) { |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 521 | for (Stmt *Child : S->children()) |
| 522 | if (Child) |
| 523 | Visit(Child); |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 524 | } |
Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 525 | |
Ted Kremenek | 299cfb7 | 2011-12-22 01:30:46 +0000 | [diff] [blame] | 526 | void VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 527 | // Non-local variables are also directly modified. |
Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 528 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 529 | if (!VD->hasLocalStorage()) { |
David Blaikie | 82e95a3 | 2014-11-19 07:49:47 +0000 | [diff] [blame] | 530 | if (Visited.insert(VD).second) |
Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 531 | BEVals.push_back(VD, BC); |
Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 532 | } |
Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 533 | } |
Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 534 | } |
Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 535 | |
Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 536 | 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 Kremenek | 299cfb7 | 2011-12-22 01:30:46 +0000 | [diff] [blame] | 541 | |
| 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 Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 551 | }; |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 552 | } // end anonymous namespace |
| 553 | |
| 554 | typedef BumpVector<const VarDecl*> DeclVec; |
| 555 | |
| 556 | static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD, |
| 557 | void *&Vec, |
| 558 | llvm::BumpPtrAllocator &A) { |
| 559 | if (Vec) |
| 560 | return (DeclVec*) Vec; |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 561 | |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 562 | BumpVectorContext BC(A); |
| 563 | DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>(); |
| 564 | new (BV) DeclVec(BC, 10); |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 565 | |
Ted Kremenek | 3e871d8 | 2012-12-06 07:17:26 +0000 | [diff] [blame] | 566 | // Go through the capture list. |
Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 567 | for (const auto &CI : BD->captures()) { |
| 568 | BV->push_back(CI.getVariable(), BC); |
Ted Kremenek | 3e871d8 | 2012-12-06 07:17:26 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // Find the referenced global/static variables. |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 572 | FindBlockDeclRefExprsVals F(*BV, BC); |
| 573 | F.Visit(BD->getBody()); |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 574 | |
| 575 | Vec = BV; |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 576 | return BV; |
| 577 | } |
| 578 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 579 | llvm::iterator_range<AnalysisDeclContext::referenced_decls_iterator> |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 580 | AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) { |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 581 | if (!ReferencedBlockVars) |
| 582 | ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>(); |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 583 | |
Benjamin Kramer | b4ef668 | 2015-02-06 17:25:10 +0000 | [diff] [blame] | 584 | const DeclVec *V = |
| 585 | LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A); |
| 586 | return llvm::make_range(V->begin(), V->end()); |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 589 | ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) { |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 590 | if (!ManagedAnalyses) |
| 591 | ManagedAnalyses = new ManagedAnalysisMap(); |
| 592 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; |
| 593 | return (*M)[tag]; |
| 594 | } |
| 595 | |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 596 | //===----------------------------------------------------------------------===// |
| 597 | // Cleanup. |
| 598 | //===----------------------------------------------------------------------===// |
| 599 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 600 | ManagedAnalysis::~ManagedAnalysis() {} |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 601 | |
Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 602 | AnalysisDeclContext::~AnalysisDeclContext() { |
Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 603 | delete forcedBlkExprs; |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 604 | delete ReferencedBlockVars; |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 605 | // Release the managed analyses. |
| 606 | if (ManagedAnalyses) { |
| 607 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; |
Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 608 | llvm::DeleteContainerSeconds(*M); |
Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 609 | delete M; |
| 610 | } |
Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Justin Lebar | 5cb35e1 | 2016-10-10 16:26:44 +0000 | [diff] [blame] | 613 | AnalysisDeclContextManager::~AnalysisDeclContextManager() {} |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 614 | |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 615 | LocationContext::~LocationContext() {} |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 616 | |
| 617 | LocationContextManager::~LocationContextManager() { |
| 618 | clear(); |
| 619 | } |
| 620 | |
| 621 | void LocationContextManager::clear() { |
| 622 | for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(), |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 623 | E = Contexts.end(); I != E; ) { |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 624 | LocationContext *LC = &*I; |
| 625 | ++I; |
| 626 | delete LC; |
| 627 | } |
Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 628 | |
Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 629 | Contexts.clear(); |
| 630 | } |
| 631 | |