Skip to content

Commit cedaaad

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

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

src/solvers/flattening/boolbv.cpp

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

99
#include "boolbv.h"
1010

11-
#include <cassert>
1211
#include <map>
1312
#include <set>
1413

@@ -36,7 +35,10 @@ bool boolbvt::literal(
3635
{
3736
if(expr.type().id()==ID_bool)
3837
{
39-
assert(bit==0);
38+
INVARIANT(
39+
bit == 0,
40+
"boolean expressions shall be represented by a single bit and hence the "
41+
"only valid bit index is 0");
4042
return prop_conv_solvert::literal(expr, dest);
4143
}
4244
else
@@ -54,7 +56,8 @@ bool boolbvt::literal(
5456

5557
const boolbv_mapt::map_entryt &map_entry=it_m->second;
5658

57-
assert(bit<map_entry.literal_map.size());
59+
INVARIANT(
60+
bit < map_entry.literal_map.size(), "bit index shall be within bounds");
5861
if(!map_entry.literal_map[bit].is_set)
5962
return true;
6063

@@ -66,13 +69,11 @@ bool boolbvt::literal(
6669
const index_exprt &index_expr=to_index_expr(expr);
6770

6871
std::size_t element_width=boolbv_width(index_expr.type());
69-
70-
if(element_width==0)
71-
throw "literal expects a bit-vector type";
72+
CHECK_RETURN(element_width != 0);
7273

7374
mp_integer index;
74-
if(to_integer(index_expr.index(), index))
75-
throw "literal expects constant index";
75+
bool error = to_integer(index_expr.index(), index);
76+
CHECK_RETURN(!error);
7677

7778
std::size_t offset=integer2unsigned(index*element_width);
7879

@@ -99,18 +100,16 @@ bool boolbvt::literal(
99100
return literal(expr.op0(), bit+offset, dest);
100101

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

106105
offset+=element_width;
107106
}
108107

109-
throw "failed to find component";
108+
INVARIANT(false, "struct type should have accessed component");
110109
}
111110
}
112111

113-
throw "found no literal for expression";
112+
INVARIANT(false, "expression should have a corresponding literal");
114113
}
115114

116115
const bvt &
@@ -258,7 +257,9 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
258257
return convert_unary_minus(to_unary_expr(expr));
259258
else if(expr.id()==ID_unary_plus)
260259
{
261-
assert(expr.operands().size()==1);
260+
DATA_INVARIANT(
261+
expr.operands().size() == 1,
262+
"unary plus expressions should have one operand");
262263
return convert_bitvector(expr.op0());
263264
}
264265
else if(expr.id()==ID_abs)
@@ -311,7 +312,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
311312
else if(expr.id()==ID_float_debug1 ||
312313
expr.id()==ID_float_debug2)
313314
{
314-
assert(expr.operands().size()==2);
315+
DATA_INVARIANT(expr.operands().size() == 2, "");
315316
bvt bv0=convert_bitvector(expr.op0());
316317
bvt bv1=convert_bitvector(expr.op1());
317318
float_utilst float_utils(prop, to_floatbv_type(expr.type()));
@@ -333,8 +334,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
333334
if(width==0)
334335
return conversion_failed(expr);
335336

336-
if(expr.operands().size()!=2)
337-
throw "lambda takes two operands";
337+
DATA_INVARIANT(
338+
expr.operands().size() == 2, "lambda expression should have two operands");
338339

339340
if(expr.type().id()!=ID_array)
340341
return conversion_failed(expr);
@@ -361,10 +362,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
361362

362363
const bvt &tmp=convert_bv(expr_op1);
363364

364-
std::size_t offset=integer2unsigned(i*tmp.size());
365+
INVARIANT(
366+
size * tmp.size() == width,
367+
"total bitvector width shall equal the number of operands times the size "
368+
"per operand");
365369

366-
if(size*tmp.size()!=width)
367-
throw "convert_lambda: unexpected operand width";
370+
std::size_t offset = integer2unsigned(i * tmp.size());
368371

369372
for(std::size_t j=0; j<tmp.size(); j++)
370373
bv[offset+j]=tmp[j];
@@ -394,18 +397,16 @@ bvt boolbvt::convert_bv_literals(const exprt &expr)
394397
return bv;
395398
}
396399

397-
bvt boolbvt::convert_symbol(const exprt &expr)
400+
bvt boolbvt::convert_symbol(const symbol_exprt &expr)
398401
{
399402
const typet &type=expr.type();
400403
std::size_t width=boolbv_width(type);
401404

402405
bvt bv;
403406
bv.resize(width);
404407

405-
const irep_idt &identifier=expr.get(ID_identifier);
406-
407-
if(identifier.empty())
408-
throw "convert_symbol got empty identifier";
408+
const irep_idt &identifier = expr.get_identifier();
409+
CHECK_RETURN(!identifier.empty());
409410

410411
if(width==0)
411412
{
@@ -416,13 +417,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
416417
{
417418
map.get_literals(identifier, type, width, bv);
418419

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

428431
return bv;

0 commit comments

Comments
 (0)