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 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 35 | const GRState* |
| 36 | GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc, |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 37 | const LiveVariables& Liveness, |
| 38 | DeadSymbolsTy& DSymbols) { |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 39 | |
| 40 | // This code essentially performs a "mark-and-sweep" of the VariableBindings. |
| 41 | // The roots are any Block-level exprs and Decls that our liveness algorithm |
| 42 | // tells us are live. We then see what Decls they may reference, and keep |
| 43 | // those around. This code more than likely can be made faster, and the |
| 44 | // frequency of which this method is called should be experimented with |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 45 | // for optimum performance. |
| 46 | DRoots.clear(); |
| 47 | StoreManager::LiveSymbolsTy LSymbols; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 48 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 49 | GRState NewSt = *St; |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 50 | |
Ted Kremenek | df9cdf8 | 2008-08-20 17:08:29 +0000 | [diff] [blame] | 51 | NewSt.Env = EnvMgr.RemoveDeadBindings(NewSt.Env, Loc, Liveness, |
| 52 | DRoots, LSymbols); |
Ted Kremenek | 016f52f | 2008-02-08 21:10:02 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | f59bf48 | 2008-07-17 18:38:48 +0000 | [diff] [blame] | 54 | // Clean up the store. |
| 55 | DSymbols.clear(); |
| 56 | NewSt.St = StMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness, DRoots, |
| 57 | LSymbols, DSymbols); |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 58 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame^] | 59 | return ConstraintMgr->RemoveDeadBindings(getPersistentState(NewSt), |
| 60 | LSymbols, DSymbols); |
Ted Kremenek | b87d909 | 2008-02-08 19:17:19 +0000 | [diff] [blame] | 61 | } |
Ted Kremenek | 862d5bb | 2008-02-06 00:54:14 +0000 | [diff] [blame] | 62 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 63 | const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV, |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 64 | RVal V) { |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 66 | Store OldStore = St->getStore(); |
| 67 | Store NewStore = StMgr->SetRVal(OldStore, LV, V); |
Ted Kremenek | 3271f8d | 2008-02-07 04:16:04 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 69 | if (NewStore == OldStore) |
| 70 | return St; |
Ted Kremenek | 692416c | 2008-02-18 22:57:02 +0000 | [diff] [blame] | 71 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 72 | GRState NewSt = *St; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 73 | NewSt.St = NewStore; |
| 74 | return getPersistentState(NewSt); |
Ted Kremenek | f66ea2cd | 2008-02-04 21:59:22 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 77 | const GRState* GRStateManager::AddDecl(const GRState* St, const VarDecl* VD, |
| 78 | Expr* Ex, unsigned Count) { |
| 79 | Store OldStore = St->getStore(); |
| 80 | Store NewStore; |
| 81 | |
| 82 | if (Ex) |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 83 | NewStore = StMgr->AddDecl(OldStore, *this, VD, Ex, |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 84 | GetRVal(St, Ex), Count); |
| 85 | else |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 86 | NewStore = StMgr->AddDecl(OldStore, *this, VD, Ex); |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 87 | |
| 88 | if (NewStore == OldStore) |
| 89 | return St; |
Ted Kremenek | e53c069 | 2008-08-23 00:50:55 +0000 | [diff] [blame] | 90 | |
Zhongxing Xu | bbe8ff4 | 2008-08-21 22:34:01 +0000 | [diff] [blame] | 91 | GRState NewSt = *St; |
| 92 | NewSt.St = NewStore; |
| 93 | return getPersistentState(NewSt); |
| 94 | } |
| 95 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 96 | const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) { |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 97 | Store OldStore = St->getStore(); |
| 98 | Store NewStore = StMgr->Remove(OldStore, LV); |
| 99 | |
| 100 | if (NewStore == OldStore) |
| 101 | return St; |
| 102 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 103 | GRState NewSt = *St; |
Ted Kremenek | 4323a57 | 2008-07-10 22:03:41 +0000 | [diff] [blame] | 104 | NewSt.St = NewStore; |
| 105 | return getPersistentState(NewSt); |
| 106 | } |
| 107 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 108 | const GRState* GRStateManager::getInitialState() { |
Ted Kremenek | 5a7b382 | 2008-02-26 23:37:01 +0000 | [diff] [blame] | 109 | |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 110 | GRState StateImpl(EnvMgr.getInitialEnvironment(), |
| 111 | StMgr->getInitialStore(*this), |
Ted Kremenek | ffdbefd | 2008-08-17 03:10:22 +0000 | [diff] [blame] | 112 | GDMFactory.GetEmptyMap()); |
Ted Kremenek | caa3724 | 2008-08-19 16:51:45 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 114 | return getPersistentState(StateImpl); |
| 115 | } |
| 116 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 117 | const GRState* GRStateManager::getPersistentState(GRState& State) { |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 118 | |
| 119 | llvm::FoldingSetNodeID ID; |
| 120 | State.Profile(ID); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 121 | void* InsertPos; |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 123 | if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos)) |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 124 | return I; |
| 125 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 126 | GRState* I = (GRState*) Alloc.Allocate<GRState>(); |
| 127 | new (I) GRState(State); |
Ted Kremenek | 9153f73 | 2008-02-05 07:17:49 +0000 | [diff] [blame] | 128 | StateSet.InsertNode(I, InsertPos); |
| 129 | return I; |
| 130 | } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 131 | |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 132 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 133 | //===----------------------------------------------------------------------===// |
| 134 | // State pretty-printing. |
| 135 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 136 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 137 | void GRState::print(std::ostream& Out, StoreManager& StoreMgr, |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame^] | 138 | ConstraintManager& ConstraintMgr, |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 139 | Printer** Beg, Printer** End, |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 140 | const char* nl, const char* sep) const { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 141 | |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 142 | // Print the store. |
| 143 | StoreMgr.print(getStore(), Out, nl, sep); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 144 | |
| 145 | // Print Subexpression bindings. |
Ted Kremenek | a622d8c | 2008-08-19 22:24:03 +0000 | [diff] [blame] | 146 | bool isFirst = true; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 147 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 148 | for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 149 | |
| 150 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 151 | Out << nl << nl << "Sub-Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 152 | isFirst = false; |
| 153 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 154 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 155 | |
| 156 | Out << " (" << (void*) I.getKey() << ") "; |
| 157 | I.getKey()->printPretty(Out); |
| 158 | Out << " : "; |
| 159 | I.getData().print(Out); |
| 160 | } |
| 161 | |
| 162 | // Print block-expression bindings. |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 163 | isFirst = true; |
| 164 | |
Ted Kremenek | aa1c4e5 | 2008-02-21 18:02:17 +0000 | [diff] [blame] | 165 | for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) { |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 166 | |
| 167 | if (isFirst) { |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 168 | Out << nl << nl << "Block-level Expressions:" << nl; |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 169 | isFirst = false; |
| 170 | } |
Ted Kremenek | 59894f9 | 2008-03-04 18:30:35 +0000 | [diff] [blame] | 171 | else { Out << nl; } |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 172 | |
| 173 | Out << " (" << (void*) I.getKey() << ") "; |
| 174 | I.getKey()->printPretty(Out); |
| 175 | Out << " : "; |
| 176 | I.getData().print(Out); |
| 177 | } |
| 178 | |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame^] | 179 | ConstraintMgr.print(this, Out, nl, sep); |
Ted Kremenek | 461f977 | 2008-03-11 18:57:24 +0000 | [diff] [blame] | 180 | |
Ted Kremenek | ae6814e | 2008-08-13 21:24:49 +0000 | [diff] [blame] | 181 | // Print checker-specific data. |
| 182 | for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep); |
Ted Kremenek | e7d2211 | 2008-02-11 19:21:59 +0000 | [diff] [blame] | 183 | } |
Ted Kremenek | 729a9a2 | 2008-07-17 23:15:45 +0000 | [diff] [blame] | 184 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 185 | void GRStateRef::printDOT(std::ostream& Out) const { |
| 186 | print(Out, "\\l", "\\|"); |
| 187 | } |
| 188 | |
| 189 | void GRStateRef::printStdErr() const { |
| 190 | print(*llvm::cerr); |
| 191 | } |
| 192 | |
| 193 | void GRStateRef::print(std::ostream& Out, const char* nl, const char* sep)const{ |
| 194 | GRState::Printer **beg = Mgr->Printers.empty() ? 0 : &Mgr->Printers[0]; |
| 195 | GRState::Printer **end = !beg ? 0 : beg + Mgr->Printers.size(); |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame^] | 196 | St->print(Out, *Mgr->StMgr, *Mgr->ConstraintMgr, beg, end, nl, sep); |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 199 | //===----------------------------------------------------------------------===// |
| 200 | // Generic Data Map. |
| 201 | //===----------------------------------------------------------------------===// |
| 202 | |
| 203 | void* const* GRState::FindGDM(void* K) const { |
| 204 | return GDM.lookup(K); |
| 205 | } |
| 206 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 207 | void* |
| 208 | GRStateManager::FindGDMContext(void* K, |
| 209 | void* (*CreateContext)(llvm::BumpPtrAllocator&), |
| 210 | void (*DeleteContext)(void*)) { |
| 211 | |
| 212 | std::pair<void*, void (*)(void*)>& p = GDMContexts[K]; |
| 213 | if (!p.first) { |
| 214 | p.first = CreateContext(Alloc); |
| 215 | p.second = DeleteContext; |
| 216 | } |
| 217 | |
| 218 | return p.first; |
| 219 | } |
| 220 | |
Ted Kremenek | 72cd17f | 2008-08-14 21:16:54 +0000 | [diff] [blame] | 221 | const GRState* GRStateManager::addGDM(const GRState* St, void* Key, void* Data){ |
| 222 | GRState::GenericDataMap M1 = St->getGDM(); |
| 223 | GRState::GenericDataMap M2 = GDMFactory.Add(M1, Key, Data); |
| 224 | |
| 225 | if (M1 == M2) |
| 226 | return St; |
| 227 | |
| 228 | GRState NewSt = *St; |
| 229 | NewSt.GDM = M2; |
| 230 | return getPersistentState(NewSt); |
| 231 | } |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 232 | |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | // Queries. |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | |
Ted Kremenek | 4adc81e | 2008-08-13 04:27:00 +0000 | [diff] [blame] | 237 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 238 | const llvm::APSInt& Y) { |
| 239 | |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 240 | RVal V = GetRVal(state, Ex); |
| 241 | |
| 242 | if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V)) |
| 243 | return X->getValue() == Y; |
| 244 | |
| 245 | if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V)) |
| 246 | return X->getValue() == Y; |
| 247 | |
| 248 | if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V)) |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame^] | 249 | return ConstraintMgr->isEqual(state, X->getSymbol(), Y); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 250 | |
| 251 | if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V)) |
Zhongxing Xu | 39cfed3 | 2008-08-29 14:52:36 +0000 | [diff] [blame^] | 252 | return ConstraintMgr->isEqual(state, X->getSymbol(), Y); |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 253 | |
| 254 | return false; |
| 255 | } |
| 256 | |
Ted Kremenek | 1c72ef0 | 2008-08-16 00:49:49 +0000 | [diff] [blame] | 257 | bool GRStateManager::isEqual(const GRState* state, Expr* Ex, uint64_t x) { |
Ted Kremenek | 584def7 | 2008-07-22 00:46:16 +0000 | [diff] [blame] | 258 | return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType())); |
| 259 | } |