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