Skip to content

Commit 00406fa

Browse files
Replacing assertions by appropriate macros in string preprocessing
1 parent 5c9eb59 commit 00406fa

File tree

1 file changed

+27
-34
lines changed

1 file changed

+27
-34
lines changed

src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,12 @@ exprt::operandst
367367
symbol_tablet &symbol_table,
368368
code_blockt &init_code)
369369
{
370-
assert(operands.size()==2);
370+
PRECONDITION(operands.size()==2);
371371
exprt::operandst ops;
372-
exprt op0=operands[0];
373-
exprt op1=operands[1];
372+
const exprt &op0=operands[0];
373+
const exprt &op1=operands[1];
374+
PRECONDITION(implements_java_char_sequence(op0.type()));
374375

375-
assert(implements_java_char_sequence(op0.type()));
376376
dereference_exprt deref0=
377377
checked_dereference(op0, to_pointer_type(op0.type()).subtype());
378378
process_single_operand(ops, deref0, loc, symbol_table, init_code);
@@ -394,21 +394,16 @@ exprt::operandst
394394
typet java_string_library_preprocesst::get_data_type(
395395
const typet &type, const symbol_tablet &symbol_table)
396396
{
397+
PRECONDITION(type.id()==ID_struct || type.id()==ID_symbol);
397398
if(type.id()==ID_symbol)
398399
{
399400
symbolt sym=symbol_table.lookup(to_symbol_type(type).get_identifier());
400-
assert(sym.type.id()!=ID_symbol);
401+
CHECK_RETURN(sym.type.id()!=ID_symbol);
401402
return get_data_type(sym.type, symbol_table);
402403
}
403-
else
404-
{
405-
assert(type.id()==ID_struct);
406-
const struct_typet &struct_type=to_struct_type(type);
407-
for(auto component : struct_type.components())
408-
if(component.get_name()=="data")
409-
return component.type();
410-
assert(false && "type does not contain data component");
411-
}
404+
// else type id is ID_struct
405+
const struct_typet &struct_type=to_struct_type(type);
406+
return struct_type.component_type("data");
412407
}
413408

414409
/// Finds the type of the length component
@@ -418,21 +413,16 @@ typet java_string_library_preprocesst::get_data_type(
418413
typet java_string_library_preprocesst::get_length_type(
419414
const typet &type, const symbol_tablet &symbol_table)
420415
{
416+
PRECONDITION(type.id()==ID_struct || type.id()==ID_symbol);
421417
if(type.id()==ID_symbol)
422418
{
423419
symbolt sym=symbol_table.lookup(to_symbol_type(type).get_identifier());
424-
assert(sym.type.id()!=ID_symbol);
420+
CHECK_RETURN(sym.type.id()!=ID_symbol);
425421
return get_length_type(sym.type, symbol_table);
426422
}
427-
else
428-
{
429-
assert(type.id()==ID_struct);
430-
const struct_typet &struct_type=to_struct_type(type);
431-
for(auto component : struct_type.components())
432-
if(component.get_name()=="length")
433-
return component.type();
434-
assert(false && "type does not contain length component");
435-
}
423+
// else type id is ID_struct
424+
const struct_typet &struct_type=to_struct_type(type);
425+
return struct_type.component_type("length");
436426
}
437427

438428
/// access length member
@@ -728,7 +718,7 @@ codet java_string_library_preprocesst::code_assign_components_to_java_string(
728718
const exprt &rhs_length,
729719
symbol_tablet &symbol_table)
730720
{
731-
assert(implements_java_char_sequence(lhs.type()));
721+
PRECONDITION(implements_java_char_sequence(lhs.type()));
732722
dereference_exprt deref=checked_dereference(lhs, lhs.type().subtype());
733723

734724
code_blockt code;
@@ -786,7 +776,7 @@ codet java_string_library_preprocesst::
786776
const source_locationt &loc,
787777
symbol_tablet &symbol_table)
788778
{
789-
assert(implements_java_char_sequence(lhs.type()));
779+
PRECONDITION(implements_java_char_sequence(lhs.type()));
790780
dereference_exprt deref=checked_dereference(lhs, lhs.type().subtype());
791781

792782
code_blockt code;
@@ -812,7 +802,7 @@ codet java_string_library_preprocesst::
812802
codet java_string_library_preprocesst::code_assign_java_string_to_string_expr(
813803
const string_exprt &lhs, const exprt &rhs, symbol_tablet &symbol_table)
814804
{
815-
assert(implements_java_char_sequence(rhs.type()));
805+
PRECONDITION(implements_java_char_sequence(rhs.type()));
816806

817807
typet deref_type;
818808
if(rhs.type().subtype().id()==ID_symbol)
@@ -901,7 +891,7 @@ codet java_string_library_preprocesst::make_float_to_string_code(
901891
{
902892
// Getting the argument
903893
code_typet::parameterst params=type.parameters();
904-
assert(params.size()==1 && "wrong number of parameters in Float.toString");
894+
PRECONDITION(params.size()==1);
905895
exprt arg=symbol_exprt(params[0].get_identifier(), params[0].type());
906896

907897
// Holder for output code
@@ -1010,7 +1000,10 @@ codet java_string_library_preprocesst::make_float_to_string_code(
10101000
string_expr_list.push_back(neg_simple_notation);
10111001

10121002
// Combining all cases
1013-
assert(string_expr_list.size()==condition_list.size());
1003+
INVARIANT(
1004+
string_expr_list.size()==condition_list.size(),
1005+
"number of created strings should correspond to number of conditions");
1006+
10141007
// We do not check the condition of the first element in the list as it
10151008
// will be reached only if all other conditions are not satisfied.
10161009
codet tmp_code=code_assign_string_expr_to_java_string(
@@ -1057,7 +1050,7 @@ codet java_string_library_preprocesst::make_init_function_from_call(
10571050
code_typet::parameterst params=type.parameters();
10581051

10591052
// The first parameter is the object to be initialized
1060-
assert(!params.empty());
1053+
PRECONDITION(!params.empty());
10611054
exprt arg_this=symbol_exprt(params[0].get_identifier(), params[0].type());
10621055
if(ignore_first_arg)
10631056
params.erase(params.begin());
@@ -1098,11 +1091,11 @@ codet java_string_library_preprocesst::
10981091
symbol_tablet &symbol_table)
10991092
{
11001093
// This is similar to assign functions except we return a pointer to `this`
1094+
code_typet::parameterst params=type.parameters();
1095+
PRECONDITION(!params.empty());
11011096
code_blockt code;
11021097
code.add(make_assign_function_from_call(
11031098
function_name, type, loc, symbol_table));
1104-
code_typet::parameterst params=type.parameters();
1105-
assert(!params.empty());
11061099
exprt arg_this=symbol_exprt(params[0].get_identifier(), params[0].type());
11071100
code.add(code_returnt(arg_this));
11081101
return code;
@@ -1149,10 +1142,10 @@ codet java_string_library_preprocesst::make_string_to_char_array_code(
11491142
symbol_tablet &symbol_table)
11501143
{
11511144
code_blockt code;
1152-
assert(!type.parameters().empty());
1145+
PRECONDITION(!type.parameters().empty());
11531146
const code_typet::parametert &p=type.parameters()[0];
11541147
symbol_exprt string_argument(p.get_identifier(), p.type());
1155-
assert(implements_java_char_sequence(string_argument.type()));
1148+
PRECONDITION(implements_java_char_sequence(string_argument.type()));
11561149

11571150
// lhs = new java::array[char]
11581151
exprt lhs=allocate_fresh_array(

0 commit comments

Comments
 (0)