Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 1 | //= GRState*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 | // |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 10 | // This file defines SymbolID, ExprBindKey, and GRState* |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Ted Kremenek | 67ff8b9 | 2008-08-17 02:59:30 +0000 | [diff] [blame] | 14 | #include "clang/Analysis/PathSensitive/GRStateTrait.h" |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallSet.h" |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 17 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace clang; |
| 20 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 21 | GRStateManager::~GRStateManager() { |
| 22 | for (std::vector<GRState::Printer*>::iterator I=Printers.begin(), |
| 23 | E=Printers.end(); I!=E; ++I) |
| 24 | delete *I; |
| 25 | |
| 26 | for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); |
| 27 | I!=E; ++I) |
| 28 | I->second.second(I->second.first); |
| 29 | } |
| 30 | |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | // Basic symbolic analysis. This will eventually be refactored into a |
| 33 | // separate component. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | typedef llvm::ImmutableMap<SymbolID,GRState::IntSetTy> ConstNotEqTy; |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 37 | typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstEqTy; |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 38 | |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 39 | static int ConstEqTyIndex = 0; |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 40 | static int ConstNotEqTyIndex = 0; |
| 41 | |
| 42 | namespace clang { |
Ted Kremenek | 67ff8b9 | 2008-08-17 02:59:30 +0000 | [diff] [blame] | 43 | template<> |
| 44 | struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> { |
| 45 | static inline void* GDMIndex() { return &ConstNotEqTyIndex; } |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 46 | }; |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 47 | |
| 48 | template<> |
| 49 | struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> { |
| 50 | static inline void* GDMIndex() { return &ConstEqTyIndex; } |
| 51 | }; |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 54 | bool GRState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 55 | |
| 56 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 57 | const ConstNotEqTy::data_type* T = get<ConstNotEqTy>(sym); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 58 | |
| 59 | // See if V is present in the NE-set. |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 60 | return T ? T->contains(&V) : false; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 63 | bool GRState::isEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 64 | // Retrieve the EQ-set associated with the given symbol. |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 65 | const ConstEqTy::data_type* T = get<ConstEqTy>(sym); |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 66 | // See if V is present in the EQ-set. |
| 67 | return T ? **T == V : false; |
| 68 | } |
| 69 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 70 | const llvm::APSInt* GRState::getSymVal(SymbolID sym) const { |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 71 | const ConstEqTy::data_type* T = get<ConstEqTy>(sym); |
Ted Kremenek | 6064a36 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 72 | return T ? *T : NULL; |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 75 | const GRState* |
| 76 | GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc, |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 77 | const LiveVariables& Liveness, |
| 78 | DeadSymbolsTy& DSymbols) { |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 79 | |
| 80 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 81 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 82 | // tells us are live. We then see what Decls they may reference, and keep |
| 83 | // those around. This code more than likely can be made faster, and the |
| 84 | // frequency of which this method is called should be experimented with |
Ted Kremenek | ee93012 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 85 | // for optimum performance. |
| 86 | DRoots.clear(); |
| 87 | StoreManager::LiveSymbolsTy LSymbols; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 88 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 89 | GRState NewSt = *St; |
Ted Kremenek | ee93012 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 90 | |
Ted Kremenek | 3a53916 | 2008-08-20 17:08:29 +0000 | [diff] [blame^] | 91 | NewSt.Env = EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, |
| 92 | DRoots, LSymbols); |
Ted Kremenek | 08cfd83 | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | ee93012 | 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 | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 98 | |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 99 | |
| 100 | GRStateRef state(getPersistentState(NewSt), *this); |
| 101 | |
Ted Kremenek | ee93012 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 102 | // Remove the dead symbols from the symbol tracker. |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 103 | // FIXME: Refactor into something else that manages symbol values. |
Ted Kremenek | 7487f94 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 105 | ConstEqTy CE = state.get<ConstEqTy>(); |
| 106 | ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>(); |
| 107 | |
| 108 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 109 | SymbolID sym = I.getKey(); |
Ted Kremenek | ee93012 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 110 | if (!LSymbols.count(sym)) { |
| 111 | DSymbols.insert(sym); |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 112 | CE = CEFactory.Remove(CE, sym); |
Ted Kremenek | 7487f94 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
Ted Kremenek | 2b697d0 | 2008-08-20 16:59:15 +0000 | [diff] [blame] | 115 | state = state.set<ConstEqTy>(CE); |
| 116 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 117 | ConstNotEqTy CNE = state.get<ConstNotEqTy>(); |
| 118 | ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>(); |
| 119 | |
| 120 | for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) { |
| 121 | SymbolID sym = I.getKey(); |
Ted Kremenek | ee93012 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 122 | if (!LSymbols.count(sym)) { |
| 123 | DSymbols.insert(sym); |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 124 | CNE = CNEFactory.Remove(CNE, sym); |
Ted Kremenek | 7487f94 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
Ted Kremenek | 0e39dcf | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 127 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 128 | return state.set<ConstNotEqTy>(CNE); |
Ted Kremenek | b8958d6 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 129 | } |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 130 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 131 | const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV, |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 132 | RVal V) { |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 133 | |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 134 | Store OldStore = St->getStore(); |
| 135 | Store NewStore = StMgr->SetRVal(OldStore, LV, V); |
Ted Kremenek | 9b32cd0 | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 137 | if (NewStore == OldStore) |
| 138 | return St; |
Ted Kremenek | bc965a6 | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 139 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 140 | GRState NewSt = *St; |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 141 | NewSt.St = NewStore; |
| 142 | return getPersistentState(NewSt); |
Ted Kremenek | 0eb0afa | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 145 | const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) { |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 146 | Store OldStore = St->getStore(); |
| 147 | Store NewStore = StMgr->Remove(OldStore, LV); |
| 148 | |
| 149 | if (NewStore == OldStore) |
| 150 | return St; |
| 151 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 152 | GRState NewSt = *St; |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 153 | NewSt.St = NewStore; |
| 154 | return getPersistentState(NewSt); |
| 155 | } |
| 156 | |
| 157 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 158 | const GRState* GRStateManager::AddNE(const GRState* St, SymbolID sym, |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 159 | const llvm::APSInt& V) { |
| 160 | |
| 161 | GRStateRef state(St, *this); |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 162 | |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 163 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 164 | ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym); |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 165 | GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 166 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 167 | // Now add V to the NE set. |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 168 | S = ISetFactory.Add(S, &V); |
| 169 | |
| 170 | // Create a new state with the old binding replaced. |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 171 | return state.set<ConstNotEqTy>(sym, S); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 174 | const GRState* GRStateManager::AddEQ(const GRState* St, SymbolID sym, |
Ted Kremenek | f22f868 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 175 | const llvm::APSInt& V) { |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 176 | // Create a new state with the old binding replaced. |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 177 | GRStateRef state(St, *this); |
| 178 | return state.set<ConstEqTy>(sym, &V); |
Ted Kremenek | 13f3156 | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 181 | const GRState* GRStateManager::getInitialState() { |
Ted Kremenek | ad5d5c5 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 182 | |
Ted Kremenek | e2fb8c7 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 183 | GRState StateImpl(EnvMgr.getInitialEnvironment(), |
| 184 | StMgr->getInitialStore(*this), |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 185 | GDMFactory.GetEmptyMap()); |
Ted Kremenek | e2fb8c7 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 187 | return getPersistentState(StateImpl); |
| 188 | } |
| 189 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 190 | const GRState* GRStateManager::getPersistentState(GRState& State) { |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 191 | |
| 192 | llvm::FoldingSetNodeID ID; |
| 193 | State.Profile(ID); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 194 | void* InsertPos; |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 195 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 196 | if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 197 | return I; |
| 198 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 199 | GRState* I = (GRState*) Alloc.Allocate<GRState>(); |
| 200 | new (I) GRState(State); |
Ted Kremenek | 2d8dce3 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 201 | StateSet.InsertNode(I, InsertPos); |
| 202 | return I; |
| 203 | } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 204 | |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 206 | //===----------------------------------------------------------------------===// |
| 207 | // State pretty-printing. |
| 208 | //===----------------------------------------------------------------------===// |
Ted Kremenek | d365649 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | bdff9a9 | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 210 | void GRState::print(std::ostream& Out, StoreManager& StoreMgr, |
| 211 | Printer** Beg, Printer** End, |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 212 | const char* nl, const char* sep) const { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 213 | |
Ted Kremenek | bdff9a9 | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 214 | // Print the store. |
| 215 | StoreMgr.print(getStore(), Out, nl, sep); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 216 | |
| 217 | // Print Subexpression bindings. |
Ted Kremenek | bdff9a9 | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 218 | bool isFirst = true; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 219 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 220 | for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 221 | |
| 222 | if (isFirst) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 223 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 224 | isFirst = false; |
| 225 | } |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 226 | else { Out << nl; } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 227 | |
| 228 | Out << " (" << (void*) I.getKey() << ") "; |
| 229 | I.getKey()->printPretty(Out); |
| 230 | Out << " : "; |
| 231 | I.getData().print(Out); |
| 232 | } |
| 233 | |
| 234 | // Print block-expression bindings. |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 235 | isFirst = true; |
| 236 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 237 | for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) { |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 238 | |
| 239 | if (isFirst) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 240 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 241 | isFirst = false; |
| 242 | } |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 243 | else { Out << nl; } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 244 | |
| 245 | Out << " (" << (void*) I.getKey() << ") "; |
| 246 | I.getKey()->printPretty(Out); |
| 247 | Out << " : "; |
| 248 | I.getData().print(Out); |
| 249 | } |
| 250 | |
| 251 | // Print equality constraints. |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 252 | // FIXME: Make just another printer do this. |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 253 | ConstEqTy CE = get<ConstEqTy>(); |
| 254 | |
| 255 | if (!CE.isEmpty()) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 256 | Out << nl << sep << "'==' constraints:"; |
Ted Kremenek | 80fcbb9 | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 257 | |
| 258 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 259 | Out << nl << " $" << I.getKey() |
Chris Lattner | ead053a | 2008-08-17 07:19:51 +0000 | [diff] [blame] | 260 | << " : " << *I.getData(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 261 | } |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 262 | |
| 263 | // Print != constraints. |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 264 | // FIXME: Make just another printer do this. |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 265 | |
| 266 | ConstNotEqTy CNE = get<ConstNotEqTy>(); |
| 267 | |
| 268 | if (!CNE.isEmpty()) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 269 | Out << nl << sep << "'!=' constraints:"; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 270 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 271 | for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) { |
Ted Kremenek | e1cfa99 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 272 | Out << nl << " $" << I.getKey() << " : "; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 273 | isFirst = true; |
| 274 | |
Ted Kremenek | 07baa25 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 275 | IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end(); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 276 | |
| 277 | for ( ; J != EJ; ++J) { |
| 278 | if (isFirst) isFirst = false; |
| 279 | else Out << ", "; |
| 280 | |
Chris Lattner | ead053a | 2008-08-17 07:19:51 +0000 | [diff] [blame] | 281 | Out << *J; |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
Ted Kremenek | d365649 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 285 | |
Ted Kremenek | bccfbcc | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 286 | // Print checker-specific data. |
| 287 | for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep); |
Ted Kremenek | 17c5f11 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 288 | } |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 289 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 290 | void GRStateRef::printDOT(std::ostream& Out) const { |
| 291 | print(Out, "\\l", "\\|"); |
| 292 | } |
| 293 | |
| 294 | void GRStateRef::printStdErr() const { |
| 295 | print(*llvm::cerr); |
| 296 | } |
| 297 | |
| 298 | void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{ |
| 299 | GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0]; |
| 300 | GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size(); |
Ted Kremenek | bdff9a9 | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 301 | St->print(Out, *Mgr->StMgr, beg, end, nl, sep); |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 304 | //===----------------------------------------------------------------------===// |
| 305 | // Generic Data Map. |
| 306 | //===----------------------------------------------------------------------===// |
| 307 | |
| 308 | void* const* GRState::FindGDM(void* K) const { |
| 309 | return GDM.lookup(K); |
| 310 | } |
| 311 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 312 | void* |
| 313 | GRStateManager::FindGDMContext(void* K, |
| 314 | void* (*CreateContext)(llvm::BumpPtrAllocator&), |
| 315 | void (*DeleteContext)(void*)) { |
| 316 | |
| 317 | std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; |
| 318 | if (!p.first) { |
| 319 | p.first = CreateContext(Alloc); |
| 320 | p.second = DeleteContext; |
| 321 | } |
| 322 | |
| 323 | return p.first; |
| 324 | } |
| 325 | |
Ted Kremenek | 4ae925c | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 326 | const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){ |
| 327 | GRState::GenericDataMap M1 = St->getGDM(); |
| 328 | GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data); |
| 329 | |
| 330 | if (M1 == M2) |
| 331 | return St; |
| 332 | |
| 333 | GRState NewSt = *St; |
| 334 | NewSt.GDM = M2; |
| 335 | return getPersistentState(NewSt); |
| 336 | } |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 337 | |
| 338 | //===----------------------------------------------------------------------===// |
| 339 | // Queries. |
| 340 | //===----------------------------------------------------------------------===// |
| 341 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 342 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 343 | const llvm::APSInt& Y) { |
| 344 | |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 345 | RVal V = GetRVal(state, Ex); |
| 346 | |
| 347 | if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V)) |
| 348 | return X->getValue() == Y; |
| 349 | |
| 350 | if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V)) |
| 351 | return X->getValue() == Y; |
| 352 | |
| 353 | if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V)) |
| 354 | return state->isEqual(X->getSymbol(), Y); |
| 355 | |
| 356 | if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V)) |
| 357 | return state->isEqual(X->getSymbol(), Y); |
| 358 | |
| 359 | return false; |
| 360 | } |
| 361 | |
Ted Kremenek | b0f2b9e | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 362 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) { |
Ted Kremenek | bbafa5b | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 363 | return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType())); |
| 364 | } |
| 365 | |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 366 | //===----------------------------------------------------------------------===// |
| 367 | // "Assume" logic. |
| 368 | //===----------------------------------------------------------------------===// |
| 369 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 370 | const GRState* GRStateManager::Assume(const GRState* St, LVal Cond, |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 371 | bool Assumption, bool& isFeasible) { |
| 372 | |
| 373 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 374 | |
| 375 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 376 | : St; |
| 377 | } |
| 378 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 379 | const GRState* GRStateManager::AssumeAux(const GRState* St, LVal Cond, |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 380 | bool Assumption, bool& isFeasible) { |
| 381 | |
| 382 | switch (Cond.getSubKind()) { |
| 383 | default: |
| 384 | assert (false && "'Assume' not implemented for this LVal."); |
| 385 | return St; |
| 386 | |
| 387 | case lval::SymbolValKind: |
| 388 | if (Assumption) |
| 389 | return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 390 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 391 | else |
| 392 | return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(), |
| 393 | BasicVals.getZeroWithPtrWidth(), isFeasible); |
| 394 | |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 395 | case lval::DeclValKind: |
| 396 | case lval::FuncValKind: |
| 397 | case lval::GotoLabelKind: |
| 398 | case lval::StringLiteralValKind: |
| 399 | isFeasible = Assumption; |
| 400 | return St; |
| 401 | |
| 402 | case lval::FieldOffsetKind: |
| 403 | return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(), |
| 404 | Assumption, isFeasible); |
| 405 | |
| 406 | case lval::ArrayOffsetKind: |
| 407 | return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(), |
| 408 | Assumption, isFeasible); |
| 409 | |
| 410 | case lval::ConcreteIntKind: { |
| 411 | bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0; |
| 412 | isFeasible = b ? Assumption : !Assumption; |
| 413 | return St; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 418 | const GRState* GRStateManager::Assume(const GRState* St, NonLVal Cond, |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 419 | bool Assumption, bool& isFeasible) { |
| 420 | |
| 421 | St = AssumeAux(St, Cond, Assumption, isFeasible); |
| 422 | |
| 423 | return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible) |
| 424 | : St; |
| 425 | } |
| 426 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 427 | const GRState* GRStateManager::AssumeAux(const GRState* St, NonLVal Cond, |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 428 | bool Assumption, bool& isFeasible) { |
| 429 | switch (Cond.getSubKind()) { |
| 430 | default: |
| 431 | assert (false && "'Assume' not implemented for this NonLVal."); |
| 432 | return St; |
| 433 | |
| 434 | |
| 435 | case nonlval::SymbolValKind: { |
| 436 | nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond); |
| 437 | SymbolID sym = SV.getSymbol(); |
| 438 | |
| 439 | if (Assumption) |
| 440 | return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 441 | isFeasible); |
| 442 | else |
| 443 | return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)), |
| 444 | isFeasible); |
| 445 | } |
| 446 | |
| 447 | case nonlval::SymIntConstraintValKind: |
| 448 | return |
| 449 | AssumeSymInt(St, Assumption, |
| 450 | cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(), |
| 451 | isFeasible); |
| 452 | |
| 453 | case nonlval::ConcreteIntKind: { |
| 454 | bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0; |
| 455 | isFeasible = b ? Assumption : !Assumption; |
| 456 | return St; |
| 457 | } |
| 458 | |
| 459 | case nonlval::LValAsIntegerKind: { |
| 460 | return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(), |
| 461 | Assumption, isFeasible); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 466 | |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 467 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 468 | const GRState* GRStateManager::AssumeSymInt(const GRState* St, |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 469 | bool Assumption, |
| 470 | const SymIntConstraint& C, |
| 471 | bool& isFeasible) { |
| 472 | |
| 473 | switch (C.getOpcode()) { |
| 474 | default: |
| 475 | // No logic yet for other operators. |
| 476 | isFeasible = true; |
| 477 | return St; |
| 478 | |
| 479 | case BinaryOperator::EQ: |
| 480 | if (Assumption) |
| 481 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
| 482 | else |
| 483 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 484 | |
| 485 | case BinaryOperator::NE: |
| 486 | if (Assumption) |
| 487 | return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 488 | else |
| 489 | return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible); |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 490 | |
| 491 | case BinaryOperator::GE: |
| 492 | if (Assumption) |
| 493 | return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 494 | else |
| 495 | return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible); |
| 496 | |
| 497 | case BinaryOperator::LE: |
| 498 | if (Assumption) |
| 499 | return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible); |
| 500 | else |
| 501 | return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible); |
Ted Kremenek | c746954 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 504 | |
| 505 | //===----------------------------------------------------------------------===// |
| 506 | // FIXME: This should go into a plug-in constraint engine. |
| 507 | //===----------------------------------------------------------------------===// |
| 508 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 509 | const GRState* |
| 510 | GRStateManager::AssumeSymNE(const GRState* St, SymbolID sym, |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 511 | const llvm::APSInt& V, bool& isFeasible) { |
| 512 | |
| 513 | // First, determine if sym == X, where X != V. |
| 514 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 515 | isFeasible = *X != V; |
| 516 | return St; |
| 517 | } |
| 518 | |
| 519 | // Second, determine if sym != V. |
| 520 | if (St->isNotEqual(sym, V)) { |
| 521 | isFeasible = true; |
| 522 | return St; |
| 523 | } |
| 524 | |
| 525 | // If we reach here, sym is not a constant and we don't know if it is != V. |
| 526 | // Make that assumption. |
| 527 | |
| 528 | isFeasible = true; |
| 529 | return AddNE(St, sym, V); |
| 530 | } |
| 531 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 532 | const GRState* |
| 533 | GRStateManager::AssumeSymEQ(const GRState* St, SymbolID sym, |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 534 | const llvm::APSInt& V, bool& isFeasible) { |
| 535 | |
| 536 | // First, determine if sym == X, where X != V. |
| 537 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 538 | isFeasible = *X == V; |
| 539 | return St; |
| 540 | } |
| 541 | |
| 542 | // Second, determine if sym != V. |
| 543 | if (St->isNotEqual(sym, V)) { |
| 544 | isFeasible = false; |
| 545 | return St; |
| 546 | } |
| 547 | |
| 548 | // If we reach here, sym is not a constant and we don't know if it is == V. |
| 549 | // Make that assumption. |
| 550 | |
| 551 | isFeasible = true; |
| 552 | return AddEQ(St, sym, V); |
| 553 | } |
| 554 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 555 | const GRState* |
| 556 | GRStateManager::AssumeSymLT(const GRState* St, SymbolID sym, |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 557 | const llvm::APSInt& V, bool& isFeasible) { |
| 558 | |
| 559 | // FIXME: For now have assuming x < y be the same as assuming sym != V; |
| 560 | return AssumeSymNE(St, sym, V, isFeasible); |
| 561 | } |
| 562 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 563 | const GRState* |
| 564 | GRStateManager::AssumeSymGT(const GRState* St, SymbolID sym, |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 565 | const llvm::APSInt& V, bool& isFeasible) { |
| 566 | |
| 567 | // FIXME: For now have assuming x > y be the same as assuming sym != V; |
| 568 | return AssumeSymNE(St, sym, V, isFeasible); |
| 569 | } |
| 570 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 571 | const GRState* |
| 572 | GRStateManager::AssumeSymGE(const GRState* St, SymbolID sym, |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 573 | const llvm::APSInt& V, bool& isFeasible) { |
| 574 | |
| 575 | // FIXME: Primitive logic for now. Only reject a path if the value of |
| 576 | // sym is a constant X and !(X >= V). |
| 577 | |
| 578 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 579 | isFeasible = *X >= V; |
| 580 | return St; |
| 581 | } |
| 582 | |
| 583 | isFeasible = true; |
| 584 | return St; |
| 585 | } |
| 586 | |
Ted Kremenek | abd89ac | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 587 | const GRState* |
| 588 | GRStateManager::AssumeSymLE(const GRState* St, SymbolID sym, |
Ted Kremenek | 26da970 | 2008-08-07 22:30:22 +0000 | [diff] [blame] | 589 | const llvm::APSInt& V, bool& isFeasible) { |
| 590 | |
| 591 | // FIXME: Primitive logic for now. Only reject a path if the value of |
| 592 | // sym is a constant X and !(X <= V). |
| 593 | |
| 594 | if (const llvm::APSInt* X = St->getSymVal(sym)) { |
| 595 | isFeasible = *X <= V; |
| 596 | return St; |
| 597 | } |
| 598 | |
| 599 | isFeasible = true; |
| 600 | return St; |
| 601 | } |
| 602 | |