Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 1 | //== AnalysisDeclContext.cpp - Analysis context for Path Sens analysis -*- C++ -*-// |
Zhongxing Xu | 97ab394 | 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 | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 10 | // This file defines AnalysisDeclContext, a class that manages the analysis context |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 11 | // data for path sensitive analysis. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | 471c8b4 | 2012-07-04 20:19:54 +0000 | [diff] [blame] | 15 | #include "clang/AST/ASTContext.h" |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 16 | #include "clang/AST/Decl.h" |
| 17 | #include "clang/AST/DeclObjC.h" |
Mike Stump | fa6ef18 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclTemplate.h" |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 19 | #include "clang/AST/ParentMap.h" |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 20 | #include "clang/AST/StmtVisitor.h" |
Ted Kremenek | 2cfe28b | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 21 | #include "clang/Analysis/Analyses/LiveVariables.h" |
Tom Care | db34ab7 | 2010-08-23 19:51:57 +0000 | [diff] [blame] | 22 | #include "clang/Analysis/Analyses/PseudoConstantAnalysis.h" |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 23 | #include "clang/Analysis/Analyses/CFGReachabilityAnalysis.h" |
Ted Kremenek | 2cfe28b | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 24 | #include "clang/Analysis/AnalysisContext.h" |
| 25 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 26 | #include "clang/Analysis/CFGStmtMap.h" |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 27 | #include "clang/Analysis/Support/BumpVector.h" |
Argyrios Kyrtzidis | b2c60b0 | 2012-03-01 19:45:56 +0000 | [diff] [blame] | 28 | #include "llvm/Support/SaveAndRestore.h" |
Benjamin Kramer | 9b20a90 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallPtrSet.h" |
Mike Stump | 87a05f1 | 2009-07-31 01:10:29 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 32 | #include "BodyFarm.h" |
| 33 | |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 36 | typedef llvm::DenseMap<const void *, ManagedAnalysis *> ManagedAnalysisMap; |
| 37 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 38 | AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 39 | const Decl *d, |
| 40 | const CFG::BuildOptions &buildOptions) |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 41 | : Manager(Mgr), |
| 42 | D(d), |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 43 | cfgBuildOptions(buildOptions), |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 44 | forcedBlkExprs(0), |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 45 | builtCFG(false), |
| 46 | builtCompleteCFG(false), |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 47 | ReferencedBlockVars(0), |
| 48 | ManagedAnalyses(0) |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 49 | { |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 50 | cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs; |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 53 | AnalysisDeclContext::AnalysisDeclContext(AnalysisDeclContextManager *Mgr, |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 54 | const Decl *d) |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 55 | : Manager(Mgr), |
| 56 | D(d), |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 57 | forcedBlkExprs(0), |
| 58 | builtCFG(false), |
| 59 | builtCompleteCFG(false), |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 60 | ReferencedBlockVars(0), |
| 61 | ManagedAnalyses(0) |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 62 | { |
| 63 | cfgBuildOptions.forcedBlkExprs = &forcedBlkExprs; |
| 64 | } |
| 65 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 66 | AnalysisDeclContextManager::AnalysisDeclContextManager(bool useUnoptimizedCFG, |
Jordan Rose | 5a1ffe9 | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 67 | bool addImplicitDtors, |
| 68 | bool addInitializers, |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 69 | bool addTemporaryDtors, |
| 70 | bool synthesizeBodies) |
| 71 | : SynthesizeBodies(synthesizeBodies) |
| 72 | { |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 73 | cfgBuildOptions.PruneTriviallyFalseEdges = !useUnoptimizedCFG; |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 74 | cfgBuildOptions.AddImplicitDtors = addImplicitDtors; |
| 75 | cfgBuildOptions.AddInitializers = addInitializers; |
Jordan Rose | 5a1ffe9 | 2012-09-05 22:55:23 +0000 | [diff] [blame] | 76 | cfgBuildOptions.AddTemporaryDtors = addTemporaryDtors; |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 79 | void AnalysisDeclContextManager::clear() { |
Ted Kremenek | 58f5ec7 | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 80 | for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I) |
| 81 | delete I->second; |
| 82 | Contexts.clear(); |
| 83 | } |
| 84 | |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 85 | static BodyFarm &getBodyFarm(ASTContext &C) { |
| 86 | static BodyFarm *BF = new BodyFarm(C); |
| 87 | return *BF; |
| 88 | } |
| 89 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 90 | Stmt *AnalysisDeclContext::getBody() const { |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 91 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 92 | Stmt *Body = FD->getBody(); |
| 93 | if (!Body && Manager && Manager->synthesizeBodies()) |
| 94 | return getBodyFarm(getASTContext()).getBody(FD); |
| 95 | return Body; |
| 96 | } |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 97 | else if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 98 | return MD->getBody(); |
Ted Kremenek | 30a4534 | 2009-12-04 20:34:55 +0000 | [diff] [blame] | 99 | else if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) |
| 100 | return BD->getBody(); |
Mike Stump | fa6ef18 | 2010-01-13 02:59:54 +0000 | [diff] [blame] | 101 | else if (const FunctionTemplateDecl *FunTmpl |
| 102 | = dyn_cast_or_null<FunctionTemplateDecl>(D)) |
| 103 | return FunTmpl->getTemplatedDecl()->getBody(); |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 104 | |
Jeffrey Yasskin | 9f61aa9 | 2009-12-12 05:05:38 +0000 | [diff] [blame] | 105 | llvm_unreachable("unknown code decl"); |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 108 | const ImplicitParamDecl *AnalysisDeclContext::getSelfDecl() const { |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 109 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) |
| 110 | return MD->getSelfDecl(); |
Ted Kremenek | ccf1bfd | 2011-11-14 19:36:08 +0000 | [diff] [blame] | 111 | if (const BlockDecl *BD = dyn_cast<BlockDecl>(D)) { |
| 112 | // See if 'self' was captured by the block. |
| 113 | for (BlockDecl::capture_const_iterator it = BD->capture_begin(), |
| 114 | et = BD->capture_end(); it != et; ++it) { |
| 115 | const VarDecl *VD = it->getVariable(); |
| 116 | if (VD->getName() == "self") |
| 117 | return dyn_cast<ImplicitParamDecl>(VD); |
| 118 | } |
| 119 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Ted Kremenek | 82cd37c | 2009-08-21 23:25:54 +0000 | [diff] [blame] | 121 | return NULL; |
| 122 | } |
| 123 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 124 | void AnalysisDeclContext::registerForcedBlockExpression(const Stmt *stmt) { |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 125 | if (!forcedBlkExprs) |
| 126 | forcedBlkExprs = new CFG::BuildOptions::ForcedBlkExprs(); |
| 127 | // Default construct an entry for 'stmt'. |
Jordy Rose | ac73ea8 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 128 | if (const Expr *e = dyn_cast<Expr>(stmt)) |
| 129 | stmt = e->IgnoreParens(); |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 130 | (void) (*forcedBlkExprs)[stmt]; |
| 131 | } |
| 132 | |
| 133 | const CFGBlock * |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 134 | AnalysisDeclContext::getBlockForRegisteredExpression(const Stmt *stmt) { |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 135 | assert(forcedBlkExprs); |
Jordy Rose | ac73ea8 | 2011-06-10 08:49:37 +0000 | [diff] [blame] | 136 | if (const Expr *e = dyn_cast<Expr>(stmt)) |
| 137 | stmt = e->IgnoreParens(); |
Ted Kremenek | 0d28d36 | 2011-03-10 03:50:34 +0000 | [diff] [blame] | 138 | CFG::BuildOptions::ForcedBlkExprs::const_iterator itr = |
| 139 | forcedBlkExprs->find(stmt); |
| 140 | assert(itr != forcedBlkExprs->end()); |
| 141 | return itr->second; |
| 142 | } |
| 143 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 144 | CFG *AnalysisDeclContext::getCFG() { |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 145 | if (!cfgBuildOptions.PruneTriviallyFalseEdges) |
Ted Kremenek | 9b823e8 | 2010-08-03 00:09:51 +0000 | [diff] [blame] | 146 | return getUnoptimizedCFG(); |
| 147 | |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 148 | if (!builtCFG) { |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 149 | cfg.reset(CFG::buildCFG(D, getBody(), |
| 150 | &D->getASTContext(), cfgBuildOptions)); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 151 | // Even when the cfg is not successfully built, we don't |
| 152 | // want to try building it again. |
| 153 | builtCFG = true; |
| 154 | } |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 155 | return cfg.get(); |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 158 | CFG *AnalysisDeclContext::getUnoptimizedCFG() { |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 159 | if (!builtCompleteCFG) { |
Ted Kremenek | bc5cb8a | 2011-07-21 05:22:47 +0000 | [diff] [blame] | 160 | SaveAndRestore<bool> NotPrune(cfgBuildOptions.PruneTriviallyFalseEdges, |
| 161 | false); |
| 162 | completeCFG.reset(CFG::buildCFG(D, getBody(), &D->getASTContext(), |
| 163 | cfgBuildOptions)); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 164 | // Even when the cfg is not successfully built, we don't |
| 165 | // want to try building it again. |
| 166 | builtCompleteCFG = true; |
| 167 | } |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 168 | return completeCFG.get(); |
Ted Kremenek | ad5a894 | 2010-08-02 23:46:59 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 171 | CFGStmtMap *AnalysisDeclContext::getCFGStmtMap() { |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 172 | if (cfgStmtMap) |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 173 | return cfgStmtMap.get(); |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 174 | |
| 175 | if (CFG *c = getCFG()) { |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 176 | cfgStmtMap.reset(CFGStmtMap::Build(c, &getParentMap())); |
| 177 | return cfgStmtMap.get(); |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | } |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 183 | CFGReverseBlockReachabilityAnalysis *AnalysisDeclContext::getCFGReachablityAnalysis() { |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 184 | if (CFA) |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 185 | return CFA.get(); |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 187 | if (CFG *c = getCFG()) { |
Ted Kremenek | af13d5b | 2011-03-19 01:00:33 +0000 | [diff] [blame] | 188 | CFA.reset(new CFGReverseBlockReachabilityAnalysis(*c)); |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 189 | return CFA.get(); |
Ted Kremenek | 42461ee | 2011-02-23 01:51:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
Ted Kremenek | 283a358 | 2011-02-23 01:51:53 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | 682060c | 2011-12-22 23:33:52 +0000 | [diff] [blame] | 195 | void AnalysisDeclContext::dumpCFG(bool ShowColors) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 196 | getCFG()->dump(getASTContext().getLangOpts(), ShowColors); |
Anders Carlsson | 04eeba4 | 2011-01-16 22:05:23 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 199 | ParentMap &AnalysisDeclContext::getParentMap() { |
Jordan Rose | 1d3ca25 | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 200 | if (!PM) { |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 201 | PM.reset(new ParentMap(getBody())); |
Jordan Rose | 1d3ca25 | 2012-07-26 20:04:30 +0000 | [diff] [blame] | 202 | if (const CXXConstructorDecl *C = dyn_cast<CXXConstructorDecl>(getDecl())) { |
| 203 | for (CXXConstructorDecl::init_const_iterator I = C->init_begin(), |
| 204 | E = C->init_end(); |
| 205 | I != E; ++I) { |
| 206 | PM->addStmt((*I)->getInit()); |
| 207 | } |
| 208 | } |
| 209 | } |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 210 | return *PM; |
| 211 | } |
| 212 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 213 | PseudoConstantAnalysis *AnalysisDeclContext::getPseudoConstantAnalysis() { |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 214 | if (!PCA) |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 215 | PCA.reset(new PseudoConstantAnalysis(getBody())); |
| 216 | return PCA.get(); |
Tom Care | 245adab | 2010-08-18 21:17:24 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Jordy Rose | d200187 | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 219 | AnalysisDeclContext *AnalysisDeclContextManager::getContext(const Decl *D) { |
Ted Kremenek | a43df95 | 2012-09-21 00:09:11 +0000 | [diff] [blame^] | 220 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 221 | FD->hasBody(FD); |
| 222 | D = FD; |
| 223 | } |
| 224 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 225 | AnalysisDeclContext *&AC = Contexts[D]; |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 226 | if (!AC) |
Jordy Rose | d200187 | 2012-04-28 01:58:08 +0000 | [diff] [blame] | 227 | AC = new AnalysisDeclContext(this, D, cfgBuildOptions); |
Ted Kremenek | 2376002 | 2009-08-21 23:58:43 +0000 | [diff] [blame] | 228 | return AC; |
Zhongxing Xu | 97ab394 | 2009-07-30 01:17:21 +0000 | [diff] [blame] | 229 | } |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 230 | |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 231 | const StackFrameContext * |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 232 | AnalysisDeclContext::getStackFrame(LocationContext const *Parent, const Stmt *S, |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 233 | const CFGBlock *Blk, unsigned Idx) { |
| 234 | return getLocationContextManager().getStackFrame(this, Parent, S, Blk, Idx); |
| 235 | } |
| 236 | |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 237 | const BlockInvocationContext * |
| 238 | AnalysisDeclContext::getBlockInvocationContext(const LocationContext *parent, |
| 239 | const clang::BlockDecl *BD, |
| 240 | const void *ContextData) { |
| 241 | return getLocationContextManager().getBlockInvocationContext(this, parent, |
| 242 | BD, ContextData); |
| 243 | } |
| 244 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 245 | LocationContextManager & AnalysisDeclContext::getLocationContextManager() { |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 246 | assert(Manager && |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 247 | "Cannot create LocationContexts without an AnalysisDeclContextManager!"); |
Ted Kremenek | b1b5daf | 2011-10-23 02:31:52 +0000 | [diff] [blame] | 248 | return Manager->getLocationContextManager(); |
| 249 | } |
| 250 | |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 251 | //===----------------------------------------------------------------------===// |
| 252 | // FoldingSet profiling. |
| 253 | //===----------------------------------------------------------------------===// |
| 254 | |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 255 | void LocationContext::ProfileCommon(llvm::FoldingSetNodeID &ID, |
| 256 | ContextKind ck, |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 257 | AnalysisDeclContext *ctx, |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 258 | const LocationContext *parent, |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 259 | const void *data) { |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 260 | ID.AddInteger(ck); |
| 261 | ID.AddPointer(ctx); |
| 262 | ID.AddPointer(parent); |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 263 | ID.AddPointer(data); |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 266 | void StackFrameContext::Profile(llvm::FoldingSetNodeID &ID) { |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 267 | Profile(ID, getAnalysisDeclContext(), getParent(), CallSite, Block, Index); |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 270 | void ScopeContext::Profile(llvm::FoldingSetNodeID &ID) { |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 271 | Profile(ID, getAnalysisDeclContext(), getParent(), Enter); |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void BlockInvocationContext::Profile(llvm::FoldingSetNodeID &ID) { |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 275 | Profile(ID, getAnalysisDeclContext(), getParent(), BD, ContextData); |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 279 | // LocationContext creation. |
Ted Kremenek | dc0d909 | 2009-12-04 00:50:10 +0000 | [diff] [blame] | 280 | //===----------------------------------------------------------------------===// |
| 281 | |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 282 | template <typename LOC, typename DATA> |
| 283 | const LOC* |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 284 | LocationContextManager::getLocationContext(AnalysisDeclContext *ctx, |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 285 | const LocationContext *parent, |
| 286 | const DATA *d) { |
| 287 | llvm::FoldingSetNodeID ID; |
| 288 | LOC::Profile(ID, ctx, parent, d); |
| 289 | void *InsertPos; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 291 | LOC *L = cast_or_null<LOC>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 293 | if (!L) { |
| 294 | L = new LOC(ctx, parent, d); |
| 295 | Contexts.InsertNode(L, InsertPos); |
| 296 | } |
| 297 | return L; |
Ted Kremenek | 58f5ec7 | 2009-10-20 21:39:41 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 300 | const StackFrameContext* |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 301 | LocationContextManager::getStackFrame(AnalysisDeclContext *ctx, |
Ted Kremenek | 54c809b | 2009-08-21 23:39:58 +0000 | [diff] [blame] | 302 | const LocationContext *parent, |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 303 | const Stmt *s, |
Zhongxing Xu | d706434 | 2010-11-24 13:08:51 +0000 | [diff] [blame] | 304 | const CFGBlock *blk, unsigned idx) { |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 305 | llvm::FoldingSetNodeID ID; |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 306 | StackFrameContext::Profile(ID, ctx, parent, s, blk, idx); |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 307 | void *InsertPos; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 308 | StackFrameContext *L = |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 309 | cast_or_null<StackFrameContext>(Contexts.FindNodeOrInsertPos(ID, InsertPos)); |
| 310 | if (!L) { |
Ted Kremenek | 892697d | 2010-12-16 07:46:53 +0000 | [diff] [blame] | 311 | L = new StackFrameContext(ctx, parent, s, blk, idx); |
Zhongxing Xu | 62d399e | 2009-12-24 03:34:38 +0000 | [diff] [blame] | 312 | Contexts.InsertNode(L, InsertPos); |
| 313 | } |
| 314 | return L; |
Zhongxing Xu | 18c7c06 | 2009-08-03 07:23:22 +0000 | [diff] [blame] | 315 | } |
| 316 | |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 317 | const ScopeContext * |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 318 | LocationContextManager::getScope(AnalysisDeclContext *ctx, |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 319 | const LocationContext *parent, |
| 320 | const Stmt *s) { |
| 321 | return getLocationContext<ScopeContext, Stmt>(ctx, parent, s); |
| 322 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 323 | |
Ted Kremenek | 7fa9b4f | 2012-06-01 20:04:04 +0000 | [diff] [blame] | 324 | const BlockInvocationContext * |
| 325 | LocationContextManager::getBlockInvocationContext(AnalysisDeclContext *ctx, |
| 326 | const LocationContext *parent, |
| 327 | const BlockDecl *BD, |
| 328 | const void *ContextData) { |
| 329 | llvm::FoldingSetNodeID ID; |
| 330 | BlockInvocationContext::Profile(ID, ctx, parent, BD, ContextData); |
| 331 | void *InsertPos; |
| 332 | BlockInvocationContext *L = |
| 333 | cast_or_null<BlockInvocationContext>(Contexts.FindNodeOrInsertPos(ID, |
| 334 | InsertPos)); |
| 335 | if (!L) { |
| 336 | L = new BlockInvocationContext(ctx, parent, BD, ContextData); |
| 337 | Contexts.InsertNode(L, InsertPos); |
| 338 | } |
| 339 | return L; |
| 340 | } |
| 341 | |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 342 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 343 | // LocationContext methods. |
| 344 | //===----------------------------------------------------------------------===// |
| 345 | |
| 346 | const StackFrameContext *LocationContext::getCurrentStackFrame() const { |
| 347 | const LocationContext *LC = this; |
| 348 | while (LC) { |
| 349 | if (const StackFrameContext *SFC = dyn_cast<StackFrameContext>(LC)) |
| 350 | return SFC; |
| 351 | LC = LC->getParent(); |
| 352 | } |
| 353 | return NULL; |
| 354 | } |
| 355 | |
Zhongxing Xu | 8ddf7ce | 2010-02-17 08:45:06 +0000 | [diff] [blame] | 356 | bool LocationContext::isParentOf(const LocationContext *LC) const { |
| 357 | do { |
| 358 | const LocationContext *Parent = LC->getParent(); |
| 359 | if (Parent == this) |
| 360 | return true; |
| 361 | else |
| 362 | LC = Parent; |
| 363 | } while (LC); |
| 364 | |
| 365 | return false; |
| 366 | } |
| 367 | |
Ted Kremenek | 67d1287 | 2009-12-07 22:05:27 +0000 | [diff] [blame] | 368 | //===----------------------------------------------------------------------===// |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 369 | // Lazily generated map to query the external variables referenced by a Block. |
| 370 | //===----------------------------------------------------------------------===// |
| 371 | |
| 372 | namespace { |
| 373 | class FindBlockDeclRefExprsVals : public StmtVisitor<FindBlockDeclRefExprsVals>{ |
| 374 | BumpVector<const VarDecl*> &BEVals; |
| 375 | BumpVectorContext &BC; |
Benjamin Kramer | 9b20a90 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 376 | llvm::SmallPtrSet<const VarDecl*, 4> Visited; |
| 377 | llvm::SmallPtrSet<const DeclContext*, 4> IgnoredContexts; |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 378 | public: |
| 379 | FindBlockDeclRefExprsVals(BumpVector<const VarDecl*> &bevals, |
| 380 | BumpVectorContext &bc) |
| 381 | : BEVals(bevals), BC(bc) {} |
Ted Kremenek | 2cfe28b | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 382 | |
| 383 | bool IsTrackedDecl(const VarDecl *VD) { |
| 384 | const DeclContext *DC = VD->getDeclContext(); |
| 385 | return IgnoredContexts.count(DC) == 0; |
| 386 | } |
| 387 | |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 388 | void VisitStmt(Stmt *S) { |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 389 | for (Stmt::child_range I = S->children(); I; ++I) |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 390 | if (Stmt *child = *I) |
| 391 | Visit(child); |
| 392 | } |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 393 | |
Ted Kremenek | 15ce164 | 2011-12-22 01:30:46 +0000 | [diff] [blame] | 394 | void VisitDeclRefExpr(DeclRefExpr *DR) { |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 395 | // Non-local variables are also directly modified. |
Benjamin Kramer | 9b20a90 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 396 | if (const VarDecl *VD = dyn_cast<VarDecl>(DR->getDecl())) { |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 397 | if (!VD->hasLocalStorage()) { |
Benjamin Kramer | 9b20a90 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 398 | if (Visited.insert(VD)) |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 399 | BEVals.push_back(VD, BC); |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 400 | } else if (DR->refersToEnclosingLocal()) { |
Benjamin Kramer | 9b20a90 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 401 | if (Visited.insert(VD) && IsTrackedDecl(VD)) |
| 402 | BEVals.push_back(VD, BC); |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 403 | } |
Benjamin Kramer | 9b20a90 | 2012-03-10 15:08:09 +0000 | [diff] [blame] | 404 | } |
Ted Kremenek | 8524873 | 2010-02-06 00:30:00 +0000 | [diff] [blame] | 405 | } |
Ted Kremenek | 2cfe28b | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 406 | |
Ted Kremenek | 2cfe28b | 2010-03-10 00:18:11 +0000 | [diff] [blame] | 407 | void VisitBlockExpr(BlockExpr *BR) { |
| 408 | // Blocks containing blocks can transitively capture more variables. |
| 409 | IgnoredContexts.insert(BR->getBlockDecl()); |
| 410 | Visit(BR->getBlockDecl()->getBody()); |
| 411 | } |
Ted Kremenek | 15ce164 | 2011-12-22 01:30:46 +0000 | [diff] [blame] | 412 | |
| 413 | void VisitPseudoObjectExpr(PseudoObjectExpr *PE) { |
| 414 | for (PseudoObjectExpr::semantics_iterator it = PE->semantics_begin(), |
| 415 | et = PE->semantics_end(); it != et; ++it) { |
| 416 | Expr *Semantic = *it; |
| 417 | if (OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Semantic)) |
| 418 | Semantic = OVE->getSourceExpr(); |
| 419 | Visit(Semantic); |
| 420 | } |
| 421 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 422 | }; |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 423 | } // end anonymous namespace |
| 424 | |
| 425 | typedef BumpVector<const VarDecl*> DeclVec; |
| 426 | |
| 427 | static DeclVec* LazyInitializeReferencedDecls(const BlockDecl *BD, |
| 428 | void *&Vec, |
| 429 | llvm::BumpPtrAllocator &A) { |
| 430 | if (Vec) |
| 431 | return (DeclVec*) Vec; |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 432 | |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 433 | BumpVectorContext BC(A); |
| 434 | DeclVec *BV = (DeclVec*) A.Allocate<DeclVec>(); |
| 435 | new (BV) DeclVec(BC, 10); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 436 | |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 437 | // Find the referenced variables. |
| 438 | FindBlockDeclRefExprsVals F(*BV, BC); |
| 439 | F.Visit(BD->getBody()); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 440 | |
| 441 | Vec = BV; |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 442 | return BV; |
| 443 | } |
| 444 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 445 | std::pair<AnalysisDeclContext::referenced_decls_iterator, |
| 446 | AnalysisDeclContext::referenced_decls_iterator> |
| 447 | AnalysisDeclContext::getReferencedBlockVars(const BlockDecl *BD) { |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 448 | if (!ReferencedBlockVars) |
| 449 | ReferencedBlockVars = new llvm::DenseMap<const BlockDecl*,void*>(); |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 450 | |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 451 | DeclVec *V = LazyInitializeReferencedDecls(BD, (*ReferencedBlockVars)[BD], A); |
| 452 | return std::make_pair(V->begin(), V->end()); |
| 453 | } |
| 454 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 455 | ManagedAnalysis *&AnalysisDeclContext::getAnalysisImpl(const void *tag) { |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 456 | if (!ManagedAnalyses) |
| 457 | ManagedAnalyses = new ManagedAnalysisMap(); |
| 458 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; |
| 459 | return (*M)[tag]; |
| 460 | } |
| 461 | |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 462 | //===----------------------------------------------------------------------===// |
| 463 | // Cleanup. |
| 464 | //===----------------------------------------------------------------------===// |
| 465 | |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 466 | ManagedAnalysis::~ManagedAnalysis() {} |
| 467 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 468 | AnalysisDeclContext::~AnalysisDeclContext() { |
Ted Kremenek | b8ad5ee | 2011-03-10 01:14:05 +0000 | [diff] [blame] | 469 | delete forcedBlkExprs; |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 470 | delete ReferencedBlockVars; |
Ted Kremenek | a5937bb | 2011-10-07 22:21:02 +0000 | [diff] [blame] | 471 | // Release the managed analyses. |
| 472 | if (ManagedAnalyses) { |
| 473 | ManagedAnalysisMap *M = (ManagedAnalysisMap*) ManagedAnalyses; |
| 474 | for (ManagedAnalysisMap::iterator I = M->begin(), E = M->end(); I!=E; ++I) |
| 475 | delete I->second; |
| 476 | delete M; |
| 477 | } |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Ted Kremenek | 1d26f48 | 2011-10-24 01:32:45 +0000 | [diff] [blame] | 480 | AnalysisDeclContextManager::~AnalysisDeclContextManager() { |
Ted Kremenek | b1a7b65 | 2009-11-26 02:31:33 +0000 | [diff] [blame] | 481 | for (ContextMap::iterator I = Contexts.begin(), E = Contexts.end(); I!=E; ++I) |
| 482 | delete I->second; |
| 483 | } |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 484 | |
| 485 | LocationContext::~LocationContext() {} |
| 486 | |
| 487 | LocationContextManager::~LocationContextManager() { |
| 488 | clear(); |
| 489 | } |
| 490 | |
| 491 | void LocationContextManager::clear() { |
| 492 | for (llvm::FoldingSet<LocationContext>::iterator I = Contexts.begin(), |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 493 | E = Contexts.end(); I != E; ) { |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 494 | LocationContext *LC = &*I; |
| 495 | ++I; |
| 496 | delete LC; |
| 497 | } |
Ted Kremenek | d064fdc | 2010-03-23 00:13:23 +0000 | [diff] [blame] | 498 | |
Ted Kremenek | 0ee4124 | 2009-12-04 01:28:56 +0000 | [diff] [blame] | 499 | Contexts.clear(); |
| 500 | } |
| 501 | |