Skip to content

Commit da8d2ab

Browse files
authored
Merge pull request #3044 from hannes-steffenhagen-diffblue/feature-invariant_cleanup-flattening-index
Invariant cleanup in flattening/boolbv_index
2 parents ffe62af + 7baf62d commit da8d2ab

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/solvers/flattening/boolbv_index.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,17 @@ bvt boolbvt::convert_index(const index_exprt &expr)
6161
}
6262

6363
// Must have a finite size
64-
mp_integer array_size;
65-
if(to_integer(array_type.size(), array_size))
66-
throw "failed to convert array size";
67-
68-
// see if the index address is constant
69-
// many of these are compacted by simplify_expr
70-
// but variable location writes will block this
71-
mp_integer index_value;
72-
if(!to_integer(index, index_value))
73-
return convert_index(array, index_value);
64+
mp_integer array_size = numeric_cast_v<mp_integer>(array_type.size());
65+
{
66+
// see if the index address is constant
67+
// many of these are compacted by simplify_expr
68+
// but variable location writes will block this
69+
auto maybe_index_value = numeric_cast<mp_integer>(index);
70+
if(maybe_index_value.has_value())
71+
{
72+
return convert_index(array, maybe_index_value.value());
73+
}
74+
}
7475

7576
// Special case : arrays of one thing (useful for constants)
7677
// TODO : merge with ACTUAL_ARRAY_HACK so that ranges of the same
@@ -115,12 +116,10 @@ bvt boolbvt::convert_index(const index_exprt &expr)
115116

116117
binary_relation_exprt lower_bound(
117118
from_integer(0, index.type()), ID_le, index);
119+
CHECK_RETURN(lower_bound.lhs().is_not_nil());
118120
binary_relation_exprt upper_bound(
119121
index, ID_lt, from_integer(array_size, index.type()));
120-
121-
if(lower_bound.lhs().is_nil() ||
122-
upper_bound.rhs().is_nil())
123-
throw "number conversion failed (2)";
122+
CHECK_RETURN(upper_bound.rhs().is_not_nil());
124123

125124
and_exprt range_condition(lower_bound, upper_bound);
126125
implies_exprt implication(range_condition, value_equality);
@@ -172,11 +171,12 @@ bvt boolbvt::convert_index(const index_exprt &expr)
172171
for(mp_integer i=0; i<array_size; i=i+1)
173172
{
174173
index_equality.rhs()=from_integer(i, index_equality.lhs().type());
174+
CHECK_RETURN(index_equality.rhs().is_not_nil());
175175

176-
if(index_equality.rhs().is_nil())
177-
throw "number conversion failed (1)";
178-
179-
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");
180180

181181
value_equality.rhs()=*it++;
182182

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

199199
// get literals for the whole array
200200

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

206204
// TODO: maybe a shifter-like construction would be better
207205
// Would be a lot more compact but propagate worse
@@ -229,9 +227,7 @@ bvt boolbvt::convert_index(const index_exprt &expr)
229227
for(mp_integer i=0; i<array_size; i=i+1)
230228
{
231229
index_equality.rhs()=from_integer(i, index_equality.lhs().type());
232-
233-
if(index_equality.rhs().is_nil())
234-
throw "number conversion failed (1)";
230+
CHECK_RETURN(index_equality.rhs().is_not_nil());
235231

236232
mp_integer offset=i*width;
237233

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

257253
typet constant_type=index.type(); // type of index operand
258254

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

261259
for(mp_integer i=0; i<array_size; i=i+1)
262260
{

0 commit comments

Comments
 (0)