Skip to content

Commit a721b1b

Browse files
committed
Error handling cleanup in solvers/flattening
Files boolbv_constant.cpp, boolbv.cpp
1 parent da71b08 commit a721b1b

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

src/solvers/flattening/boolbv.cpp

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Author: Daniel Kroening, [email protected]
88

99
#include "boolbv.h"
1010

11-
#include <cassert>
11+
#include <algorithm>
1212
#include <map>
1313
#include <set>
1414

@@ -36,7 +36,10 @@ bool boolbvt::literal(
3636
{
3737
if(expr.type().id()==ID_bool)
3838
{
39-
assert(bit==0);
39+
INVARIANT(
40+
bit == 0,
41+
"boolean expressions shall be represented by a single bit and hence the "
42+
"only valid bit index is 0");
4043
return prop_conv_solvert::literal(expr, dest);
4144
}
4245
else
@@ -54,7 +57,8 @@ bool boolbvt::literal(
5457

5558
const boolbv_mapt::map_entryt &map_entry=it_m->second;
5659

57-
assert(bit<map_entry.literal_map.size());
60+
INVARIANT(
61+
bit < map_entry.literal_map.size(), "bit index shall be within bounds");
5862
if(!map_entry.literal_map[bit].is_set)
5963
return true;
6064

@@ -66,15 +70,11 @@ bool boolbvt::literal(
6670
const index_exprt &index_expr=to_index_expr(expr);
6771

6872
std::size_t element_width=boolbv_width(index_expr.type());
73+
CHECK_RETURN(element_width != 0);
6974

70-
if(element_width==0)
71-
throw "literal expects a bit-vector type";
75+
mp_integer index = numeric_cast_v<mp_integer>(index_expr.index());
7276

73-
mp_integer index;
74-
if(to_integer(index_expr.index(), index))
75-
throw "literal expects constant index";
76-
77-
std::size_t offset=integer2unsigned(index*element_width);
77+
std::size_t offset = numeric_cast_v<std::size_t>(index * element_width);
7878

7979
return literal(index_expr.array(), bit+offset, dest);
8080
}
@@ -99,18 +99,16 @@ bool boolbvt::literal(
9999
return literal(expr.op0(), bit+offset, dest);
100100

101101
std::size_t element_width=boolbv_width(subtype);
102-
103-
if(element_width==0)
104-
throw "literal expects a bit-vector type";
102+
CHECK_RETURN(element_width != 0);
105103

106104
offset+=element_width;
107105
}
108106

109-
throw "failed to find component";
107+
INVARIANT(false, "struct type should have accessed component");
110108
}
111109
}
112110

113-
throw "found no literal for expression";
111+
INVARIANT(false, "expression should have a corresponding literal");
114112
}
115113

116114
const bvt &
@@ -309,7 +307,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
309307
else if(expr.id()==ID_float_debug1 ||
310308
expr.id()==ID_float_debug2)
311309
{
312-
assert(expr.operands().size()==2);
310+
DATA_INVARIANT(expr.operands().size() == 2, "");
313311
bvt bv0=convert_bitvector(expr.op0());
314312
bvt bv1=convert_bitvector(expr.op1());
315313
float_utilst float_utils(prop, to_floatbv_type(expr.type()));
@@ -331,8 +329,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
331329
if(width==0)
332330
return conversion_failed(expr);
333331

334-
if(expr.operands().size()!=2)
335-
throw "lambda takes two operands";
332+
DATA_INVARIANT(
333+
expr.operands().size() == 2, "lambda expression should have two operands");
336334

337335
if(expr.type().id()!=ID_array)
338336
return conversion_failed(expr);
@@ -359,10 +357,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
359357

360358
const bvt &tmp=convert_bv(expr_op1);
361359

362-
std::size_t offset=integer2unsigned(i*tmp.size());
360+
INVARIANT(
361+
size * tmp.size() == width,
362+
"total bitvector width shall equal the number of operands times the size "
363+
"per operand");
363364

364-
if(size*tmp.size()!=width)
365-
throw "convert_lambda: unexpected operand width";
365+
std::size_t offset = numeric_cast_v<std::size_t>(i * tmp.size());
366366

367367
for(std::size_t j=0; j<tmp.size(); j++)
368368
bv[offset+j]=tmp[j];
@@ -400,10 +400,8 @@ bvt boolbvt::convert_symbol(const exprt &expr)
400400
bvt bv;
401401
bv.resize(width);
402402

403-
const irep_idt &identifier=expr.get(ID_identifier);
404-
405-
if(identifier.empty())
406-
throw "convert_symbol got empty identifier";
403+
const irep_idt &identifier = expr.get(ID_identifier);
404+
CHECK_RETURN(!identifier.empty());
407405

408406
if(width==0)
409407
{
@@ -414,13 +412,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
414412
{
415413
map.get_literals(identifier, type, width, bv);
416414

417-
forall_literals(it, bv)
418-
if(it->var_no()>=prop.no_variables() &&
419-
!it->is_constant())
420-
{
421-
error() << identifier << eom;
422-
assert(false);
423-
}
415+
INVARIANT_WITH_DIAGNOSTICS(
416+
std::all_of(
417+
bv.begin(),
418+
bv.end(),
419+
[this](const literalt &l) {
420+
return l.var_no() < prop.no_variables() || l.is_constant();
421+
}),
422+
"variable number of non-constant literals should be within bounds",
423+
id2string(identifier));
424424
}
425425

426426
return bv;

0 commit comments

Comments
 (0)