Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1 | //= ValueState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=// |
Ted Kremenek | 2d8dce3 | 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 | // |
Gabor Greif | 2224fcb | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines SymbolID, ExprBindKey, and ValueState* |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 9f6b161 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/PathSensitive/ValueState.h" |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang; |
| 18 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 19 | bool ValueState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 20 | |
| 21 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 22 | const ConstNotEqTy::data_type* T = ConstNotEq.lookup(sym); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 23 | |
| 24 | // See if V is present in the NE-set. |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 25 | return T ? T->contains(&V) : false; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | const llvm::APSInt* ValueState::getSymVal(SymbolID sym) const { |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 29 | ConstEqTy::data_type* T = ConstEq.lookup(sym); |
| 30 | return T ? *T : NULL; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 33 | ValueState* |
| 34 | ValueStateManager::RemoveDeadBindings(ValueState* St, Stmt* Loc, |
Ted Kremenek | 7487f94 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 35 | const LiveVariables& Liveness, |
| 36 | DeadSymbolsTy& DeadSymbols) { |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 37 | |
| 38 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 39 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 40 | // tells us are live. We then see what Decls they may reference, and keep |
| 41 | // those around. This code more than likely can be made faster, and the |
| 42 | // frequency of which this method is called should be experimented with |
| 43 | // for optimum performance. |
| 44 | |
| 45 | llvm::SmallVector<ValueDecl*, 10> WList; |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 46 | llvm::SmallPtrSet<ValueDecl*, 10> Marked; |
| 47 | llvm::SmallSet<SymbolID, 20> MarkedSymbols; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 49 | ValueState NewSt = *St; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 50 | |
| 51 | // Drop bindings for subexpressions. |
Ted Kremenek | 587ecc5 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 52 | NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 53 | |
| 54 | // Iterate over the block-expr bindings. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 55 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 56 | for (ValueState::beb_iterator I = St->beb_begin(), E = St->beb_end(); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 57 | I!=E ; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 58 | Expr* BlkExpr = I.getKey(); |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 60 | if (Liveness.isLive(Loc, BlkExpr)) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 61 | RVal X = I.getData(); |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 62 | |
| 63 | if (isa<lval::DeclVal>(X)) { |
| 64 | lval::DeclVal LV = cast<lval::DeclVal>(X); |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 65 | WList.push_back(LV.getDecl()); |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 66 | } |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 67 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 68 | for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 69 | SI != SE; ++SI) { |
| 70 | MarkedSymbols.insert(*SI); |
| 71 | } |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 72 | } |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 73 | else { |
| 74 | RVal X = I.getData(); |
| 75 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 76 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 77 | continue; |
| 78 | |
Ted Kremenek | 587ecc5 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 79 | NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, BlkExpr); |
Ted Kremenek | 99ecce7 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 81 | } |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 83 | // Iterate over the variable bindings. |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 84 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 85 | for (ValueState::vb_iterator I = St->vb_begin(), E = St->vb_end(); I!=E ; ++I) |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 86 | if (Liveness.isLive(Loc, I.getKey())) { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 87 | WList.push_back(I.getKey()); |
Ted Kremenek | ef0007f | 2008-02-26 02:15:56 +0000 | [diff] [blame] | 88 | |
| 89 | RVal X = I.getData(); |
| 90 | |
| 91 | for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 92 | SI != SE; ++SI) { |
| 93 | MarkedSymbols.insert(*SI); |
| 94 | } |
| 95 | } |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 97 | // Perform the mark-and-sweep. |
| 98 | |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 99 | while (!WList.empty()) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 100 | |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 101 | ValueDecl* V = WList.back(); |
| 102 | WList.pop_back(); |
| 103 | |
| 104 | if (Marked.count(V)) |
| 105 | continue; |
| 106 | |
| 107 | Marked.insert(V); |
| 108 | |
Ted Kremenek | 7a1f0dc | 2008-04-29 23:58:03 +0000 | [diff] [blame] | 109 | RVal X = GetRVal(St, lval::DeclVal(cast<VarDecl>(V))); |
Ted Kremenek | 3087ac2 | 2008-02-21 19:30:14 +0000 | [diff] [blame] | 110 | |
Ted Kremenek | 7a1f0dc | 2008-04-29 23:58:03 +0000 | [diff] [blame] | 111 | for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 112 | SI != SE; ++SI) { |
| 113 | MarkedSymbols.insert(*SI); |
| 114 | } |
Ted Kremenek | 3087ac2 | 2008-02-21 19:30:14 +0000 | [diff] [blame] | 115 | |
Ted Kremenek | 7a1f0dc | 2008-04-29 23:58:03 +0000 | [diff] [blame] | 116 | if (!isa<lval::DeclVal>(X)) |
| 117 | continue; |
Ted Kremenek | 3087ac2 | 2008-02-21 19:30:14 +0000 | [diff] [blame] | 118 | |
Ted Kremenek | 7a1f0dc | 2008-04-29 23:58:03 +0000 | [diff] [blame] | 119 | const lval::DeclVal& LVD = cast<lval::DeclVal>(X); |
| 120 | WList.push_back(LVD.getDecl()); |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 123 | // Remove dead variable bindings. |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 124 | |
Ted Kremenek | 7487f94 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 125 | DeadSymbols.clear(); |
| 126 | |
Ted Kremenek | ac91ce9 | 2008-04-25 01:25:15 +0000 | [diff] [blame] | 127 | for (ValueState::vb_iterator I = St->vb_begin(), E = St->vb_end(); I!=E ; ++I) |
| 128 | if (!Marked.count(I.getKey())) { |
| 129 | NewSt.VarBindings = Remove(NewSt, I.getKey()); |
| 130 | |
| 131 | RVal X = I.getData(); |
| 132 | |
| 133 | for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 134 | SI != SE; ++SI) |
| 135 | if (!MarkedSymbols.count(*SI)) DeadSymbols.insert(*SI); |
| 136 | } |
| 137 | |
| 138 | // Remove dead symbols. |
| 139 | |
Ted Kremenek | 7487f94 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 140 | for (ValueState::ce_iterator I = St->ce_begin(), E=St->ce_end(); I!=E; ++I) { |
| 141 | |
| 142 | SymbolID sym = I.getKey(); |
| 143 | |
| 144 | if (!MarkedSymbols.count(sym)) { |
| 145 | DeadSymbols.insert(sym); |
| 146 | NewSt.ConstEq = CEFactory.Remove(NewSt.ConstEq, sym); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | for (ValueState::cne_iterator I = St->cne_begin(), E=St->cne_end(); I!=E;++I){ |
| 151 | |
| 152 | SymbolID sym = I.getKey(); |
| 153 | |
| 154 | if (!MarkedSymbols.count(sym)) { |
| 155 | DeadSymbols.insert(sym); |
| 156 | NewSt.ConstNotEq = CNEFactory.Remove(NewSt.ConstNotEq, sym); |
| 157 | } |
| 158 | } |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 159 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 160 | return getPersistentState(NewSt); |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 161 | } |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 162 | |
| 163 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 164 | RVal ValueStateManager::GetRVal(ValueState* St, LVal LV, QualType T) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 165 | |
Ted Kremenek | adec14b | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 166 | if (isa<UnknownVal>(LV)) |
| 167 | return UnknownVal(); |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 168 | |
Ted Kremenek | b31af24 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 169 | assert (!isa<UndefinedVal>(LV)); |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 171 | switch (LV.getSubKind()) { |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 172 | case lval::DeclValKind: { |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 173 | ValueState::VarBindingsTy::data_type* T = |
| 174 | St->VarBindings.lookup(cast<lval::DeclVal>(LV).getDecl()); |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 175 | |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 176 | return T ? *T : UnknownVal(); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 177 | } |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 178 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 179 | // FIXME: We should limit how far a "ContentsOf" will go... |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 180 | |
| 181 | case lval::SymbolValKind: { |
Ted Kremenek | 05724ad | 2008-03-15 22:07:05 +0000 | [diff] [blame] | 182 | |
| 183 | |
| 184 | // FIXME: This is a broken representation of memory, and is prone |
| 185 | // to crashing the analyzer when addresses to symbolic values are |
| 186 | // passed through casts. We need a better representation of symbolic |
| 187 | // memory (or just memory in general); probably we should do this |
| 188 | // as a plugin class (similar to GRTransferFuncs). |
| 189 | |
| 190 | #if 0 |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 191 | const lval::SymbolVal& SV = cast<lval::SymbolVal>(LV); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 192 | assert (T.getTypePtr()); |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 193 | |
Ted Kremenek | 53a8d9e | 2008-02-21 23:17:39 +0000 | [diff] [blame] | 194 | // Punt on "symbolic" function pointers. |
| 195 | if (T->isFunctionType()) |
Ted Kremenek | 05724ad | 2008-03-15 22:07:05 +0000 | [diff] [blame] | 196 | return UnknownVal(); |
| 197 | |
Ted Kremenek | 53a8d9e | 2008-02-21 23:17:39 +0000 | [diff] [blame] | 198 | if (T->isPointerType()) |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 199 | return lval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol())); |
| 200 | else |
| 201 | return nonlval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol())); |
Ted Kremenek | 05724ad | 2008-03-15 22:07:05 +0000 | [diff] [blame] | 202 | #endif |
| 203 | |
| 204 | return UnknownVal(); |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 205 | } |
Ted Kremenek | 3366180 | 2008-05-01 21:31:50 +0000 | [diff] [blame] | 206 | |
| 207 | case lval::ConcreteIntKind: |
| 208 | // Some clients may call GetRVal with such an option simply because |
| 209 | // they are doing a quick scan through their LVals (potentially to |
| 210 | // invalidate their bindings). Just return Undefined. |
| 211 | return UndefinedVal(); |
Ted Kremenek | 80d52d0 | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | c4385b4 | 2008-04-29 23:24:44 +0000 | [diff] [blame] | 213 | case lval::ArrayOffsetKind: |
Ted Kremenek | 465f25a | 2008-04-29 22:17:41 +0000 | [diff] [blame] | 214 | case lval::FieldOffsetKind: |
| 215 | return UnknownVal(); |
| 216 | |
Ted Kremenek | db47c0e | 2008-04-25 01:56:50 +0000 | [diff] [blame] | 217 | case lval::FuncValKind: |
| 218 | return LV; |
| 219 | |
Ted Kremenek | 2d25638 | 2008-04-25 01:45:38 +0000 | [diff] [blame] | 220 | case lval::StringLiteralValKind: |
| 221 | // FIXME: Implement better support for fetching characters from strings. |
| 222 | return UnknownVal(); |
| 223 | |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 224 | default: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 225 | assert (false && "Invalid LVal."); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
| 228 | |
Ted Kremenek | adec14b | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 229 | return UnknownVal(); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Ted Kremenek | 3538bfe | 2008-03-15 22:11:54 +0000 | [diff] [blame] | 232 | ValueState* ValueStateManager::AddNE(ValueState* St, SymbolID sym, |
| 233 | const llvm::APSInt& V) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 234 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 235 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 236 | ValueState::ConstNotEqTy::data_type* T = St->ConstNotEq.lookup(sym); |
| 237 | ValueState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 238 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 239 | // Now add V to the NE set. |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 240 | S = ISetFactory.Add(S, &V); |
| 241 | |
| 242 | // Create a new state with the old binding replaced. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 243 | ValueState NewSt = *St; |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 244 | NewSt.ConstNotEq = CNEFactory.Add(NewSt.ConstNotEq, sym, S); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 245 | |
| 246 | // Get the persistent copy. |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 247 | return getPersistentState(NewSt); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Ted Kremenek | 3538bfe | 2008-03-15 22:11:54 +0000 | [diff] [blame] | 250 | ValueState* ValueStateManager::AddEQ(ValueState* St, SymbolID sym, |
| 251 | const llvm::APSInt& V) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 252 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 253 | // Create a new state with the old binding replaced. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 254 | ValueState NewSt = *St; |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 255 | NewSt.ConstEq = CEFactory.Add(NewSt.ConstEq, sym, &V); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 256 | |
| 257 | // Get the persistent copy. |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 258 | return getPersistentState(NewSt); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ted Kremenek | ad5d5c5 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 261 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 262 | ValueState* ValueStateManager::SetRVal(ValueState* St, LVal LV, RVal V) { |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 263 | |
| 264 | switch (LV.getSubKind()) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 265 | |
Ted Kremenek | 1b63a3b | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 266 | case lval::DeclValKind: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 267 | return V.isUnknown() |
| 268 | ? UnbindVar(St, cast<lval::DeclVal>(LV).getDecl()) |
| 269 | : BindVar(St, cast<lval::DeclVal>(LV).getDecl(), V); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 270 | |
| 271 | default: |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 272 | assert ("SetRVal for given LVal type not yet implemented."); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 273 | return St; |
| 274 | } |
| 275 | } |
| 276 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 277 | void ValueStateManager::BindVar(ValueState& StImpl, VarDecl* D, RVal V) { |
Ted Kremenek | b5175bf | 2008-02-27 06:47:26 +0000 | [diff] [blame] | 278 | StImpl.VarBindings = VBFactory.Add(StImpl.VarBindings, D, V); |
| 279 | } |
| 280 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 281 | ValueState* ValueStateManager::BindVar(ValueState* St, VarDecl* D, RVal V) { |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 283 | // Create a new state with the old binding removed. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 284 | ValueState NewSt = *St; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 285 | NewSt.VarBindings = VBFactory.Add(NewSt.VarBindings, D, V); |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 287 | // Get the persistent copy. |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 288 | return getPersistentState(NewSt); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 289 | } |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 290 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 291 | ValueState* ValueStateManager::UnbindVar(ValueState* St, VarDecl* D) { |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 292 | |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 293 | // Create a new state with the old binding removed. |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 294 | ValueState NewSt = *St; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 295 | NewSt.VarBindings = VBFactory.Remove(NewSt.VarBindings, D); |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 296 | |
| 297 | // Get the persistent copy. |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 298 | return getPersistentState(NewSt); |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Ted Kremenek | a7338b4 | 2008-03-11 06:39:11 +0000 | [diff] [blame] | 301 | void ValueStateManager::Unbind(ValueState& StImpl, LVal LV) { |
| 302 | |
| 303 | if (isa<lval::DeclVal>(LV)) |
| 304 | StImpl.VarBindings = VBFactory.Remove(StImpl.VarBindings, |
| 305 | cast<lval::DeclVal>(LV).getDecl()); |
| 306 | |
| 307 | } |
| 308 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 309 | ValueState* ValueStateManager::getInitialState() { |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 310 | |
| 311 | // Create a state with empty variable bindings. |
Ted Kremenek | 587ecc5 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 312 | ValueState StateImpl(EnvMgr.getInitialEnvironment(), |
| 313 | VBFactory.GetEmptyMap(), |
| 314 | CNEFactory.GetEmptyMap(), |
| 315 | CEFactory.GetEmptyMap()); |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 316 | |
| 317 | return getPersistentState(StateImpl); |
| 318 | } |
| 319 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 320 | ValueState* ValueStateManager::getPersistentState(ValueState& State) { |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 321 | |
| 322 | llvm::FoldingSetNodeID ID; |
| 323 | State.Profile(ID); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 324 | void* InsertPos; |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 325 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 326 | if (ValueState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 327 | return I; |
| 328 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 329 | ValueState* I = (ValueState*) Alloc.Allocate<ValueState>(); |
| 330 | new (I) ValueState(State); |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 331 | StateSet.InsertNode(I, InsertPos); |
| 332 | return I; |
| 333 | } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 334 | |
Ted Kremenek | d365649 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 335 | void ValueState::printDOT(std::ostream& Out, CheckerStatePrinter* P) const { |
| 336 | print(Out, P, "\\l", "\\|"); |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Ted Kremenek | d365649 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 339 | void ValueState::printStdErr(CheckerStatePrinter* P) const { |
| 340 | print(*llvm::cerr, P); |
| 341 | } |
| 342 | |
| 343 | void ValueState::print(std::ostream& Out, CheckerStatePrinter* P, |
| 344 | const char* nl, const char* sep) const { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 345 | |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 346 | // Print Variable Bindings |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 347 | Out << "Variables:" << nl; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 348 | |
| 349 | bool isFirst = true; |
| 350 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 351 | for (vb_iterator I = vb_begin(), E = vb_end(); I != E; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 352 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 353 | if (isFirst) isFirst = false; |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 354 | else Out << nl; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 355 | |
| 356 | Out << ' ' << I.getKey()->getName() << " : "; |
| 357 | I.getData().print(Out); |
| 358 | } |
| 359 | |
| 360 | // Print Subexpression bindings. |
| 361 | |
| 362 | isFirst = true; |
| 363 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 364 | for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 365 | |
| 366 | if (isFirst) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 367 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 368 | isFirst = false; |
| 369 | } |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 370 | else { Out << nl; } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 371 | |
| 372 | Out << " (" << (void*) I.getKey() << ") "; |
| 373 | I.getKey()->printPretty(Out); |
| 374 | Out << " : "; |
| 375 | I.getData().print(Out); |
| 376 | } |
| 377 | |
| 378 | // Print block-expression bindings. |
| 379 | |
| 380 | isFirst = true; |
| 381 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 382 | for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 383 | |
| 384 | if (isFirst) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 385 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 386 | isFirst = false; |
| 387 | } |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 388 | else { Out << nl; } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 389 | |
| 390 | Out << " (" << (void*) I.getKey() << ") "; |
| 391 | I.getKey()->printPretty(Out); |
| 392 | Out << " : "; |
| 393 | I.getData().print(Out); |
| 394 | } |
| 395 | |
| 396 | // Print equality constraints. |
| 397 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 398 | if (!ConstEq.isEmpty()) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 399 | |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 400 | Out << nl << sep << "'==' constraints:"; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 401 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 402 | for (ConstEqTy::iterator I = ConstEq.begin(), |
| 403 | E = ConstEq.end(); I!=E; ++I) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 404 | |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 405 | Out << nl << " $" << I.getKey() |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 406 | << " : " << I.getData()->toString(); |
| 407 | } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 408 | } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 409 | |
| 410 | // Print != constraints. |
| 411 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 412 | if (!ConstNotEq.isEmpty()) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 413 | |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 414 | Out << nl << sep << "'!=' constraints:"; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 415 | |
Ted Kremenek | f4b49df | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 416 | for (ConstNotEqTy::iterator I = ConstNotEq.begin(), |
| 417 | EI = ConstNotEq.end(); I != EI; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 419 | Out << nl << " $" << I.getKey() << " : "; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 420 | isFirst = true; |
| 421 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 422 | IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 423 | |
| 424 | for ( ; J != EJ; ++J) { |
| 425 | if (isFirst) isFirst = false; |
| 426 | else Out << ", "; |
| 427 | |
| 428 | Out << (*J)->toString(); |
| 429 | } |
| 430 | } |
| 431 | } |
Ted Kremenek | d365649 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 432 | |
| 433 | // Print checker-specific data. |
| 434 | |
| 435 | if (P && CheckerState) |
| 436 | P->PrintCheckerState(Out, CheckerState, nl, sep); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 437 | } |