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