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