8
8
9
9
#include " boolbv.h"
10
10
11
- #include < cassert >
11
+ #include < algorithm >
12
12
#include < map>
13
13
#include < set>
14
14
@@ -36,7 +36,10 @@ bool boolbvt::literal(
36
36
{
37
37
if (expr.type ().id ()==ID_bool)
38
38
{
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" );
40
43
return prop_conv_solvert::literal (expr, dest);
41
44
}
42
45
else
@@ -54,7 +57,8 @@ bool boolbvt::literal(
54
57
55
58
const boolbv_mapt::map_entryt &map_entry=it_m->second ;
56
59
57
- assert (bit<map_entry.literal_map .size ());
60
+ INVARIANT (
61
+ bit < map_entry.literal_map .size (), " bit index shall be within bounds" );
58
62
if (!map_entry.literal_map [bit].is_set )
59
63
return true ;
60
64
@@ -66,15 +70,11 @@ bool boolbvt::literal(
66
70
const index_exprt &index_expr=to_index_expr (expr);
67
71
68
72
std::size_t element_width=boolbv_width (index_expr.type ());
73
+ CHECK_RETURN (element_width != 0 );
69
74
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 ());
72
76
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);
78
78
79
79
return literal (index_expr.array (), bit+offset, dest);
80
80
}
@@ -99,18 +99,16 @@ bool boolbvt::literal(
99
99
return literal (expr.op0 (), bit+offset, dest);
100
100
101
101
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 );
105
103
106
104
offset+=element_width;
107
105
}
108
106
109
- throw " failed to find component" ;
107
+ INVARIANT ( false , " struct type should have accessed component" ) ;
110
108
}
111
109
}
112
110
113
- throw " found no literal for expression " ;
111
+ INVARIANT ( false , " expression should have a corresponding literal " ) ;
114
112
}
115
113
116
114
const bvt &
@@ -309,7 +307,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
309
307
else if (expr.id ()==ID_float_debug1 ||
310
308
expr.id ()==ID_float_debug2)
311
309
{
312
- assert (expr.operands ().size ()== 2 );
310
+ DATA_INVARIANT (expr.operands ().size () == 2 , " " );
313
311
bvt bv0=convert_bitvector (expr.op0 ());
314
312
bvt bv1=convert_bitvector (expr.op1 ());
315
313
float_utilst float_utils (prop, to_floatbv_type (expr.type ()));
@@ -331,8 +329,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
331
329
if (width==0 )
332
330
return conversion_failed (expr);
333
331
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" ) ;
336
334
337
335
if (expr.type ().id ()!=ID_array)
338
336
return conversion_failed (expr);
@@ -359,10 +357,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
359
357
360
358
const bvt &tmp=convert_bv (expr_op1);
361
359
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" );
363
364
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 ());
366
366
367
367
for (std::size_t j=0 ; j<tmp.size (); j++)
368
368
bv[offset+j]=tmp[j];
@@ -400,10 +400,8 @@ bvt boolbvt::convert_symbol(const exprt &expr)
400
400
bvt bv;
401
401
bv.resize (width);
402
402
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 ());
407
405
408
406
if (width==0 )
409
407
{
@@ -414,13 +412,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
414
412
{
415
413
map.get_literals (identifier, type, width, bv);
416
414
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));
424
424
}
425
425
426
426
return bv;
0 commit comments