Skip to content

Commit 7baf62d

Browse files
Replace throws and asserts with invariants in boolbv_index
Also use the expected_width parameter to move one check into convert_bv
1 parent af1d428 commit 7baf62d

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/solvers/flattening/boolbv_index.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ bvt boolbvt::convert_index(const index_exprt &expr)
6262

6363
// Must have a finite size
6464
mp_integer array_size = numeric_cast_v<mp_integer>(array_type.size());
65-
6665
{
6766
// see if the index address is constant
6867
// many of these are compacted by simplify_expr
@@ -117,12 +116,10 @@ bvt boolbvt::convert_index(const index_exprt &expr)
117116

118117
binary_relation_exprt lower_bound(
119118
from_integer(0, index.type()), ID_le, index);
119+
CHECK_RETURN(lower_bound.lhs().is_not_nil());
120120
binary_relation_exprt upper_bound(
121121
index, ID_lt, from_integer(array_size, index.type()));
122-
123-
if(lower_bound.lhs().is_nil() ||
124-
upper_bound.rhs().is_nil())
125-
throw "number conversion failed (2)";
122+
CHECK_RETURN(upper_bound.rhs().is_not_nil());
126123

127124
and_exprt range_condition(lower_bound, upper_bound);
128125
implies_exprt implication(range_condition, value_equality);
@@ -174,11 +171,12 @@ bvt boolbvt::convert_index(const index_exprt &expr)
174171
for(mp_integer i=0; i<array_size; i=i+1)
175172
{
176173
index_equality.rhs()=from_integer(i, index_equality.lhs().type());
174+
CHECK_RETURN(index_equality.rhs().is_not_nil());
177175

178-
if(index_equality.rhs().is_nil())
179-
throw "number conversion failed (1)";
180-
181-
assert(it != array.operands().end());
176+
INVARIANT(
177+
it != array.operands().end(),
178+
"this loop iterates over the array, so `it` shouldn't be increased "
179+
"past the array's end");
182180

183181
value_equality.rhs()=*it++;
184182

@@ -200,10 +198,8 @@ bvt boolbvt::convert_index(const index_exprt &expr)
200198

201199
// get literals for the whole array
202200

203-
const bvt &array_bv=convert_bv(array);
204-
205-
if(array_size*width!=array_bv.size())
206-
throw "unexpected array size";
201+
const bvt &array_bv =
202+
convert_bv(array, numeric_cast_v<std::size_t>(array_size * width));
207203

208204
// TODO: maybe a shifter-like construction would be better
209205
// Would be a lot more compact but propagate worse
@@ -231,9 +227,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
231227
for(mp_integer i=0; i<array_size; i=i+1)
232228
{
233229
index_equality.rhs()=from_integer(i, index_equality.lhs().type());
234-
235-
if(index_equality.rhs().is_nil())
236-
throw "number conversion failed (1)";
230+
CHECK_RETURN(index_equality.rhs().is_not_nil());
237231

238232
mp_integer offset=i*width;
239233

@@ -258,7 +252,9 @@ bvt boolbvt::convert_index(const index_exprt &expr)
258252

259253
typet constant_type=index.type(); // type of index operand
260254

261-
assert(array_size>0);
255+
DATA_INVARIANT(
256+
array_size > 0,
257+
"non-positive array sizes are forbidden in goto programs");
262258

263259
for(mp_integer i=0; i<array_size; i=i+1)
264260
{

0 commit comments

Comments
 (0)