blob: ac289b89c2eb13159a3b16dd3828f2d241eba16e [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"
Argyrios Kyrtzidis98cabba2010-12-22 18:51:49 +000016#include "clang/GR/PathSensitive/GRState.h"
17#include "clang/GR/PathSensitive/GRStateTrait.h"
18#include "clang/GR/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;
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000022using namespace GR;
Zhongxing Xu30ad1672008-08-27 14:03:33 +000023
Ted Kremenek8ee74d52009-01-26 06:04:53 +000024
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000025namespace { class ConstNotEq {}; }
26namespace { class ConstEq {}; }
Zhongxing Xu30ad1672008-08-27 14:03:33 +000027
Ted Kremenek2dabd432008-12-05 02:27:51 +000028typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy;
29typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Ted Kremenek8ee74d52009-01-26 06:04:53 +000031static int ConstEqIndex = 0;
32static int ConstNotEqIndex = 0;
Zhongxing Xu39cfed32008-08-29 14:52:36 +000033
Ted Kremenek8ee74d52009-01-26 06:04:53 +000034namespace clang {
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000035namespace GR {
Ted Kremenek8ee74d52009-01-26 06:04:53 +000036template<>
37struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> {
Mike Stump1eb44332009-09-09 15:08:12 +000038 static inline void* GDMIndex() { return &ConstNotEqIndex; }
Ted Kremenek8ee74d52009-01-26 06:04:53 +000039};
40
41template<>
42struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> {
Mike Stump1eb44332009-09-09 15:08:12 +000043 static inline void* GDMIndex() { return &ConstEqIndex; }
Ted Kremenek8ee74d52009-01-26 06:04:53 +000044};
Mike Stump1eb44332009-09-09 15:08:12 +000045}
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +000046}
Mike Stump1eb44332009-09-09 15:08:12 +000047
Ted Kremenek8ee74d52009-01-26 06:04:53 +000048namespace {
Zhongxing Xu30ad1672008-08-27 14:03:33 +000049// BasicConstraintManager only tracks equality and inequality constraints of
50// constants and integer variables.
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000051class BasicConstraintManager
Ted Kremenek45021952009-02-14 17:08:39 +000052 : public SimpleConstraintManager {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +000053 GRState::IntSetTy::Factory ISetFactory;
Zhongxing Xu30ad1672008-08-27 14:03:33 +000054public:
Ted Kremenek32a58082010-01-05 00:15:18 +000055 BasicConstraintManager(GRStateManager &statemgr, GRSubEngine &subengine)
56 : SimpleConstraintManager(subengine),
57 ISetFactory(statemgr.getAllocator()) {}
Zhongxing Xu30ad1672008-08-27 14:03:33 +000058
Ted Kremenek28f47b92010-12-01 22:16:56 +000059 const GRState *assumeSymNE(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000060 const llvm::APSInt& V,
61 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000062
Ted Kremenek28f47b92010-12-01 22:16:56 +000063 const GRState *assumeSymEQ(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000064 const llvm::APSInt& V,
65 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000066
Ted Kremenek28f47b92010-12-01 22:16:56 +000067 const GRState *assumeSymLT(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000068 const llvm::APSInt& V,
69 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000070
Ted Kremenek28f47b92010-12-01 22:16:56 +000071 const GRState *assumeSymGT(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000072 const llvm::APSInt& V,
73 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000074
Ted Kremenek28f47b92010-12-01 22:16:56 +000075 const GRState *assumeSymGE(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000076 const llvm::APSInt& V,
77 const llvm::APSInt& Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +000078
Ted Kremenek28f47b92010-12-01 22:16:56 +000079 const GRState *assumeSymLE(const GRState* state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +000080 const llvm::APSInt& V,
81 const llvm::APSInt& Adjustment);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000082
Ted Kremeneka591bc02009-06-18 22:57:13 +000083 const GRState* AddEQ(const GRState* state, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000084
Ted Kremeneka591bc02009-06-18 22:57:13 +000085 const GRState* AddNE(const GRState* state, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xu39cfed32008-08-29 14:52:36 +000086
Ted Kremeneka591bc02009-06-18 22:57:13 +000087 const llvm::APSInt* getSymVal(const GRState* state, SymbolRef sym) const;
88 bool isNotEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V)
Ted Kremenek45021952009-02-14 17:08:39 +000089 const;
Ted Kremeneka591bc02009-06-18 22:57:13 +000090 bool isEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V)
Ted Kremenek45021952009-02-14 17:08:39 +000091 const;
Zhongxing Xu39cfed32008-08-29 14:52:36 +000092
Ted Kremeneka591bc02009-06-18 22:57:13 +000093 const GRState* RemoveDeadBindings(const GRState* state, SymbolReaper& SymReaper);
Ted Kremenek241677a2009-01-21 22:26:05 +000094
Mike Stump1eb44332009-09-09 15:08:12 +000095 void print(const GRState* state, llvm::raw_ostream& Out,
Zhongxing Xu39cfed32008-08-29 14:52:36 +000096 const char* nl, const char *sep);
97};
Zhongxing Xu30ad1672008-08-27 14:03:33 +000098
99} // end anonymous namespace
100
Argyrios Kyrtzidis5a4f98f2010-12-22 18:53:20 +0000101ConstraintManager* GR::CreateBasicConstraintManager(GRStateManager& statemgr,
Ted Kremenek32a58082010-01-05 00:15:18 +0000102 GRSubEngine &subengine) {
103 return new BasicConstraintManager(statemgr, subengine);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000104}
105
Jordy Roseba0f61c2010-06-18 22:49:11 +0000106
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000107const GRState*
Ted Kremenek28f47b92010-12-01 22:16:56 +0000108BasicConstraintManager::assumeSymNE(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000109 const llvm::APSInt &V,
110 const llvm::APSInt &Adjustment) {
111 // First, determine if sym == X, where X+Adjustment != V.
112 llvm::APSInt Adjusted = V-Adjustment;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000113 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000114 bool isFeasible = (*X != Adjusted);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000115 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000116 }
117
Jordy Roseba0f61c2010-06-18 22:49:11 +0000118 // Second, determine if sym+Adjustment != V.
119 if (isNotEqual(state, sym, Adjusted))
Ted Kremeneka591bc02009-06-18 22:57:13 +0000120 return state;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000121
122 // If we reach here, sym is not a constant and we don't know if it is != V.
123 // Make that assumption.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000124 return AddNE(state, sym, Adjusted);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000125}
126
Jordy Roseba0f61c2010-06-18 22:49:11 +0000127const GRState*
Ted Kremenek28f47b92010-12-01 22:16:56 +0000128BasicConstraintManager::assumeSymEQ(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000129 const llvm::APSInt &V,
130 const llvm::APSInt &Adjustment) {
131 // First, determine if sym == X, where X+Adjustment != V.
132 llvm::APSInt Adjusted = V-Adjustment;
Ted Kremeneka591bc02009-06-18 22:57:13 +0000133 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000134 bool isFeasible = (*X == Adjusted);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000135 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000136 }
137
Jordy Roseba0f61c2010-06-18 22:49:11 +0000138 // Second, determine if sym+Adjustment != V.
139 if (isNotEqual(state, sym, Adjusted))
Ted Kremeneka591bc02009-06-18 22:57:13 +0000140 return NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000141
142 // If we reach here, sym is not a constant and we don't know if it is == V.
143 // Make that assumption.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000144 return AddEQ(state, sym, Adjusted);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000145}
146
Jordy Roseba0f61c2010-06-18 22:49:11 +0000147// The logic for these will be handled in another ConstraintManager.
148const GRState*
Ted Kremenek28f47b92010-12-01 22:16:56 +0000149BasicConstraintManager::assumeSymLT(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000150 const llvm::APSInt &V,
151 const llvm::APSInt &Adjustment) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000152 // Is 'V' the smallest possible value?
Chris Lattner071e04e2009-01-30 01:58:33 +0000153 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenek73abd132008-12-03 18:56:12 +0000154 // sym cannot be any value less than 'V'. This path is infeasible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000155 return NULL;
Ted Kremenek73abd132008-12-03 18:56:12 +0000156 }
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000157
158 // FIXME: For now have assuming x < y be the same as assuming sym != V;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000159 return assumeSymNE(state, sym, V, Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000160}
161
Jordy Roseba0f61c2010-06-18 22:49:11 +0000162const GRState*
Ted Kremenek28f47b92010-12-01 22:16:56 +0000163BasicConstraintManager::assumeSymGT(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000164 const llvm::APSInt &V,
165 const llvm::APSInt &Adjustment) {
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000166 // Is 'V' the largest possible value?
Chris Lattner071e04e2009-01-30 01:58:33 +0000167 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000168 // sym cannot be any value greater than 'V'. This path is infeasible.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000169 return NULL;
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000170 }
171
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000172 // FIXME: For now have assuming x > y be the same as assuming sym != V;
Ted Kremenek28f47b92010-12-01 22:16:56 +0000173 return assumeSymNE(state, sym, V, Adjustment);
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000174}
175
Jordy Roseba0f61c2010-06-18 22:49:11 +0000176const GRState*
Ted Kremenek28f47b92010-12-01 22:16:56 +0000177BasicConstraintManager::assumeSymGE(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000178 const llvm::APSInt &V,
179 const llvm::APSInt &Adjustment) {
180 // Reject a path if the value of sym is a constant X and !(X+Adj >= V).
Ted Kremeneka591bc02009-06-18 22:57:13 +0000181 if (const llvm::APSInt *X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000182 bool isFeasible = (*X >= V-Adjustment);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000183 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000186 // Sym is not a constant, but it is worth looking to see if V is the
187 // maximum integer value.
Chris Lattner071e04e2009-01-30 01:58:33 +0000188 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000189 llvm::APSInt Adjusted = V-Adjustment;
190
191 // If we know that sym != V (after adjustment), then this condition
192 // is infeasible since there is no other value greater than V.
193 bool isFeasible = !isNotEqual(state, sym, Adjusted);
Mike Stump1eb44332009-09-09 15:08:12 +0000194
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000195 // If the path is still feasible then as a consequence we know that
Jordy Roseba0f61c2010-06-18 22:49:11 +0000196 // 'sym+Adjustment == V' because there are no larger values.
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000197 // Add this constraint.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000198 return isFeasible ? AddEQ(state, sym, Adjusted) : NULL;
Ted Kremenekd7ff4872008-12-03 19:06:30 +0000199 }
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000200
Ted Kremeneka591bc02009-06-18 22:57:13 +0000201 return state;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000202}
203
204const GRState*
Ted Kremenek28f47b92010-12-01 22:16:56 +0000205BasicConstraintManager::assumeSymLE(const GRState *state, SymbolRef sym,
Jordy Roseba0f61c2010-06-18 22:49:11 +0000206 const llvm::APSInt &V,
207 const llvm::APSInt &Adjustment) {
208 // Reject a path if the value of sym is a constant X and !(X+Adj <= V).
Ted Kremeneka591bc02009-06-18 22:57:13 +0000209 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000210 bool isFeasible = (*X <= V-Adjustment);
Ted Kremeneka591bc02009-06-18 22:57:13 +0000211 return isFeasible ? state : NULL;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Ted Kremenek73abd132008-12-03 18:56:12 +0000214 // Sym is not a constant, but it is worth looking to see if V is the
215 // minimum integer value.
Chris Lattner071e04e2009-01-30 01:58:33 +0000216 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Jordy Roseba0f61c2010-06-18 22:49:11 +0000217 llvm::APSInt Adjusted = V-Adjustment;
218
219 // If we know that sym != V (after adjustment), then this condition
220 // is infeasible since there is no other value less than V.
221 bool isFeasible = !isNotEqual(state, sym, Adjusted);
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Ted Kremenek73abd132008-12-03 18:56:12 +0000223 // If the path is still feasible then as a consequence we know that
Jordy Roseba0f61c2010-06-18 22:49:11 +0000224 // 'sym+Adjustment == V' because there are no smaller values.
Ted Kremenek73abd132008-12-03 18:56:12 +0000225 // Add this constraint.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000226 return isFeasible ? AddEQ(state, sym, Adjusted) : NULL;
Ted Kremenek73abd132008-12-03 18:56:12 +0000227 }
Mike Stump1eb44332009-09-09 15:08:12 +0000228
Ted Kremeneka591bc02009-06-18 22:57:13 +0000229 return state;
Zhongxing Xu30ad1672008-08-27 14:03:33 +0000230}
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000231
Ted Kremenekc8781382009-06-17 22:28:13 +0000232const GRState* BasicConstraintManager::AddEQ(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000233 const llvm::APSInt& V) {
234 // Create a new state with the old binding replaced.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000235 return state->set<ConstEq>(sym, &state->getBasicVals().getValue(V));
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000236}
237
Ted Kremenekc8781382009-06-17 22:28:13 +0000238const GRState* BasicConstraintManager::AddNE(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000239 const llvm::APSInt& V) {
Zhongxing Xuf0bc50e2008-11-27 06:08:40 +0000240
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000241 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenekc8781382009-06-17 22:28:13 +0000242 ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Ted Kremenek3baf6722010-11-24 00:54:37 +0000243 GRState::IntSetTy S = T ? *T : ISetFactory.getEmptySet();
Mike Stump1eb44332009-09-09 15:08:12 +0000244
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000245 // Now add V to the NE set.
Ted Kremenek3baf6722010-11-24 00:54:37 +0000246 S = ISetFactory.add(S, &state->getBasicVals().getValue(V));
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000248 // Create a new state with the old binding replaced.
Ted Kremenekc8781382009-06-17 22:28:13 +0000249 return state->set<ConstNotEq>(sym, S);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000250}
251
Ted Kremeneka591bc02009-06-18 22:57:13 +0000252const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* state,
Ted Kremenek45021952009-02-14 17:08:39 +0000253 SymbolRef sym) const {
Ted Kremeneka591bc02009-06-18 22:57:13 +0000254 const ConstEqTy::data_type* T = state->get<ConstEq>(sym);
Ted Kremenek45021952009-02-14 17:08:39 +0000255 return T ? *T : NULL;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000256}
257
Mike Stump1eb44332009-09-09 15:08:12 +0000258bool BasicConstraintManager::isNotEqual(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000259 const llvm::APSInt& V) const {
260
261 // Retrieve the NE-set associated with the given symbol.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000262 const ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000263
264 // See if V is present in the NE-set.
Jordy Roseba0f61c2010-06-18 22:49:11 +0000265 return T ? T->contains(&state->getBasicVals().getValue(V)) : false;
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000266}
267
Ted Kremeneka591bc02009-06-18 22:57:13 +0000268bool BasicConstraintManager::isEqual(const GRState* state, SymbolRef sym,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000269 const llvm::APSInt& V) const {
270 // Retrieve the EQ-set associated with the given symbol.
Ted Kremeneka591bc02009-06-18 22:57:13 +0000271 const ConstEqTy::data_type* T = state->get<ConstEq>(sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000272 // See if V is present in the EQ-set.
273 return T ? **T == V : false;
274}
275
Zhongxing Xu8fd9b352008-11-27 02:39:34 +0000276/// Scan all symbols referenced by the constraints. If the symbol is not alive
277/// as marked in LSymbols, mark it as dead in DSymbols.
Ted Kremenek241677a2009-01-21 22:26:05 +0000278const GRState*
Ted Kremenekc8781382009-06-17 22:28:13 +0000279BasicConstraintManager::RemoveDeadBindings(const GRState* state,
Ted Kremenek241677a2009-01-21 22:26:05 +0000280 SymbolReaper& SymReaper) {
281
Ted Kremenekc8781382009-06-17 22:28:13 +0000282 ConstEqTy CE = state->get<ConstEq>();
283 ConstEqTy::Factory& CEFactory = state->get_context<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000284
285 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
Ted Kremenek241677a2009-01-21 22:26:05 +0000286 SymbolRef sym = I.getKey();
Ted Kremenek3baf6722010-11-24 00:54:37 +0000287 if (SymReaper.maybeDead(sym))
288 CE = CEFactory.remove(CE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000289 }
Ted Kremenekc8781382009-06-17 22:28:13 +0000290 state = state->set<ConstEq>(CE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000291
Ted Kremenekc8781382009-06-17 22:28:13 +0000292 ConstNotEqTy CNE = state->get<ConstNotEq>();
293 ConstNotEqTy::Factory& CNEFactory = state->get_context<ConstNotEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000294
295 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000296 SymbolRef sym = I.getKey();
Ted Kremenek3baf6722010-11-24 00:54:37 +0000297 if (SymReaper.maybeDead(sym))
298 CNE = CNEFactory.remove(CNE, sym);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000299 }
Mike Stump1eb44332009-09-09 15:08:12 +0000300
Ted Kremenekc8781382009-06-17 22:28:13 +0000301 return state->set<ConstNotEq>(CNE);
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000302}
303
Mike Stump1eb44332009-09-09 15:08:12 +0000304void BasicConstraintManager::print(const GRState* state, llvm::raw_ostream& Out,
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000305 const char* nl, const char *sep) {
306 // Print equality constraints.
307
Ted Kremeneka591bc02009-06-18 22:57:13 +0000308 ConstEqTy CE = state->get<ConstEq>();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000309
310 if (!CE.isEmpty()) {
311 Out << nl << sep << "'==' constraints:";
Ted Kremenek53ba0b62009-06-24 23:06:47 +0000312 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I)
313 Out << nl << " $" << I.getKey() << " : " << *I.getData();
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000314 }
315
316 // Print != constraints.
Mike Stump1eb44332009-09-09 15:08:12 +0000317
Ted Kremeneka591bc02009-06-18 22:57:13 +0000318 ConstNotEqTy CNE = state->get<ConstNotEq>();
Mike Stump1eb44332009-09-09 15:08:12 +0000319
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000320 if (!CNE.isEmpty()) {
321 Out << nl << sep << "'!=' constraints:";
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000323 for (ConstNotEqTy::iterator I = CNE.begin(), EI = CNE.end(); I!=EI; ++I) {
324 Out << nl << " $" << I.getKey() << " : ";
325 bool isFirst = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000326
327 GRState::IntSetTy::iterator J = I.getData().begin(),
328 EJ = I.getData().end();
329
330 for ( ; J != EJ; ++J) {
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000331 if (isFirst) isFirst = false;
332 else Out << ", ";
Mike Stump1eb44332009-09-09 15:08:12 +0000333
Zhongxing Xu7d94e262008-11-10 05:00:06 +0000334 Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream.
Zhongxing Xu39cfed32008-08-29 14:52:36 +0000335 }
336 }
337 }
Daniel Dunbar0e194dd2008-08-30 02:06:22 +0000338}