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 (to_symbol_expr (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
}
@@ -96,18 +96,16 @@ bool boolbvt::literal(
96
96
return literal (expr.op0 (), bit+offset, dest);
97
97
98
98
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 );
102
100
103
101
offset+=element_width;
104
102
}
105
103
106
- throw " failed to find component" ;
104
+ INVARIANT ( false , " struct type should have accessed component" ) ;
107
105
}
108
106
}
109
107
110
- throw " found no literal for expression " ;
108
+ INVARIANT ( false , " expression should have a corresponding literal " ) ;
111
109
}
112
110
113
111
const bvt &
@@ -308,7 +306,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
308
306
else if (expr.id ()==ID_float_debug1 ||
309
307
expr.id ()==ID_float_debug2)
310
308
{
311
- assert (expr.operands ().size ()== 2 );
309
+ DATA_INVARIANT (expr.operands ().size () == 2 , " " );
312
310
bvt bv0=convert_bitvector (expr.op0 ());
313
311
bvt bv1=convert_bitvector (expr.op1 ());
314
312
float_utilst float_utils (prop, to_floatbv_type (expr.type ()));
@@ -330,8 +328,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
330
328
if (width==0 )
331
329
return conversion_failed (expr);
332
330
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" ) ;
335
333
336
334
if (expr.type ().id ()!=ID_array)
337
335
return conversion_failed (expr);
@@ -358,10 +356,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
358
356
359
357
const bvt &tmp=convert_bv (expr_op1);
360
358
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" );
362
363
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 ());
365
365
366
366
for (std::size_t j=0 ; j<tmp.size (); j++)
367
367
bv[offset+j]=tmp[j];
@@ -399,10 +399,8 @@ bvt boolbvt::convert_symbol(const exprt &expr)
399
399
bvt bv;
400
400
bv.resize (width);
401
401
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 ());
406
404
407
405
if (width==0 )
408
406
{
@@ -413,13 +411,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
413
411
{
414
412
map.get_literals (identifier, type, width, bv);
415
413
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));
423
423
}
424
424
425
425
return bv;
0 commit comments