Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 1 | //= ValueState*cpp - Path-Sens. "State" for tracking valuues -----*- 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 | // |
Gabor Greif | 843e934 | 2008-03-06 10:40:09 +0000 | [diff] [blame] | 10 | // This file defines SymbolID, ExprBindKey, and ValueState* |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 0f5f059 | 2008-02-27 06:07:00 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/PathSensitive/ValueState.h" |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace clang; |
| 19 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 20 | bool ValueState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 21 | |
| 22 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 23 | const ConstNotEqTy::data_type* T = ConstNotEq.lookup(sym); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 24 | |
| 25 | // See if V is present in the NE-set. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 26 | return T ? T->contains(&V) : false; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 29 | bool ValueState::isEqual(SymbolID sym, const llvm::APSInt& V) const { |
| 30 | |
| 31 | // Retrieve the EQ-set associated with the given symbol. |
| 32 | const ConstEqTy::data_type* T = ConstEq.lookup(sym); |
| 33 | |
| 34 | // See if V is present in the EQ-set. |
| 35 | return T ? **T == V : false; |
| 36 | } |
| 37 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 38 | const llvm::APSInt* ValueState::getSymVal(SymbolID sym) const { |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 39 | ConstEqTy::data_type* T = ConstEq.lookup(sym); |
| 40 | return T ? *T : NULL; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 43 | const ValueState* |
| 44 | ValueStateManager::RemoveDeadBindings(const ValueState* St, Stmt* Loc, |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 45 | const LiveVariables& Liveness, |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 46 | DeadSymbolsTy& DSymbols) { |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 47 | |
| 48 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 49 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 50 | // tells us are live. We then see what Decls they may reference, and keep |
| 51 | // those around. This code more than likely can be made faster, and the |
| 52 | // frequency of which this method is called should be experimented with |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 53 | // for optimum performance. |
| 54 | DRoots.clear(); |
| 55 | StoreManager::LiveSymbolsTy LSymbols; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 56 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 57 | ValueState NewSt = *St; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 58 | |
| 59 | // FIXME: Put this in environment. |
| 60 | // Clean up the environment. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 61 | |
| 62 | // Drop bindings for subexpressions. |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 63 | NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 64 | |
| 65 | // Iterate over the block-expr bindings. |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 67 | for (ValueState::beb_iterator I = St->beb_begin(), E = St->beb_end(); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 68 | I!=E ; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 69 | Expr* BlkExpr = I.getKey(); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 71 | if (Liveness.isLive(Loc, BlkExpr)) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 72 | RVal X = I.getData(); |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 73 | |
| 74 | if (isa<lval::DeclVal>(X)) { |
| 75 | lval::DeclVal LV = cast<lval::DeclVal>(X); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 76 | DRoots.push_back(LV.getDecl()); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 77 | } |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 79 | for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end(); |
| 80 | SI != SE; ++SI) { |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 81 | LSymbols.insert(*SI); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 82 | } |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 83 | } |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 84 | else { |
| 85 | RVal X = I.getData(); |
| 86 | |
Ted Kremenek | 4a4e524 | 2008-02-28 09:25:22 +0000 | [diff] [blame] | 87 | if (X.isUndef() && cast<UndefinedVal>(X).getData()) |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 88 | continue; |
| 89 | |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 90 | NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, BlkExpr); |
Ted Kremenek | 05a2378 | 2008-02-26 19:05:15 +0000 | [diff] [blame] | 91 | } |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 92 | } |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 94 | // Clean up the store. |
| 95 | DSymbols.clear(); |
| 96 | NewSt.St = StMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness, DRoots, |
| 97 | LSymbols, DSymbols); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 99 | // Remove the dead symbols from the symbol tracker. |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 100 | for (ValueState::ce_iterator I = St->ce_begin(), E=St->ce_end(); I!=E; ++I) { |
| 101 | |
| 102 | SymbolID sym = I.getKey(); |
| 103 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 104 | if (!LSymbols.count(sym)) { |
| 105 | DSymbols.insert(sym); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 106 | NewSt.ConstEq = CEFactory.Remove(NewSt.ConstEq, sym); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | for (ValueState::cne_iterator I = St->cne_begin(), E=St->cne_end(); I!=E;++I){ |
| 111 | |
| 112 | SymbolID sym = I.getKey(); |
| 113 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 114 | if (!LSymbols.count(sym)) { |
| 115 | DSymbols.insert(sym); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 116 | NewSt.ConstNotEq = CNEFactory.Remove(NewSt.ConstNotEq, sym); |
| 117 | } |
| 118 | } |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 120 | return getPersistentState(NewSt); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 121 | } |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 123 | const ValueState* ValueStateManager::SetRVal(const ValueState* St, LVal LV, |
| 124 | RVal V) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 125 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 126 | Store OldStore = St->getStore(); |
| 127 | Store NewStore = StMgr->SetRVal(OldStore, LV, V); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 128 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 129 | if (NewStore == OldStore) |
| 130 | return St; |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 132 | ValueState NewSt = *St; |
| 133 | NewSt.St = NewStore; |
| 134 | return getPersistentState(NewSt); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 137 | const ValueState* ValueStateManager::Unbind(const ValueState* St, LVal LV) { |
| 138 | Store OldStore = St->getStore(); |
| 139 | Store NewStore = StMgr->Remove(OldStore, LV); |
| 140 | |
| 141 | if (NewStore == OldStore) |
| 142 | return St; |
| 143 | |
| 144 | ValueState NewSt = *St; |
| 145 | NewSt.St = NewStore; |
| 146 | return getPersistentState(NewSt); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | const ValueState* ValueStateManager::AddNE(const ValueState* St, SymbolID sym, |
| 151 | const llvm::APSInt& V) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 153 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 154 | ValueState::ConstNotEqTy::data_type* T = St->ConstNotEq.lookup(sym); |
| 155 | ValueState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 157 | // Now add V to the NE set. |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 158 | S = ISetFactory.Add(S, &V); |
| 159 | |
| 160 | // Create a new state with the old binding replaced. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 161 | ValueState NewSt = *St; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 162 | NewSt.ConstNotEq = CNEFactory.Add(NewSt.ConstNotEq, sym, S); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 163 | |
| 164 | // Get the persistent copy. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 165 | return getPersistentState(NewSt); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 168 | const ValueState* ValueStateManager::AddEQ(const ValueState* St, SymbolID sym, |
| 169 | const llvm::APSInt& V) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 170 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 171 | // Create a new state with the old binding replaced. |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 172 | ValueState NewSt = *St; |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 173 | NewSt.ConstEq = CEFactory.Add(NewSt.ConstEq, sym, &V); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 174 | |
| 175 | // Get the persistent copy. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 176 | return getPersistentState(NewSt); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 179 | const ValueState* ValueStateManager::getInitialState() { |
Ted Kremenek | 5a7b382 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 181 | ValueState StateImpl(EnvMgr.getInitialEnvironment(), |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 182 | StMgr->getInitialStore(), |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 183 | CNEFactory.GetEmptyMap(), |
| 184 | CEFactory.GetEmptyMap()); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 185 | |
| 186 | return getPersistentState(StateImpl); |
| 187 | } |
| 188 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 189 | const ValueState* ValueStateManager::getPersistentState(ValueState& State) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 190 | |
| 191 | llvm::FoldingSetNodeID ID; |
| 192 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 193 | void* InsertPos; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 194 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 195 | if (ValueState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 196 | return I; |
| 197 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 198 | ValueState* I = (ValueState*) Alloc.Allocate<ValueState>(); |
| 199 | new (I) ValueState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 200 | StateSet.InsertNode(I, InsertPos); |
| 201 | return I; |
| 202 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 203 | |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 204 | void ValueState::printDOT(std::ostream& Out, CheckerStatePrinter* P) const { |
| 205 | print(Out, P, "\\l", "\\|"); |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 208 | void ValueState::printStdErr(CheckerStatePrinter* P) const { |
| 209 | print(*llvm::cerr, P); |
| 210 | } |
| 211 | |
| 212 | void ValueState::print(std::ostream& Out, CheckerStatePrinter* P, |
| 213 | const char* nl, const char* sep) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 215 | // Print Variable Bindings |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 216 | Out << "Variables:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 217 | |
| 218 | bool isFirst = true; |
| 219 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 220 | for (vb_iterator I = vb_begin(), E = vb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 221 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 222 | if (isFirst) isFirst = false; |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 223 | else Out << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 224 | |
| 225 | Out << ' ' << I.getKey()->getName() << " : "; |
| 226 | I.getData().print(Out); |
| 227 | } |
| 228 | |
| 229 | // Print Subexpression bindings. |
| 230 | |
| 231 | isFirst = true; |
| 232 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 233 | for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 234 | |
| 235 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 236 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 237 | isFirst = false; |
| 238 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 239 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 240 | |
| 241 | Out << " (" << (void*) I.getKey() << ") "; |
| 242 | I.getKey()->printPretty(Out); |
| 243 | Out << " : "; |
| 244 | I.getData().print(Out); |
| 245 | } |
| 246 | |
| 247 | // Print block-expression bindings. |
| 248 | |
| 249 | isFirst = true; |
| 250 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 251 | for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 252 | |
| 253 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 254 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 255 | isFirst = false; |
| 256 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 257 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 258 | |
| 259 | Out << " (" << (void*) I.getKey() << ") "; |
| 260 | I.getKey()->printPretty(Out); |
| 261 | Out << " : "; |
| 262 | I.getData().print(Out); |
| 263 | } |
| 264 | |
| 265 | // Print equality constraints. |
| 266 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 267 | if (!ConstEq.isEmpty()) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 268 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 269 | Out << nl << sep << "'==' constraints:"; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 271 | for (ConstEqTy::iterator I = ConstEq.begin(), |
| 272 | E = ConstEq.end(); I!=E; ++I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 274 | Out << nl << " $" << I.getKey() |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 275 | << " : " << I.getData()->toString(); |
| 276 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 277 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 278 | |
| 279 | // Print != constraints. |
| 280 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 281 | if (!ConstNotEq.isEmpty()) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 282 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 283 | Out << nl << sep << "'!=' constraints:"; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 284 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 285 | for (ConstNotEqTy::iterator I = ConstNotEq.begin(), |
| 286 | EI = ConstNotEq.end(); I != EI; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 287 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 288 | Out << nl << " $" << I.getKey() << " : "; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 289 | isFirst = true; |
| 290 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 291 | IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 292 | |
| 293 | for ( ; J != EJ; ++J) { |
| 294 | if (isFirst) isFirst = false; |
| 295 | else Out << ", "; |
| 296 | |
| 297 | Out << (*J)->toString(); |
| 298 | } |
| 299 | } |
| 300 | } |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 301 | |
| 302 | // Print checker-specific data. |
| 303 | |
| 304 | if (P && CheckerState) |
| 305 | P->PrintCheckerState(Out, CheckerState, nl, sep); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 306 | } |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 307 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 308 | |
| 309 | //===----------------------------------------------------------------------===// |
| 310 | // Queries. |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
| 313 | bool ValueStateManager::isEqual(const ValueState* state, Expr* Ex, |
| 314 | const llvm::APSInt& Y) { |
| 315 | RVal V = GetRVal(state, Ex); |
| 316 | |
| 317 | if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V)) |
| 318 | return X->getValue() == Y; |
| 319 | |
| 320 | if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V)) |
| 321 | return X->getValue() == Y; |
| 322 | |
| 323 | if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V)) |
| 324 | return state->isEqual(X->getSymbol(), Y); |
| 325 | |
| 326 | if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V)) |
| 327 | return state->isEqual(X->getSymbol(), Y); |
| 328 | |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | bool ValueStateManager::isEqual(const ValueState* state, Expr* Ex, |
| 333 | uint64_t x) { |
| 334 | return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType())); |
| 335 | } |
| 336 | |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 337 | //===----------------------------------------------------------------------===// |
| 338 | // "Assume" logic. |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | |
| 341 | const ValueState* ValueStateManager::Assume(const ValueState* St, LVal Cond, |
| 342 | bool Assumption, bool& isFeasible) { |
| 343 | |
| 344 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 345 | |
| 346 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 347 | : St; |
| 348 | } |
| 349 | |
| 350 | const ValueState* ValueStateManager::AssumeAux(const ValueState* St, LVal Cond, |
| 351 | bool Assumption, bool& isFeasible) { |
| 352 | |
| 353 | switch (Cond.getSubKind()) { |
| 354 | default: |
| 355 | assert (false && "'Assume' not implemented for this LVal."); |
| 356 | return St; |
| 357 | |
| 358 | case lval::SymbolValKind: |
| 359 | if (Assumption) |
| 360 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 361 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 362 | else |
| 363 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 364 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 365 | |
| 366 | |
| 367 | case lval::DeclValKind: |
| 368 | case lval::FuncValKind: |
| 369 | case lval::GotoLabelKind: |
| 370 | case lval::StringLiteralValKind: |
| 371 | isFeasible = Assumption; |
| 372 | return St; |
| 373 | |
| 374 | case lval::FieldOffsetKind: |
| 375 | return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(), |
| 376 | Assumption, isFeasible); |
| 377 | |
| 378 | case lval::ArrayOffsetKind: |
| 379 | return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(), |
| 380 | Assumption, isFeasible); |
| 381 | |
| 382 | case lval::ConcreteIntKind: { |
| 383 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
| 384 | isFeasible = b ? Assumption : !Assumption; |
| 385 | return St; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | const ValueState* ValueStateManager::Assume(const ValueState* St, NonLVal Cond, |
| 391 | bool Assumption, bool& isFeasible) { |
| 392 | |
| 393 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 394 | |
| 395 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 396 | : St; |
| 397 | } |
| 398 | |
| 399 | const ValueState* ValueStateManager::AssumeAux(const ValueState* St, NonLVal Cond, |
| 400 | bool Assumption, bool& isFeasible) { |
| 401 | switch (Cond.getSubKind()) { |
| 402 | default: |
| 403 | assert (false && "'Assume' not implemented for this NonLVal."); |
| 404 | return St; |
| 405 | |
| 406 | |
| 407 | case nonlval::SymbolValKind: { |
| 408 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
| 409 | SymbolID sym = SV.getSymbol(); |
| 410 | |
| 411 | if (Assumption) |
| 412 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 413 | isFeasible); |
| 414 | else |
| 415 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 416 | isFeasible); |
| 417 | } |
| 418 | |
| 419 | case nonlval::SymIntConstraintValKind: |
| 420 | return |
| 421 | AssumeSymInt(St, Assumption, |
| 422 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 423 | isFeasible); |
| 424 | |
| 425 | case nonlval::ConcreteIntKind: { |
| 426 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
| 427 | isFeasible = b ? Assumption : !Assumption; |
| 428 | return St; |
| 429 | } |
| 430 | |
| 431 | case nonlval::LValAsIntegerKind: { |
| 432 | return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(), |
| 433 | Assumption, isFeasible); |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | const ValueState* ValueStateManager::AssumeSymNE(const ValueState* St, |
| 439 | SymbolID sym, const llvm::APSInt& V, |
| 440 | bool& isFeasible) { |
| 441 | |
| 442 | // First, determine if sym == X, where X != V. |
| 443 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 444 | isFeasible = *X != V; |
| 445 | return St; |
| 446 | } |
| 447 | |
| 448 | // Second, determine if sym != V. |
| 449 | if (St->isNotEqual(sym, V)) { |
| 450 | isFeasible = true; |
| 451 | return St; |
| 452 | } |
| 453 | |
| 454 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 455 | // Make that assumption. |
| 456 | |
| 457 | isFeasible = true; |
| 458 | return AddNE(St, sym, V); |
| 459 | } |
| 460 | |
| 461 | const ValueState* ValueStateManager::AssumeSymEQ(const ValueState* St, SymbolID sym, |
| 462 | const llvm::APSInt& V, bool& isFeasible) { |
| 463 | |
| 464 | // First, determine if sym == X, where X != V. |
| 465 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 466 | isFeasible = *X == V; |
| 467 | return St; |
| 468 | } |
| 469 | |
| 470 | // Second, determine if sym != V. |
| 471 | if (St->isNotEqual(sym, V)) { |
| 472 | isFeasible = false; |
| 473 | return St; |
| 474 | } |
| 475 | |
| 476 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 477 | // Make that assumption. |
| 478 | |
| 479 | isFeasible = true; |
| 480 | return AddEQ(St, sym, V); |
| 481 | } |
| 482 | |
| 483 | const ValueState* ValueStateManager::AssumeSymInt(const ValueState* St, |
| 484 | bool Assumption, |
| 485 | const SymIntConstraint& C, |
| 486 | bool& isFeasible) { |
| 487 | |
| 488 | switch (C.getOpcode()) { |
| 489 | default: |
| 490 | // No logic yet for other operators. |
| 491 | isFeasible = true; |
| 492 | return St; |
| 493 | |
| 494 | case BinaryOperator::EQ: |
| 495 | if (Assumption) |
| 496 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 497 | else |
| 498 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 499 | |
| 500 | case BinaryOperator::NE: |
| 501 | if (Assumption) |
| 502 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 503 | else |
| 504 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 505 | } |
| 506 | } |