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