15
15
16
16
#include < util/arith_tools.h>
17
17
#include < util/c_types.h>
18
+ #include < util/exception_utils.h>
18
19
#include < util/pointer_predicates.h>
19
20
#include < util/type_eq.h>
20
21
@@ -379,7 +380,9 @@ exprt string_abstractiont::make_val_or_dummy_rec(goto_programt &dest,
379
380
++it2;
380
381
}
381
382
382
- assert (components.size ()==seen);
383
+ INVARIANT (
384
+ components.size () == seen,
385
+ " some of the symbol's component names were not found in the source" );
383
386
}
384
387
385
388
return nil_exprt ();
@@ -549,8 +552,8 @@ void string_abstractiont::abstract_function_call(
549
552
550
553
if (it1==arguments.end ())
551
554
{
552
- error () << " function call: not enough arguments " << eom;
553
- throw 0 ;
555
+ throw incorrect_goto_program_exceptiont (
556
+ " function call: not enough arguments " , target-> source_location ) ;
554
557
}
555
558
556
559
str_args.push_back (exprt ());
@@ -562,8 +565,9 @@ void string_abstractiont::abstract_function_call(
562
565
if (str_args.back ().type ().id ()==ID_array &&
563
566
abstract_type.id ()==ID_pointer)
564
567
{
565
- assert (type_eq (str_args.back ().type ().subtype (),
566
- abstract_type.subtype (), ns));
568
+ INVARIANT (
569
+ type_eq (str_args.back ().type ().subtype (), abstract_type.subtype (), ns),
570
+ " argument array type differs from formal parameter pointer type" );
567
571
568
572
index_exprt idx (str_args.back (), from_integer (0 , index_type ()));
569
573
// disable bounds check on that one
@@ -600,19 +604,19 @@ void string_abstractiont::replace_string_macros(
600
604
{
601
605
if (expr.id ()==" is_zero_string" )
602
606
{
603
- assert (expr.operands ().size ()== 1 );
607
+ PRECONDITION (expr.operands ().size () == 1 );
604
608
exprt tmp=build (expr.op0 (), whatt::IS_ZERO, lhs, source_location);
605
609
expr.swap (tmp);
606
610
}
607
611
else if (expr.id ()==" zero_string_length" )
608
612
{
609
- assert (expr.operands ().size ()== 1 );
613
+ PRECONDITION (expr.operands ().size () == 1 );
610
614
exprt tmp=build (expr.op0 (), whatt::LENGTH, lhs, source_location);
611
615
expr.swap (tmp);
612
616
}
613
617
else if (expr.id ()==" buffer_size" )
614
618
{
615
- assert (expr.operands ().size ()== 1 );
619
+ PRECONDITION (expr.operands ().size () == 1 );
616
620
exprt tmp=build (expr.op0 (), whatt::SIZE, false , source_location);
617
621
expr.swap (tmp);
618
622
}
@@ -631,8 +635,10 @@ exprt string_abstractiont::build(
631
635
if (pointer.id ()==ID_typecast)
632
636
{
633
637
// cast from another pointer type?
634
- assert (pointer.operands ().size ()==1 );
635
- if (pointer.op0 ().type ().id ()!=ID_pointer)
638
+ INVARIANT (
639
+ pointer.operands ().size () == 1 ,
640
+ " pointer typecast takes exactly 1 argument" );
641
+ if (pointer.op0 ().type ().id () != ID_pointer)
636
642
return build_unknown (what, write );
637
643
638
644
// recursive call
@@ -669,7 +675,7 @@ const typet &string_abstractiont::build_abstraction_type(const typet &type)
669
675
670
676
abstraction_types_map.swap (tmp);
671
677
map_entry=tmp.find (eff_type);
672
- assert (map_entry!= tmp.end ());
678
+ CHECK_RETURN (map_entry != tmp.end ());
673
679
return abstraction_types_map.insert (
674
680
std::make_pair (eff_type, map_entry->second )).first ->second ;
675
681
}
@@ -836,7 +842,7 @@ bool string_abstractiont::build_if(const if_exprt &o_if,
836
842
bool string_abstractiont::build_array (const array_exprt &object,
837
843
exprt &dest, bool write)
838
844
{
839
- assert (is_char_type (object.type ().subtype ()));
845
+ PRECONDITION (is_char_type (object.type ().subtype ()));
840
846
841
847
// writing is invalid
842
848
if (write )
@@ -847,7 +853,8 @@ bool string_abstractiont::build_array(const array_exprt &object,
847
853
// don't do anything, if we cannot determine the size
848
854
if (to_integer (a_size, size))
849
855
return true ;
850
- assert (size==object.operands ().size ());
856
+ INVARIANT (
857
+ size == object.operands ().size (), " wrong number of array object arguments" );
851
858
852
859
exprt::operandst::const_iterator it=object.operands ().begin ();
853
860
for (mp_integer i=0 ; i<size; ++i, ++it)
@@ -860,7 +867,7 @@ bool string_abstractiont::build_array(const array_exprt &object,
860
867
bool string_abstractiont::build_pointer (const exprt &object,
861
868
exprt &dest, bool write)
862
869
{
863
- assert (object.type ().id ()== ID_pointer);
870
+ PRECONDITION (object.type ().id () == ID_pointer);
864
871
865
872
pointer_arithmetict ptr (object);
866
873
if (ptr.pointer .id ()==ID_address_of)
@@ -945,7 +952,7 @@ bool string_abstractiont::build_symbol(const symbol_exprt &sym, exprt &dest)
945
952
const symbolt &symbol=ns.lookup (sym.get_identifier ());
946
953
947
954
const typet &abstract_type=build_abstraction_type (symbol.type );
948
- assert (!abstract_type.is_nil ());
955
+ CHECK_RETURN (!abstract_type.is_nil ());
949
956
950
957
irep_idt identifier=" " ;
951
958
@@ -1125,7 +1132,9 @@ goto_programt::targett string_abstractiont::abstract_char_assign(
1125
1132
if (!build_wrap (i_lhs.array (), new_lhs, true ))
1126
1133
{
1127
1134
exprt i2=member (new_lhs, whatt::LENGTH);
1128
- assert (i2.is_not_nil ());
1135
+ INVARIANT (
1136
+ i2.is_not_nil (),
1137
+ " failed to create length-component for the left-hand-side" );
1129
1138
1130
1139
exprt new_length=i_lhs.index ();
1131
1140
make_type (new_length, i2.type ());
@@ -1143,7 +1152,9 @@ goto_programt::targett string_abstractiont::abstract_char_assign(
1143
1152
if (!build_wrap (ptr.pointer , new_lhs, true ))
1144
1153
{
1145
1154
const exprt i2=member (new_lhs, whatt::LENGTH);
1146
- assert (i2.is_not_nil ());
1155
+ INVARIANT (
1156
+ i2.is_not_nil (),
1157
+ " failed to create length-component for the left-hand-side" );
1147
1158
1148
1159
make_type (ptr.offset , build_type (whatt::LENGTH));
1149
1160
return
@@ -1171,7 +1182,9 @@ goto_programt::targett string_abstractiont::char_assign(
1171
1182
goto_programt tmp;
1172
1183
1173
1184
const exprt i1=member (new_lhs, whatt::IS_ZERO);
1174
- assert (i1.is_not_nil ());
1185
+ INVARIANT (
1186
+ i1.is_not_nil (),
1187
+ " failed to create is_zero-component for the left-hand-side" );
1175
1188
1176
1189
goto_programt::targett assignment1=tmp.add_instruction ();
1177
1190
assignment1->make_assignment ();
@@ -1207,7 +1220,7 @@ goto_programt::targett string_abstractiont::value_assignments(
1207
1220
if (rhs.id ()==ID_if)
1208
1221
return value_assignments_if (dest, target, lhs, to_if_expr (rhs));
1209
1222
1210
- assert (type_eq (lhs.type (), rhs.type (), ns));
1223
+ PRECONDITION (type_eq (lhs.type (), rhs.type (), ns));
1211
1224
1212
1225
if (lhs.type ().id ()==ID_array)
1213
1226
{
@@ -1234,7 +1247,8 @@ goto_programt::targett string_abstractiont::value_assignments(
1234
1247
1235
1248
for (const auto &comp : struct_union_type.components ())
1236
1249
{
1237
- assert (!comp.get_name ().empty ());
1250
+ INVARIANT (
1251
+ !comp.get_name ().empty (), " struct/union components must have a name" );
1238
1252
1239
1253
target=value_assignments (dest, target,
1240
1254
member_exprt (lhs, comp.get_name (), comp.type ()),
@@ -1334,8 +1348,9 @@ exprt string_abstractiont::member(const exprt &a, whatt what)
1334
1348
if (a.is_nil ())
1335
1349
return a;
1336
1350
1337
- assert (type_eq (a.type (), string_struct, ns) ||
1338
- is_ptr_string_struct (a.type ()));
1351
+ PRECONDITION_WITH_DIAGNOSTICS (
1352
+ type_eq (a.type (), string_struct, ns) || is_ptr_string_struct (a.type ()),
1353
+ " either the expression is not a string or it is not a pointer to one" );
1339
1354
1340
1355
exprt struct_op=
1341
1356
a.type ().id ()==ID_pointer?
0 commit comments