blob: eee5c59466744b516b02420f76dd70039b4d5238 [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//
Mike Stump1eb44332009-09-09 15:08:12 +000010// This file defines BasicConstraintManager, a class that tracks simple
Zhongxing Xud19e21b2008-08-29 15:09:12 +000011// equality and inequality constraints on symbolic values of GRState.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek45021952009-02-14 17:08:39 +000015#include "SimpleConstraintManager.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/GRState.h"
17#include "clang/Checker/PathSensitive/GRStateTrait.h"
18#include "clang/Checker/PathSensitive/GRTransferFuncs.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
Ted Kremenek8ee74d52009-01-26 06:04:53 +000023
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000024namespace { class ConstNotEq {}; }
25namespace { class ConstEq {}; }
Zhongxing Xu30ad1672008-08-27 14:03:33 +000026
Ted Kremenek2dabd432008-12-05 02:27:51 +000027typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy;
28typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy;
Mike Stump1eb44332009-09-09 15:08:12 +000029
Ted Kremenek8ee74d52009-01-26 06:04:53 +000030static int ConstEqIndex = 0;
31static int ConstNotEqIndex = 0;
Zhongxing Xu39cfed32008-08-29 14:52:36 +000032
Ted Kremenek8ee74d52009-01-26 06:04:53 +000033namespace clang {
34template<>
35struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> {
Mike Stump1eb44332009-09-09 15:08:12 +000036 static inline void* GDMIndex() { return &ConstNotEqIndex; }
Ted Kremenek8ee74d52009-01-26 06:04:53 +000037};
38
39template<>
40struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> {
Mike Stump1eb44332009-09-09 15:08:12 +000041 static inline void* GDMIndex() { return &ConstEqIndex; }
Ted Kremenek8ee74d52009-01-26 06:04:53 +000042};
Mike Stump1eb44332009-09-09 15:08:12 +000043}
44
Ted Kremenek8ee74d52009-01-26 06:04:53 +000045namespace {
Zhongxing Xu30ad1672008-08-27 14:03:33 +000046// BasicConstraintManager only tracks equality and inequality constraints of
47// constants and integer variables.
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000048class BasicConstraintManager
Ted Kremenek45021952009-02-14 17:08:39 +000049 : public SimpleConstraintManager {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +000050 GRState::IntSetTy::Factory ISetFactory;
Zhongxing Xu30ad1672008-08-27 14:03:33 +000051public:
Ted Kremenek32a58082010-01-05 00:15:18 +000052 BasicConstraintManager(GRStateManager &statemgr, GRSubEngine &subengine)
53 : SimpleConstraintManager(subengine),
54 ISetFactory(statemgr.getAllocator()) {}
Zhongxing Xu30ad1672008-08-27 14:03:33 +000055
Ted Kremeneka591bc02009-06-18 22:57:13 +000056 const GRState* AssumeSymNE(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000057 const llvm::APSInt& V,
58 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000059
Ted Kremeneka591bc02009-06-18 22:57:13 +000060 const GRState* AssumeSymEQ(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000061 const llvm::APSInt& V,
62 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000063
Ted Kremeneka591bc02009-06-18 22:57:13 +000064 const GRState* AssumeSymLT(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000065 const llvm::APSInt& V,
66 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000067
Ted Kremeneka591bc02009-06-18 22:57:13 +000068 const GRState* AssumeSymGT(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000069 const llvm::APSInt& V,
70 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000071
Ted Kremeneka591bc02009-06-18 22:57:13 +000072 const GRState* AssumeSymGE(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000073 const llvm::APSInt& V,
74 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000075
Ted Kremeneka591bc02009-06-18 22:57:13 +000076 const GRState* AssumeSymLE(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000077 const llvm::APSInt& V,
78 const llvm::APSInt& Adjustment);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000079
Ted Kremeneka591bc02009-06-18 22:57:13 +000080 const GRState* AddEQ(const GRState* state, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000081
Ted Kremeneka591bc02009-06-18 22:57:13 +000082 const GRState* AddNE(const GRState* state, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000083
Ted Kremeneka591bc02009-06-18 22:57:13 +000084 const llvm::APSInt* getSymVal(const GRState* state, SymbolRef sym) const;
85 bool isNotEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V)
Ted Kremenek45021952009-02-14 17:08:39 +000086 const;
Ted Kremeneka591bc02009-06-18 22:57:13 +000087 bool isEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V)
Ted Kremenek45021952009-02-14 17:08:39 +000088 const;
Zhongxing Xu39cfed32008-08-29 14:52:36 +000089
Ted Kremeneka591bc02009-06-18 22:57:13 +000090 const GRState* RemoveDeadBindings(const GRState* state, SymbolReaper& SymReaper);
Ted Kremenek241677a2009-01-21 22:26:05 +000091
Mike Stump1eb44332009-09-09 15:08:12 +000092 void print(const GRState* state, llvm::raw_ostream& Out,
Zhongxing Xu39cfed32008-08-29 14:52:36 +000093 const char* nl, const char *sep);
94};
Zhongxing Xu30ad1672008-08-27 14:03:33 +000095
96} // end anonymous namespace
97
Ted Kremenek32a58082010-01-05 00:15:18 +000098ConstraintManager* clang::CreateBasicConstraintManager(GRStateManager& statemgr,
99 GRSubEngine &subengine) {
100 return new BasicConstraintManager(statemgr, subengine);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000101}
102
Jordy Roseba0f61c2010-06-18 22:49:11 +0000103
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000104const GRState*
Ted Kremeneka591bc02009-06-18 22:57:13 +0000105BasicConstraintManager::AssumeSymNE(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000106 const llvm::APSInt &V,
107 const llvm::APSInt &Adjustment) {
108 // First, determine if sym == X, where X+Adjustment != V.
109 llvm::APSInt Adjusted = V-Adjustment;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000110 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000111 bool isFeasible = (*X != Adjusted);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000112 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000113 }
114
Jordy Roseba0f61c2010-06-18 22:49:11 +0000115 // Second, determine if sym+Adjustment != V.
116 if (isNotEqual(state, sym, Adjusted))
Ted Kremeneka591bc02009-06-18 22:57:13 +0000117 return state;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000118
119 // If we reach here, sym is not a constant and we don't know if it is != V.
120 // Make that assumption.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000121 return AddNE(state, sym, Adjusted);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000122}
123
Jordy Roseba0f61c2010-06-18 22:49:11 +0000124const GRState*
125BasicConstraintManager::AssumeSymEQ(const GRState *state, SymbolRef sym,
126 const llvm::APSInt &V,
127 const llvm::APSInt &Adjustment) {
128 // First, determine if sym == X, where X+Adjustment != V.
129 llvm::APSInt Adjusted = V-Adjustment;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000130 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000131 bool isFeasible = (*X == Adjusted);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000132 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000133 }
134
Jordy Roseba0f61c2010-06-18 22:49:11 +0000135 // Second, determine if sym+Adjustment != V.
136 if (isNotEqual(state, sym, Adjusted))
Ted Kremeneka591bc02009-06-18 22:57:13 +0000137 return NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000138
139 // If we reach here, sym is not a constant and we don't know if it is == V.
140 // Make that assumption.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000141 return AddEQ(state, sym, Adjusted);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000142}
143
Jordy Roseba0f61c2010-06-18 22:49:11 +0000144// The logic for these will be handled in another ConstraintManager.
145const GRState*
146BasicConstraintManager::AssumeSymLT(const GRState *state, SymbolRef sym,
147 const llvm::APSInt &V,
148 const llvm::APSInt &Adjustment) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000149 // Is 'V' the smallest possible value?
Chris Lattner071e04e2009-01-30 01:58:33 +0000150 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000151 // sym cannot be any value less than 'V'. This path is infeasible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000152 return NULL;
Ted Kremenek73abd132008-12-03 18:56:12 +0000153 }
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000154
155 // FIXME: For now have assuming x < y be the same as assuming sym != V;
Jordy Roseba0f61c2010-06-18 22:49:11 +0000156 return AssumeSymNE(state, sym, V, Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000157}
158
Jordy Roseba0f61c2010-06-18 22:49:11 +0000159const GRState*
160BasicConstraintManager::AssumeSymGT(const GRState *state, SymbolRef sym,
161 const llvm::APSInt &V,
162 const llvm::APSInt &Adjustment) {
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000163 // Is 'V' the largest possible value?
Chris Lattner071e04e2009-01-30 01:58:33 +0000164 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000165 // sym cannot be any value greater than 'V'. This path is infeasible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000166 return NULL;
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000167 }
168
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000169 // FIXME: For now have assuming x > y be the same as assuming sym != V;
Jordy Roseba0f61c2010-06-18 22:49:11 +0000170 return AssumeSymNE(state, sym, V, Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000171}
172
Jordy Roseba0f61c2010-06-18 22:49:11 +0000173const GRState*
174BasicConstraintManager::AssumeSymGE(const GRState *state, SymbolRef sym,
175 const llvm::APSInt &V,
176 const llvm::APSInt &Adjustment) {
177 // Reject a path if the value of sym is a constant X and !(X+Adj >= V).
Ted Kremeneka591bc02009-06-18 22:57:13 +0000178 if (const llvm::APSInt *X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000179 bool isFeasible = (*X >= V-Adjustment);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000180 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000181 }
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000183 // Sym is not a constant, but it is worth looking to see if V is the
184 // maximum integer value.
Chris Lattner071e04e2009-01-30 01:58:33 +0000185 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000186 llvm::APSInt Adjusted = V-Adjustment;
187
188 // If we know that sym != V (after adjustment), then this condition
189 // is infeasible since there is no other value greater than V.
190 bool isFeasible = !isNotEqual(state, sym, Adjusted);
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000192 // If the path is still feasible then as a consequence we know that
Jordy Roseba0f61c2010-06-18 22:49:11 +0000193 // 'sym+Adjustment == V' because there are no larger values.
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000194 // Add this constraint.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000195 return isFeasible ? AddEQ(state, sym, Adjusted) : NULL;
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000196 }
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000197
Ted Kremeneka591bc02009-06-18 22:57:13 +0000198 return state;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000199}
200
201const GRState*
Jordy Roseba0f61c2010-06-18 22:49:11 +0000202BasicConstraintManager::AssumeSymLE(const GRState *state, SymbolRef sym,
203 const llvm::APSInt &V,
204 const llvm::APSInt &Adjustment) {
205 // Reject a path if the value of sym is a constant X and !(X+Adj <= V).
Ted Kremeneka591bc02009-06-18 22:57:13 +0000206 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000207 bool isFeasible = (*X <= V-Adjustment);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000208 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Ted Kremenek73abd132008-12-03 18:56:12 +0000211 // Sym is not a constant, but it is worth looking to see if V is the
212 // minimum integer value.
Chris Lattner071e04e2009-01-30 01:58:33 +0000213 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000214 llvm::APSInt Adjusted = V-Adjustment;
215
216 // If we know that sym != V (after adjustment), then this condition
217 // is infeasible since there is no other value less than V.
218 bool isFeasible = !isNotEqual(state, sym, Adjusted);
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Ted Kremenek73abd132008-12-03 18:56:12 +0000220 // If the path is still feasible then as a consequence we know that
Jordy Roseba0f61c2010-06-18 22:49:11 +0000221 // 'sym+Adjustment == V' because there are no smaller values.
Ted Kremenek73abd132008-12-03 18:56:12 +0000222 // Add this constraint.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000223 return isFeasible ? AddEQ(state, sym, Adjusted) : NULL;
Ted Kremenek73abd132008-12-03 18:56:12 +0000224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremeneka591bc02009-06-18 22:57:13 +0000226 return state;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000227}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000228
Ted Kremenekc8781382009-06-17 22:28:13 +0000229const GRState* BasicConstraintManager::AddEQ(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000230 const llvm::APSInt& V) {
231 // Create a new state with the old binding replaced.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000232 return state->set<ConstEq>(sym, &state->getBasicVals().getValue(V));
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000233}
234
Ted Kremenekc8781382009-06-17 22:28:13 +0000235const GRState* BasicConstraintManager::AddNE(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000236 const llvm::APSInt& V) {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +0000237
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000238 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenekc8781382009-06-17 22:28:13 +0000239 ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000240 GRState::IntSetTy S = T ? *T : ISetFactory.GetEmptySet();
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000242 // Now add V to the NE set.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000243 S = ISetFactory.Add(S, &state->getBasicVals().getValue(V));
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000245 // Create a new state with the old binding replaced.
Ted Kremenekc8781382009-06-17 22:28:13 +0000246 return state->set<ConstNotEq>(sym, S);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000247}
248
Ted Kremeneka591bc02009-06-18 22:57:13 +0000249const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* state,
Ted Kremenek45021952009-02-14 17:08:39 +0000250 SymbolRef sym) const {
Ted Kremeneka591bc02009-06-18 22:57:13 +0000251 const ConstEqTy::data_type* T = state->get<ConstEq>(sym);
Ted Kremenek45021952009-02-14 17:08:39 +0000252 return T ? *T : NULL;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000253}
254
Mike Stump1eb44332009-09-09 15:08:12 +0000255bool BasicConstraintManager::isNotEqual(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000256 const llvm::APSInt& V) const {
257
258 // Retrieve the NE-set associated with the given symbol.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000259 const ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000260
261 // See if V is present in the NE-set.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000262 return T ? T->contains(&state->getBasicVals().getValue(V)) : false;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000263}
264
Ted Kremeneka591bc02009-06-18 22:57:13 +0000265bool BasicConstraintManager::isEqual(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000266 const llvm::APSInt& V) const {
267 // Retrieve the EQ-set associated with the given symbol.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000268 const ConstEqTy::data_type* T = state->get<ConstEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000269 // See if V is present in the EQ-set.
270 return T ? **T == V : false;
271}
272
Zhongxing Xu8fd9b352008-11-27 02:39:34 +0000273/// Scan all symbols referenced by the constraints. If the symbol is not alive
274/// as marked in LSymbols, mark it as dead in DSymbols.
Ted Kremenek241677a2009-01-21 22:26:05 +0000275const GRState*
Ted Kremenekc8781382009-06-17 22:28:13 +0000276BasicConstraintManager::RemoveDeadBindings(const GRState* state,
Ted Kremenek241677a2009-01-21 22:26:05 +0000277 SymbolReaper& SymReaper) {
278
Ted Kremenekc8781382009-06-17 22:28:13 +0000279 ConstEqTy CE = state->get<ConstEq>();
280 ConstEqTy::Factory& CEFactory = state->get_context<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000281
282 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000283 SymbolRef sym = I.getKey();
284 if (SymReaper.maybeDead(sym)) CE = CEFactory.Remove(CE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000285 }
Ted Kremenekc8781382009-06-17 22:28:13 +0000286 state = state->set<ConstEq>(CE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000287
Ted Kremenekc8781382009-06-17 22:28:13 +0000288 ConstNotEqTy CNE = state->get<ConstNotEq>();
289 ConstNotEqTy::Factory& CNEFactory = state->get_context<ConstNotEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000290
291 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000292 SymbolRef sym = I.getKey();
Ted Kremenek241677a2009-01-21 22:26:05 +0000293 if (SymReaper.maybeDead(sym)) CNE = CNEFactory.Remove(CNE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000294 }
Mike Stump1eb44332009-09-09 15:08:12 +0000295
Ted Kremenekc8781382009-06-17 22:28:13 +0000296 return state->set<ConstNotEq>(CNE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000297}
298
Mike Stump1eb44332009-09-09 15:08:12 +0000299void BasicConstraintManager::print(const GRState* state, llvm::raw_ostream& Out,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000300 const char* nl, const char *sep) {
301 // Print equality constraints.
302
Ted Kremeneka591bc02009-06-18 22:57:13 +0000303 ConstEqTy CE = state->get<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000304
305 if (!CE.isEmpty()) {
306 Out << nl << sep << "'==' constraints:";
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000307 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I)
308 Out << nl << " $" << I.getKey() << " : " << *I.getData();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000309 }
310
311 // Print != constraints.
Mike Stump1eb44332009-09-09 15:08:12 +0000312
Ted Kremeneka591bc02009-06-18 22:57:13 +0000313 ConstNotEqTy CNE = state->get<ConstNotEq>();
Mike Stump1eb44332009-09-09 15:08:12 +0000314
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000315 if (!CNE.isEmpty()) {
316 Out << nl << sep << "'!=' constraints:";
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000318 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
319 Out << nl << " $" << I.getKey() << " : ";
320 bool isFirst = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
322 GRState::IntSetTy::iterator J = I.getData().begin(),
323 EJ = I.getData().end();
324
325 for ( ; J != EJ; ++J) {
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000326 if (isFirst) isFirst = false;
327 else Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Zhongxing Xu7d94e262008-11-10 05:00:06 +0000329 Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000330 }
331 }
332 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000333}