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