blob: a359b23c5492273689cebb598bc04b4b98fe4231 [file] [log] [blame]
Zhongxing Xud19e21b2008-08-29 15:09:12 +00001//== BasicConstraintManager.cpp - Manage basic constraints.------*- C++ -*--==//
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//
10// This file defines BasicConstraintManager, a class that tracks simple
11// equality and inequality constraints on symbolic values of GRState.
12//
13//===----------------------------------------------------------------------===//
14
Zhongxing Xu30ad1672008-08-27 14:03:33 +000015#include "clang/Analysis/PathSensitive/ConstraintManager.h"
16#include "clang/Analysis/PathSensitive/GRState.h"
Zhongxing Xu39cfed32008-08-29 14:52:36 +000017#include "clang/Analysis/PathSensitive/GRStateTrait.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000018#include "llvm/Support/Compiler.h"
Zhongxing Xu39cfed32008-08-29 14:52:36 +000019#include "llvm/Support/raw_ostream.h"
Zhongxing Xu30ad1672008-08-27 14:03:33 +000020
21using namespace clang;
22
23namespace {
24
Zhongxing Xu39cfed32008-08-29 14:52:36 +000025typedef llvm::ImmutableMap<SymbolID,GRState::IntSetTy> ConstNotEqTy;
26typedef llvm::ImmutableMap<SymbolID,const llvm::APSInt*> ConstEqTy;
27
Zhongxing Xu30ad1672008-08-27 14:03:33 +000028// BasicConstraintManager only tracks equality and inequality constraints of
29// constants and integer variables.
30class VISIBILITY_HIDDEN BasicConstraintManager : public ConstraintManager {
Zhongxing Xu30ad1672008-08-27 14:03:33 +000031 GRStateManager& StateMgr;
32
33public:
34 BasicConstraintManager(GRStateManager& statemgr) : StateMgr(statemgr) {}
35
Zhongxing Xu1c96b242008-10-17 05:57:07 +000036 virtual const GRState* Assume(const GRState* St, SVal Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000037 bool Assumption, bool& isFeasible);
38
Zhongxing Xu1c96b242008-10-17 05:57:07 +000039 const GRState* Assume(const GRState* St, Loc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000040 bool& isFeasible);
41
Zhongxing Xu1c96b242008-10-17 05:57:07 +000042 const GRState* AssumeAux(const GRState* St, Loc Cond,bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000043 bool& isFeasible);
44
Zhongxing Xu1c96b242008-10-17 05:57:07 +000045 const GRState* Assume(const GRState* St, NonLoc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000046 bool& isFeasible);
47
Zhongxing Xu1c96b242008-10-17 05:57:07 +000048 const GRState* AssumeAux(const GRState* St, NonLoc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +000049 bool& isFeasible);
50
51 const GRState* AssumeSymInt(const GRState* St, bool Assumption,
52 const SymIntConstraint& C, bool& isFeasible);
53
54 const GRState* AssumeSymNE(const GRState* St, SymbolID sym,
55 const llvm::APSInt& V, bool& isFeasible);
56
57 const GRState* AssumeSymEQ(const GRState* St, SymbolID sym,
58 const llvm::APSInt& V, bool& isFeasible);
59
60 const GRState* AssumeSymLT(const GRState* St, SymbolID sym,
61 const llvm::APSInt& V, bool& isFeasible);
62
63 const GRState* AssumeSymGT(const GRState* St, SymbolID sym,
64 const llvm::APSInt& V, bool& isFeasible);
65
66 const GRState* AssumeSymGE(const GRState* St, SymbolID sym,
67 const llvm::APSInt& V, bool& isFeasible);
68
69 const GRState* AssumeSymLE(const GRState* St, SymbolID sym,
70 const llvm::APSInt& V, bool& isFeasible);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000071
Zhongxing Xue8a964b2008-11-22 13:21:46 +000072 const GRState* AssumeInBound(const GRState* St, SVal Idx, SVal UpperBound,
73 bool Assumption, bool& isFeasible);
74
Zhongxing Xu39cfed32008-08-29 14:52:36 +000075 const GRState* AddEQ(const GRState* St, SymbolID sym, const llvm::APSInt& V);
76
77 const GRState* AddNE(const GRState* St, SymbolID sym, const llvm::APSInt& V);
78
79 const llvm::APSInt* getSymVal(const GRState* St, SymbolID sym);
80 bool isNotEqual(const GRState* St, SymbolID sym, const llvm::APSInt& V) const;
81 bool isEqual(const GRState* St, SymbolID sym, const llvm::APSInt& V) const;
82
83 const GRState* RemoveDeadBindings(const GRState* St,
84 StoreManager::LiveSymbolsTy& LSymbols,
85 StoreManager::DeadSymbolsTy& DSymbols);
86
87 void print(const GRState* St, std::ostream& Out,
88 const char* nl, const char *sep);
Zhongxing Xue8a964b2008-11-22 13:21:46 +000089
90private:
91 BasicValueFactory& getBasicVals() { return StateMgr.getBasicVals(); }
Zhongxing Xu39cfed32008-08-29 14:52:36 +000092};
Zhongxing Xu30ad1672008-08-27 14:03:33 +000093
94} // end anonymous namespace
95
96ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& StateMgr)
97{
98 return new BasicConstraintManager(StateMgr);
99}
100
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000101const GRState* BasicConstraintManager::Assume(const GRState* St, SVal Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000102 bool Assumption, bool& isFeasible) {
103 if (Cond.isUnknown()) {
104 isFeasible = true;
105 return St;
106 }
107
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000108 if (isa<NonLoc>(Cond))
109 return Assume(St, cast<NonLoc>(Cond), Assumption, isFeasible);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000110 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000111 return Assume(St, cast<Loc>(Cond), Assumption, isFeasible);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000112}
113
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000114const GRState* BasicConstraintManager::Assume(const GRState* St, Loc Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000115 bool Assumption, bool& isFeasible) {
116 St = AssumeAux(St, Cond, Assumption, isFeasible);
117 // TF->EvalAssume(*this, St, Cond, Assumption, isFeasible)
118 return St;
119}
120
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000121const GRState* BasicConstraintManager::AssumeAux(const GRState* St, Loc Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000122 bool Assumption, bool& isFeasible) {
123 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
124
125 switch (Cond.getSubKind()) {
126 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000127 assert (false && "'Assume' not implemented for this Loc.");
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000128 return St;
129
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000130 case loc::SymbolValKind:
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000131 if (Assumption)
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000132 return AssumeSymNE(St, cast<loc::SymbolVal>(Cond).getSymbol(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000133 BasicVals.getZeroWithPtrWidth(), isFeasible);
134 else
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000135 return AssumeSymEQ(St, cast<loc::SymbolVal>(Cond).getSymbol(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000136 BasicVals.getZeroWithPtrWidth(), isFeasible);
137
Ted Kremenekc5234712008-10-17 21:22:20 +0000138 case loc::MemRegionKind: {
139 // FIXME: Should this go into the storemanager?
140
141 const MemRegion* R = cast<loc::MemRegionVal>(Cond).getRegion();
142
143 while (R) {
144 if (const SubRegion* SubR = dyn_cast<SubRegion>(R)) {
145 R = SubR->getSuperRegion();
146 continue;
147 }
148 else if (const SymbolicRegion* SymR = dyn_cast<SymbolicRegion>(R))
149 return AssumeAux(St, loc::SymbolVal(SymR->getSymbol()), Assumption,
150 isFeasible);
151
152 break;
153 }
154
155 // FALL-THROUGH.
156 }
157
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000158 case loc::FuncValKind:
159 case loc::GotoLabelKind:
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000160 isFeasible = Assumption;
161 return St;
162
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000163 case loc::ConcreteIntKind: {
164 bool b = cast<loc::ConcreteInt>(Cond).getValue() != 0;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000165 isFeasible = b ? Assumption : !Assumption;
166 return St;
167 }
168 } // end switch
169}
170
171const GRState*
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000172BasicConstraintManager::Assume(const GRState* St, NonLoc Cond, bool Assumption,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000173 bool& isFeasible) {
174 St = AssumeAux(St, Cond, Assumption, isFeasible);
175 // TF->EvalAssume() does nothing now.
176 return St;
177}
178
179const GRState*
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000180BasicConstraintManager::AssumeAux(const GRState* St,NonLoc Cond,
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000181 bool Assumption, bool& isFeasible) {
182 BasicValueFactory& BasicVals = StateMgr.getBasicVals();
183 SymbolManager& SymMgr = StateMgr.getSymbolManager();
184
185 switch (Cond.getSubKind()) {
186 default:
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000187 assert(false && "'Assume' not implemented for this NonLoc");
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000188
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000189 case nonloc::SymbolValKind: {
190 nonloc::SymbolVal& SV = cast<nonloc::SymbolVal>(Cond);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000191 SymbolID sym = SV.getSymbol();
192
193 if (Assumption)
194 return AssumeSymNE(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
195 isFeasible);
196 else
197 return AssumeSymEQ(St, sym, BasicVals.getValue(0, SymMgr.getType(sym)),
198 isFeasible);
199 }
200
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000201 case nonloc::SymIntConstraintValKind:
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000202 return
203 AssumeSymInt(St, Assumption,
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000204 cast<nonloc::SymIntConstraintVal>(Cond).getConstraint(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000205 isFeasible);
206
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000207 case nonloc::ConcreteIntKind: {
208 bool b = cast<nonloc::ConcreteInt>(Cond).getValue() != 0;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000209 isFeasible = b ? Assumption : !Assumption;
210 return St;
211 }
212
Zhongxing Xu1c96b242008-10-17 05:57:07 +0000213 case nonloc::LocAsIntegerKind:
214 return AssumeAux(St, cast<nonloc::LocAsInteger>(Cond).getLoc(),
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000215 Assumption, isFeasible);
216 } // end switch
217}
218
219const GRState*
220BasicConstraintManager::AssumeSymInt(const GRState* St, bool Assumption,
221 const SymIntConstraint& C, bool& isFeasible) {
222
223 switch (C.getOpcode()) {
224 default:
225 // No logic yet for other operators.
226 isFeasible = true;
227 return St;
228
229 case BinaryOperator::EQ:
230 if (Assumption)
231 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
232 else
233 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
234
235 case BinaryOperator::NE:
236 if (Assumption)
237 return AssumeSymNE(St, C.getSymbol(), C.getInt(), isFeasible);
238 else
239 return AssumeSymEQ(St, C.getSymbol(), C.getInt(), isFeasible);
240
Zhongxing Xu94b83122008-09-19 06:07:59 +0000241 case BinaryOperator::GT:
242 if (Assumption)
243 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
244 else
245 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
246
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000247 case BinaryOperator::GE:
248 if (Assumption)
249 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
250 else
251 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
252
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000253 case BinaryOperator::LT:
254 if (Assumption)
255 return AssumeSymLT(St, C.getSymbol(), C.getInt(), isFeasible);
256 else
257 return AssumeSymGE(St, C.getSymbol(), C.getInt(), isFeasible);
258
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000259 case BinaryOperator::LE:
260 if (Assumption)
261 return AssumeSymLE(St, C.getSymbol(), C.getInt(), isFeasible);
262 else
263 return AssumeSymGT(St, C.getSymbol(), C.getInt(), isFeasible);
264 } // end switch
265}
266
267const GRState*
268BasicConstraintManager::AssumeSymNE(const GRState* St, SymbolID sym,
269 const llvm::APSInt& V, bool& isFeasible) {
270 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000271 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000272 isFeasible = (*X != V);
273 return St;
274 }
275
276 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000277 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000278 isFeasible = true;
279 return St;
280 }
281
282 // If we reach here, sym is not a constant and we don't know if it is != V.
283 // Make that assumption.
284 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000285 return AddNE(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000286}
287
288const GRState*
289BasicConstraintManager::AssumeSymEQ(const GRState* St, SymbolID sym,
290 const llvm::APSInt& V, bool& isFeasible) {
291 // First, determine if sym == X, where X != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000292 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000293 isFeasible = *X == V;
294 return St;
295 }
296
297 // Second, determine if sym != V.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000298 if (isNotEqual(St, sym, V)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000299 isFeasible = false;
300 return St;
301 }
302
303 // If we reach here, sym is not a constant and we don't know if it is == V.
304 // Make that assumption.
305
306 isFeasible = true;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000307 return AddEQ(St, sym, V);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000308}
309
310// These logic will be handled in another ConstraintManager.
311const GRState*
312BasicConstraintManager::AssumeSymLT(const GRState* St, SymbolID sym,
313 const llvm::APSInt& V, bool& isFeasible) {
314
315 // FIXME: For now have assuming x < y be the same as assuming sym != V;
316 return AssumeSymNE(St, sym, V, isFeasible);
317}
318
319const GRState*
320BasicConstraintManager::AssumeSymGT(const GRState* St, SymbolID sym,
321 const llvm::APSInt& V, bool& isFeasible) {
322
323 // FIXME: For now have assuming x > y be the same as assuming sym != V;
324 return AssumeSymNE(St, sym, V, isFeasible);
325}
326
327const GRState*
328BasicConstraintManager::AssumeSymGE(const GRState* St, SymbolID sym,
329 const llvm::APSInt& V, bool& isFeasible) {
330
Ted Kremenek8c3e7fb2008-09-16 23:24:45 +0000331 // Reject a path if the value of sym is a constant X and !(X >= V).
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000332 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000333 isFeasible = *X >= V;
334 return St;
335 }
336
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000337 isFeasible = !isNotEqual(St, sym, V) ||
338 (V != llvm::APSInt::getMaxValue(V.getBitWidth(), V.isSigned()));
339
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000340 return St;
341}
342
343const GRState*
344BasicConstraintManager::AssumeSymLE(const GRState* St, SymbolID sym,
345 const llvm::APSInt& V, bool& isFeasible) {
346
347 // FIXME: Primitive logic for now. Only reject a path if the value of
348 // sym is a constant X and !(X <= V).
349
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000350 if (const llvm::APSInt* X = getSymVal(St, sym)) {
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000351 isFeasible = *X <= V;
352 return St;
353 }
Ted Kremenek0a41e5a2008-09-19 18:00:36 +0000354
355 isFeasible = !isNotEqual(St, sym, V) ||
356 (V != llvm::APSInt::getMinValue(V.getBitWidth(), V.isSigned()));
357
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000358 return St;
359}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000360
Zhongxing Xue8a964b2008-11-22 13:21:46 +0000361const GRState*
362BasicConstraintManager::AssumeInBound(const GRState* St, SVal Idx,
363 SVal UpperBound, bool Assumption,
364 bool& isFeasible) {
365 // Only support ConcreteInt for now.
366 if (!(isa<nonloc::ConcreteInt>(Idx) && isa<nonloc::ConcreteInt>(UpperBound))){
367 isFeasible = true;
368 return St;
369 }
370
371 const llvm::APSInt& Zero = getBasicVals().getZeroWithPtrWidth(false);
372 const llvm::APSInt& IdxV = cast<nonloc::ConcreteInt>(Idx).getValue();
373 const llvm::APSInt& UBV = cast<nonloc::ConcreteInt>(UpperBound).getValue();
374
375 bool InBound = (Zero <= IdxV) && (IdxV < UBV);
376
377 isFeasible = Assumption ? InBound : !InBound;
378
379 return St;
380}
381
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000382static int ConstEqTyIndex = 0;
383static int ConstNotEqTyIndex = 0;
384
385namespace clang {
386 template<>
387 struct GRStateTrait<ConstNotEqTy> : public GRStatePartialTrait<ConstNotEqTy> {
388 static inline void* GDMIndex() { return &ConstNotEqTyIndex; }
389 };
390
391 template<>
392 struct GRStateTrait<ConstEqTy> : public GRStatePartialTrait<ConstEqTy> {
393 static inline void* GDMIndex() { return &ConstEqTyIndex; }
394 };
395}
396
397const GRState* BasicConstraintManager::AddEQ(const GRState* St, SymbolID sym,
398 const llvm::APSInt& V) {
399 // Create a new state with the old binding replaced.
400 GRStateRef state(St, StateMgr);
401 return state.set<ConstEqTy>(sym, &V);
402}
403
404const GRState* BasicConstraintManager::AddNE(const GRState* St, SymbolID sym,
405 const llvm::APSInt& V) {
406 GRState::IntSetTy::Factory ISetFactory(StateMgr.getAllocator());
407 GRStateRef state(St, StateMgr);
408
409 // First, retrieve the NE-set associated with the given symbol.
410 ConstNotEqTy::data_type* T = state.get<ConstNotEqTy>(sym);
411 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
412
413
414 // Now add V to the NE set.
415 S = ISetFactory.Add(S, &V);
416
417 // Create a new state with the old binding replaced.
418 return state.set<ConstNotEqTy>(sym, S);
419}
420
421const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* St,
422 SymbolID sym) {
423 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
424 return T ? *T : NULL;
425}
426
427bool BasicConstraintManager::isNotEqual(const GRState* St, SymbolID sym,
428 const llvm::APSInt& V) const {
429
430 // Retrieve the NE-set associated with the given symbol.
431 const ConstNotEqTy::data_type* T = St->get<ConstNotEqTy>(sym);
432
433 // See if V is present in the NE-set.
434 return T ? T->contains(&V) : false;
435}
436
437bool BasicConstraintManager::isEqual(const GRState* St, SymbolID sym,
438 const llvm::APSInt& V) const {
439 // Retrieve the EQ-set associated with the given symbol.
440 const ConstEqTy::data_type* T = St->get<ConstEqTy>(sym);
441 // See if V is present in the EQ-set.
442 return T ? **T == V : false;
443}
444
445const GRState* BasicConstraintManager::RemoveDeadBindings(const GRState* St,
446 StoreManager::LiveSymbolsTy& LSymbols,
447 StoreManager::DeadSymbolsTy& DSymbols) {
448 GRStateRef state(St, StateMgr);
449 ConstEqTy CE = state.get<ConstEqTy>();
450 ConstEqTy::Factory& CEFactory = state.get_context<ConstEqTy>();
451
452 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
453 SymbolID sym = I.getKey();
454 if (!LSymbols.count(sym)) {
455 DSymbols.insert(sym);
456 CE = CEFactory.Remove(CE, sym);
457 }
458 }
459 state = state.set<ConstEqTy>(CE);
460
461 ConstNotEqTy CNE = state.get<ConstNotEqTy>();
462 ConstNotEqTy::Factory& CNEFactory = state.get_context<ConstNotEqTy>();
463
464 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
465 SymbolID sym = I.getKey();
466 if (!LSymbols.count(sym)) {
467 DSymbols.insert(sym);
468 CNE = CNEFactory.Remove(CNE, sym);
469 }
470 }
471
472 return state.set<ConstNotEqTy>(CNE);
473}
474
475void BasicConstraintManager::print(const GRState* St, std::ostream& Out,
476 const char* nl, const char *sep) {
477 // Print equality constraints.
478
479 ConstEqTy CE = St->get<ConstEqTy>();
480
481 if (!CE.isEmpty()) {
482 Out << nl << sep << "'==' constraints:";
483
484 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
485 Out << nl << " $" << I.getKey();
486 llvm::raw_os_ostream OS(Out);
487 OS << " : " << *I.getData();
488 }
489 }
490
491 // Print != constraints.
492
493 ConstNotEqTy CNE = St->get<ConstNotEqTy>();
494
495 if (!CNE.isEmpty()) {
496 Out << nl << sep << "'!=' constraints:";
497
498 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
499 Out << nl << " $" << I.getKey() << " : ";
500 bool isFirst = true;
501
502 GRState::IntSetTy::iterator J = I.getData().begin(),
503 EJ = I.getData().end();
504
505 for ( ; J != EJ; ++J) {
506 if (isFirst) isFirst = false;
507 else Out << ", ";
508
Zhongxing Xu7d94e262008-11-10 05:00:06 +0000509 Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000510 }
511 }
512 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000513}