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