blob: 2704bf2f3712b7eee7e0a06a2f5f2e30aa354ac4 [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 Kremenek4adc81e2008-08-13 04:27:00 +0000205void GRState::printDOT(std::ostream& Out, CheckerStatePrinter* P) const {
Ted Kremenek461f9772008-03-11 18:57:24 +0000206 print(Out, P, "\\l", "\\|");
Ted Kremenek59894f92008-03-04 18:30:35 +0000207}
208
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000209void GRState::printStdErr(CheckerStatePrinter* P) const {
Ted Kremenek461f9772008-03-11 18:57:24 +0000210 print(*llvm::cerr, P);
211}
212
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000213void GRState::print(std::ostream& Out, CheckerStatePrinter* P,
Ted Kremenek461f9772008-03-11 18:57:24 +0000214 const char* nl, const char* sep) const {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000215
Ted Kremeneke7d22112008-02-11 19:21:59 +0000216 // Print Variable Bindings
Ted Kremenek59894f92008-03-04 18:30:35 +0000217 Out << "Variables:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000218
219 bool isFirst = true;
220
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000221 for (vb_iterator I = vb_begin(), E = vb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000222
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000223 if (isFirst) isFirst = false;
Ted Kremenek59894f92008-03-04 18:30:35 +0000224 else Out << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000225
226 Out << ' ' << I.getKey()->getName() << " : ";
227 I.getData().print(Out);
228 }
229
230 // Print Subexpression bindings.
231
232 isFirst = true;
233
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000234 for (seb_iterator I = seb_begin(), E = seb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000235
236 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000237 Out << nl << nl << "Sub-Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000238 isFirst = false;
239 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000240 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000241
242 Out << " (" << (void*) I.getKey() << ") ";
243 I.getKey()->printPretty(Out);
244 Out << " : ";
245 I.getData().print(Out);
246 }
247
248 // Print block-expression bindings.
249
250 isFirst = true;
251
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000252 for (beb_iterator I = beb_begin(), E = beb_end(); I != E; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000253
254 if (isFirst) {
Ted Kremenek59894f92008-03-04 18:30:35 +0000255 Out << nl << nl << "Block-level Expressions:" << nl;
Ted Kremeneke7d22112008-02-11 19:21:59 +0000256 isFirst = false;
257 }
Ted Kremenek59894f92008-03-04 18:30:35 +0000258 else { Out << nl; }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000259
260 Out << " (" << (void*) I.getKey() << ") ";
261 I.getKey()->printPretty(Out);
262 Out << " : ";
263 I.getData().print(Out);
264 }
265
266 // Print equality constraints.
267
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000268 if (!ConstEq.isEmpty()) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000269
Ted Kremenek59894f92008-03-04 18:30:35 +0000270 Out << nl << sep << "'==' constraints:";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000271
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000272 for (ConstEqTy::iterator I = ConstEq.begin(),
273 E = ConstEq.end(); I!=E; ++I) {
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000274
Ted Kremenek59894f92008-03-04 18:30:35 +0000275 Out << nl << " $" << I.getKey()
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000276 << " : " << I.getData()->toString();
277 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000278 }
Ted Kremeneke7d22112008-02-11 19:21:59 +0000279
280 // Print != constraints.
281
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000282 if (!ConstNotEq.isEmpty()) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000283
Ted Kremenek59894f92008-03-04 18:30:35 +0000284 Out << nl << sep << "'!=' constraints:";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000285
Ted Kremenekaed9b6a2008-02-28 10:21:43 +0000286 for (ConstNotEqTy::iterator I = ConstNotEq.begin(),
287 EI = ConstNotEq.end(); I != EI; ++I) {
Ted Kremeneke7d22112008-02-11 19:21:59 +0000288
Ted Kremenek59894f92008-03-04 18:30:35 +0000289 Out << nl << " $" << I.getKey() << " : ";
Ted Kremeneke7d22112008-02-11 19:21:59 +0000290 isFirst = true;
291
Ted Kremenekaa1c4e52008-02-21 18:02:17 +0000292 IntSetTy::iterator J = I.getData().begin(), EJ = I.getData().end();
Ted Kremeneke7d22112008-02-11 19:21:59 +0000293
294 for ( ; J != EJ; ++J) {
295 if (isFirst) isFirst = false;
296 else Out << ", ";
297
298 Out << (*J)->toString();
299 }
300 }
301 }
Ted Kremenek461f9772008-03-11 18:57:24 +0000302
303 // Print checker-specific data.
304
305 if (P && CheckerState)
306 P->PrintCheckerState(Out, CheckerState, nl, sep);
Ted Kremeneke7d22112008-02-11 19:21:59 +0000307}
Ted Kremenek729a9a22008-07-17 23:15:45 +0000308
Ted Kremenek584def72008-07-22 00:46:16 +0000309
310//===----------------------------------------------------------------------===//
311// Queries.
312//===----------------------------------------------------------------------===//
313
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000314bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek584def72008-07-22 00:46:16 +0000315 const llvm::APSInt& Y) {
316 RVal V = GetRVal(state, Ex);
317
318 if (lval::ConcreteInt* X = dyn_cast<lval::ConcreteInt>(&V))
319 return X->getValue() == Y;
320
321 if (nonlval::ConcreteInt* X = dyn_cast<nonlval::ConcreteInt>(&V))
322 return X->getValue() == Y;
323
324 if (nonlval::SymbolVal* X = dyn_cast<nonlval::SymbolVal>(&V))
325 return state->isEqual(X->getSymbol(), Y);
326
327 if (lval::SymbolVal* X = dyn_cast<lval::SymbolVal>(&V))
328 return state->isEqual(X->getSymbol(), Y);
329
330 return false;
331}
332
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000333bool GRStateManager::isEqual(const GRState* state, Expr* Ex,
Ted Kremenek584def72008-07-22 00:46:16 +0000334 uint64_t x) {
335 return isEqual(state, Ex, BasicVals.getValue(x, Ex->getType()));
336}
337
Ted Kremenek729a9a22008-07-17 23:15:45 +0000338//===----------------------------------------------------------------------===//
339// "Assume" logic.
340//===----------------------------------------------------------------------===//
341
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000342const GRState* GRStateManager::Assume(const GRState* St, LVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000343 bool Assumption, bool& isFeasible) {
344
345 St = AssumeAux(St, Cond, Assumption, isFeasible);
346
347 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
348 : St;
349}
350
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000351const GRState* GRStateManager::AssumeAux(const GRState* St, LVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000352 bool Assumption, bool& isFeasible) {
353
354 switch (Cond.getSubKind()) {
355 default:
356 assert (false && "'Assume' not implemented for this LVal.");
357 return St;
358
359 case lval::SymbolValKind:
360 if (Assumption)
361 return AssumeSymNE(St, cast<lval::SymbolVal>(Cond).getSymbol(),
362 BasicVals.getZeroWithPtrWidth(), isFeasible);
363 else
364 return AssumeSymEQ(St, cast<lval::SymbolVal>(Cond).getSymbol(),
365 BasicVals.getZeroWithPtrWidth(), isFeasible);
366
367
368 case lval::DeclValKind:
369 case lval::FuncValKind:
370 case lval::GotoLabelKind:
371 case lval::StringLiteralValKind:
372 isFeasible = Assumption;
373 return St;
374
375 case lval::FieldOffsetKind:
376 return AssumeAux(St, cast<lval::FieldOffset>(Cond).getBase(),
377 Assumption, isFeasible);
378
379 case lval::ArrayOffsetKind:
380 return AssumeAux(St, cast<lval::ArrayOffset>(Cond).getBase(),
381 Assumption, isFeasible);
382
383 case lval::ConcreteIntKind: {
384 bool b = cast<lval::ConcreteInt>(Cond).getValue() != 0;
385 isFeasible = b ? Assumption : !Assumption;
386 return St;
387 }
388 }
389}
390
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000391const GRState* GRStateManager::Assume(const GRState* St, NonLVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000392 bool Assumption, bool& isFeasible) {
393
394 St = AssumeAux(St, Cond, Assumption, isFeasible);
395
396 return isFeasible ? TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
397 : St;
398}
399
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000400const GRState* GRStateManager::AssumeAux(const GRState* St, NonLVal Cond,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000401 bool Assumption, bool& isFeasible) {
402 switch (Cond.getSubKind()) {
403 default:
404 assert (false && "'Assume' not implemented for this NonLVal.");
405 return St;
406
407
408 case nonlval::SymbolValKind: {
409 nonlval::SymbolVal& SV = cast<nonlval::SymbolVal>(Cond);
410 SymbolID sym = SV.getSymbol();
411
412 if (Assumption)
413 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
414 isFeasible);
415 else
416 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
417 isFeasible);
418 }
419
420 case nonlval::SymIntConstraintValKind:
421 return
422 AssumeSymInt(St, Assumption,
423 cast<nonlval::SymIntConstraintVal>(Cond).getConstraint(),
424 isFeasible);
425
426 case nonlval::ConcreteIntKind: {
427 bool b = cast<nonlval::ConcreteInt>(Cond).getValue() != 0;
428 isFeasible = b ? Assumption : !Assumption;
429 return St;
430 }
431
432 case nonlval::LValAsIntegerKind: {
433 return AssumeAux(St, cast<nonlval::LValAsInteger>(Cond).getLVal(),
434 Assumption, isFeasible);
435 }
436 }
437}
438
Ted Kremenek729a9a22008-07-17 23:15:45 +0000439
Ted Kremenek729a9a22008-07-17 23:15:45 +0000440
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000441const GRState* GRStateManager::AssumeSymInt(const GRState* St,
Ted Kremenek729a9a22008-07-17 23:15:45 +0000442 bool Assumption,
443 const SymIntConstraint& C,
444 bool& isFeasible) {
445
446 switch (C.getOpcode()) {
447 default:
448 // No logic yet for other operators.
449 isFeasible = true;
450 return St;
451
452 case BinaryOperator::EQ:
453 if (Assumption)
454 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
455 else
456 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
457
458 case BinaryOperator::NE:
459 if (Assumption)
460 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
461 else
462 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
Ted Kremenek2619be02008-08-07 22:30:22 +0000463
464 case BinaryOperator::GE:
465 if (Assumption)
466 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
467 else
468 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
469
470 case BinaryOperator::LE:
471 if (Assumption)
472 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
473 else
474 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
Ted Kremenek729a9a22008-07-17 23:15:45 +0000475 }
476}
Ted Kremenek2619be02008-08-07 22:30:22 +0000477
478//===----------------------------------------------------------------------===//
479// FIXME: This should go into a plug-in constraint engine.
480//===----------------------------------------------------------------------===//
481
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000482const GRState*
483GRStateManager::AssumeSymNE(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000484 const llvm::APSInt& V, bool& isFeasible) {
485
486 // First, determine if sym == X, where X != V.
487 if (const llvm::APSInt* X = St->getSymVal(sym)) {
488 isFeasible = *X != V;
489 return St;
490 }
491
492 // Second, determine if sym != V.
493 if (St->isNotEqual(sym, V)) {
494 isFeasible = true;
495 return St;
496 }
497
498 // If we reach here, sym is not a constant and we don't know if it is != V.
499 // Make that assumption.
500
501 isFeasible = true;
502 return AddNE(St, sym, V);
503}
504
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000505const GRState*
506GRStateManager::AssumeSymEQ(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000507 const llvm::APSInt& V, bool& isFeasible) {
508
509 // First, determine if sym == X, where X != V.
510 if (const llvm::APSInt* X = St->getSymVal(sym)) {
511 isFeasible = *X == V;
512 return St;
513 }
514
515 // Second, determine if sym != V.
516 if (St->isNotEqual(sym, V)) {
517 isFeasible = false;
518 return St;
519 }
520
521 // If we reach here, sym is not a constant and we don't know if it is == V.
522 // Make that assumption.
523
524 isFeasible = true;
525 return AddEQ(St, sym, V);
526}
527
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000528const GRState*
529GRStateManager::AssumeSymLT(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000530 const llvm::APSInt& V, bool& isFeasible) {
531
532 // FIXME: For now have assuming x < y be the same as assuming sym != V;
533 return AssumeSymNE(St, sym, V, isFeasible);
534}
535
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000536const GRState*
537GRStateManager::AssumeSymGT(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000538 const llvm::APSInt& V, bool& isFeasible) {
539
540 // FIXME: For now have assuming x > y be the same as assuming sym != V;
541 return AssumeSymNE(St, sym, V, isFeasible);
542}
543
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000544const GRState*
545GRStateManager::AssumeSymGE(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000546 const llvm::APSInt& V, bool& isFeasible) {
547
548 // FIXME: Primitive logic for now. Only reject a path if the value of
549 // sym is a constant X and !(X >= V).
550
551 if (const llvm::APSInt* X = St->getSymVal(sym)) {
552 isFeasible = *X >= V;
553 return St;
554 }
555
556 isFeasible = true;
557 return St;
558}
559
Ted Kremenek4adc81e2008-08-13 04:27:00 +0000560const GRState*
561GRStateManager::AssumeSymLE(const GRState* St, SymbolID sym,
Ted Kremenek2619be02008-08-07 22:30:22 +0000562 const llvm::APSInt& V, bool& isFeasible) {
563
564 // FIXME: Primitive logic for now. Only reject a path if the value of
565 // sym is a constant X and !(X <= V).
566
567 if (const llvm::APSInt* X = St->getSymVal(sym)) {
568 isFeasible = *X <= V;
569 return St;
570 }
571
572 isFeasible = true;
573 return St;
574}
575