Skip to content

Commit 0c6ed43

Browse files
committed
Error handling cleanup in solvers/flattening
Files boolbv_constant.cpp, boolbv.cpp
1 parent 424f1ac commit 0c6ed43

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 &
@@ -257,7 +256,9 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
257256
return convert_unary_minus(to_unary_expr(expr));
258257
else if(expr.id()==ID_unary_plus)
259258
{
260-
assert(expr.operands().size()==1);
259+
DATA_INVARIANT(
260+
expr.operands().size() == 1,
261+
"unary plus expressions should have one operand");
261262
return convert_bitvector(expr.op0());
262263
}
263264
else if(expr.id()==ID_abs)
@@ -310,7 +311,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
310311
else if(expr.id()==ID_float_debug1 ||
311312
expr.id()==ID_float_debug2)
312313
{
313-
assert(expr.operands().size()==2);
314+
DATA_INVARIANT(expr.operands().size() == 2, "");
314315
bvt bv0=convert_bitvector(expr.op0());
315316
bvt bv1=convert_bitvector(expr.op1());
316317
float_utilst float_utils(prop, to_floatbv_type(expr.type()));
@@ -332,8 +333,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
332333
if(width==0)
333334
return conversion_failed(expr);
334335

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

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

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

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

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

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

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

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

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

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

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

427430
return bv;

0 commit comments

Comments
 (0)