Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1 | //= GRState*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 | // |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 10 | // This file defines SymbolID, ExprBindKey, and GRState* |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/PathSensitive/GRState.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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 20 | bool GRState::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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 29 | bool GRState::isEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 38 | const llvm::APSInt* GRState::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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 43 | const GRState* |
| 44 | GRStateManager::RemoveDeadBindings(const GRState* 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 57 | GRState 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 67 | for (GRState::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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 100 | for (GRState::ce_iterator I = St->ce_begin(), E=St->ce_end(); I!=E; ++I) { |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 110 | for (GRState::cne_iterator I = St->cne_begin(), E=St->cne_end(); I!=E;++I){ |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 123 | const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 132 | GRState NewSt = *St; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 133 | NewSt.St = NewStore; |
| 134 | return getPersistentState(NewSt); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 137 | const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 138 | Store OldStore = St->getStore(); |
| 139 | Store NewStore = StMgr->Remove(OldStore, LV); |
| 140 | |
| 141 | if (NewStore == OldStore) |
| 142 | return St; |
| 143 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 144 | GRState NewSt = *St; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 145 | NewSt.St = NewStore; |
| 146 | return getPersistentState(NewSt); |
| 147 | } |
| 148 | |
| 149 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 150 | const GRState* GRStateManager::AddNE(const GRState* St, SymbolID sym, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 154 | GRState::ConstNotEqTy::data_type* T = St->ConstNotEq.lookup(sym); |
| 155 | GRState::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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 161 | GRState 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 168 | const GRState* GRStateManager::AddEQ(const GRState* St, SymbolID sym, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 172 | GRState 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 | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 179 | const GRState* GRStateManager::getInitialState() { |
Ted Kremenek | 5a7b382 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 181 | GRState StateImpl(EnvMgr.getInitialEnvironment(), |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 182 | StMgr->getInitialStore(), |
Ted Kremenek | 4502022 | 2008-08-12 21:49:24 +0000 | [diff] [blame] | 183 | GDMFactory.GetEmptyMap(), |
Ted Kremenek | 8133a26 | 2008-07-08 21:46:56 +0000 | [diff] [blame] | 184 | CNEFactory.GetEmptyMap(), |
| 185 | CEFactory.GetEmptyMap()); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 186 | |
| 187 | return getPersistentState(StateImpl); |
| 188 | } |
| 189 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 190 | const GRState* GRStateManager::getPersistentState(GRState& State) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 191 | |
| 192 | llvm::FoldingSetNodeID ID; |
| 193 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 194 | void* InsertPos; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 196 | if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 197 | return I; |
| 198 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 199 | GRState* I = (GRState*) Alloc.Allocate<GRState>(); |
| 200 | new (I) GRState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 201 | StateSet.InsertNode(I, InsertPos); |
| 202 | return I; |
| 203 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame^] | 205 | void GRState::printDOT(std::ostream& Out, |
| 206 | Printer** Beg, Printer** End) const { |
| 207 | print(Out, Beg, End, "\\l", "\\|"); |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame^] | 210 | void GRState::printStdErr(Printer** Beg, Printer** End) const { |
| 211 | print(*llvm::cerr, Beg, End); |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame^] | 214 | void GRState::print(std::ostream& Out, Printer** Beg, Printer** End, |
| 215 | const char* nl, const char* sep) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 216 | |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 217 | // Print Variable Bindings |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 218 | Out << "Variables:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 219 | |
| 220 | bool isFirst = true; |
| 221 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 222 | for (vb_iterator I = vb_begin(), E = vb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 224 | if (isFirst) isFirst = false; |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 225 | else Out << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 226 | |
| 227 | Out << ' ' << I.getKey()->getName() << " : "; |
| 228 | I.getData().print(Out); |
| 229 | } |
| 230 | |
| 231 | // Print Subexpression bindings. |
| 232 | |
| 233 | isFirst = true; |
| 234 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 235 | for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 236 | |
| 237 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 238 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 239 | isFirst = false; |
| 240 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 241 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 242 | |
| 243 | Out << " (" << (void*) I.getKey() << ") "; |
| 244 | I.getKey()->printPretty(Out); |
| 245 | Out << " : "; |
| 246 | I.getData().print(Out); |
| 247 | } |
| 248 | |
| 249 | // Print block-expression bindings. |
| 250 | |
| 251 | isFirst = true; |
| 252 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 253 | for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 254 | |
| 255 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 256 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 257 | isFirst = false; |
| 258 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 259 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 260 | |
| 261 | Out << " (" << (void*) I.getKey() << ") "; |
| 262 | I.getKey()->printPretty(Out); |
| 263 | Out << " : "; |
| 264 | I.getData().print(Out); |
| 265 | } |
| 266 | |
| 267 | // Print equality constraints. |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame^] | 268 | // FIXME: Make just another printer do this. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 269 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 270 | if (!ConstEq.isEmpty()) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 271 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 272 | Out << nl << sep << "'==' constraints:"; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 273 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 274 | for (ConstEqTy::iterator I = ConstEq.begin(), |
| 275 | E = ConstEq.end(); I!=E; ++I) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 276 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 277 | Out << nl << " $" << I.getKey() |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 278 | << " : " << I.getData()->toString(); |
| 279 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 280 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 281 | |
| 282 | // Print != constraints. |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame^] | 283 | // FIXME: Make just another printer do this. |
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 | if (!ConstNotEq.isEmpty()) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 286 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 287 | Out << nl << sep << "'!=' constraints:"; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 288 | |
Ted Kremenek | aed9b6a | 2008-02-28 10:21:43 +0000 | [diff] [blame] | 289 | for (ConstNotEqTy::iterator I = ConstNotEq.begin(), |
| 290 | EI = ConstNotEq.end(); I != EI; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 291 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 292 | Out << nl << " $" << I.getKey() << " : "; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 293 | isFirst = true; |
| 294 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 295 | IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 296 | |
| 297 | for ( ; J != EJ; ++J) { |
| 298 | if (isFirst) isFirst = false; |
| 299 | else Out << ", "; |
| 300 | |
| 301 | Out << (*J)->toString(); |
| 302 | } |
| 303 | } |
| 304 | } |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 305 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame^] | 306 | // Print checker-specific data. |
| 307 | for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 308 | } |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 309 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 310 | |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | // Queries. |
| 313 | //===----------------------------------------------------------------------===// |
| 314 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 315 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 316 | const llvm::APSInt& Y) { |
| 317 | RVal V = GetRVal(state, Ex); |
| 318 | |
| 319 | if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V)) |
| 320 | return X->getValue() == Y; |
| 321 | |
| 322 | if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V)) |
| 323 | return X->getValue() == Y; |
| 324 | |
| 325 | if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V)) |
| 326 | return state->isEqual(X->getSymbol(), Y); |
| 327 | |
| 328 | if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V)) |
| 329 | return state->isEqual(X->getSymbol(), Y); |
| 330 | |
| 331 | return false; |
| 332 | } |
| 333 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 334 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 335 | uint64_t x) { |
| 336 | return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType())); |
| 337 | } |
| 338 | |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 339 | //===----------------------------------------------------------------------===// |
| 340 | // "Assume" logic. |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 343 | const GRState* GRStateManager::Assume(const GRState* St, LVal Cond, |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 344 | bool Assumption, bool& isFeasible) { |
| 345 | |
| 346 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 347 | |
| 348 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 349 | : St; |
| 350 | } |
| 351 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 352 | const GRState* GRStateManager::AssumeAux(const GRState* St, LVal Cond, |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 353 | bool Assumption, bool& isFeasible) { |
| 354 | |
| 355 | switch (Cond.getSubKind()) { |
| 356 | default: |
| 357 | assert (false && "'Assume' not implemented for this LVal."); |
| 358 | return St; |
| 359 | |
| 360 | case lval::SymbolValKind: |
| 361 | if (Assumption) |
| 362 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 363 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 364 | else |
| 365 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 366 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 367 | |
| 368 | |
| 369 | case lval::DeclValKind: |
| 370 | case lval::FuncValKind: |
| 371 | case lval::GotoLabelKind: |
| 372 | case lval::StringLiteralValKind: |
| 373 | isFeasible = Assumption; |
| 374 | return St; |
| 375 | |
| 376 | case lval::FieldOffsetKind: |
| 377 | return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(), |
| 378 | Assumption, isFeasible); |
| 379 | |
| 380 | case lval::ArrayOffsetKind: |
| 381 | return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(), |
| 382 | Assumption, isFeasible); |
| 383 | |
| 384 | case lval::ConcreteIntKind: { |
| 385 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
| 386 | isFeasible = b ? Assumption : !Assumption; |
| 387 | return St; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 392 | const GRState* GRStateManager::Assume(const GRState* St, NonLVal Cond, |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 393 | bool Assumption, bool& isFeasible) { |
| 394 | |
| 395 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 396 | |
| 397 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 398 | : St; |
| 399 | } |
| 400 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 401 | const GRState* GRStateManager::AssumeAux(const GRState* St, NonLVal Cond, |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 402 | bool Assumption, bool& isFeasible) { |
| 403 | switch (Cond.getSubKind()) { |
| 404 | default: |
| 405 | assert (false && "'Assume' not implemented for this NonLVal."); |
| 406 | return St; |
| 407 | |
| 408 | |
| 409 | case nonlval::SymbolValKind: { |
| 410 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
| 411 | SymbolID sym = SV.getSymbol(); |
| 412 | |
| 413 | if (Assumption) |
| 414 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 415 | isFeasible); |
| 416 | else |
| 417 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 418 | isFeasible); |
| 419 | } |
| 420 | |
| 421 | case nonlval::SymIntConstraintValKind: |
| 422 | return |
| 423 | AssumeSymInt(St, Assumption, |
| 424 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 425 | isFeasible); |
| 426 | |
| 427 | case nonlval::ConcreteIntKind: { |
| 428 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
| 429 | isFeasible = b ? Assumption : !Assumption; |
| 430 | return St; |
| 431 | } |
| 432 | |
| 433 | case nonlval::LValAsIntegerKind: { |
| 434 | return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(), |
| 435 | Assumption, isFeasible); |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 440 | |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 442 | const GRState* GRStateManager::AssumeSymInt(const GRState* St, |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 443 | bool Assumption, |
| 444 | const SymIntConstraint& C, |
| 445 | bool& isFeasible) { |
| 446 | |
| 447 | switch (C.getOpcode()) { |
| 448 | default: |
| 449 | // No logic yet for other operators. |
| 450 | isFeasible = true; |
| 451 | return St; |
| 452 | |
| 453 | case BinaryOperator::EQ: |
| 454 | if (Assumption) |
| 455 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 456 | else |
| 457 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 458 | |
| 459 | case BinaryOperator::NE: |
| 460 | if (Assumption) |
| 461 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 462 | else |
| 463 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 464 | |
| 465 | case BinaryOperator::GE: |
| 466 | if (Assumption) |
| 467 | return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 468 | else |
| 469 | return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 470 | |
| 471 | case BinaryOperator::LE: |
| 472 | if (Assumption) |
| 473 | return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 474 | else |
| 475 | return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible); |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 476 | } |
| 477 | } |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 478 | |
| 479 | //===----------------------------------------------------------------------===// |
| 480 | // FIXME: This should go into a plug-in constraint engine. |
| 481 | //===----------------------------------------------------------------------===// |
| 482 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 483 | const GRState* |
| 484 | GRStateManager::AssumeSymNE(const GRState* St, SymbolID sym, |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 485 | const llvm::APSInt& V, bool& isFeasible) { |
| 486 | |
| 487 | // First, determine if sym == X, where X != V. |
| 488 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 489 | isFeasible = *X != V; |
| 490 | return St; |
| 491 | } |
| 492 | |
| 493 | // Second, determine if sym != V. |
| 494 | if (St->isNotEqual(sym, V)) { |
| 495 | isFeasible = true; |
| 496 | return St; |
| 497 | } |
| 498 | |
| 499 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 500 | // Make that assumption. |
| 501 | |
| 502 | isFeasible = true; |
| 503 | return AddNE(St, sym, V); |
| 504 | } |
| 505 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 506 | const GRState* |
| 507 | GRStateManager::AssumeSymEQ(const GRState* St, SymbolID sym, |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 508 | const llvm::APSInt& V, bool& isFeasible) { |
| 509 | |
| 510 | // First, determine if sym == X, where X != V. |
| 511 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 512 | isFeasible = *X == V; |
| 513 | return St; |
| 514 | } |
| 515 | |
| 516 | // Second, determine if sym != V. |
| 517 | if (St->isNotEqual(sym, V)) { |
| 518 | isFeasible = false; |
| 519 | return St; |
| 520 | } |
| 521 | |
| 522 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 523 | // Make that assumption. |
| 524 | |
| 525 | isFeasible = true; |
| 526 | return AddEQ(St, sym, V); |
| 527 | } |
| 528 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 529 | const GRState* |
| 530 | GRStateManager::AssumeSymLT(const GRState* St, SymbolID sym, |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 531 | const llvm::APSInt& V, bool& isFeasible) { |
| 532 | |
| 533 | // FIXME: For now have assuming x < y be the same as assuming sym != V; |
| 534 | return AssumeSymNE(St, sym, V, isFeasible); |
| 535 | } |
| 536 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 537 | const GRState* |
| 538 | GRStateManager::AssumeSymGT(const GRState* St, SymbolID sym, |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 539 | const llvm::APSInt& V, bool& isFeasible) { |
| 540 | |
| 541 | // FIXME: For now have assuming x > y be the same as assuming sym != V; |
| 542 | return AssumeSymNE(St, sym, V, isFeasible); |
| 543 | } |
| 544 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 545 | const GRState* |
| 546 | GRStateManager::AssumeSymGE(const GRState* St, SymbolID sym, |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 547 | const llvm::APSInt& V, bool& isFeasible) { |
| 548 | |
| 549 | // FIXME: Primitive logic for now. Only reject a path if the value of |
| 550 | // sym is a constant X and !(X >= V). |
| 551 | |
| 552 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 553 | isFeasible = *X >= V; |
| 554 | return St; |
| 555 | } |
| 556 | |
| 557 | isFeasible = true; |
| 558 | return St; |
| 559 | } |
| 560 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 561 | const GRState* |
| 562 | GRStateManager::AssumeSymLE(const GRState* St, SymbolID sym, |
Ted Kremenek | 2619be0 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 563 | const llvm::APSInt& V, bool& isFeasible) { |
| 564 | |
| 565 | // FIXME: Primitive logic for now. Only reject a path if the value of |
| 566 | // sym is a constant X and !(X <= V). |
| 567 | |
| 568 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 569 | isFeasible = *X <= V; |
| 570 | return St; |
| 571 | } |
| 572 | |
| 573 | isFeasible = true; |
| 574 | return St; |
| 575 | } |
| 576 | |