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