blob: 8bdc4fa00306deb7edf96c239ae42db4ba20d98b [file] [log] [blame]
Zhongxing Xu4c239632008-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 Stump11289f42009-09-09 15:08:12 +000010// This file defines BasicConstraintManager, a class that tracks simple
Zhongxing Xu4c239632008-08-29 15:09:12 +000011// equality and inequality constraints on symbolic values of GRState.
12//
13//===----------------------------------------------------------------------===//
14
Ted Kremenek7efe43d2009-02-14 17:08:39 +000015#include "SimpleConstraintManager.h"
Ted Kremenekd99bd552010-12-23 19:38:26 +000016#include "clang/StaticAnalyzer/PathSensitive/GRState.h"
17#include "clang/StaticAnalyzer/PathSensitive/GRStateTrait.h"
18#include "clang/StaticAnalyzer/PathSensitive/TransferFuncs.h"
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000019#include "llvm/Support/raw_ostream.h"
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000020
21using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000022using namespace ento;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000023
Ted Kremeneke9193282009-01-26 06:04:53 +000024
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000025namespace { class ConstNotEq {}; }
26namespace { class ConstEq {}; }
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000027
Ted Kremenekd8242f12008-12-05 02:27:51 +000028typedef llvm::ImmutableMap<SymbolRef,GRState::IntSetTy> ConstNotEqTy;
29typedef llvm::ImmutableMap<SymbolRef,const llvm::APSInt*> ConstEqTy;
Mike Stump11289f42009-09-09 15:08:12 +000030
Ted Kremeneke9193282009-01-26 06:04:53 +000031static int ConstEqIndex = 0;
32static int ConstNotEqIndex = 0;
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000033
Ted Kremeneke9193282009-01-26 06:04:53 +000034namespace clang {
Ted Kremenek98857c92010-12-23 07:20:52 +000035namespace ento {
Ted Kremeneke9193282009-01-26 06:04:53 +000036template<>
37struct GRStateTrait<ConstNotEq> : public GRStatePartialTrait<ConstNotEqTy> {
Mike Stump11289f42009-09-09 15:08:12 +000038 static inline void* GDMIndex() { return &ConstNotEqIndex; }
Ted Kremeneke9193282009-01-26 06:04:53 +000039};
40
41template<>
42struct GRStateTrait<ConstEq> : public GRStatePartialTrait<ConstEqTy> {
Mike Stump11289f42009-09-09 15:08:12 +000043 static inline void* GDMIndex() { return &ConstEqIndex; }
Ted Kremeneke9193282009-01-26 06:04:53 +000044};
Mike Stump11289f42009-09-09 15:08:12 +000045}
Argyrios Kyrtzidisca08fba2010-12-22 18:53:20 +000046}
Mike Stump11289f42009-09-09 15:08:12 +000047
Ted Kremeneke9193282009-01-26 06:04:53 +000048namespace {
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000049// BasicConstraintManager only tracks equality and inequality constraints of
50// constants and integer variables.
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000051class BasicConstraintManager
Ted Kremenek7efe43d2009-02-14 17:08:39 +000052 : public SimpleConstraintManager {
Zhongxing Xu59e2d412008-11-27 06:08:40 +000053 GRState::IntSetTy::Factory ISetFactory;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000054public:
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +000055 BasicConstraintManager(GRStateManager &statemgr, SubEngine &subengine)
Ted Kremenekde8e7442010-01-05 00:15:18 +000056 : SimpleConstraintManager(subengine),
57 ISetFactory(statemgr.getAllocator()) {}
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000058
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000059 const GRState *assumeSymNE(const GRState* state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +000060 const llvm::APSInt& V,
61 const llvm::APSInt& Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000062
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000063 const GRState *assumeSymEQ(const GRState* state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +000064 const llvm::APSInt& V,
65 const llvm::APSInt& Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000066
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000067 const GRState *assumeSymLT(const GRState* state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +000068 const llvm::APSInt& V,
69 const llvm::APSInt& Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000070
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000071 const GRState *assumeSymGT(const GRState* state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +000072 const llvm::APSInt& V,
73 const llvm::APSInt& Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000074
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000075 const GRState *assumeSymGE(const GRState* state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +000076 const llvm::APSInt& V,
77 const llvm::APSInt& Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000078
Ted Kremenekc5bea1e2010-12-01 22:16:56 +000079 const GRState *assumeSymLE(const GRState* state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +000080 const llvm::APSInt& V,
81 const llvm::APSInt& Adjustment);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000082
Ted Kremenekf9906842009-06-18 22:57:13 +000083 const GRState* AddEQ(const GRState* state, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000084
Ted Kremenekf9906842009-06-18 22:57:13 +000085 const GRState* AddNE(const GRState* state, SymbolRef sym, const llvm::APSInt& V);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000086
Ted Kremenekf9906842009-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 Kremenek7efe43d2009-02-14 17:08:39 +000089 const;
Ted Kremenekf9906842009-06-18 22:57:13 +000090 bool isEqual(const GRState* state, SymbolRef sym, const llvm::APSInt& V)
Ted Kremenek7efe43d2009-02-14 17:08:39 +000091 const;
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000092
Ted Kremenekf9906842009-06-18 22:57:13 +000093 const GRState* RemoveDeadBindings(const GRState* state, SymbolReaper& SymReaper);
Ted Kremenek16fbfe62009-01-21 22:26:05 +000094
Mike Stump11289f42009-09-09 15:08:12 +000095 void print(const GRState* state, llvm::raw_ostream& Out,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +000096 const char* nl, const char *sep);
97};
Zhongxing Xuf71b5f32008-08-27 14:03:33 +000098
99} // end anonymous namespace
100
Ted Kremenek98857c92010-12-23 07:20:52 +0000101ConstraintManager* ento::CreateBasicConstraintManager(GRStateManager& statemgr,
Argyrios Kyrtzidis1696f502010-12-22 18:53:44 +0000102 SubEngine &subengine) {
Ted Kremenekde8e7442010-01-05 00:15:18 +0000103 return new BasicConstraintManager(statemgr, subengine);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000104}
105
Jordy Rosec0fe8422010-06-18 22:49:11 +0000106
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000107const GRState*
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000108BasicConstraintManager::assumeSymNE(const GRState *state, SymbolRef sym,
Jordy Rosec0fe8422010-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 Kremenekf9906842009-06-18 22:57:13 +0000113 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Rosec0fe8422010-06-18 22:49:11 +0000114 bool isFeasible = (*X != Adjusted);
Ted Kremenekf9906842009-06-18 22:57:13 +0000115 return isFeasible ? state : NULL;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000116 }
117
Jordy Rosec0fe8422010-06-18 22:49:11 +0000118 // Second, determine if sym+Adjustment != V.
119 if (isNotEqual(state, sym, Adjusted))
Ted Kremenekf9906842009-06-18 22:57:13 +0000120 return state;
Zhongxing Xuf71b5f32008-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 Rosec0fe8422010-06-18 22:49:11 +0000124 return AddNE(state, sym, Adjusted);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000125}
126
Jordy Rosec0fe8422010-06-18 22:49:11 +0000127const GRState*
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000128BasicConstraintManager::assumeSymEQ(const GRState *state, SymbolRef sym,
Jordy Rosec0fe8422010-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 Kremenekf9906842009-06-18 22:57:13 +0000133 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Rosec0fe8422010-06-18 22:49:11 +0000134 bool isFeasible = (*X == Adjusted);
Ted Kremenekf9906842009-06-18 22:57:13 +0000135 return isFeasible ? state : NULL;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000136 }
137
Jordy Rosec0fe8422010-06-18 22:49:11 +0000138 // Second, determine if sym+Adjustment != V.
139 if (isNotEqual(state, sym, Adjusted))
Ted Kremenekf9906842009-06-18 22:57:13 +0000140 return NULL;
Zhongxing Xuf71b5f32008-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 Rosec0fe8422010-06-18 22:49:11 +0000144 return AddEQ(state, sym, Adjusted);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000145}
146
Jordy Rosec0fe8422010-06-18 22:49:11 +0000147// The logic for these will be handled in another ConstraintManager.
148const GRState*
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000149BasicConstraintManager::assumeSymLT(const GRState *state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +0000150 const llvm::APSInt &V,
151 const llvm::APSInt &Adjustment) {
Ted Kremenekf935cfe2008-12-03 18:56:12 +0000152 // Is 'V' the smallest possible value?
Chris Lattner34beb042009-01-30 01:58:33 +0000153 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenekf935cfe2008-12-03 18:56:12 +0000154 // sym cannot be any value less than 'V'. This path is infeasible.
Ted Kremenekf9906842009-06-18 22:57:13 +0000155 return NULL;
Ted Kremenekf935cfe2008-12-03 18:56:12 +0000156 }
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000157
158 // FIXME: For now have assuming x < y be the same as assuming sym != V;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000159 return assumeSymNE(state, sym, V, Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000160}
161
Jordy Rosec0fe8422010-06-18 22:49:11 +0000162const GRState*
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000163BasicConstraintManager::assumeSymGT(const GRState *state, SymbolRef sym,
Jordy Rosec0fe8422010-06-18 22:49:11 +0000164 const llvm::APSInt &V,
165 const llvm::APSInt &Adjustment) {
Ted Kremenekfff9f4a2008-12-03 19:06:30 +0000166 // Is 'V' the largest possible value?
Chris Lattner34beb042009-01-30 01:58:33 +0000167 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Ted Kremenekfff9f4a2008-12-03 19:06:30 +0000168 // sym cannot be any value greater than 'V'. This path is infeasible.
Ted Kremenekf9906842009-06-18 22:57:13 +0000169 return NULL;
Ted Kremenekfff9f4a2008-12-03 19:06:30 +0000170 }
171
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000172 // FIXME: For now have assuming x > y be the same as assuming sym != V;
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000173 return assumeSymNE(state, sym, V, Adjustment);
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000174}
175
Jordy Rosec0fe8422010-06-18 22:49:11 +0000176const GRState*
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000177BasicConstraintManager::assumeSymGE(const GRState *state, SymbolRef sym,
Jordy Rosec0fe8422010-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 Kremenekf9906842009-06-18 22:57:13 +0000181 if (const llvm::APSInt *X = getSymVal(state, sym)) {
Jordy Rosec0fe8422010-06-18 22:49:11 +0000182 bool isFeasible = (*X >= V-Adjustment);
Ted Kremenekf9906842009-06-18 22:57:13 +0000183 return isFeasible ? state : NULL;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000184 }
Mike Stump11289f42009-09-09 15:08:12 +0000185
Ted Kremenekfff9f4a2008-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 Lattner34beb042009-01-30 01:58:33 +0000188 if (V == llvm::APSInt::getMaxValue(V.getBitWidth(), V.isUnsigned())) {
Jordy Rosec0fe8422010-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 Stump11289f42009-09-09 15:08:12 +0000194
Ted Kremenekfff9f4a2008-12-03 19:06:30 +0000195 // If the path is still feasible then as a consequence we know that
Jordy Rosec0fe8422010-06-18 22:49:11 +0000196 // 'sym+Adjustment == V' because there are no larger values.
Ted Kremenekfff9f4a2008-12-03 19:06:30 +0000197 // Add this constraint.
Jordy Rosec0fe8422010-06-18 22:49:11 +0000198 return isFeasible ? AddEQ(state, sym, Adjusted) : NULL;
Ted Kremenekfff9f4a2008-12-03 19:06:30 +0000199 }
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000200
Ted Kremenekf9906842009-06-18 22:57:13 +0000201 return state;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000202}
203
204const GRState*
Ted Kremenekc5bea1e2010-12-01 22:16:56 +0000205BasicConstraintManager::assumeSymLE(const GRState *state, SymbolRef sym,
Jordy Rosec0fe8422010-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 Kremenekf9906842009-06-18 22:57:13 +0000209 if (const llvm::APSInt* X = getSymVal(state, sym)) {
Jordy Rosec0fe8422010-06-18 22:49:11 +0000210 bool isFeasible = (*X <= V-Adjustment);
Ted Kremenekf9906842009-06-18 22:57:13 +0000211 return isFeasible ? state : NULL;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000212 }
Mike Stump11289f42009-09-09 15:08:12 +0000213
Ted Kremenekf935cfe2008-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 Lattner34beb042009-01-30 01:58:33 +0000216 if (V == llvm::APSInt::getMinValue(V.getBitWidth(), V.isUnsigned())) {
Jordy Rosec0fe8422010-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 Stump11289f42009-09-09 15:08:12 +0000222
Ted Kremenekf935cfe2008-12-03 18:56:12 +0000223 // If the path is still feasible then as a consequence we know that
Jordy Rosec0fe8422010-06-18 22:49:11 +0000224 // 'sym+Adjustment == V' because there are no smaller values.
Ted Kremenekf935cfe2008-12-03 18:56:12 +0000225 // Add this constraint.
Jordy Rosec0fe8422010-06-18 22:49:11 +0000226 return isFeasible ? AddEQ(state, sym, Adjusted) : NULL;
Ted Kremenekf935cfe2008-12-03 18:56:12 +0000227 }
Mike Stump11289f42009-09-09 15:08:12 +0000228
Ted Kremenekf9906842009-06-18 22:57:13 +0000229 return state;
Zhongxing Xuf71b5f32008-08-27 14:03:33 +0000230}
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000231
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000232const GRState* BasicConstraintManager::AddEQ(const GRState* state, SymbolRef sym,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000233 const llvm::APSInt& V) {
234 // Create a new state with the old binding replaced.
Jordy Rosec0fe8422010-06-18 22:49:11 +0000235 return state->set<ConstEq>(sym, &state->getBasicVals().getValue(V));
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000236}
237
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000238const GRState* BasicConstraintManager::AddNE(const GRState* state, SymbolRef sym,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000239 const llvm::APSInt& V) {
Zhongxing Xu59e2d412008-11-27 06:08:40 +0000240
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000241 // First, retrieve the NE-set associated with the given symbol.
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000242 ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Ted Kremenekb3b56c62010-11-24 00:54:37 +0000243 GRState::IntSetTy S = T ? *T : ISetFactory.getEmptySet();
Mike Stump11289f42009-09-09 15:08:12 +0000244
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000245 // Now add V to the NE set.
Ted Kremenekb3b56c62010-11-24 00:54:37 +0000246 S = ISetFactory.add(S, &state->getBasicVals().getValue(V));
Mike Stump11289f42009-09-09 15:08:12 +0000247
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000248 // Create a new state with the old binding replaced.
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000249 return state->set<ConstNotEq>(sym, S);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000250}
251
Ted Kremenekf9906842009-06-18 22:57:13 +0000252const llvm::APSInt* BasicConstraintManager::getSymVal(const GRState* state,
Ted Kremenek7efe43d2009-02-14 17:08:39 +0000253 SymbolRef sym) const {
Ted Kremenekf9906842009-06-18 22:57:13 +0000254 const ConstEqTy::data_type* T = state->get<ConstEq>(sym);
Ted Kremenek7efe43d2009-02-14 17:08:39 +0000255 return T ? *T : NULL;
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000256}
257
Mike Stump11289f42009-09-09 15:08:12 +0000258bool BasicConstraintManager::isNotEqual(const GRState* state, SymbolRef sym,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000259 const llvm::APSInt& V) const {
260
261 // Retrieve the NE-set associated with the given symbol.
Ted Kremenekf9906842009-06-18 22:57:13 +0000262 const ConstNotEqTy::data_type* T = state->get<ConstNotEq>(sym);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000263
264 // See if V is present in the NE-set.
Jordy Rosec0fe8422010-06-18 22:49:11 +0000265 return T ? T->contains(&state->getBasicVals().getValue(V)) : false;
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000266}
267
Ted Kremenekf9906842009-06-18 22:57:13 +0000268bool BasicConstraintManager::isEqual(const GRState* state, SymbolRef sym,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000269 const llvm::APSInt& V) const {
270 // Retrieve the EQ-set associated with the given symbol.
Ted Kremenekf9906842009-06-18 22:57:13 +0000271 const ConstEqTy::data_type* T = state->get<ConstEq>(sym);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000272 // See if V is present in the EQ-set.
273 return T ? **T == V : false;
274}
275
Zhongxing Xub94f4402008-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 Kremenek16fbfe62009-01-21 22:26:05 +0000278const GRState*
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000279BasicConstraintManager::RemoveDeadBindings(const GRState* state,
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000280 SymbolReaper& SymReaper) {
281
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000282 ConstEqTy CE = state->get<ConstEq>();
283 ConstEqTy::Factory& CEFactory = state->get_context<ConstEq>();
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000284
285 for (ConstEqTy::iterator I = CE.begin(), E = CE.end(); I!=E; ++I) {
Ted Kremenek16fbfe62009-01-21 22:26:05 +0000286 SymbolRef sym = I.getKey();
Ted Kremenekb3b56c62010-11-24 00:54:37 +0000287 if (SymReaper.maybeDead(sym))
288 CE = CEFactory.remove(CE, sym);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000289 }
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000290 state = state->set<ConstEq>(CE);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000291
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000292 ConstNotEqTy CNE = state->get<ConstNotEq>();
293 ConstNotEqTy::Factory& CNEFactory = state->get_context<ConstNotEq>();
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000294
295 for (ConstNotEqTy::iterator I = CNE.begin(), E = CNE.end(); I != E; ++I) {
Mike Stump11289f42009-09-09 15:08:12 +0000296 SymbolRef sym = I.getKey();
Ted Kremenekb3b56c62010-11-24 00:54:37 +0000297 if (SymReaper.maybeDead(sym))
298 CNE = CNEFactory.remove(CNE, sym);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000299 }
Mike Stump11289f42009-09-09 15:08:12 +0000300
Ted Kremenekb35e2ca2009-06-17 22:28:13 +0000301 return state->set<ConstNotEq>(CNE);
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000302}
303
Mike Stump11289f42009-09-09 15:08:12 +0000304void BasicConstraintManager::print(const GRState* state, llvm::raw_ostream& Out,
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000305 const char* nl, const char *sep) {
306 // Print equality constraints.
307
Ted Kremenekf9906842009-06-18 22:57:13 +0000308 ConstEqTy CE = state->get<ConstEq>();
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000309
310 if (!CE.isEmpty()) {
311 Out << nl << sep << "'==' constraints:";
Ted Kremenek799bb6e2009-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 Xuc1bd3a52008-08-29 14:52:36 +0000314 }
315
316 // Print != constraints.
Mike Stump11289f42009-09-09 15:08:12 +0000317
Ted Kremenekf9906842009-06-18 22:57:13 +0000318 ConstNotEqTy CNE = state->get<ConstNotEq>();
Mike Stump11289f42009-09-09 15:08:12 +0000319
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000320 if (!CNE.isEmpty()) {
321 Out << nl << sep << "'!=' constraints:";
Mike Stump11289f42009-09-09 15:08:12 +0000322
Zhongxing Xuc1bd3a52008-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 Stump11289f42009-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 Xuc1bd3a52008-08-29 14:52:36 +0000331 if (isFirst) isFirst = false;
332 else Out << ", ";
Mike Stump11289f42009-09-09 15:08:12 +0000333
Zhongxing Xu1c31dbe2008-11-10 05:00:06 +0000334 Out << (*J)->getSExtValue(); // Hack: should print to raw_ostream.
Zhongxing Xuc1bd3a52008-08-29 14:52:36 +0000335 }
336 }
337 }
Daniel Dunbarc62cf792008-08-30 02:06:22 +0000338}