blob: 1c7df79c02966657b4547bf0386c1b65c462f450 [file] [log] [blame]
Ted Kremenek4adc81e2008-08-13 04:27:00 +00001//= GRState*cpp - Path-Sens. "State" for tracking valuues -----*- C++ -*--=//
Ted Kremenek9153f732008-02-05 07:17:49 +00002//
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 Kremenek4adc81e2008-08-13 04:27:00 +000010// This file defines SymbolID, ExprBindKey, and GRState*
Ted Kremenek9153f732008-02-05 07:17:49 +000011//
12//===----------------------------------------------------------------------===//
13
Ted Kremenek4adc81e2008-08-13 04:27:00 +000014#include "clang/Analysis/PathSensitive/GRState.h"
Ted Kremenek90e14812008-02-14 23:25:54 +000015#include "llvm/ADT/SmallSet.h"
Ted Kremenek729a9a22008-07-17 23:15:45 +000016#include "clang/Analysis/PathSensitive/GRTransferFuncs.h"
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +000017
18using namespace clang;
19
Ted Kremenek4adc81e2008-08-13 04:27:00 +000020bool GRState::isNotEqual(SymbolID sym, const llvm::APSInt& V) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000021
22 // Retrieve the NE-set associated with the given symbol.
Ted Kremeneke8fdc832008-07-07 16:21:19 +000023 const ConstNotEqTy::data_type* T = ConstNotEq.lookup(sym);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000024
25 // See if V is present in the NE-set.
Ted Kremeneke8fdc832008-07-07 16:21:19 +000026 return T ? T->contains(&V) : false;
Ted Kremenek862d5bb2008-02-06 00:54:14 +000027}
28
Ted Kremenek4adc81e2008-08-13 04:27:00 +000029bool GRState::isEqual(SymbolID sym, const llvm::APSInt& V) const {
Ted Kremenek584def72008-07-22 00:46:16 +000030
31 // Retrieve the EQ-set associated with the given symbol.
32 const ConstEqTy::data_type* T = ConstEq.lookup(sym);
33
34 // See if V is present in the EQ-set.
35 return T ? **T == V : false;
36}
37
Ted Kremenek4adc81e2008-08-13 04:27:00 +000038const llvm::APSInt* GRState::getSymVal(SymbolID sym) const {
Ted Kremeneke8fdc832008-07-07 16:21:19 +000039 ConstEqTy::data_type* T = ConstEq.lookup(sym);
40 return T ? *T : NULL;
Ted Kremenek862d5bb2008-02-06 00:54:14 +000041}
42
Ted Kremenek4adc81e2008-08-13 04:27:00 +000043const GRState*
44GRStateManager::RemoveDeadBindings(const GRState* St, Stmt* Loc,
Ted Kremenek77d7ef82008-04-24 18:31:42 +000045 const LiveVariables& Liveness,
Ted Kremenekf59bf482008-07-17 18:38:48 +000046 DeadSymbolsTy& DSymbols) {
Ted Kremenekb87d9092008-02-08 19:17:19 +000047
48 // This code essentially performs a "mark-and-sweep" of the VariableBindings.
49 // The roots are any Block-level exprs and Decls that our liveness algorithm
50 // tells us are live. We then see what Decls they may reference, and keep
51 // those around. This code more than likely can be made faster, and the
52 // frequency of which this method is called should be experimented with
Ted Kremenekf59bf482008-07-17 18:38:48 +000053 // for optimum performance.
54 DRoots.clear();
55 StoreManager::LiveSymbolsTy LSymbols;
Ted Kremeneke7d22112008-02-11 19:21:59 +000056
Ted Kremenek4adc81e2008-08-13 04:27:00 +000057 GRState NewSt = *St;
Ted Kremenekf59bf482008-07-17 18:38:48 +000058
59 // FIXME: Put this in environment.
60 // Clean up the environment.
Ted Kremeneke7d22112008-02-11 19:21:59 +000061
62 // Drop bindings for subexpressions.
Ted Kremenek8133a262008-07-08 21:46:56 +000063 NewSt.Env = EnvMgr.RemoveSubExprBindings(NewSt.Env);
Ted Kremeneke7d22112008-02-11 19:21:59 +000064
65 // Iterate over the block-expr bindings.
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000066
Ted Kremenek4adc81e2008-08-13 04:27:00 +000067 for (GRState::beb_iterator I = St->beb_begin(), E = St->beb_end();
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000068 I!=E ; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +000069 Expr* BlkExpr = I.getKey();
Ted Kremenekb87d9092008-02-08 19:17:19 +000070
Ted Kremeneke7d22112008-02-11 19:21:59 +000071 if (Liveness.isLive(Loc, BlkExpr)) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000072 RVal X = I.getData();
Ted Kremenek90e14812008-02-14 23:25:54 +000073
74 if (isa<lval::DeclVal>(X)) {
75 lval::DeclVal LV = cast<lval::DeclVal>(X);
Ted Kremenekf59bf482008-07-17 18:38:48 +000076 DRoots.push_back(LV.getDecl());
Ted Kremenekb87d9092008-02-08 19:17:19 +000077 }
Ted Kremenek90e14812008-02-14 23:25:54 +000078
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000079 for (RVal::symbol_iterator SI = X.symbol_begin(), SE = X.symbol_end();
80 SI != SE; ++SI) {
Ted Kremenekf59bf482008-07-17 18:38:48 +000081 LSymbols.insert(*SI);
Ted Kremenekaa1c4e52008-02-21 18:02:17 +000082 }
Ted Kremenekb87d9092008-02-08 19:17:19 +000083 }
Ted Kremenek05a23782008-02-26 19:05:15 +000084 else {
85 RVal X = I.getData();
86
Ted Kremenek4a4e5242008-02-28 09:25:22 +000087 if (X.isUndef() && cast<UndefinedVal>(X).getData())
Ted Kremenek05a23782008-02-26 19:05:15 +000088 continue;
89
Ted Kremenek8133a262008-07-08 21:46:56 +000090 NewSt.Env = EnvMgr.RemoveBlkExpr(NewSt.Env, BlkExpr);
Ted Kremenek05a23782008-02-26 19:05:15 +000091 }
Ted Kremenekb87d9092008-02-08 19:17:19 +000092 }
Ted Kremenek016f52f2008-02-08 21:10:02 +000093
Ted Kremenekf59bf482008-07-17 18:38:48 +000094 // Clean up the store.
95 DSymbols.clear();
96 NewSt.St = StMgr->RemoveDeadBindings(St->getStore(), Loc, Liveness, DRoots,
97 LSymbols, DSymbols);
Ted Kremenekb87d9092008-02-08 19:17:19 +000098
Ted Kremenekf59bf482008-07-17 18:38:48 +000099 // Remove the dead symbols from the symbol tracker.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000100 for (GRState::ce_iterator I = St->ce_begin(), E=St->ce_end(); I!=E; ++I) {
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000101
102 SymbolID sym = I.getKey();
103
Ted Kremenekf59bf482008-07-17 18:38:48 +0000104 if (!LSymbols.count(sym)) {
105 DSymbols.insert(sym);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000106 NewSt.ConstEq = CEFactory.Remove(NewSt.ConstEq, sym);
107 }
108 }
109
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000110 for (GRState::cne_iterator I = St->cne_begin(), E=St->cne_end(); I!=E;++I){
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000111
112 SymbolID sym = I.getKey();
113
Ted Kremenekf59bf482008-07-17 18:38:48 +0000114 if (!LSymbols.count(sym)) {
115 DSymbols.insert(sym);
Ted Kremenek77d7ef82008-04-24 18:31:42 +0000116 NewSt.ConstNotEq = CNEFactory.Remove(NewSt.ConstNotEq, sym);
117 }
118 }
Ted Kremenek90e14812008-02-14 23:25:54 +0000119
Ted Kremeneke7d22112008-02-11 19:21:59 +0000120 return getPersistentState(NewSt);
Ted Kremenekb87d9092008-02-08 19:17:19 +0000121}
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000122
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000123const GRState* GRStateManager::SetRVal(const GRState* St, LVal LV,
Ted Kremenek4323a572008-07-10 22:03:41 +0000124 RVal V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000125
Ted Kremenek4323a572008-07-10 22:03:41 +0000126 Store OldStore = St->getStore();
127 Store NewStore = StMgr->SetRVal(OldStore, LV, V);
Ted Kremenek3271f8d2008-02-07 04:16:04 +0000128
Ted Kremenek4323a572008-07-10 22:03:41 +0000129 if (NewStore == OldStore)
130 return St;
Ted Kremenek692416c2008-02-18 22:57:02 +0000131
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000132 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +0000133 NewSt.St = NewStore;
134 return getPersistentState(NewSt);
Ted Kremenekf66ea2cd2008-02-04 21:59:22 +0000135}
136
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000137const GRState* GRStateManager::Unbind(const GRState* St, LVal LV) {
Ted Kremenek4323a572008-07-10 22:03:41 +0000138 Store OldStore = St->getStore();
139 Store NewStore = StMgr->Remove(OldStore, LV);
140
141 if (NewStore == OldStore)
142 return St;
143
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000144 GRState NewSt = *St;
Ted Kremenek4323a572008-07-10 22:03:41 +0000145 NewSt.St = NewStore;
146 return getPersistentState(NewSt);
147}
148
149
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000150const GRState* GRStateManager::AddNE(const GRState* St, SymbolID sym,
Ted Kremenek4323a572008-07-10 22:03:41 +0000151 const llvm::APSInt& V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000152
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000153 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000154 GRState::ConstNotEqTy::data_type* T = St->ConstNotEq.lookup(sym);
155 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000156
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000157 // Now add V to the NE set.
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000158 S = ISetFactory.Add(S, &V);
159
160 // Create a new state with the old binding replaced.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000161 GRState NewSt = *St;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000162 NewSt.ConstNotEq = CNEFactory.Add(NewSt.ConstNotEq, sym, S);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000163
164 // Get the persistent copy.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000165 return getPersistentState(NewSt);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000166}
167
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000168const GRState* GRStateManager::AddEQ(const GRState* St, SymbolID sym,
Ted Kremenek4323a572008-07-10 22:03:41 +0000169 const llvm::APSInt& V) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000170
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000171 // Create a new state with the old binding replaced.
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000172 GRState NewSt = *St;
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000173 NewSt.ConstEq = CEFactory.Add(NewSt.ConstEq, sym, &V);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000174
175 // Get the persistent copy.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000176 return getPersistentState(NewSt);
Ted Kremenek862d5bb2008-02-06 00:54:14 +0000177}
178
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000179const GRState* GRStateManager::getInitialState() {
Ted Kremenek5a7b3822008-02-26 23:37:01 +0000180
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000181 GRState StateImpl(EnvMgr.getInitialEnvironment(),
Ted Kremenek4323a572008-07-10 22:03:41 +0000182 StMgr->getInitialStore(),
Ted Kremenek45020222008-08-12 21:49:24 +0000183 GDMFactory.GetEmptyMap(),
Ted Kremenek8133a262008-07-08 21:46:56 +0000184 CNEFactory.GetEmptyMap(),
185 CEFactory.GetEmptyMap());
Ted Kremenek9153f732008-02-05 07:17:49 +0000186
187 return getPersistentState(StateImpl);
188}
189
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000190const GRState* GRStateManager::getPersistentState(GRState& State) {
Ted Kremenek9153f732008-02-05 07:17:49 +0000191
192 llvm::FoldingSetNodeID ID;
193 State.Profile(ID);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000194 void* InsertPos;
Ted Kremenek9153f732008-02-05 07:17:49 +0000195
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000196 if (GRState* I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
Ted Kremenek9153f732008-02-05 07:17:49 +0000197 return I;
198
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000199 GRState* I = (GRState*) Alloc.Allocate<GRState>();
200 new (I) GRState(State);
Ted Kremenek9153f732008-02-05 07:17:49 +0000201 StateSet.InsertNode(I, InsertPos);
202 return I;
203}
Ted Kremeneke7d22112008-02-11 19:21:59 +0000204
Ted Kremenekae6814e2008-08-13 21:24:49 +0000205void GRState::printDOT(std::ostream& Out,
206 Printer** Beg, Printer** End) const {
207 print(Out, Beg, End, "\\l", "\\|");
Ted Kremenek59894f92008-03-04 18:30:35 +0000208}
209
Ted Kremenekae6814e2008-08-13 21:24:49 +0000210void GRState::printStdErr(Printer** Beg, Printer** End) const {
211 print(*llvm::cerr, Beg, End);
Ted Kremenek461f9772008-03-11 18:57:24 +0000212}
213
Ted Kremenekae6814e2008-08-13 21:24:49 +0000214void GRState::print(std::ostream& Out, Printer** Beg, Printer** End,
215 const char* nl, const char* sep) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000216
Ted Kremeneke7d22112008-02-11 19:21:59 +0000217 // Print Variable Bindings
Ted Kremenek59894f92008-03-04 18:30:35 +0000218 Out << "Variables:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000219
220 bool isFirst = true;
221
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000222 for (vb_iterator I = vb_begin(), E = vb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000223
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000224 if (isFirst) isFirst = false;
Ted Kremenek59894f92008-03-04 18:30:35 +0000225 else Out << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000226
227 Out << ' ' << I.getKey()->getName() << " : ";
228 I.getData().print(Out);
229 }
230
231 // Print Subexpression bindings.
232
233 isFirst = true;
234
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000235 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000236
237 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000238 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000239 isFirst = false;
240 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000241 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000242
243 Out << " (" << (void*) I.getKey() << ") ";
244 I.getKey()->printPretty(Out);
245 Out << " : ";
246 I.getData().print(Out);
247 }
248
249 // Print block-expression bindings.
250
251 isFirst = true;
252
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000253 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000254
255 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000256 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000257 isFirst = false;
258 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000259 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000260
261 Out << " (" << (void*) I.getKey() << ") ";
262 I.getKey()->printPretty(Out);
263 Out << " : ";
264 I.getData().print(Out);
265 }
266
267 // Print equality constraints.
Ted Kremenekae6814e2008-08-13 21:24:49 +0000268 // FIXME: Make just another printer do this.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000269
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000270 if (!ConstEq.isEmpty()) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000271
Ted Kremenek59894f92008-03-04 18:30:35 +0000272 Out << nl << sep << "'==' constraints:";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000273
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000274 for (ConstEqTy::iterator I = ConstEq.begin(),
275 E = ConstEq.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000276
Ted Kremenek59894f92008-03-04 18:30:35 +0000277 Out << nl << " $" << I.getKey()
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000278 << " : " << I.getData()->toString();
279 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000280 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000281
282 // Print != constraints.
Ted Kremenekae6814e2008-08-13 21:24:49 +0000283 // FIXME: Make just another printer do this.
Ted Kremeneke7d22112008-02-11 19:21:59 +0000284
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000285 if (!ConstNotEq.isEmpty()) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000286
Ted Kremenek59894f92008-03-04 18:30:35 +0000287 Out << nl << sep << "'!=' constraints:";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000288
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000289 for (ConstNotEqTy::iterator I = ConstNotEq.begin(),
290 EI = ConstNotEq.end(); I != EI; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000291
Ted Kremenek59894f92008-03-04 18:30:35 +0000292 Out << nl << " $" << I.getKey() << " : ";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000293 isFirst = true;
294
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000295 IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000296
297 for ( ; J != EJ; ++J) {
298 if (isFirst) isFirst = false;
299 else Out << ", ";
300
301 Out << (*J)->toString();
302 }
303 }
304 }
Ted Kremenek461f9772008-03-11 18:57:24 +0000305
Ted Kremenekae6814e2008-08-13 21:24:49 +0000306 // Print checker-specific data.
307 for ( ; Beg != End ; ++Beg) (*Beg)->Print(Out, this, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000308}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000309
Ted Kremenek584def72008-07-22 00:46:16 +0000310
311//===----------------------------------------------------------------------===//
312// Queries.
313//===----------------------------------------------------------------------===//
314
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000315bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek584def72008-07-22 00:46:16 +0000316 const llvm::APSInt& Y) {
317 RVal V = GetRVal(state, Ex);
318
319 if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V))
320 return X->getValue() == Y;
321
322 if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V))
323 return X->getValue() == Y;
324
325 if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V))
326 return state->isEqual(X->getSymbol(), Y);
327
328 if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V))
329 return state->isEqual(X->getSymbol(), Y);
330
331 return false;
332}
333
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000334bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek584def72008-07-22 00:46:16 +0000335 uint64_t x) {
336 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
337}
338
Ted Kremenek729a9a22008-07-17 23:15:45 +0000339//===----------------------------------------------------------------------===//
340// "Assume" logic.
341//===----------------------------------------------------------------------===//
342
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000343const GRState* GRStateManager::Assume(const GRState* St, LVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000344 bool Assumption, bool& isFeasible) {
345
346 St = AssumeAux(St, Cond, Assumption, isFeasible);
347
348 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
349 : St;
350}
351
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000352const GRState* GRStateManager::AssumeAux(const GRState* St, LVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000353 bool Assumption, bool& isFeasible) {
354
355 switch (Cond.getSubKind()) {
356 default:
357 assert (false && "'Assume' not implemented for this LVal.");
358 return St;
359
360 case lval::SymbolValKind:
361 if (Assumption)
362 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
363 BasicVals.getZeroWithPtrWidth(), isFeasible);
364 else
365 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
366 BasicVals.getZeroWithPtrWidth(), isFeasible);
367
368
369 case lval::DeclValKind:
370 case lval::FuncValKind:
371 case lval::GotoLabelKind:
372 case lval::StringLiteralValKind:
373 isFeasible = Assumption;
374 return St;
375
376 case lval::FieldOffsetKind:
377 return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(),
378 Assumption, isFeasible);
379
380 case lval::ArrayOffsetKind:
381 return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(),
382 Assumption, isFeasible);
383
384 case lval::ConcreteIntKind: {
385 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
386 isFeasible = b ? Assumption : !Assumption;
387 return St;
388 }
389 }
390}
391
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000392const GRState* GRStateManager::Assume(const GRState* St, NonLVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000393 bool Assumption, bool& isFeasible) {
394
395 St = AssumeAux(St, Cond, Assumption, isFeasible);
396
397 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
398 : St;
399}
400
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000401const GRState* GRStateManager::AssumeAux(const GRState* St, NonLVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000402 bool Assumption, bool& isFeasible) {
403 switch (Cond.getSubKind()) {
404 default:
405 assert (false && "'Assume' not implemented for this NonLVal.");
406 return St;
407
408
409 case nonlval::SymbolValKind: {
410 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
411 SymbolID sym = SV.getSymbol();
412
413 if (Assumption)
414 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
415 isFeasible);
416 else
417 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
418 isFeasible);
419 }
420
421 case nonlval::SymIntConstraintValKind:
422 return
423 AssumeSymInt(St, Assumption,
424 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
425 isFeasible);
426
427 case nonlval::ConcreteIntKind: {
428 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
429 isFeasible = b ? Assumption : !Assumption;
430 return St;
431 }
432
433 case nonlval::LValAsIntegerKind: {
434 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
435 Assumption, isFeasible);
436 }
437 }
438}
439
Ted Kremenek729a9a22008-07-17 23:15:45 +0000440
Ted Kremenek729a9a22008-07-17 23:15:45 +0000441
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000442const GRState* GRStateManager::AssumeSymInt(const GRState* St,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000443 bool Assumption,
444 const SymIntConstraint& C,
445 bool& isFeasible) {
446
447 switch (C.getOpcode()) {
448 default:
449 // No logic yet for other operators.
450 isFeasible = true;
451 return St;
452
453 case BinaryOperator::EQ:
454 if (Assumption)
455 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
456 else
457 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
458
459 case BinaryOperator::NE:
460 if (Assumption)
461 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
462 else
463 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
Ted Kremenek2619be02008-08-07 22:30:22 +0000464
465 case BinaryOperator::GE:
466 if (Assumption)
467 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
468 else
469 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
470
471 case BinaryOperator::LE:
472 if (Assumption)
473 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
474 else
475 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
Ted Kremenek729a9a22008-07-17 23:15:45 +0000476 }
477}
Ted Kremenek2619be02008-08-07 22:30:22 +0000478
479//===----------------------------------------------------------------------===//
480// FIXME: This should go into a plug-in constraint engine.
481//===----------------------------------------------------------------------===//
482
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000483const GRState*
484GRStateManager::AssumeSymNE(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000485 const llvm::APSInt& V, bool& isFeasible) {
486
487 // First, determine if sym == X, where X != V.
488 if (const llvm::APSInt* X = St->getSymVal(sym)) {
489 isFeasible = *X != V;
490 return St;
491 }
492
493 // Second, determine if sym != V.
494 if (St->isNotEqual(sym, V)) {
495 isFeasible = true;
496 return St;
497 }
498
499 // If we reach here, sym is not a constant and we don't know if it is != V.
500 // Make that assumption.
501
502 isFeasible = true;
503 return AddNE(St, sym, V);
504}
505
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000506const GRState*
507GRStateManager::AssumeSymEQ(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000508 const llvm::APSInt& V, bool& isFeasible) {
509
510 // First, determine if sym == X, where X != V.
511 if (const llvm::APSInt* X = St->getSymVal(sym)) {
512 isFeasible = *X == V;
513 return St;
514 }
515
516 // Second, determine if sym != V.
517 if (St->isNotEqual(sym, V)) {
518 isFeasible = false;
519 return St;
520 }
521
522 // If we reach here, sym is not a constant and we don't know if it is == V.
523 // Make that assumption.
524
525 isFeasible = true;
526 return AddEQ(St, sym, V);
527}
528
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000529const GRState*
530GRStateManager::AssumeSymLT(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000531 const llvm::APSInt& V, bool& isFeasible) {
532
533 // FIXME: For now have assuming x < y be the same as assuming sym != V;
534 return AssumeSymNE(St, sym, V, isFeasible);
535}
536
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000537const GRState*
538GRStateManager::AssumeSymGT(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000539 const llvm::APSInt& V, bool& isFeasible) {
540
541 // FIXME: For now have assuming x > y be the same as assuming sym != V;
542 return AssumeSymNE(St, sym, V, isFeasible);
543}
544
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000545const GRState*
546GRStateManager::AssumeSymGE(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000547 const llvm::APSInt& V, bool& isFeasible) {
548
549 // FIXME: Primitive logic for now. Only reject a path if the value of
550 // sym is a constant X and !(X >= V).
551
552 if (const llvm::APSInt* X = St->getSymVal(sym)) {
553 isFeasible = *X >= V;
554 return St;
555 }
556
557 isFeasible = true;
558 return St;
559}
560
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000561const GRState*
562GRStateManager::AssumeSymLE(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000563 const llvm::APSInt& V, bool& isFeasible) {
564
565 // FIXME: Primitive logic for now. Only reject a path if the value of
566 // sym is a constant X and !(X <= V).
567
568 if (const llvm::APSInt* X = St->getSymVal(sym)) {
569 isFeasible = *X <= V;
570 return St;
571 }
572
573 isFeasible = true;
574 return St;
575}
576