Skip to content

Commit 6d1be13

Browse files
Merge pull request #1282 from romainbrenguier/cleaning/assertion-in-preprocessing
Replacing assertions by appropriate macros in string preprocessing
2 parents d099f9b + 00406fa commit 6d1be13

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
@@ -369,12 +369,12 @@ exprt::operandst
369369
symbol_tablet &symbol_table,
370370
code_blockt &init_code)
371371
{
372-
assert(operands.size()==2);
372+
PRECONDITION(operands.size()==2);
373373
exprt::operandst ops;
374-
exprt op0=operands[0];
375-
exprt op1=operands[1];
374+
const exprt &op0=operands[0];
375+
const exprt &op1=operands[1];
376+
PRECONDITION(implements_java_char_sequence(op0.type()));
376377

377-
assert(implements_java_char_sequence(op0.type()));
378378
dereference_exprt deref0=
379379
checked_dereference(op0, to_pointer_type(op0.type()).subtype());
380380
process_single_operand(ops, deref0, loc, symbol_table, init_code);
@@ -396,21 +396,16 @@ exprt::operandst
396396
typet java_string_library_preprocesst::get_data_type(
397397
const typet &type, const symbol_tablet &symbol_table)
398398
{
399+
PRECONDITION(type.id()==ID_struct || type.id()==ID_symbol);
399400
if(type.id()==ID_symbol)
400401
{
401402
symbolt sym=symbol_table.lookup(to_symbol_type(type).get_identifier());
402-
assert(sym.type.id()!=ID_symbol);
403+
CHECK_RETURN(sym.type.id()!=ID_symbol);
403404
return get_data_type(sym.type, symbol_table);
404405
}
405-
else
406-
{
407-
assert(type.id()==ID_struct);
408-
const struct_typet &struct_type=to_struct_type(type);
409-
for(auto component : struct_type.components())
410-
if(component.get_name()=="data")
411-
return component.type();
412-
assert(false && "type does not contain data component");
413-
}
406+
// else type id is ID_struct
407+
const struct_typet &struct_type=to_struct_type(type);
408+
return struct_type.component_type("data");
414409
}
415410

416411
/// Finds the type of the length component
@@ -420,21 +415,16 @@ typet java_string_library_preprocesst::get_data_type(
420415
typet java_string_library_preprocesst::get_length_type(
421416
const typet &type, const symbol_tablet &symbol_table)
422417
{
418+
PRECONDITION(type.id()==ID_struct || type.id()==ID_symbol);
423419
if(type.id()==ID_symbol)
424420
{
425421
symbolt sym=symbol_table.lookup(to_symbol_type(type).get_identifier());
426-
assert(sym.type.id()!=ID_symbol);
422+
CHECK_RETURN(sym.type.id()!=ID_symbol);
427423
return get_length_type(sym.type, symbol_table);
428424
}
429-
else
430-
{
431-
assert(type.id()==ID_struct);
432-
const struct_typet &struct_type=to_struct_type(type);
433-
for(auto component : struct_type.components())
434-
if(component.get_name()=="length")
435-
return component.type();
436-
assert(false && "type does not contain length component");
437-
}
425+
// else type id is ID_struct
426+
const struct_typet &struct_type=to_struct_type(type);
427+
return struct_type.component_type("length");
438428
}
439429

440430
/// access length member
@@ -730,7 +720,7 @@ codet java_string_library_preprocesst::code_assign_components_to_java_string(
730720
const exprt &rhs_length,
731721
symbol_tablet &symbol_table)
732722
{
733-
assert(implements_java_char_sequence(lhs.type()));
723+
PRECONDITION(implements_java_char_sequence(lhs.type()));
734724
dereference_exprt deref=checked_dereference(lhs, lhs.type().subtype());
735725

736726
code_blockt code;
@@ -788,7 +778,7 @@ codet java_string_library_preprocesst::
788778
const source_locationt &loc,
789779
symbol_tablet &symbol_table)
790780
{
791-
assert(implements_java_char_sequence(lhs.type()));
781+
PRECONDITION(implements_java_char_sequence(lhs.type()));
792782
dereference_exprt deref=checked_dereference(lhs, lhs.type().subtype());
793783

794784
code_blockt code;
@@ -814,7 +804,7 @@ codet java_string_library_preprocesst::
814804
codet java_string_library_preprocesst::code_assign_java_string_to_string_expr(
815805
const string_exprt &lhs, const exprt &rhs, symbol_tablet &symbol_table)
816806
{
817-
assert(implements_java_char_sequence(rhs.type()));
807+
PRECONDITION(implements_java_char_sequence(rhs.type()));
818808

819809
typet deref_type;
820810
if(rhs.type().subtype().id()==ID_symbol)
@@ -903,7 +893,7 @@ codet java_string_library_preprocesst::make_float_to_string_code(
903893
{
904894
// Getting the argument
905895
code_typet::parameterst params=type.parameters();
906-
assert(params.size()==1 && "wrong number of parameters in Float.toString");
896+
PRECONDITION(params.size()==1);
907897
exprt arg=symbol_exprt(params[0].get_identifier(), params[0].type());
908898

909899
// Holder for output code
@@ -1012,7 +1002,10 @@ codet java_string_library_preprocesst::make_float_to_string_code(
10121002
string_expr_list.push_back(neg_simple_notation);
10131003

10141004
// Combining all cases
1015-
assert(string_expr_list.size()==condition_list.size());
1005+
INVARIANT(
1006+
string_expr_list.size()==condition_list.size(),
1007+
"number of created strings should correspond to number of conditions");
1008+
10161009
// We do not check the condition of the first element in the list as it
10171010
// will be reached only if all other conditions are not satisfied.
10181011
codet tmp_code=code_assign_string_expr_to_java_string(
@@ -1059,7 +1052,7 @@ codet java_string_library_preprocesst::make_init_function_from_call(
10591052
code_typet::parameterst params=type.parameters();
10601053

10611054
// The first parameter is the object to be initialized
1062-
assert(!params.empty());
1055+
PRECONDITION(!params.empty());
10631056
exprt arg_this=symbol_exprt(params[0].get_identifier(), params[0].type());
10641057
if(ignore_first_arg)
10651058
params.erase(params.begin());
@@ -1100,11 +1093,11 @@ codet java_string_library_preprocesst::
11001093
symbol_tablet &symbol_table)
11011094
{
11021095
// This is similar to assign functions except we return a pointer to `this`
1096+
code_typet::parameterst params=type.parameters();
1097+
PRECONDITION(!params.empty());
11031098
code_blockt code;
11041099
code.add(make_assign_function_from_call(
11051100
function_name, type, loc, symbol_table));
1106-
code_typet::parameterst params=type.parameters();
1107-
assert(!params.empty());
11081101
exprt arg_this=symbol_exprt(params[0].get_identifier(), params[0].type());
11091102
code.add(code_returnt(arg_this));
11101103
return code;
@@ -1151,10 +1144,10 @@ codet java_string_library_preprocesst::make_string_to_char_array_code(
11511144
symbol_tablet &symbol_table)
11521145
{
11531146
code_blockt code;
1154-
assert(!type.parameters().empty());
1147+
PRECONDITION(!type.parameters().empty());
11551148
const code_typet::parametert &p=type.parameters()[0];
11561149
symbol_exprt string_argument(p.get_identifier(), p.type());
1157-
assert(implements_java_char_sequence(string_argument.type()));
1150+
PRECONDITION(implements_java_char_sequence(string_argument.type()));
11581151

11591152
// lhs = new java::array[char]
11601153
exprt lhs=allocate_fresh_array(

0 commit comments

Comments
 (0)