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 | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRTransferFuncs.h" |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallSet.h" |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 19 | |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Ted Kremenek | 05125f1 | 2008-08-27 23:13:01 +0000 | [diff] [blame] | 22 | // Give the vtable for ConstraintManager somewhere to live. |
| 23 | ConstraintManager::~ConstraintManager() {} |
| 24 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 25 | GRStateManager::~GRStateManager() { |
| 26 | for (std::vector<GRState::Printer*>::iterator I=Printers.begin(), |
| 27 | E=Printers.end(); I!=E; ++I) |
| 28 | delete *I; |
| 29 | |
| 30 | for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end(); |
| 31 | I!=E; ++I) |
| 32 | I->second.second(I->second.first); |
| 33 | } |
| 34 | |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | // Basic symbolic analysis. This will eventually be refactored into a |
| 37 | // separate component. |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
| 40 | typedef llvm::ImmutableMap<SymbolID,GRState::IntSetTy> ConstNotEqTy; |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 41 | typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstEqTy; |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 42 | |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 43 | static int ConstEqTyIndex = 0; |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 44 | static int ConstNotEqTyIndex = 0; |
| 45 | |
| 46 | namespace clang { |
Ted Kremenek | e7aa9a1 | 2008-08-17 02:59:30 +0000 | [diff] [blame] | 47 | template<> |
| 48 | struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> { |
| 49 | static inline void* GDMIndex() { return &ConstNotEqTyIndex; } |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 50 | }; |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 51 | |
| 52 | template<> |
| 53 | struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> { |
| 54 | static inline void* GDMIndex() { return &ConstEqTyIndex; } |
| 55 | }; |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 58 | bool GRState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 59 | |
| 60 | // Retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 61 | const ConstNotEqTy::data_type* T = get<ConstNotEqTy>(sym); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 62 | |
| 63 | // See if V is present in the NE-set. |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 64 | return T ? T->contains(&V) : false; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 67 | bool GRState::isEqual(SymbolID sym, const llvm::APSInt& V) const { |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 68 | // Retrieve the EQ-set associated with the given symbol. |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 69 | const ConstEqTy::data_type* T = get<ConstEqTy>(sym); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 70 | // See if V is present in the EQ-set. |
| 71 | return T ? **T == V : false; |
| 72 | } |
| 73 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 74 | const llvm::APSInt* GRState::getSymVal(SymbolID sym) const { |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 75 | const ConstEqTy::data_type* T = get<ConstEqTy>(sym); |
Ted Kremenek | e8fdc83 | 2008-07-07 16:21:19 +0000 | [diff] [blame] | 76 | return T ? *T : NULL; |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 79 | const GRState* |
| 80 | GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc, |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 81 | const LiveVariables& Liveness, |
| 82 | DeadSymbolsTy& DSymbols) { |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 83 | |
| 84 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 85 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 86 | // tells us are live. We then see what Decls they may reference, and keep |
| 87 | // those around. This code more than likely can be made faster, and the |
| 88 | // frequency of which this method is called should be experimented with |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 89 | // for optimum performance. |
| 90 | DRoots.clear(); |
| 91 | StoreManager::LiveSymbolsTy LSymbols; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 93 | GRState NewSt = *St; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 94 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 95 | NewSt.Env = EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, |
| 96 | DRoots, LSymbols); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 97 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 98 | // Clean up the store. |
| 99 | DSymbols.clear(); |
| 100 | NewSt.St = StMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness, DRoots, |
| 101 | LSymbols, DSymbols); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 103 | |
| 104 | GRStateRef state(getPersistentState(NewSt), *this); |
| 105 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 106 | // Remove the dead symbols from the symbol tracker. |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 107 | // FIXME: Refactor into something else that manages symbol values. |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 109 | ConstEqTy CE = state.get<ConstEqTy>(); |
| 110 | ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>(); |
| 111 | |
| 112 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 113 | SymbolID sym = I.getKey(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 114 | if (!LSymbols.count(sym)) { |
| 115 | DSymbols.insert(sym); |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 116 | CE = CEFactory.Remove(CE, sym); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Ted Kremenek | 4f7b483 | 2008-08-20 16:59:15 +0000 | [diff] [blame] | 119 | state = state.set<ConstEqTy>(CE); |
| 120 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 121 | ConstNotEqTy CNE = state.get<ConstNotEqTy>(); |
| 122 | ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>(); |
| 123 | |
| 124 | for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) { |
| 125 | SymbolID sym = I.getKey(); |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 126 | if (!LSymbols.count(sym)) { |
| 127 | DSymbols.insert(sym); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 128 | CNE = CNEFactory.Remove(CNE, sym); |
Ted Kremenek | 77d7ef8 | 2008-04-24 18:31:42 +0000 | [diff] [blame] | 129 | } |
| 130 | } |
Ted Kremenek | 90e1481 | 2008-02-14 23:25:54 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 132 | return state.set<ConstNotEqTy>(CNE); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 133 | } |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 134 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 135 | const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 136 | RVal V) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 137 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 138 | Store OldStore = St->getStore(); |
| 139 | Store NewStore = StMgr->SetRVal(OldStore, LV, V); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 140 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 141 | if (NewStore == OldStore) |
| 142 | return St; |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 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); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 149 | const GRState* GRStateManager::AddDecl(const GRState* St, const VarDecl* VD, |
| 150 | Expr* Ex, unsigned Count) { |
| 151 | Store OldStore = St->getStore(); |
| 152 | Store NewStore; |
| 153 | |
| 154 | if (Ex) |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 155 | NewStore = StMgr->AddDecl(OldStore, *this, VD, Ex, |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 156 | GetRVal(St, Ex), Count); |
| 157 | else |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 158 | NewStore = StMgr->AddDecl(OldStore, *this, VD, Ex); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 159 | |
| 160 | if (NewStore == OldStore) |
| 161 | return St; |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 162 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 163 | GRState NewSt = *St; |
| 164 | NewSt.St = NewStore; |
| 165 | return getPersistentState(NewSt); |
| 166 | } |
| 167 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 168 | const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 169 | Store OldStore = St->getStore(); |
| 170 | Store NewStore = StMgr->Remove(OldStore, LV); |
| 171 | |
| 172 | if (NewStore == OldStore) |
| 173 | return St; |
| 174 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 175 | GRState NewSt = *St; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 176 | NewSt.St = NewStore; |
| 177 | return getPersistentState(NewSt); |
| 178 | } |
| 179 | |
| 180 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 181 | const GRState* GRStateManager::AddNE(const GRState* St, SymbolID sym, |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 182 | const llvm::APSInt& V) { |
| 183 | |
| 184 | GRStateRef state(St, *this); |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 185 | |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 186 | // First, retrieve the NE-set associated with the given symbol. |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 187 | ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym); |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 188 | GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet(); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 189 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 190 | // Now add V to the NE set. |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 191 | S = ISetFactory.Add(S, &V); |
| 192 | |
| 193 | // Create a new state with the old binding replaced. |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 194 | return state.set<ConstNotEqTy>(sym, S); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 197 | const GRState* GRStateManager::AddEQ(const GRState* St, SymbolID sym, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 198 | const llvm::APSInt& V) { |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 199 | // Create a new state with the old binding replaced. |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 200 | GRStateRef state(St, *this); |
| 201 | return state.set<ConstEqTy>(sym, &V); |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 204 | const GRState* GRStateManager::getInitialState() { |
Ted Kremenek | 5a7b382 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 206 | GRState StateImpl(EnvMgr.getInitialEnvironment(), |
| 207 | StMgr->getInitialStore(*this), |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 208 | GDMFactory.GetEmptyMap()); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 209 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 210 | return getPersistentState(StateImpl); |
| 211 | } |
| 212 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 213 | const GRState* GRStateManager::getPersistentState(GRState& State) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 214 | |
| 215 | llvm::FoldingSetNodeID ID; |
| 216 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 217 | void* InsertPos; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 218 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 219 | if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 220 | return I; |
| 221 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 222 | GRState* I = (GRState*) Alloc.Allocate<GRState>(); |
| 223 | new (I) GRState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 224 | StateSet.InsertNode(I, InsertPos); |
| 225 | return I; |
| 226 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 228 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
| 230 | // State pretty-printing. |
| 231 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 232 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 233 | void GRState::print(std::ostream& Out, StoreManager& StoreMgr, |
| 234 | Printer** Beg, Printer** End, |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 235 | const char* nl, const char* sep) const { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 236 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 237 | // Print the store. |
| 238 | StoreMgr.print(getStore(), Out, nl, sep); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 239 | |
| 240 | // Print Subexpression bindings. |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 241 | bool isFirst = true; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 242 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 243 | for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 244 | |
| 245 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 246 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 247 | isFirst = false; |
| 248 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 249 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 250 | |
| 251 | Out << " (" << (void*) I.getKey() << ") "; |
| 252 | I.getKey()->printPretty(Out); |
| 253 | Out << " : "; |
| 254 | I.getData().print(Out); |
| 255 | } |
| 256 | |
| 257 | // Print block-expression bindings. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 258 | isFirst = true; |
| 259 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 260 | for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 261 | |
| 262 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 263 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 264 | isFirst = false; |
| 265 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 266 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 267 | |
| 268 | Out << " (" << (void*) I.getKey() << ") "; |
| 269 | I.getKey()->printPretty(Out); |
| 270 | Out << " : "; |
| 271 | I.getData().print(Out); |
| 272 | } |
| 273 | |
| 274 | // Print equality constraints. |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 275 | // FIXME: Make just another printer do this. |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 276 | ConstEqTy CE = get<ConstEqTy>(); |
| 277 | |
| 278 | if (!CE.isEmpty()) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 279 | Out << nl << sep << "'==' constraints:"; |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 280 | |
Chris Lattner | 405674c | 2008-08-23 22:23:37 +0000 | [diff] [blame] | 281 | for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) { |
| 282 | Out << nl << " $" << I.getKey(); |
| 283 | llvm::raw_os_ostream OS(Out); |
| 284 | OS << " : " << *I.getData(); |
| 285 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 286 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 287 | |
| 288 | // Print != constraints. |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 289 | // FIXME: Make just another printer do this. |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 290 | |
| 291 | ConstNotEqTy CNE = get<ConstNotEqTy>(); |
| 292 | |
| 293 | if (!CNE.isEmpty()) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 294 | Out << nl << sep << "'!=' constraints:"; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 295 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 296 | for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 297 | Out << nl << " $" << I.getKey() << " : "; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 298 | isFirst = true; |
| 299 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 300 | IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end(); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 301 | |
| 302 | for ( ; J != EJ; ++J) { |
| 303 | if (isFirst) isFirst = false; |
| 304 | else Out << ", "; |
| 305 | |
Chris Lattner | 9aa77f1 | 2008-08-17 07:19:51 +0000 | [diff] [blame] | 306 | Out << *J; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | } |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 310 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 311 | // Print checker-specific data. |
| 312 | for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 313 | } |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 314 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 315 | void GRStateRef::printDOT(std::ostream& Out) const { |
| 316 | print(Out, "\\l", "\\|"); |
| 317 | } |
| 318 | |
| 319 | void GRStateRef::printStdErr() const { |
| 320 | print(*llvm::cerr); |
| 321 | } |
| 322 | |
| 323 | void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{ |
| 324 | GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0]; |
| 325 | GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size(); |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 326 | St->print(Out, *Mgr->StMgr, beg, end, nl, sep); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 329 | //===----------------------------------------------------------------------===// |
| 330 | // Generic Data Map. |
| 331 | //===----------------------------------------------------------------------===// |
| 332 | |
| 333 | void* const* GRState::FindGDM(void* K) const { |
| 334 | return GDM.lookup(K); |
| 335 | } |
| 336 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 337 | void* |
| 338 | GRStateManager::FindGDMContext(void* K, |
| 339 | void* (*CreateContext)(llvm::BumpPtrAllocator&), |
| 340 | void (*DeleteContext)(void*)) { |
| 341 | |
| 342 | std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; |
| 343 | if (!p.first) { |
| 344 | p.first = CreateContext(Alloc); |
| 345 | p.second = DeleteContext; |
| 346 | } |
| 347 | |
| 348 | return p.first; |
| 349 | } |
| 350 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 351 | const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){ |
| 352 | GRState::GenericDataMap M1 = St->getGDM(); |
| 353 | GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data); |
| 354 | |
| 355 | if (M1 == M2) |
| 356 | return St; |
| 357 | |
| 358 | GRState NewSt = *St; |
| 359 | NewSt.GDM = M2; |
| 360 | return getPersistentState(NewSt); |
| 361 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 362 | |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | // Queries. |
| 365 | //===----------------------------------------------------------------------===// |
| 366 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 367 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 368 | const llvm::APSInt& Y) { |
| 369 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 370 | RVal V = GetRVal(state, Ex); |
| 371 | |
| 372 | if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V)) |
| 373 | return X->getValue() == Y; |
| 374 | |
| 375 | if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V)) |
| 376 | return X->getValue() == Y; |
| 377 | |
| 378 | if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V)) |
| 379 | return state->isEqual(X->getSymbol(), Y); |
| 380 | |
| 381 | if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V)) |
| 382 | return state->isEqual(X->getSymbol(), Y); |
| 383 | |
| 384 | return false; |
| 385 | } |
| 386 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 387 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) { |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 388 | return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType())); |
| 389 | } |