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 &
@@ -257,7 +256,9 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
257
256
return convert_unary_minus (to_unary_expr (expr));
258
257
else if (expr.id ()==ID_unary_plus)
259
258
{
260
- assert (expr.operands ().size ()==1 );
259
+ DATA_INVARIANT (
260
+ expr.operands ().size () == 1 ,
261
+ " unary plus expressions should have one operand" );
261
262
return convert_bitvector (expr.op0 ());
262
263
}
263
264
else if (expr.id ()==ID_abs)
@@ -310,7 +311,7 @@ bvt boolbvt::convert_bitvector(const exprt &expr)
310
311
else if (expr.id ()==ID_float_debug1 ||
311
312
expr.id ()==ID_float_debug2)
312
313
{
313
- assert (expr.operands ().size ()== 2 );
314
+ DATA_INVARIANT (expr.operands ().size () == 2 , " " );
314
315
bvt bv0=convert_bitvector (expr.op0 ());
315
316
bvt bv1=convert_bitvector (expr.op1 ());
316
317
float_utilst float_utils (prop, to_floatbv_type (expr.type ()));
@@ -332,8 +333,8 @@ bvt boolbvt::convert_lambda(const exprt &expr)
332
333
if (width==0 )
333
334
return conversion_failed (expr);
334
335
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" ) ;
337
338
338
339
if (expr.type ().id ()!=ID_array)
339
340
return conversion_failed (expr);
@@ -360,10 +361,12 @@ bvt boolbvt::convert_lambda(const exprt &expr)
360
361
361
362
const bvt &tmp=convert_bv (expr_op1);
362
363
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" );
364
368
365
- if (size*tmp.size ()!=width)
366
- throw " convert_lambda: unexpected operand width" ;
369
+ std::size_t offset = integer2unsigned (i * tmp.size ());
367
370
368
371
for (std::size_t j=0 ; j<tmp.size (); j++)
369
372
bv[offset+j]=tmp[j];
@@ -393,18 +396,16 @@ bvt boolbvt::convert_bv_literals(const exprt &expr)
393
396
return bv;
394
397
}
395
398
396
- bvt boolbvt::convert_symbol (const exprt &expr)
399
+ bvt boolbvt::convert_symbol (const symbol_exprt &expr)
397
400
{
398
401
const typet &type=expr.type ();
399
402
std::size_t width=boolbv_width (type);
400
403
401
404
bvt bv;
402
405
bv.resize (width);
403
406
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 ());
408
409
409
410
if (width==0 )
410
411
{
@@ -415,13 +416,15 @@ bvt boolbvt::convert_symbol(const exprt &expr)
415
416
{
416
417
map.get_literals (identifier, type, width, bv);
417
418
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));
425
428
}
426
429
427
430
return bv;
0 commit comments