Skip to content

Commit e9df7b9

Browse files
committed
Error handling cleanup in solvers/flattening
Files boolbv_constant.cpp, boolbv.cpp
1 parent 6ad0612 commit e9df7b9

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(to_symbol_expr(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
}
@@ -96,18 +96,16 @@ bool boolbvt::literal(
9696
return literal(expr.op0(), bit+offset, dest);
9797

9898
std::size_t element_width=boolbv_width(subtype);
99-
100-
if(element_width==0)
101-
throw "literal expects a bit-vector type";
99+
CHECK_RETURN(element_width != 0);
102100

103101
offset+=element_width;
104102
}
105103

106-
throw "failed to find component";
104+
INVARIANT(false, "struct type should have accessed component");
107105
}
108106
}
109107

110-
throw "found no literal for expression";
108+
INVARIANT(false, "expression should have a corresponding literal");
111109
}
112110

113111
const bvt &
@@ -308,7 +306,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
308306
else if(expr.id()==ID_float_debug1 ||
309307
expr.id()==ID_float_debug2)
310308
{
311-
assert(expr.operands().size()==2);
309+
DATA_INVARIANT(expr.operands().size() == 2, "");
312310
bvt bv0=convert_bitvector(expr.op0());
313311
bvt bv1=convert_bitvector(expr.op1());
314312
float_utilst float_utils(prop, to_floatbv_type(expr.type()));
@@ -330,8 +328,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
330328
if(width==0)
331329
return conversion_failed(expr);
332330

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

336334
if(expr.type().id()!=ID_array)
337335
return conversion_failed(expr);
@@ -358,10 +356,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
358356

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

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

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

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

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

407405
if(width==0)
408406
{
@@ -413,13 +411,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
413411
{
414412
map.get_literals(identifier, type, width, bv);
415413

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

425425
return bv;

0 commit comments

Comments
 (0)