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