8
8
9
9
#include " boolbv.h"
10
10
11
- #include < cassert>
12
11
#include < map>
13
12
#include < set>
14
13
@@ -36,7 +35,10 @@ bool boolbvt::literal(
36
35
{
37
36
if (expr.type ().id ()==ID_bool)
38
37
{
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" );
40
42
return prop_conv_solvert::literal (expr, dest);
41
43
}
42
44
else
@@ -54,7 +56,8 @@ bool boolbvt::literal(
54
56
55
57
const boolbv_mapt::map_entryt &map_entry=it_m->second ;
56
58
57
- assert (bit<map_entry.literal_map .size ());
59
+ INVARIANT (
60
+ bit < map_entry.literal_map .size (), " bit index shall be within bounds" );
58
61
if (!map_entry.literal_map [bit].is_set )
59
62
return true ;
60
63
@@ -66,13 +69,11 @@ bool boolbvt::literal(
66
69
const index_exprt &index_expr=to_index_expr (expr);
67
70
68
71
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 );
72
73
73
74
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) ;
76
77
77
78
std::size_t offset=integer2unsigned (index *element_width);
78
79
@@ -99,18 +100,16 @@ bool boolbvt::literal(
99
100
return literal (expr.op0 (), bit+offset, dest);
100
101
101
102
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 );
105
104
106
105
offset+=element_width;
107
106
}
108
107
109
- throw " failed to find component" ;
108
+ INVARIANT ( false , " struct type should have accessed component" ) ;
110
109
}
111
110
}
112
111
113
- throw " found no literal for expression " ;
112
+ INVARIANT ( false , " expression should have a corresponding literal " ) ;
114
113
}
115
114
116
115
const bvt &
@@ -258,7 +257,9 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
258
257
return convert_unary_minus (to_unary_expr (expr));
259
258
else if (expr.id ()==ID_unary_plus)
260
259
{
261
- assert (expr.operands ().size ()==1 );
260
+ DATA_INVARIANT (
261
+ expr.operands ().size () == 1 ,
262
+ " unary plus expressions should have one operand" );
262
263
return convert_bitvector (expr.op0 ());
263
264
}
264
265
else if (expr.id ()==ID_abs)
@@ -311,7 +312,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
311
312
else if (expr.id ()==ID_float_debug1 ||
312
313
expr.id ()==ID_float_debug2)
313
314
{
314
- assert (expr.operands ().size ()== 2 );
315
+ DATA_INVARIANT (expr.operands ().size () == 2 , " " );
315
316
bvt bv0=convert_bitvector (expr.op0 ());
316
317
bvt bv1=convert_bitvector (expr.op1 ());
317
318
float_utilst float_utils (prop, to_floatbv_type (expr.type ()));
@@ -333,8 +334,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
333
334
if (width==0 )
334
335
return conversion_failed (expr);
335
336
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" ) ;
338
339
339
340
if (expr.type ().id ()!=ID_array)
340
341
return conversion_failed (expr);
@@ -361,10 +362,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
361
362
362
363
const bvt &tmp=convert_bv (expr_op1);
363
364
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" );
365
369
366
- if (size*tmp.size ()!=width)
367
- throw " convert_lambda: unexpected operand width" ;
370
+ std::size_t offset = integer2unsigned (i * tmp.size ());
368
371
369
372
for (std::size_t j=0 ; j<tmp.size (); j++)
370
373
bv[offset+j]=tmp[j];
@@ -394,18 +397,16 @@ bvt boolbvt::convert_bv_literals(const exprt &expr)
394
397
return bv;
395
398
}
396
399
397
- bvt boolbvt::convert_symbol (const exprt &expr)
400
+ bvt boolbvt::convert_symbol (const symbol_exprt &expr)
398
401
{
399
402
const typet &type=expr.type ();
400
403
std::size_t width=boolbv_width (type);
401
404
402
405
bvt bv;
403
406
bv.resize (width);
404
407
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 ());
409
410
410
411
if (width==0 )
411
412
{
@@ -416,13 +417,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
416
417
{
417
418
map.get_literals (identifier, type, width, bv);
418
419
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));
426
429
}
427
430
428
431
return bv;
0 commit comments