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