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