Skip to content

Commit 3e8c886

Browse files
authored
Merge pull request #3013 from sonodtt/invariant_cleanup-string_abstraction
Cleanup asserts & throws - replace with invariants
2 parents 1e32c2f + 165e0e6 commit 3e8c886

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/goto-programs/string_abstraction.cpp

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515

1616
#include <util/arith_tools.h>
1717
#include <util/c_types.h>
18+
#include <util/exception_utils.h>
1819
#include <util/pointer_predicates.h>
1920
#include <util/type_eq.h>
2021

@@ -379,7 +380,9 @@ exprt string_abstractiont::make_val_or_dummy_rec(goto_programt &dest,
379380
++it2;
380381
}
381382

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");
383386
}
384387

385388
return nil_exprt();
@@ -549,8 +552,8 @@ void string_abstractiont::abstract_function_call(
549552

550553
if(it1==arguments.end())
551554
{
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);
554557
}
555558

556559
str_args.push_back(exprt());
@@ -562,8 +565,9 @@ void string_abstractiont::abstract_function_call(
562565
if(str_args.back().type().id()==ID_array &&
563566
abstract_type.id()==ID_pointer)
564567
{
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");
567571

568572
index_exprt idx(str_args.back(), from_integer(0, index_type()));
569573
// disable bounds check on that one
@@ -600,19 +604,19 @@ void string_abstractiont::replace_string_macros(
600604
{
601605
if(expr.id()=="is_zero_string")
602606
{
603-
assert(expr.operands().size()==1);
607+
PRECONDITION(expr.operands().size() == 1);
604608
exprt tmp=build(expr.op0(), whatt::IS_ZERO, lhs, source_location);
605609
expr.swap(tmp);
606610
}
607611
else if(expr.id()=="zero_string_length")
608612
{
609-
assert(expr.operands().size()==1);
613+
PRECONDITION(expr.operands().size() == 1);
610614
exprt tmp=build(expr.op0(), whatt::LENGTH, lhs, source_location);
611615
expr.swap(tmp);
612616
}
613617
else if(expr.id()=="buffer_size")
614618
{
615-
assert(expr.operands().size()==1);
619+
PRECONDITION(expr.operands().size() == 1);
616620
exprt tmp=build(expr.op0(), whatt::SIZE, false, source_location);
617621
expr.swap(tmp);
618622
}
@@ -631,8 +635,10 @@ exprt string_abstractiont::build(
631635
if(pointer.id()==ID_typecast)
632636
{
633637
// 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)
636642
return build_unknown(what, write);
637643

638644
// recursive call
@@ -669,7 +675,7 @@ const typet &string_abstractiont::build_abstraction_type(const typet &type)
669675

670676
abstraction_types_map.swap(tmp);
671677
map_entry=tmp.find(eff_type);
672-
assert(map_entry!=tmp.end());
678+
CHECK_RETURN(map_entry != tmp.end());
673679
return abstraction_types_map.insert(
674680
std::make_pair(eff_type, map_entry->second)).first->second;
675681
}
@@ -836,7 +842,7 @@ bool string_abstractiont::build_if(const if_exprt &o_if,
836842
bool string_abstractiont::build_array(const array_exprt &object,
837843
exprt &dest, bool write)
838844
{
839-
assert(is_char_type(object.type().subtype()));
845+
PRECONDITION(is_char_type(object.type().subtype()));
840846

841847
// writing is invalid
842848
if(write)
@@ -847,7 +853,8 @@ bool string_abstractiont::build_array(const array_exprt &object,
847853
// don't do anything, if we cannot determine the size
848854
if(to_integer(a_size, size))
849855
return true;
850-
assert(size==object.operands().size());
856+
INVARIANT(
857+
size == object.operands().size(), "wrong number of array object arguments");
851858

852859
exprt::operandst::const_iterator it=object.operands().begin();
853860
for(mp_integer i=0; i<size; ++i, ++it)
@@ -860,7 +867,7 @@ bool string_abstractiont::build_array(const array_exprt &object,
860867
bool string_abstractiont::build_pointer(const exprt &object,
861868
exprt &dest, bool write)
862869
{
863-
assert(object.type().id()==ID_pointer);
870+
PRECONDITION(object.type().id() == ID_pointer);
864871

865872
pointer_arithmetict ptr(object);
866873
if(ptr.pointer.id()==ID_address_of)
@@ -945,7 +952,7 @@ bool string_abstractiont::build_symbol(const symbol_exprt &sym, exprt &dest)
945952
const symbolt &symbol=ns.lookup(sym.get_identifier());
946953

947954
const typet &abstract_type=build_abstraction_type(symbol.type);
948-
assert(!abstract_type.is_nil());
955+
CHECK_RETURN(!abstract_type.is_nil());
949956

950957
irep_idt identifier="";
951958

@@ -1125,7 +1132,9 @@ goto_programt::targett string_abstractiont::abstract_char_assign(
11251132
if(!build_wrap(i_lhs.array(), new_lhs, true))
11261133
{
11271134
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");
11291138

11301139
exprt new_length=i_lhs.index();
11311140
make_type(new_length, i2.type());
@@ -1143,7 +1152,9 @@ goto_programt::targett string_abstractiont::abstract_char_assign(
11431152
if(!build_wrap(ptr.pointer, new_lhs, true))
11441153
{
11451154
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");
11471158

11481159
make_type(ptr.offset, build_type(whatt::LENGTH));
11491160
return
@@ -1171,7 +1182,9 @@ goto_programt::targett string_abstractiont::char_assign(
11711182
goto_programt tmp;
11721183

11731184
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");
11751188

11761189
goto_programt::targett assignment1=tmp.add_instruction();
11771190
assignment1->make_assignment();
@@ -1207,7 +1220,7 @@ goto_programt::targett string_abstractiont::value_assignments(
12071220
if(rhs.id()==ID_if)
12081221
return value_assignments_if(dest, target, lhs, to_if_expr(rhs));
12091222

1210-
assert(type_eq(lhs.type(), rhs.type(), ns));
1223+
PRECONDITION(type_eq(lhs.type(), rhs.type(), ns));
12111224

12121225
if(lhs.type().id()==ID_array)
12131226
{
@@ -1234,7 +1247,8 @@ goto_programt::targett string_abstractiont::value_assignments(
12341247

12351248
for(const auto &comp : struct_union_type.components())
12361249
{
1237-
assert(!comp.get_name().empty());
1250+
INVARIANT(
1251+
!comp.get_name().empty(), "struct/union components must have a name");
12381252

12391253
target=value_assignments(dest, target,
12401254
member_exprt(lhs, comp.get_name(), comp.type()),
@@ -1334,8 +1348,9 @@ exprt string_abstractiont::member(const exprt &a, whatt what)
13341348
if(a.is_nil())
13351349
return a;
13361350

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");
13391354

13401355
exprt struct_op=
13411356
a.type().id()==ID_pointer?

0 commit comments

Comments
 (0)