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