| 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 |  | 
| Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/AnalysisContext.h" | 
|  | 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), | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 44 | forcedBlkExprs(0), | 
| Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 45 | builtCFG(false), | 
|  | 46 | builtCompleteCFG(false), | 
| Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 47 | ReferencedBlockVars(0), | 
|  | 48 | ManagedAnalyses(0) | 
| 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), | 
| Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 57 | forcedBlkExprs(0), | 
|  | 58 | builtCFG(false), | 
|  | 59 | builtCompleteCFG(false), | 
| Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 60 | ReferencedBlockVars(0), | 
|  | 61 | ManagedAnalyses(0) | 
| 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, | 
| Ted Kremenek | 233c1b0 | 2013-03-29 00:09:22 +0000 | [diff] [blame] | 70 | bool synthesizeBodies, | 
| Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 71 | bool addStaticInitBranch, | 
|  | 72 | bool addCXXNewAllocator) | 
| Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 73 | : SynthesizeBodies(synthesizeBodies) | 
|  | 74 | { | 
| Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 75 | cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG; | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 76 | cfgBuildOptions.AddImplicitDtors = addImplicitDtors; | 
|  | 77 | cfgBuildOptions.AddInitializers = addInitializers; | 
| Jordan Rose | 6d671cc | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 78 | cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors; | 
| Ted Kremenek | 233c1b0 | 2013-03-29 00:09:22 +0000 | [diff] [blame] | 79 | cfgBuildOptions.AddStaticInitBranches = addStaticInitBranch; | 
| Jordan Rose | c917607 | 2014-01-13 17:59:19 +0000 | [diff] [blame] | 80 | cfgBuildOptions.AddCXXNewAllocator = addCXXNewAllocator; | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 81 | } | 
|  | 82 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 83 | void AnalysisDeclContextManager::clear() { | 
| Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 84 | llvm::DeleteContainerSeconds(Contexts); | 
| Ted Kremenek | d45ff6c | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 87 | static BodyFarm &getBodyFarm(ASTContext &C) { | 
|  | 88 | static BodyFarm *BF = new BodyFarm(C); | 
|  | 89 | return *BF; | 
|  | 90 | } | 
|  | 91 |  | 
| Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 92 | Stmt *AnalysisDeclContext::getBody(bool &IsAutosynthesized) const { | 
| NAKAMURA Takumi | cc4aaef | 2013-02-04 05:06:21 +0000 | [diff] [blame] | 93 | IsAutosynthesized = false; | 
| Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 94 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | 
|  | 95 | Stmt *Body = FD->getBody(); | 
| Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 96 | if (!Body && Manager && Manager->synthesizeBodies()) { | 
| Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 97 | Body = getBodyFarm(getASTContext()).getBody(FD); | 
|  | 98 | if (Body) | 
|  | 99 | IsAutosynthesized = true; | 
| Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 100 | } | 
| Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 101 | return Body; | 
|  | 102 | } | 
| Jordan Rose | 1a866cd | 2014-01-10 20:06:06 +0000 | [diff] [blame] | 103 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { | 
|  | 104 | Stmt *Body = MD->getBody(); | 
|  | 105 | if (!Body && Manager && Manager->synthesizeBodies()) { | 
|  | 106 | Body = getBodyFarm(getASTContext()).getBody(MD); | 
|  | 107 | if (Body) | 
|  | 108 | IsAutosynthesized = true; | 
|  | 109 | } | 
|  | 110 | return Body; | 
|  | 111 | } else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) | 
| Ted Kremenek | 45805b9 | 2009-12-04 20:34:55 +0000 | [diff] [blame] | 112 | return BD->getBody(); | 
| Mike Stump | 1bacb81 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 113 | else if (const FunctionTemplateDecl *FunTmpl | 
|  | 114 | = dyn_cast_or_null<FunctionTemplateDecl>(D)) | 
|  | 115 | return FunTmpl->getTemplatedDecl()->getBody(); | 
| Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 116 |  | 
| Jeffrey Yasskin | 1615d45 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 117 | llvm_unreachable("unknown code decl"); | 
| Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 118 | } | 
|  | 119 |  | 
| Anna Zaks | 00c69a5 | 2013-02-02 00:30:04 +0000 | [diff] [blame] | 120 | Stmt *AnalysisDeclContext::getBody() const { | 
|  | 121 | bool Tmp; | 
|  | 122 | return getBody(Tmp); | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | bool AnalysisDeclContext::isBodyAutosynthesized() const { | 
|  | 126 | bool Tmp; | 
|  | 127 | getBody(Tmp); | 
|  | 128 | return Tmp; | 
|  | 129 | } | 
|  | 130 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 131 | const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const { | 
| Ted Kremenek | 608677a | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 132 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) | 
|  | 133 | return MD->getSelfDecl(); | 
| Ted Kremenek | b39fcfa | 2011-11-14 19:36:08 +0000 | [diff] [blame] | 134 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { | 
|  | 135 | // See if 'self' was captured by the block. | 
| Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 136 | for (const auto &I : BD->captures()) { | 
|  | 137 | const VarDecl *VD = I.getVariable(); | 
| Ted Kremenek | b39fcfa | 2011-11-14 19:36:08 +0000 | [diff] [blame] | 138 | if (VD->getName() == "self") | 
|  | 139 | return dyn_cast<ImplicitParamDecl>(VD); | 
|  | 140 | } | 
|  | 141 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 |  | 
| Ted Kremenek | 608677a | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 143 | return NULL; | 
|  | 144 | } | 
|  | 145 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 146 | void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) { | 
| Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 147 | if (!forcedBlkExprs) | 
|  | 148 | forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs(); | 
|  | 149 | // Default construct an entry for 'stmt'. | 
| Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 150 | if (const Expr *e = dyn_cast<Expr>(stmt)) | 
|  | 151 | stmt = e->IgnoreParens(); | 
| Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 152 | (void) (*forcedBlkExprs)[stmt]; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | const CFGBlock * | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 156 | AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) { | 
| Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 157 | assert(forcedBlkExprs); | 
| Jordy Rose | 1734737 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 158 | if (const Expr *e = dyn_cast<Expr>(stmt)) | 
|  | 159 | stmt = e->IgnoreParens(); | 
| Ted Kremenek | a099c59 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 160 | CFG::BuildOptions::ForcedBlkExprs::const_iterator itr = | 
|  | 161 | forcedBlkExprs->find(stmt); | 
|  | 162 | assert(itr != forcedBlkExprs->end()); | 
|  | 163 | return itr->second; | 
|  | 164 | } | 
|  | 165 |  | 
| Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 166 | /// Add each synthetic statement in the CFG to the parent map, using the | 
|  | 167 | /// source statement's parent. | 
|  | 168 | static void addParentsForSyntheticStmts(const CFG *TheCFG, ParentMap &PM) { | 
|  | 169 | if (!TheCFG) | 
|  | 170 | return; | 
|  | 171 |  | 
|  | 172 | for (CFG::synthetic_stmt_iterator I = TheCFG->synthetic_stmt_begin(), | 
|  | 173 | E = TheCFG->synthetic_stmt_end(); | 
|  | 174 | I != E; ++I) { | 
|  | 175 | PM.setParent(I->first, PM.getParent(I->second)); | 
|  | 176 | } | 
|  | 177 | } | 
|  | 178 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 179 | CFG *AnalysisDeclContext::getCFG() { | 
| Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 180 | if (!cfgBuildOptions.PruneTriviallyFalseEdges) | 
| Ted Kremenek | 4a2b237 | 2010-08-03 00:09:51 +0000 | [diff] [blame] | 181 | return getUnoptimizedCFG(); | 
|  | 182 |  | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 183 | if (!builtCFG) { | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 184 | cfg.reset(CFG::buildCFG(D, getBody(), | 
|  | 185 | &D->getASTContext(), cfgBuildOptions)); | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 186 | // Even when the cfg is not successfully built, we don't | 
|  | 187 | // want to try building it again. | 
|  | 188 | builtCFG = true; | 
| Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 189 |  | 
|  | 190 | if (PM) | 
|  | 191 | addParentsForSyntheticStmts(cfg.get(), *PM); | 
| Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 192 |  | 
| Richard Trieu | e729d9b | 2014-04-15 01:06:38 +0000 | [diff] [blame] | 193 | // The Observer should only observe one build of the CFG. | 
| Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 194 | getCFGBuildOptions().Observer = 0; | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 195 | } | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 196 | return cfg.get(); | 
| Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 199 | CFG *AnalysisDeclContext::getUnoptimizedCFG() { | 
| Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 200 | if (!builtCompleteCFG) { | 
| Ted Kremenek | 189ecec | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 201 | SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges, | 
|  | 202 | false); | 
|  | 203 | completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(), | 
|  | 204 | cfgBuildOptions)); | 
| Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 205 | // Even when the cfg is not successfully built, we don't | 
|  | 206 | // want to try building it again. | 
|  | 207 | builtCompleteCFG = true; | 
| Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 208 |  | 
|  | 209 | if (PM) | 
|  | 210 | addParentsForSyntheticStmts(completeCFG.get(), *PM); | 
| Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 211 |  | 
| Richard Trieu | e729d9b | 2014-04-15 01:06:38 +0000 | [diff] [blame] | 212 | // The Observer should only observe one build of the CFG. | 
| Richard Trieu | e9fa266 | 2014-04-15 00:57:50 +0000 | [diff] [blame] | 213 | getCFGBuildOptions().Observer = 0; | 
| Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 214 | } | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 215 | return completeCFG.get(); | 
| Ted Kremenek | dc03bd0 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 216 | } | 
|  | 217 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 218 | CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() { | 
| Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 219 | if (cfgStmtMap) | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 220 | return cfgStmtMap.get(); | 
| Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 221 |  | 
|  | 222 | if (CFG *c = getCFG()) { | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 223 | cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap())); | 
|  | 224 | return cfgStmtMap.get(); | 
| Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | return 0; | 
|  | 228 | } | 
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 229 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 230 | CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() { | 
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 231 | if (CFA) | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 232 | return CFA.get(); | 
| Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 233 |  | 
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 234 | if (CFG *c = getCFG()) { | 
| Ted Kremenek | ddc06d0 | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 235 | CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c)); | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 236 | return CFA.get(); | 
| Ted Kremenek | 80861ca | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 237 | } | 
|  | 238 |  | 
|  | 239 | return 0; | 
|  | 240 | } | 
| Ted Kremenek | cc7f1f8 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 241 |  | 
| Ted Kremenek | 72be32a | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 242 | void AnalysisDeclContext::dumpCFG(bool ShowColors) { | 
| David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 243 | getCFG()->dump(getASTContext().getLangOpts(), ShowColors); | 
| Anders Carlsson | 36ecb1f | 2011-01-16 22:05:23 +0000 | [diff] [blame] | 244 | } | 
|  | 245 |  | 
| Ted Kremenek | 35de145 | 2013-05-17 09:41:40 +0000 | [diff] [blame] | 246 | ParentMap &AnalysisDeclContext::getParentMap() { | 
|  | 247 | if (!PM) { | 
| Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 248 | PM.reset(new ParentMap(getBody())); | 
|  | 249 | if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) { | 
| Aaron Ballman | 0ad7830 | 2014-03-13 17:34:31 +0000 | [diff] [blame] | 250 | for (const auto *I : C->inits()) { | 
|  | 251 | PM->addStmt(I->getInit()); | 
| Jordan Rose | 433b0f5 | 2013-05-18 02:26:50 +0000 | [diff] [blame] | 252 | } | 
|  | 253 | } | 
| Jordan Rose | cf10ea8 | 2013-06-06 21:53:45 +0000 | [diff] [blame] | 254 | if (builtCFG) | 
|  | 255 | addParentsForSyntheticStmts(getCFG(), *PM); | 
|  | 256 | if (builtCompleteCFG) | 
|  | 257 | addParentsForSyntheticStmts(getUnoptimizedCFG(), *PM); | 
| Ted Kremenek | 35de145 | 2013-05-17 09:41:40 +0000 | [diff] [blame] | 258 | } | 
| Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 259 | return *PM; | 
|  | 260 | } | 
|  | 261 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 262 | PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() { | 
| Tom Care | b9933f3 | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 263 | if (!PCA) | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 264 | PCA.reset(new PseudoConstantAnalysis(getBody())); | 
|  | 265 | return PCA.get(); | 
| Tom Care | b9933f3 | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Jordy Rose | 4f8198e | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 268 | AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) { | 
| Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 269 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { | 
| Ted Kremenek | 3d0ec38 | 2012-09-24 21:17:14 +0000 | [diff] [blame] | 270 | // Calling 'hasBody' replaces 'FD' in place with the FunctionDecl | 
|  | 271 | // that has the body. | 
| Ted Kremenek | 14f779c | 2012-09-21 00:09:11 +0000 | [diff] [blame] | 272 | FD->hasBody(FD); | 
|  | 273 | D = FD; | 
|  | 274 | } | 
|  | 275 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 276 | AnalysisDeclContext *&AC = Contexts[D]; | 
| Ted Kremenek | cdf5f4a | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 277 | if (!AC) | 
| Jordy Rose | 4f8198e | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 278 | AC = new AnalysisDeclContext(this, D, cfgBuildOptions); | 
| Ted Kremenek | cdf5f4a | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 279 | return AC; | 
| Zhongxing Xu | 14407bf | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 280 | } | 
| Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 281 |  | 
| Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 282 | const StackFrameContext * | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 283 | AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S, | 
| Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 284 | const CFGBlock *Blk, unsigned Idx) { | 
|  | 285 | return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx); | 
|  | 286 | } | 
|  | 287 |  | 
| Ted Kremenek | c3da376 | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 288 | const BlockInvocationContext * | 
|  | 289 | AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent, | 
|  | 290 | const clang::BlockDecl *BD, | 
|  | 291 | const void *ContextData) { | 
|  | 292 | return getLocationContextManager().getBlockInvocationContext(this, parent, | 
|  | 293 | BD, ContextData); | 
|  | 294 | } | 
|  | 295 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 296 | LocationContextManager & AnalysisDeclContext::getLocationContextManager() { | 
| Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 297 | assert(Manager && | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 298 | "Cannot create LocationContexts without an AnalysisDeclContextManager!"); | 
| Ted Kremenek | 142adc4 | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 299 | return Manager->getLocationContextManager(); | 
|  | 300 | } | 
|  | 301 |  | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 302 | //===----------------------------------------------------------------------===// | 
|  | 303 | // FoldingSet profiling. | 
|  | 304 | //===----------------------------------------------------------------------===// | 
|  | 305 |  | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 306 | void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID, | 
|  | 307 | ContextKind ck, | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 308 | AnalysisDeclContext *ctx, | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 309 | const LocationContext *parent, | 
| Ted Kremenek | 5ef32db | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 310 | const void *data) { | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 311 | ID.AddInteger(ck); | 
|  | 312 | ID.AddPointer(ctx); | 
|  | 313 | ID.AddPointer(parent); | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 314 | ID.AddPointer(data); | 
| Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 317 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) { | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 318 | Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index); | 
| Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 319 | } | 
|  | 320 |  | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 321 | void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) { | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 322 | Profile(ID, getAnalysisDeclContext(), getParent(), Enter); | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 323 | } | 
|  | 324 |  | 
|  | 325 | void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) { | 
| Ted Kremenek | c3da376 | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 326 | Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData); | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 327 | } | 
|  | 328 |  | 
|  | 329 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 330 | // LocationContext creation. | 
| Ted Kremenek | 2538824 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 331 | //===----------------------------------------------------------------------===// | 
|  | 332 |  | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 333 | template <typename LOC, typename DATA> | 
|  | 334 | const LOC* | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 335 | LocationContextManager::getLocationContext(AnalysisDeclContext *ctx, | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 336 | const LocationContext *parent, | 
|  | 337 | const DATA *d) { | 
|  | 338 | llvm::FoldingSetNodeID ID; | 
|  | 339 | LOC::Profile(ID, ctx, parent, d); | 
|  | 340 | void *InsertPos; | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 341 |  | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 342 | LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 343 |  | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 344 | if (!L) { | 
|  | 345 | L = new LOC(ctx, parent, d); | 
|  | 346 | Contexts.InsertNode(L, InsertPos); | 
|  | 347 | } | 
|  | 348 | return L; | 
| Ted Kremenek | d45ff6c | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 351 | const StackFrameContext* | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 352 | LocationContextManager::getStackFrame(AnalysisDeclContext *ctx, | 
| Ted Kremenek | 00aeae9 | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 353 | const LocationContext *parent, | 
| Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 354 | const Stmt *s, | 
| Zhongxing Xu | a1a9ba1 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 355 | const CFGBlock *blk, unsigned idx) { | 
| Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 356 | llvm::FoldingSetNodeID ID; | 
| Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 357 | StackFrameContext::Profile(ID, ctx, parent, s, blk, idx); | 
| Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 358 | void *InsertPos; | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 359 | StackFrameContext *L = | 
| Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 360 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); | 
|  | 361 | if (!L) { | 
| Ted Kremenek | 8219b82 | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 362 | L = new StackFrameContext(ctx, parent, s, blk, idx); | 
| Zhongxing Xu | 51f1ca8 | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 363 | Contexts.InsertNode(L, InsertPos); | 
|  | 364 | } | 
|  | 365 | return L; | 
| Zhongxing Xu | 9ad0b46 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 366 | } | 
|  | 367 |  | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 368 | const ScopeContext * | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 369 | LocationContextManager::getScope(AnalysisDeclContext *ctx, | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 370 | const LocationContext *parent, | 
|  | 371 | const Stmt *s) { | 
|  | 372 | return getLocationContext<ScopeContext, Stmt>(ctx, parent, s); | 
|  | 373 | } | 
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 |  | 
| Ted Kremenek | c3da376 | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 375 | const BlockInvocationContext * | 
|  | 376 | LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx, | 
|  | 377 | const LocationContext *parent, | 
|  | 378 | const BlockDecl *BD, | 
|  | 379 | const void *ContextData) { | 
|  | 380 | llvm::FoldingSetNodeID ID; | 
|  | 381 | BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData); | 
|  | 382 | void *InsertPos; | 
|  | 383 | BlockInvocationContext *L = | 
|  | 384 | cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID, | 
|  | 385 | InsertPos)); | 
|  | 386 | if (!L) { | 
|  | 387 | L = new BlockInvocationContext(ctx, parent, BD, ContextData); | 
|  | 388 | Contexts.InsertNode(L, InsertPos); | 
|  | 389 | } | 
|  | 390 | return L; | 
|  | 391 | } | 
|  | 392 |  | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 393 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 04af9f2 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 394 | // LocationContext methods. | 
|  | 395 | //===----------------------------------------------------------------------===// | 
|  | 396 |  | 
|  | 397 | const StackFrameContext *LocationContext::getCurrentStackFrame() const { | 
|  | 398 | const LocationContext *LC = this; | 
|  | 399 | while (LC) { | 
|  | 400 | if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) | 
|  | 401 | return SFC; | 
|  | 402 | LC = LC->getParent(); | 
|  | 403 | } | 
|  | 404 | return NULL; | 
|  | 405 | } | 
|  | 406 |  | 
| Anna Zaks | 44dc91b | 2012-11-03 02:54:16 +0000 | [diff] [blame] | 407 | bool LocationContext::inTopFrame() const { | 
|  | 408 | return getCurrentStackFrame()->inTopFrame(); | 
|  | 409 | } | 
|  | 410 |  | 
| Zhongxing Xu | 86bab2c | 2010-02-17 08:45:06 +0000 | [diff] [blame] | 411 | bool LocationContext::isParentOf(const LocationContext *LC) const { | 
|  | 412 | do { | 
|  | 413 | const LocationContext *Parent = LC->getParent(); | 
|  | 414 | if (Parent == this) | 
|  | 415 | return true; | 
|  | 416 | else | 
|  | 417 | LC = Parent; | 
|  | 418 | } while (LC); | 
|  | 419 |  | 
|  | 420 | return false; | 
|  | 421 | } | 
|  | 422 |  | 
| Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 423 | void LocationContext::dumpStack(raw_ostream &OS, StringRef Indent) const { | 
| Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 424 | ASTContext &Ctx = getAnalysisDeclContext()->getASTContext(); | 
|  | 425 | PrintingPolicy PP(Ctx.getLangOpts()); | 
|  | 426 | PP.TerseOutput = 1; | 
|  | 427 |  | 
|  | 428 | unsigned Frame = 0; | 
|  | 429 | for (const LocationContext *LCtx = this; LCtx; LCtx = LCtx->getParent()) { | 
|  | 430 | switch (LCtx->getKind()) { | 
|  | 431 | case StackFrame: | 
| Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 432 | OS << Indent << '#' << Frame++ << ' '; | 
|  | 433 | cast<StackFrameContext>(LCtx)->getDecl()->print(OS, PP); | 
|  | 434 | OS << '\n'; | 
| Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 435 | break; | 
|  | 436 | case Scope: | 
| Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 437 | OS << Indent << "    (scope)\n"; | 
| Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 438 | break; | 
|  | 439 | case Block: | 
| Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 440 | OS << Indent << "    (block context: " | 
| Jordan Rose | 6fdef11 | 2013-03-30 01:31:35 +0000 | [diff] [blame] | 441 | << cast<BlockInvocationContext>(LCtx)->getContextData() | 
|  | 442 | << ")\n"; | 
|  | 443 | break; | 
|  | 444 | } | 
|  | 445 | } | 
|  | 446 | } | 
|  | 447 |  | 
| Alp Toker | ef6b007 | 2014-01-04 13:47:14 +0000 | [diff] [blame] | 448 | LLVM_DUMP_METHOD void LocationContext::dumpStack() const { | 
| Jordan Rose | e9c5722 | 2013-07-19 00:59:08 +0000 | [diff] [blame] | 449 | dumpStack(llvm::errs()); | 
|  | 450 | } | 
|  | 451 |  | 
| Ted Kremenek | 04af9f2 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 453 | // Lazily generated map to query the external variables referenced by a Block. | 
|  | 454 | //===----------------------------------------------------------------------===// | 
|  | 455 |  | 
|  | 456 | namespace { | 
|  | 457 | class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{ | 
|  | 458 | BumpVector<const VarDecl*> &BEVals; | 
|  | 459 | BumpVectorContext &BC; | 
| Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 460 | llvm::SmallPtrSet<const VarDecl*, 4> Visited; | 
|  | 461 | llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts; | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 462 | public: | 
|  | 463 | FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals, | 
|  | 464 | BumpVectorContext &bc) | 
|  | 465 | : BEVals(bevals), BC(bc) {} | 
| Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 466 |  | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 467 | void VisitStmt(Stmt *S) { | 
| John McCall | 8322c3a | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 468 | for (Stmt::child_range I = S->children(); I; ++I) | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 469 | if (Stmt *child = *I) | 
|  | 470 | Visit(child); | 
|  | 471 | } | 
| Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 472 |  | 
| Ted Kremenek | 299cfb7 | 2011-12-22 01:30:46 +0000 | [diff] [blame] | 473 | void VisitDeclRefExpr(DeclRefExpr *DR) { | 
| Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 474 | // Non-local variables are also directly modified. | 
| Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 475 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { | 
| Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 476 | if (!VD->hasLocalStorage()) { | 
| Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 477 | if (Visited.insert(VD)) | 
| Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 478 | BEVals.push_back(VD, BC); | 
| Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 479 | } | 
| Benjamin Kramer | 616f802 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 480 | } | 
| Ted Kremenek | 5abd69d | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 481 | } | 
| Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 482 |  | 
| Ted Kremenek | 575398e | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 483 | void VisitBlockExpr(BlockExpr *BR) { | 
|  | 484 | // Blocks containing blocks can transitively capture more variables. | 
|  | 485 | IgnoredContexts.insert(BR->getBlockDecl()); | 
|  | 486 | Visit(BR->getBlockDecl()->getBody()); | 
|  | 487 | } | 
| Ted Kremenek | 299cfb7 | 2011-12-22 01:30:46 +0000 | [diff] [blame] | 488 |  | 
|  | 489 | void VisitPseudoObjectExpr(PseudoObjectExpr *PE) { | 
|  | 490 | for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(), | 
|  | 491 | et = PE->semantics_end(); it != et; ++it) { | 
|  | 492 | Expr *Semantic = *it; | 
|  | 493 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic)) | 
|  | 494 | Semantic = OVE->getSourceExpr(); | 
|  | 495 | Visit(Semantic); | 
|  | 496 | } | 
|  | 497 | } | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 498 | }; | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 499 | } // end anonymous namespace | 
|  | 500 |  | 
|  | 501 | typedef BumpVector<const VarDecl*> DeclVec; | 
|  | 502 |  | 
|  | 503 | static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD, | 
|  | 504 | void *&Vec, | 
|  | 505 | llvm::BumpPtrAllocator &A) { | 
|  | 506 | if (Vec) | 
|  | 507 | return (DeclVec*) Vec; | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 508 |  | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 509 | BumpVectorContext BC(A); | 
|  | 510 | DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>(); | 
|  | 511 | new (BV) DeclVec(BC, 10); | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 512 |  | 
| Ted Kremenek | 3e871d8 | 2012-12-06 07:17:26 +0000 | [diff] [blame] | 513 | // Go through the capture list. | 
| Aaron Ballman | 9371dd2 | 2014-03-14 18:34:04 +0000 | [diff] [blame] | 514 | for (const auto &CI : BD->captures()) { | 
|  | 515 | BV->push_back(CI.getVariable(), BC); | 
| Ted Kremenek | 3e871d8 | 2012-12-06 07:17:26 +0000 | [diff] [blame] | 516 | } | 
|  | 517 |  | 
|  | 518 | // Find the referenced global/static variables. | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 519 | FindBlockDeclRefExprsVals F(*BV, BC); | 
|  | 520 | F.Visit(BD->getBody()); | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 521 |  | 
|  | 522 | Vec = BV; | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 523 | return BV; | 
|  | 524 | } | 
|  | 525 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 526 | std::pair<AnalysisDeclContext::referenced_decls_iterator, | 
|  | 527 | AnalysisDeclContext::referenced_decls_iterator> | 
|  | 528 | AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) { | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 529 | if (!ReferencedBlockVars) | 
|  | 530 | ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>(); | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 531 |  | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 532 | DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A); | 
|  | 533 | return std::make_pair(V->begin(), V->end()); | 
|  | 534 | } | 
|  | 535 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 536 | ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) { | 
| Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 537 | if (!ManagedAnalyses) | 
|  | 538 | ManagedAnalyses = new ManagedAnalysisMap(); | 
|  | 539 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; | 
|  | 540 | return (*M)[tag]; | 
|  | 541 | } | 
|  | 542 |  | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 543 | //===----------------------------------------------------------------------===// | 
|  | 544 | // Cleanup. | 
|  | 545 | //===----------------------------------------------------------------------===// | 
|  | 546 |  | 
| Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 547 | ManagedAnalysis::~ManagedAnalysis() {} | 
|  | 548 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 549 | AnalysisDeclContext::~AnalysisDeclContext() { | 
| Ted Kremenek | f9d8290 | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 550 | delete forcedBlkExprs; | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 551 | delete ReferencedBlockVars; | 
| Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 552 | // Release the managed analyses. | 
|  | 553 | if (ManagedAnalyses) { | 
|  | 554 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; | 
| Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 555 | llvm::DeleteContainerSeconds(*M); | 
| Ted Kremenek | dccc2b2 | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 556 | delete M; | 
|  | 557 | } | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Ted Kremenek | 81ce1c8 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 560 | AnalysisDeclContextManager::~AnalysisDeclContextManager() { | 
| Reid Kleckner | 588c937 | 2014-02-19 23:44:52 +0000 | [diff] [blame] | 561 | llvm::DeleteContainerSeconds(Contexts); | 
| Ted Kremenek | 0f5e6f88 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 562 | } | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 563 |  | 
|  | 564 | LocationContext::~LocationContext() {} | 
|  | 565 |  | 
|  | 566 | LocationContextManager::~LocationContextManager() { | 
|  | 567 | clear(); | 
|  | 568 | } | 
|  | 569 |  | 
|  | 570 | void LocationContextManager::clear() { | 
|  | 571 | for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(), | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 572 | E = Contexts.end(); I != E; ) { | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 573 | LocationContext *LC = &*I; | 
|  | 574 | ++I; | 
|  | 575 | delete LC; | 
|  | 576 | } | 
| Ted Kremenek | 0b40532 | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 577 |  | 
| Ted Kremenek | 43d4a89 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 578 | Contexts.clear(); | 
|  | 579 | } | 
|  | 580 |  |