Skip to content

Commit 232617c

Browse files
Add code for String constructor from array
The function what was there before was not correct. It was calling cprover_string_copy which is deprecated.
1 parent a2d7811 commit 232617c

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

src/java_bytecode/java_string_library_preprocess.cpp

+59-6
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,59 @@ codet java_string_library_preprocesst::make_copy_constructor_code(
16781678
return code;
16791679
}
16801680

1681+
/// Used to provide code for constructor from a char array.
1682+
/// The implementation is similar to substring except the 3rd argument is a
1683+
/// count instead of end index
1684+
/// \param type: type of the function call
1685+
/// \param loc: location in the program_invocation_name
1686+
/// \param symbol_table: symbol table
1687+
/// \return code implementing String intitialization from a char array and
1688+
/// arguments offset and end.
1689+
codet java_string_library_preprocesst::make_init_from_array_code(
1690+
const code_typet &type,
1691+
const source_locationt &loc,
1692+
symbol_tablet &symbol_table)
1693+
{
1694+
// Code for the output
1695+
code_blockt code;
1696+
1697+
code_typet::parameterst params = type.parameters();
1698+
PRECONDITION(params.size() == 4);
1699+
exprt::operandst args =
1700+
process_parameters(type.parameters(), loc, symbol_table, code);
1701+
INVARIANT(
1702+
args.size() == 4, "process_parameters preserves number of arguments");
1703+
1704+
/// \todo this assumes the array to be constant between all calls to
1705+
/// string primitives, which may not be true in general.
1706+
refined_string_exprt string_arg = to_string_expr(args[1]);
1707+
add_pointer_to_array_association(
1708+
string_arg.content(),
1709+
dereference_exprt(
1710+
string_arg.content(),
1711+
array_typet(java_char_type(), infinity_exprt(java_int_type()))),
1712+
symbol_table,
1713+
loc,
1714+
code);
1715+
1716+
// The third argument is `count`, whereas the third argument of substring
1717+
// is `end` which corresponds to `offset+count`
1718+
refined_string_exprt string_expr = string_expr_of_function(
1719+
ID_cprover_string_substring_func,
1720+
{args[1], args[2], plus_exprt(args[2], args[3])},
1721+
loc,
1722+
symbol_table,
1723+
code);
1724+
1725+
// Assign string_expr to `this` object
1726+
symbol_exprt arg_this(params[0].get_identifier(), params[0].type());
1727+
code.add(
1728+
code_assign_string_expr_to_java_string(
1729+
arg_this, string_expr, symbol_table));
1730+
1731+
return code;
1732+
}
1733+
16811734
/// Generates code for the String.length method
16821735
/// \param type: type of the function
16831736
/// \param loc: location in the source
@@ -1788,12 +1841,12 @@ void java_string_library_preprocesst::initialize_conversion_table()
17881841
std::placeholders::_1,
17891842
std::placeholders::_2,
17901843
std::placeholders::_3);
1791-
cprover_equivalent_to_java_constructor
1792-
["java::java.lang.String.<init>:([C)V"]=
1793-
ID_cprover_string_copy_func;
1794-
cprover_equivalent_to_java_constructor
1795-
["java::java.lang.String.<init>:([CII)V"]=
1796-
ID_cprover_string_copy_func;
1844+
conversion_table["java::java.lang.String.<init>:([CII)V"] = std::bind(
1845+
&java_string_library_preprocesst::make_init_from_array_code,
1846+
this,
1847+
std::placeholders::_1,
1848+
std::placeholders::_2,
1849+
std::placeholders::_3);
17971850
cprover_equivalent_to_java_constructor
17981851
["java::java.lang.String.<init>:()V"]=
17991852
ID_cprover_string_empty_string_func;

src/java_bytecode/java_string_library_preprocess.h

+5
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ class java_string_library_preprocesst:public messaget
323323
code_blockt &code);
324324

325325
exprt get_object_at_index(const exprt &argv, int index);
326+
327+
codet make_init_from_array_code(
328+
const code_typet &type,
329+
const source_locationt &loc,
330+
symbol_tablet &symbol_table);
326331
};
327332

328333
exprt make_function_application(

0 commit comments

Comments
 (0)