Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 1 | //= GRState.cpp - Path-Sensitive "State" for tracking values -----*- C++ -*--=// |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 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 | // |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 10 | // This file implements GRState and GRStateManager. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/CFG.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 15 | #include "clang/Checker/PathSensitive/GRStateTrait.h" |
| 16 | #include "clang/Checker/PathSensitive/GRState.h" |
| 17 | #include "clang/Checker/PathSensitive/GRTransferFuncs.h" |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 19 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 22 | // Give the vtable for ConstraintManager somewhere to live. |
Ted Kremenek | 53ba0b6 | 2009-06-24 23:06:47 +0000 | [diff] [blame] | 23 | // FIXME: Move this elsewhere. |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 24 | ConstraintManager::~ConstraintManager() {} |
| 25 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 26 | GRStateManager::~GRStateManager() { |
| 27 | for (std::vector<GRState::Printer*>::iterator I=Printers.begin(), |
| 28 | E=Printers.end(); I!=E; ++I) |
| 29 | delete *I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 31 | for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); |
| 32 | I!=E; ++I) |
| 33 | I->second.second(I->second.first); |
| 34 | } |
| 35 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 36 | const GRState* |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 37 | GRStateManager::RemoveDeadBindings(const GRState* state, Stmt* Loc, |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 38 | const StackFrameContext *LCtx, |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 39 | SymbolReaper& SymReaper) { |
| 40 | |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 41 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 42 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 43 | // tells us are live. We then see what Decls they may reference, and keep |
| 44 | // those around. This code more than likely can be made faster, and the |
| 45 | // frequency of which this method is called should be experimented with |
Ted Kremenek | 9e24049 | 2008-10-04 05:50:14 +0000 | [diff] [blame] | 46 | // for optimum performance. |
| 47 | llvm::SmallVector<const MemRegion*, 10> RegionRoots; |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 48 | GRState NewState = *state; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 49 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 50 | NewState.Env = EnvMgr.RemoveDeadBindings(NewState.Env, Loc, SymReaper, |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 51 | state, RegionRoots); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 52 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 53 | // Clean up the store. |
Zhongxing Xu | 17ddf1c | 2010-03-17 03:35:08 +0000 | [diff] [blame] | 54 | NewState.St = StoreMgr->RemoveDeadBindings(NewState.St, Loc, LCtx, SymReaper, |
Zhongxing Xu | 72119c4 | 2010-02-05 05:34:29 +0000 | [diff] [blame] | 55 | RegionRoots); |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | 2ed14be | 2008-12-05 00:47:52 +0000 | [diff] [blame] | 57 | return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewState), |
Ted Kremenek | 241677a | 2009-01-21 22:26:05 +0000 | [diff] [blame] | 58 | SymReaper); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 59 | } |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 60 | |
Ted Kremenek | 76500d0 | 2009-06-23 20:38:51 +0000 | [diff] [blame] | 61 | const GRState *GRState::unbindLoc(Loc LV) const { |
| 62 | Store OldStore = getStore(); |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 63 | Store NewStore = getStateManager().StoreMgr->Remove(OldStore, LV); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 64 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 65 | if (NewStore == OldStore) |
Ted Kremenek | 76500d0 | 2009-06-23 20:38:51 +0000 | [diff] [blame] | 66 | return this; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | 76500d0 | 2009-06-23 20:38:51 +0000 | [diff] [blame] | 68 | GRState NewSt = *this; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 69 | NewSt.St = NewStore; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | return getStateManager().getPersistentState(NewSt); |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 73 | SVal GRState::getSValAsScalarOrLoc(const MemRegion *R) const { |
Ted Kremenek | 233e913 | 2009-06-24 22:15:30 +0000 | [diff] [blame] | 74 | // We only want to do fetches from regions that we can actually bind |
| 75 | // values. For example, SymbolicRegions of type 'id<...>' cannot |
| 76 | // have direct bindings (but their can be bindings on their subregions). |
| 77 | if (!R->isBoundable()) |
| 78 | return UnknownVal(); |
| 79 | |
| 80 | if (const TypedRegion *TR = dyn_cast<TypedRegion>(R)) { |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 81 | QualType T = TR->getValueType(getStateManager().getContext()); |
Ted Kremenek | 233e913 | 2009-06-24 22:15:30 +0000 | [diff] [blame] | 82 | if (Loc::IsLocType(T) || T->isIntegerType()) |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 83 | return getSVal(R); |
Ted Kremenek | 233e913 | 2009-06-24 22:15:30 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | return UnknownVal(); |
| 87 | } |
| 88 | |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 89 | |
Ted Kremenek | 8e02934 | 2009-08-27 22:17:37 +0000 | [diff] [blame] | 90 | const GRState *GRState::BindExpr(const Stmt* Ex, SVal V, bool Invalidate) const{ |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 91 | Environment NewEnv = getStateManager().EnvMgr.BindExpr(Env, Ex, V, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | Invalidate); |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 93 | if (NewEnv == Env) |
| 94 | return this; |
Ted Kremenek | 6d2c657 | 2009-08-27 22:15:20 +0000 | [diff] [blame] | 95 | |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 96 | GRState NewSt = *this; |
| 97 | NewSt.Env = NewEnv; |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 98 | return getStateManager().getPersistentState(NewSt); |
Ted Kremenek | 4f596c2 | 2009-06-27 00:24:54 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 101 | const GRState* GRStateManager::getInitialState(const LocationContext *InitLoc) { |
Ted Kremenek | 6d2c657 | 2009-08-27 22:15:20 +0000 | [diff] [blame] | 102 | GRState State(this, |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 103 | EnvMgr.getInitialEnvironment(), |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 104 | StoreMgr->getInitialStore(InitLoc), |
| 105 | GDMFactory.GetEmptyMap()); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 106 | |
Zhongxing Xu | 17fd863 | 2009-08-17 06:19:58 +0000 | [diff] [blame] | 107 | return getPersistentState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 110 | const GRState* GRStateManager::getPersistentState(GRState& State) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 112 | llvm::FoldingSetNodeID ID; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 114 | void* InsertPos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 116 | if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 117 | return I; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 119 | GRState* I = (GRState*) Alloc.Allocate<GRState>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | new (I) GRState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 121 | StateSet.InsertNode(I, InsertPos); |
| 122 | return I; |
| 123 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | 67f2853 | 2009-06-17 22:02:04 +0000 | [diff] [blame] | 125 | const GRState* GRState::makeWithStore(Store store) const { |
| 126 | GRState NewSt = *this; |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 127 | NewSt.St = store; |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 128 | return getStateManager().getPersistentState(NewSt); |
Zhongxing Xu | 4193eca | 2008-12-20 06:32:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 131 | //===----------------------------------------------------------------------===// |
| 132 | // State pretty-printing. |
| 133 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 134 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 135 | void GRState::print(llvm::raw_ostream& Out, CFG &C, const char* nl, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | const char* sep) const { |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 137 | // Print the store. |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 138 | GRStateManager &Mgr = getStateManager(); |
| 139 | Mgr.getStoreManager().print(getStore(), Out, nl, sep); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 141 | // Print Subexpression bindings. |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 142 | bool isFirst = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 143 | |
| 144 | for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) { |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 145 | if (C.isBlkExpr(I.getKey())) |
| 146 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 148 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 149 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 150 | isFirst = false; |
| 151 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 152 | else { Out << nl; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 154 | Out << " (" << (void*) I.getKey() << ") "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 155 | LangOptions LO; // FIXME. |
| 156 | I.getKey()->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | 6f9b3a4 | 2009-07-13 23:53:06 +0000 | [diff] [blame] | 157 | Out << " : " << I.getData(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 160 | // Print block-expression bindings. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 161 | isFirst = true; |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 162 | |
| 163 | for (Environment::iterator I = Env.begin(), E = Env.end(); I != E; ++I) { |
| 164 | if (!C.isBlkExpr(I.getKey())) |
| 165 | continue; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 166 | |
| 167 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 168 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 169 | isFirst = false; |
| 170 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 171 | else { Out << nl; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 173 | Out << " (" << (void*) I.getKey() << ") "; |
Chris Lattner | e4f2142 | 2009-06-30 01:26:17 +0000 | [diff] [blame] | 174 | LangOptions LO; // FIXME. |
| 175 | I.getKey()->printPretty(Out, 0, PrintingPolicy(LO)); |
Ted Kremenek | 6f9b3a4 | 2009-07-13 23:53:06 +0000 | [diff] [blame] | 176 | Out << " : " << I.getData(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 179 | Mgr.getConstraintManager().print(this, Out, nl, sep); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 181 | // Print checker-specific data. |
Ted Kremenek | 0fb0bc4 | 2009-08-27 01:39:13 +0000 | [diff] [blame] | 182 | for (std::vector<Printer*>::iterator I = Mgr.Printers.begin(), |
| 183 | E = Mgr.Printers.end(); I != E; ++I) { |
Ted Kremenek | b65be70 | 2009-06-18 01:23:53 +0000 | [diff] [blame] | 184 | (*I)->Print(Out, this, nl, sep); |
| 185 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 186 | } |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 187 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 188 | void GRState::printDOT(llvm::raw_ostream& Out, CFG &C) const { |
| 189 | print(Out, C, "\\l", "\\|"); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Zhongxing Xu | c179a7f | 2010-03-05 04:45:36 +0000 | [diff] [blame] | 192 | void GRState::printStdErr(CFG &C) const { |
| 193 | print(llvm::errs(), C); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 196 | //===----------------------------------------------------------------------===// |
| 197 | // Generic Data Map. |
| 198 | //===----------------------------------------------------------------------===// |
| 199 | |
| 200 | void* const* GRState::FindGDM(void* K) const { |
| 201 | return GDM.lookup(K); |
| 202 | } |
| 203 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 204 | void* |
| 205 | GRStateManager::FindGDMContext(void* K, |
| 206 | void* (*CreateContext)(llvm::BumpPtrAllocator&), |
| 207 | void (*DeleteContext)(void*)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 209 | std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; |
| 210 | if (!p.first) { |
| 211 | p.first = CreateContext(Alloc); |
| 212 | p.second = DeleteContext; |
| 213 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 215 | return p.first; |
| 216 | } |
| 217 | |
Zhongxing Xu | 4230da6 | 2008-11-03 05:18:34 +0000 | [diff] [blame] | 218 | const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){ |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 219 | GRState::GenericDataMap M1 = St->getGDM(); |
| 220 | GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 222 | if (M1 == M2) |
| 223 | return St; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 224 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 225 | GRState NewSt = *St; |
| 226 | NewSt.GDM = M2; |
| 227 | return getPersistentState(NewSt); |
| 228 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 229 | |
Zhongxing Xu | 0541d10 | 2010-03-25 01:39:39 +0000 | [diff] [blame] | 230 | const GRState *GRStateManager::removeGDM(const GRState *state, void *Key) { |
| 231 | GRState::GenericDataMap OldM = state->getGDM(); |
| 232 | GRState::GenericDataMap NewM = GDMFactory.Remove(OldM, Key); |
| 233 | |
| 234 | if (NewM == OldM) |
| 235 | return state; |
| 236 | |
| 237 | GRState NewState = *state; |
| 238 | NewState.GDM = NewM; |
| 239 | return getPersistentState(NewState); |
| 240 | } |
| 241 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 242 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 243 | // Utility. |
| 244 | //===----------------------------------------------------------------------===// |
| 245 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 246 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 247 | class ScanReachableSymbols : public SubRegionMap::Visitor { |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 248 | typedef llvm::DenseSet<const MemRegion*> VisitedRegionsTy; |
| 249 | |
| 250 | VisitedRegionsTy visited; |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 251 | const GRState *state; |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 252 | SymbolVisitor &visitor; |
| 253 | llvm::OwningPtr<SubRegionMap> SRM; |
| 254 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 255 | |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 256 | ScanReachableSymbols(const GRState *st, SymbolVisitor& v) |
| 257 | : state(st), visitor(v) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 258 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 259 | bool scan(nonloc::CompoundVal val); |
| 260 | bool scan(SVal val); |
| 261 | bool scan(const MemRegion *R); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 262 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 263 | // From SubRegionMap::Visitor. |
| 264 | bool Visit(const MemRegion* Parent, const MemRegion* SubRegion) { |
| 265 | return scan(SubRegion); |
| 266 | } |
| 267 | }; |
| 268 | } |
| 269 | |
| 270 | bool ScanReachableSymbols::scan(nonloc::CompoundVal val) { |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 271 | for (nonloc::CompoundVal::iterator I=val.begin(), E=val.end(); I!=E; ++I) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 272 | if (!scan(*I)) |
| 273 | return false; |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 274 | |
| 275 | return true; |
| 276 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 277 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 278 | bool ScanReachableSymbols::scan(SVal val) { |
| 279 | if (loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&val)) |
| 280 | return scan(X->getRegion()); |
Ted Kremenek | 380022d | 2009-03-30 18:45:36 +0000 | [diff] [blame] | 281 | |
Zhongxing Xu | 951b334 | 2010-01-11 07:40:00 +0000 | [diff] [blame] | 282 | if (nonloc::LocAsInteger *X = dyn_cast<nonloc::LocAsInteger>(&val)) |
| 283 | return scan(X->getLoc()); |
| 284 | |
Ted Kremenek | 380022d | 2009-03-30 18:45:36 +0000 | [diff] [blame] | 285 | if (SymbolRef Sym = val.getAsSymbol()) |
| 286 | return visitor.VisitSymbol(Sym); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 288 | if (nonloc::CompoundVal *X = dyn_cast<nonloc::CompoundVal>(&val)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 289 | return scan(*X); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 291 | return true; |
| 292 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 293 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 294 | bool ScanReachableSymbols::scan(const MemRegion *R) { |
Ted Kremenek | 1cb151e | 2009-03-04 00:13:10 +0000 | [diff] [blame] | 295 | if (isa<MemSpaceRegion>(R) || visited.count(R)) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 296 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 297 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 298 | visited.insert(R); |
| 299 | |
| 300 | // If this is a symbolic region, visit the symbol for the region. |
| 301 | if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R)) |
| 302 | if (!visitor.VisitSymbol(SR->getSymbol())) |
| 303 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 304 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 305 | // If this is a subregion, also visit the parent regions. |
| 306 | if (const SubRegion *SR = dyn_cast<SubRegion>(R)) |
Ted Kremenek | 6076e0a | 2009-03-03 18:15:30 +0000 | [diff] [blame] | 307 | if (!scan(SR->getSuperRegion())) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 308 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 310 | // Now look at the binding to this region (if any). |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 311 | if (!scan(state->getSValAsScalarOrLoc(R))) |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 312 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 313 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 314 | // Now look at the subregions. |
| 315 | if (!SRM.get()) |
Zhongxing Xu | f5416bd | 2010-02-05 05:18:47 +0000 | [diff] [blame] | 316 | SRM.reset(state->getStateManager().getStoreManager(). |
| 317 | getSubRegionMap(state->getStore())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 318 | |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 319 | return SRM->iterSubRegions(R, *this); |
| 320 | } |
| 321 | |
Ted Kremenek | 47fed90 | 2009-06-18 01:33:24 +0000 | [diff] [blame] | 322 | bool GRState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const { |
| 323 | ScanReachableSymbols S(this, visitor); |
Ted Kremenek | 5dc2746 | 2009-03-03 02:51:43 +0000 | [diff] [blame] | 324 | return S.scan(val); |
| 325 | } |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 326 | |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 327 | bool GRState::scanReachableSymbols(const SVal *I, const SVal *E, |
| 328 | SymbolVisitor &visitor) const { |
| 329 | ScanReachableSymbols S(this, visitor); |
| 330 | for ( ; I != E; ++I) { |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 331 | if (!S.scan(*I)) |
| 332 | return false; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 333 | } |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 334 | return true; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | bool GRState::scanReachableSymbols(const MemRegion * const *I, |
| 338 | const MemRegion * const *E, |
| 339 | SymbolVisitor &visitor) const { |
| 340 | ScanReachableSymbols S(this, visitor); |
| 341 | for ( ; I != E; ++I) { |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 342 | if (!S.scan(*I)) |
| 343 | return false; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 344 | } |
Ted Kremenek | 43f19f7 | 2009-12-01 17:50:25 +0000 | [diff] [blame] | 345 | return true; |
Ted Kremenek | 9dce71f | 2009-11-26 02:32:19 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Ted Kremenek | 5216ad7 | 2009-02-14 03:16:10 +0000 | [diff] [blame] | 348 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 349 | // Queries. |
| 350 | //===----------------------------------------------------------------------===// |
| 351 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 352 | bool GRStateManager::isEqual(const GRState* state, const Expr* Ex, |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 353 | const llvm::APSInt& Y) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 354 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 355 | SVal V = state->getSVal(Ex); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 357 | if (loc::ConcreteInt* X = dyn_cast<loc::ConcreteInt>(&V)) |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 358 | return X->getValue() == Y; |
| 359 | |
Zhongxing Xu | 1c96b24 | 2008-10-17 05:57:07 +0000 | [diff] [blame] | 360 | if (nonloc::ConcreteInt* X = dyn_cast<nonloc::ConcreteInt>(&V)) |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 361 | return X->getValue() == Y; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 362 | |
Ted Kremenek | 380022d | 2009-03-30 18:45:36 +0000 | [diff] [blame] | 363 | if (SymbolRef Sym = V.getAsSymbol()) |
| 364 | return ConstraintMgr->isEqual(state, Sym, Y); |
| 365 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 366 | return false; |
| 367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 368 | |
Ted Kremenek | 5f85e17 | 2009-07-22 22:35:28 +0000 | [diff] [blame] | 369 | bool GRStateManager::isEqual(const GRState* state, const Expr* Ex, uint64_t x) { |
Ted Kremenek | 044b6f0 | 2009-04-09 16:13:17 +0000 | [diff] [blame] | 370 | return isEqual(state, Ex, getBasicVals().getValue(x, Ex->getType())); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 371 | } |