Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 1 | //= ValueState.cpp - Path-Sens. "State" for tracking valuues -----*- 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 | // |
Ted Kremenek | d70b62e | 2008-02-08 20:29:23 +0000 | [diff] [blame] | 10 | // This files defines SymbolID, ExprBindKey, and ValueState. |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 14 | #include "ValueState.h" |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang; |
| 18 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 19 | bool ValueState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const { |
| 20 | // First, retrieve the NE-set associated with the given symbol. |
| 21 | ConstantNotEqTy::TreeTy* T = Data->ConstantNotEq.SlimFind(sym); |
| 22 | |
| 23 | if (!T) |
| 24 | return false; |
| 25 | |
| 26 | // Second, see if V is present in the NE-set. |
| 27 | return T->getValue().second.contains(&V); |
| 28 | } |
| 29 | |
| 30 | const llvm::APSInt* ValueState::getSymVal(SymbolID sym) const { |
| 31 | ConstantEqTy::TreeTy* T = Data->ConstantEq.SlimFind(sym); |
| 32 | return T ? T->getValue().second : NULL; |
| 33 | } |
| 34 | |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 35 | ValueState |
| 36 | ValueStateManager::RemoveDeadBindings(ValueState St, Stmt* Loc, |
| 37 | const LiveVariables& Liveness) { |
| 38 | |
| 39 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 40 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 41 | // tells us are live. We then see what Decls they may reference, and keep |
| 42 | // those around. This code more than likely can be made faster, and the |
| 43 | // frequency of which this method is called should be experimented with |
| 44 | // for optimum performance. |
| 45 | |
| 46 | llvm::SmallVector<ValueDecl*, 10> WList; |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 47 | llvm::SmallPtrSet<ValueDecl*, 10> Marked; |
| 48 | llvm::SmallSet<SymbolID, 20> MarkedSymbols; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 49 | |
| 50 | ValueStateImpl NewSt = *St; |
| 51 | |
| 52 | // Drop bindings for subexpressions. |
| 53 | NewSt.SubExprBindings = EXFactory.GetEmptyMap(); |
| 54 | |
| 55 | // Iterate over the block-expr bindings. |
| 56 | for (ValueState::beb_iterator I=St.beb_begin(), E=St.beb_end(); I!=E ; ++I) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 57 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 58 | Expr* BlkExpr = I.getKey(); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 59 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 60 | if (Liveness.isLive(Loc, BlkExpr)) { |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 61 | RValue X = I.getData(); |
| 62 | |
| 63 | if (isa<lval::DeclVal>(X)) { |
| 64 | lval::DeclVal LV = cast<lval::DeclVal>(X); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 65 | WList.push_back(LV.getDecl()); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 66 | } |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 67 | |
| 68 | for (RValue::symbol_iterator SI=X.symbol_begin(), SE=X.symbol_end(); |
| 69 | SI != SE; ++SI) |
| 70 | MarkedSymbols.insert(*SI); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 71 | } |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 72 | else |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 73 | NewSt.BlockExprBindings = Remove(NewSt, BlkExpr); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 74 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 75 | continue; |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 76 | } |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 78 | // Iterate over the variable bindings. |
| 79 | for (ValueState::vb_iterator I = St.vb_begin(), E = St.vb_end(); I!=E ; ++I) |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 80 | if (Liveness.isLive(Loc, I.getKey())) |
| 81 | WList.push_back(I.getKey()); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 82 | |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 83 | |
| 84 | while (!WList.empty()) { |
| 85 | ValueDecl* V = WList.back(); |
| 86 | WList.pop_back(); |
| 87 | |
| 88 | if (Marked.count(V)) |
| 89 | continue; |
| 90 | |
| 91 | Marked.insert(V); |
| 92 | |
| 93 | if (V->getType()->isPointerType()) { |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 94 | const LValue& LV = |
| 95 | cast<LValue>(GetValue(St, lval::DeclVal(cast<VarDecl>(V)))); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 96 | |
| 97 | for (RValue::symbol_iterator SI=LV.symbol_begin(), SE=LV.symbol_end(); |
| 98 | SI != SE; ++SI) |
| 99 | MarkedSymbols.insert(*SI); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 100 | |
| 101 | if (!isa<lval::DeclVal>(LV)) |
| 102 | continue; |
| 103 | |
| 104 | const lval::DeclVal& LVD = cast<lval::DeclVal>(LV); |
| 105 | WList.push_back(LVD.getDecl()); |
| 106 | } |
| 107 | } |
| 108 | |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 109 | // Remove dead variable bindings. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 110 | for (ValueState::vb_iterator I = St.vb_begin(), E = St.vb_end(); I!=E ; ++I) |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 111 | if (!Marked.count(I.getKey())) |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 112 | NewSt.VarBindings = Remove(NewSt, I.getKey()); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 114 | // Remove dead symbols. |
| 115 | for (ValueState::ce_iterator I = St.ce_begin(), E=St.ce_end(); I!=E; ++I) |
| 116 | if (!MarkedSymbols.count(I.getKey())) |
| 117 | NewSt.ConstantEq = CEFactory.Remove(NewSt.ConstantEq, I.getKey()); |
| 118 | |
| 119 | for (ValueState::cne_iterator I = St.cne_begin(), E=St.cne_end(); I!=E; ++I) |
| 120 | if (!MarkedSymbols.count(I.getKey())) |
| 121 | NewSt.ConstantNotEq = CNEFactory.Remove(NewSt.ConstantNotEq, I.getKey()); |
| 122 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 123 | return getPersistentState(NewSt); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 124 | } |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 125 | |
| 126 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 127 | RValue ValueStateManager::GetValue(ValueState St, const LValue& LV, |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 128 | QualType* T) { |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 129 | if (isa<UnknownVal>(LV)) |
| 130 | return UnknownVal(); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 132 | assert (!isa<UninitializedVal>(LV)); |
| 133 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 134 | switch (LV.getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 135 | case lval::DeclValKind: { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 136 | ValueState::VarBindingsTy::TreeTy* T = |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 137 | // FIXME: We should make lval::DeclVal only contain VarDecl |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 138 | St->VarBindings.SlimFind( |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 139 | cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl())); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 141 | return T ? T->getValue().second : UnknownVal(); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 142 | } |
Ted Kremenek | d131c4f | 2008-02-07 05:48:01 +0000 | [diff] [blame] | 143 | |
| 144 | // FIXME: We should bind how far a "ContentsOf" will go... |
| 145 | |
| 146 | case lval::SymbolValKind: { |
| 147 | const lval::SymbolVal& SV = cast<lval::SymbolVal>(LV); |
| 148 | assert (T); |
| 149 | |
| 150 | if (T->getTypePtr()->isPointerType()) |
| 151 | return lval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol())); |
| 152 | else |
| 153 | return nonlval::SymbolVal(SymMgr.getContentsOfSymbol(SV.getSymbol())); |
| 154 | } |
| 155 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 156 | default: |
| 157 | assert (false && "Invalid LValue."); |
| 158 | break; |
| 159 | } |
| 160 | |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 161 | return UnknownVal(); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 164 | ValueState |
| 165 | ValueStateManager::AddNE(ValueState St, SymbolID sym, const llvm::APSInt& V) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 166 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 167 | ValueState::ConstantNotEqTy::TreeTy* T = St->ConstantNotEq.SlimFind(sym); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 168 | |
| 169 | ValueState::IntSetTy S = T ? T->getValue().second : ISetFactory.GetEmptySet(); |
| 170 | |
| 171 | // Now add V to the NE set. |
| 172 | S = ISetFactory.Add(S, &V); |
| 173 | |
| 174 | // Create a new state with the old binding replaced. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 175 | ValueStateImpl NewSt = *St; |
| 176 | NewSt.ConstantNotEq = CNEFactory.Add(NewSt.ConstantNotEq, |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 177 | sym, S); |
| 178 | |
| 179 | // Get the persistent copy. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 180 | return getPersistentState(NewSt); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 183 | ValueState |
| 184 | ValueStateManager::AddEQ(ValueState St, SymbolID sym, const llvm::APSInt& V) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 185 | // Create a new state with the old binding replaced. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 186 | ValueStateImpl NewSt = *St; |
| 187 | NewSt.ConstantEq = CEFactory.Add(NewSt.ConstantEq, sym, &V); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 188 | |
| 189 | // Get the persistent copy. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 190 | return getPersistentState(NewSt); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 193 | RValue ValueStateManager::GetValue(ValueState St, Expr* E, bool* hasVal) { |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 194 | for (;;) { |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 195 | switch (E->getStmtClass()) { |
Ted Kremenek | 2a50257 | 2008-02-12 21:37:56 +0000 | [diff] [blame] | 196 | |
| 197 | case Stmt::AddrLabelExprClass: |
| 198 | return LValue::GetValue(cast<AddrLabelExpr>(E)); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 199 | |
| 200 | // ParenExprs are no-ops. |
| 201 | |
| 202 | case Stmt::ParenExprClass: |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 203 | E = cast<ParenExpr>(E)->getSubExpr(); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 204 | continue; |
| 205 | |
| 206 | // DeclRefExprs can either evaluate to an LValue or a Non-LValue |
| 207 | // (assuming an implicit "load") depending on the context. In this |
| 208 | // context we assume that we are retrieving the value contained |
| 209 | // within the referenced variables. |
| 210 | |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 211 | case Stmt::DeclRefExprClass: { |
| 212 | ValueDecl* D = cast<DeclRefExpr>(E)->getDecl(); |
| 213 | |
| 214 | if (VarDecl* VD = dyn_cast<VarDecl>(D)) |
| 215 | return GetValue(St, lval::DeclVal(VD)); |
| 216 | else if (EnumConstantDecl* ED = dyn_cast<EnumConstantDecl>(D)) { |
| 217 | // FIXME: Do we need to cache a copy of this enum, since it |
| 218 | // already has persistent storage? We do this because we |
| 219 | // are comparing states using pointer equality. Perhaps there is |
| 220 | // a better way, since APInts are fairly lightweight. |
| 221 | return nonlval::ConcreteInt(ValMgr.getValue(ED->getInitVal())); |
| 222 | } |
| 223 | |
| 224 | assert (false && |
| 225 | "ValueDecl support for this ValueDecl not implemented."); |
| 226 | |
| 227 | return UnknownVal(); |
| 228 | } |
| 229 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 230 | |
| 231 | // Integer literals evaluate to an RValue. Simply retrieve the |
| 232 | // RValue for the literal. |
| 233 | |
| 234 | case Stmt::IntegerLiteralClass: |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 235 | return NonLValue::GetValue(ValMgr, cast<IntegerLiteral>(E)); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 237 | case Stmt::CharacterLiteralClass: { |
| 238 | CharacterLiteral* C = cast<CharacterLiteral>(E); |
| 239 | |
| 240 | return NonLValue::GetValue(ValMgr, C->getValue(), C->getType(), |
| 241 | C->getLoc()); |
| 242 | } |
| 243 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 244 | // Casts where the source and target type are the same |
| 245 | // are no-ops. We blast through these to get the descendant |
| 246 | // subexpression that has a value. |
| 247 | |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 248 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 249 | case Stmt::ImplicitCastExprClass: { |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 250 | ImplicitCastExpr* C = cast<ImplicitCastExpr>(E); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 251 | if (C->getType() == C->getSubExpr()->getType()) { |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 252 | E = C->getSubExpr(); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 253 | continue; |
| 254 | } |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | case Stmt::CastExprClass: { |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 259 | CastExpr* C = cast<CastExpr>(E); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 260 | if (C->getType() == C->getSubExpr()->getType()) { |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 261 | E = C->getSubExpr(); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 262 | continue; |
| 263 | } |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | // Handle all other Stmt* using a lookup. |
| 268 | |
| 269 | default: |
| 270 | break; |
| 271 | }; |
| 272 | |
| 273 | break; |
| 274 | } |
| 275 | |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 276 | ValueState::ExprBindingsTy::TreeTy* T = St->SubExprBindings.SlimFind(E); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 277 | |
| 278 | if (T) { |
| 279 | if (hasVal) *hasVal = true; |
| 280 | return T->getValue().second; |
| 281 | } |
| 282 | |
Ted Kremenek | 5d2986b | 2008-02-12 18:50:32 +0000 | [diff] [blame] | 283 | T = St->BlockExprBindings.SlimFind(E); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 285 | if (T) { |
| 286 | if (hasVal) *hasVal = true; |
| 287 | return T->getValue().second; |
| 288 | } |
| 289 | else { |
| 290 | if (hasVal) *hasVal = false; |
Ted Kremenek | 2203118 | 2008-02-08 02:57:34 +0000 | [diff] [blame] | 291 | return UnknownVal(); |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 292 | } |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 295 | LValue ValueStateManager::GetLValue(ValueState St, Expr* E) { |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 297 | while (ParenExpr* P = dyn_cast<ParenExpr>(E)) |
| 298 | E = P->getSubExpr(); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 299 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 300 | if (DeclRefExpr* DR = dyn_cast<DeclRefExpr>(E)) |
Ted Kremenek | 50d0ac2 | 2008-02-15 22:09:30 +0000 | [diff] [blame] | 301 | return lval::DeclVal(cast<VarDecl>(DR->getDecl())); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 302 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 303 | if (UnaryOperator* U = dyn_cast<UnaryOperator>(E)) |
Ted Kremenek | 5b6dc2d | 2008-02-07 01:08:27 +0000 | [diff] [blame] | 304 | if (U->getOpcode() == UnaryOperator::Deref) |
| 305 | return cast<LValue>(GetValue(St, U->getSubExpr())); |
| 306 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 307 | return cast<LValue>(GetValue(St, E)); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 310 | ValueState |
| 311 | ValueStateManager::SetValue(ValueState St, Expr* E, bool isBlkExpr, |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 312 | const RValue& V) { |
| 313 | |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 314 | assert (E); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 315 | |
| 316 | if (V.isUnknown()) |
| 317 | return St; |
| 318 | |
| 319 | ValueStateImpl NewSt = *St; |
| 320 | |
| 321 | if (isBlkExpr) |
| 322 | NewSt.BlockExprBindings = EXFactory.Add(NewSt.BlockExprBindings, E, V); |
| 323 | else |
| 324 | NewSt.SubExprBindings = EXFactory.Add(NewSt.SubExprBindings, E, V); |
| 325 | |
| 326 | return getPersistentState(NewSt); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 329 | ValueState |
| 330 | ValueStateManager::SetValue(ValueState St, const LValue& LV, const RValue& V) { |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 331 | |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 332 | assert (!isa<UnknownVal>(LV)); |
| 333 | assert (!isa<UninitializedVal>(LV)); |
| 334 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 335 | switch (LV.getSubKind()) { |
Ted Kremenek | 329f854 | 2008-02-05 21:52:21 +0000 | [diff] [blame] | 336 | case lval::DeclValKind: |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 337 | return V.isKnown() // FIXME: Have DeclVal only contain VarDecl |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 338 | ? BindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl()), V) |
| 339 | : UnbindVar(St, cast<VarDecl>(cast<lval::DeclVal>(LV).getDecl())); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 340 | |
| 341 | default: |
| 342 | assert ("SetValue for given LValue type not yet implemented."); |
| 343 | return St; |
| 344 | } |
| 345 | } |
| 346 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 347 | ValueState |
| 348 | ValueStateManager::BindVar(ValueState St, VarDecl* D, const RValue& V) { |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 349 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 350 | // Create a new state with the old binding removed. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 351 | ValueStateImpl NewSt = *St; |
| 352 | NewSt.VarBindings = VBFactory.Add(NewSt.VarBindings, D, V); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 353 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 354 | // Get the persistent copy. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 355 | return getPersistentState(NewSt); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 356 | } |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 357 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 358 | ValueState |
| 359 | ValueStateManager::UnbindVar(ValueState St, VarDecl* D) { |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 360 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 361 | // Create a new state with the old binding removed. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 362 | ValueStateImpl NewSt = *St; |
| 363 | NewSt.VarBindings = VBFactory.Remove(NewSt.VarBindings, D); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 364 | |
| 365 | // Get the persistent copy. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 366 | return getPersistentState(NewSt); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 369 | ValueState |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 370 | ValueStateManager::getInitialState() { |
| 371 | |
| 372 | // Create a state with empty variable bindings. |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 373 | ValueStateImpl StateImpl(EXFactory.GetEmptyMap(), |
| 374 | VBFactory.GetEmptyMap(), |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 375 | CNEFactory.GetEmptyMap(), |
| 376 | CEFactory.GetEmptyMap()); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 377 | |
| 378 | return getPersistentState(StateImpl); |
| 379 | } |
| 380 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 381 | ValueState |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 382 | ValueStateManager::getPersistentState(const ValueStateImpl &State) { |
| 383 | |
| 384 | llvm::FoldingSetNodeID ID; |
| 385 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 386 | void* InsertPos; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 387 | |
| 388 | if (ValueStateImpl* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
| 389 | return I; |
| 390 | |
Ted Kremenek | 41652a9 | 2008-02-06 02:45:20 +0000 | [diff] [blame] | 391 | ValueStateImpl* I = (ValueStateImpl*) Alloc.Allocate<ValueStateImpl>(); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 392 | new (I) ValueStateImpl(State); |
| 393 | StateSet.InsertNode(I, InsertPos); |
| 394 | return I; |
| 395 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 396 | |
| 397 | void ValueState::printDOT(std::ostream& Out) const { |
| 398 | // Print Variable Bindings |
| 399 | Out << "Variables:\\l"; |
| 400 | |
| 401 | bool isFirst = true; |
| 402 | |
| 403 | for (vb_iterator I=vb_begin(), E=vb_end(); I!=E; ++I) { |
| 404 | |
| 405 | if (isFirst) |
| 406 | isFirst = false; |
| 407 | else |
| 408 | Out << "\\l"; |
| 409 | |
| 410 | Out << ' ' << I.getKey()->getName() << " : "; |
| 411 | I.getData().print(Out); |
| 412 | } |
| 413 | |
| 414 | // Print Subexpression bindings. |
| 415 | |
| 416 | isFirst = true; |
| 417 | |
| 418 | for (seb_iterator I=seb_begin(), E=seb_end(); I != E;++I) { |
| 419 | |
| 420 | if (isFirst) { |
| 421 | Out << "\\l\\lSub-Expressions:\\l"; |
| 422 | isFirst = false; |
| 423 | } |
| 424 | else |
| 425 | Out << "\\l"; |
| 426 | |
| 427 | Out << " (" << (void*) I.getKey() << ") "; |
| 428 | I.getKey()->printPretty(Out); |
| 429 | Out << " : "; |
| 430 | I.getData().print(Out); |
| 431 | } |
| 432 | |
| 433 | // Print block-expression bindings. |
| 434 | |
| 435 | isFirst = true; |
| 436 | |
| 437 | for (beb_iterator I=beb_begin(), E=beb_end(); I != E; ++I) { |
| 438 | |
| 439 | if (isFirst) { |
| 440 | Out << "\\l\\lBlock-level Expressions:\\l"; |
| 441 | isFirst = false; |
| 442 | } |
| 443 | else |
| 444 | Out << "\\l"; |
| 445 | |
| 446 | Out << " (" << (void*) I.getKey() << ") "; |
| 447 | I.getKey()->printPretty(Out); |
| 448 | Out << " : "; |
| 449 | I.getData().print(Out); |
| 450 | } |
| 451 | |
| 452 | // Print equality constraints. |
| 453 | |
| 454 | if (!Data->ConstantEq.isEmpty()) { |
| 455 | |
| 456 | Out << "\\l\\|'==' constraints:"; |
| 457 | |
| 458 | for (ConstantEqTy::iterator I=Data->ConstantEq.begin(), |
| 459 | E=Data->ConstantEq.end(); I!=E;++I) |
| 460 | Out << "\\l $" << I.getKey() << " : " << I.getData()->toString(); |
| 461 | } |
| 462 | |
| 463 | |
| 464 | // Print != constraints. |
| 465 | |
| 466 | if (!Data->ConstantNotEq.isEmpty()) { |
| 467 | |
| 468 | Out << "\\l\\|'!=' constraints:"; |
| 469 | |
| 470 | for (ConstantNotEqTy::iterator I=Data->ConstantNotEq.begin(), |
| 471 | EI=Data->ConstantNotEq.end(); I != EI; ++I) { |
| 472 | |
| 473 | Out << "\\l $" << I.getKey() << " : "; |
| 474 | isFirst = true; |
| 475 | |
| 476 | IntSetTy::iterator J=I.getData().begin(), EJ=I.getData().end(); |
| 477 | |
| 478 | for ( ; J != EJ; ++J) { |
| 479 | if (isFirst) isFirst = false; |
| 480 | else Out << ", "; |
| 481 | |
| 482 | Out << (*J)->toString(); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |