Skip to content

Commit ae79ea4

Browse files
Joel Allredromainbrenguier
Joel Allred
authored andcommitted
Overwrite String functions during bytecode conversion
Replace all String functions in the preprocessing, even if they exist already (e.g. if a model exists). This implements issue diffblue/test-gen#360
1 parent 2857ae1 commit ae79ea4

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

src/java_bytecode/java_bytecode_convert_method.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -1530,18 +1530,20 @@ codet java_bytecode_convert_methodt::convert_instructions(
15301530
symbol.type=arg0.type();
15311531
symbol.value.make_nil();
15321532
symbol.mode=ID_java;
1533-
15341533
assign_parameter_names(
15351534
to_code_type(symbol.type),
15361535
symbol.name,
15371536
symbol_table);
15381537

1539-
// The string refinement module may provide a definition for this
1540-
// function.
1538+
symbol_table.add(symbol);
1539+
}
1540+
1541+
// Create code for functions on Strings and replace the body.
1542+
if(string_preprocess.shall_function_be_overriden(id))
1543+
{
1544+
symbolt &symbol=(*symbol_table.symbols.find(id)).second;
15411545
symbol.value=string_preprocess.code_for_function(
15421546
id, to_code_type(symbol.type), loc, symbol_table);
1543-
1544-
symbol_table.add(symbol);
15451547
}
15461548

15471549
if(is_virtual)

src/java_bytecode/java_string_library_preprocess.cpp

+67
Original file line numberDiff line numberDiff line change
@@ -1793,37 +1793,104 @@ exprt java_string_library_preprocesst::code_for_function(
17931793
{
17941794
auto it_id=cprover_equivalent_to_java_function.find(function_id);
17951795
if(it_id!=cprover_equivalent_to_java_function.end())
1796+
{
1797+
overriden.insert(function_id);
17961798
return make_function_from_call(it_id->second, type, loc, symbol_table);
1799+
}
17971800

17981801
it_id=cprover_equivalent_to_java_string_returning_function.find(function_id);
17991802
if(it_id!=cprover_equivalent_to_java_string_returning_function.end())
1803+
{
1804+
overriden.insert(function_id);
18001805
return make_string_returning_function_from_call(
18011806
it_id->second, type, loc, symbol_table);
1807+
}
18021808

18031809
it_id=cprover_equivalent_to_java_constructor.find(function_id);
18041810
if(it_id!=cprover_equivalent_to_java_constructor.end())
1811+
{
1812+
overriden.insert(function_id);
18051813
return make_init_function_from_call(
18061814
it_id->second, type, loc, symbol_table);
1815+
}
18071816

18081817
it_id=cprover_equivalent_to_java_assign_and_return_function.find(function_id);
18091818
if(it_id!=cprover_equivalent_to_java_assign_and_return_function.end())
1819+
{
1820+
overriden.insert(function_id);
18101821
return make_assign_and_return_function_from_call(
18111822
it_id->second, type, loc, symbol_table);
1823+
}
18121824

18131825
it_id=cprover_equivalent_to_java_assign_function.find(function_id);
18141826
if(it_id!=cprover_equivalent_to_java_assign_function.end())
1827+
{
1828+
overriden.insert(function_id);
18151829
return make_assign_function_from_call(
18161830
it_id->second, type, loc, symbol_table);
1831+
}
18171832

18181833
auto it=conversion_table.find(function_id);
18191834
if(it!=conversion_table.end())
1835+
{
1836+
overriden.insert(function_id);
18201837
return it->second(type, loc, symbol_table);
1838+
}
18211839

18221840
return nil_exprt();
18231841
}
18241842

18251843
/*******************************************************************\
18261844
1845+
Function: java_string_library_preprocesst::shall_function_be_overriden
1846+
1847+
Inputs:
1848+
function_id - name of the function
1849+
1850+
Outputs: True if the function has a known replacement for its body
1851+
or if it has already been replaced (by a call to
1852+
code_for_function), false otherwise.
1853+
1854+
Purpose: Check whether the current function's body is to be replaced
1855+
with the code created in the preprocessing.
1856+
1857+
\*******************************************************************/
1858+
1859+
bool java_string_library_preprocesst::shall_function_be_overriden(
1860+
const irep_idt &function_id)
1861+
{
1862+
if(overriden.find(function_id)!=overriden.end())
1863+
return false;
1864+
1865+
auto it_id=cprover_equivalent_to_java_function.find(function_id);
1866+
if(it_id!=cprover_equivalent_to_java_function.end())
1867+
return true;
1868+
1869+
it_id=cprover_equivalent_to_java_string_returning_function.find(function_id);
1870+
if(it_id!=cprover_equivalent_to_java_string_returning_function.end())
1871+
return true;
1872+
1873+
it_id=cprover_equivalent_to_java_constructor.find(function_id);
1874+
if(it_id!=cprover_equivalent_to_java_constructor.end())
1875+
return true;
1876+
1877+
it_id=cprover_equivalent_to_java_assign_and_return_function.find(function_id);
1878+
if(it_id!=cprover_equivalent_to_java_assign_and_return_function.end())
1879+
return true;
1880+
1881+
it_id=cprover_equivalent_to_java_assign_function.find(function_id);
1882+
if(it_id!=cprover_equivalent_to_java_assign_function.end())
1883+
return true;
1884+
1885+
auto it=conversion_table.find(function_id);
1886+
if(it!=conversion_table.end())
1887+
return true;
1888+
1889+
return false;
1890+
}
1891+
1892+
/*******************************************************************\
1893+
18271894
Function: java_string_library_preprocesst::is_known_string_type
18281895
18291896
Inputs:

src/java_bytecode/java_string_library_preprocess.h

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class java_string_library_preprocesst:public messaget
3939
const source_locationt &loc,
4040
symbol_tablet &symbol_table);
4141

42+
bool shall_function_be_overriden(const irep_idt &function_id);
43+
4244
codet replace_character_call(code_function_callt call)
4345
{
4446
return character_preprocess.replace_character_call(call);
@@ -78,6 +80,10 @@ class java_string_library_preprocesst:public messaget
7880

7981
typedef std::unordered_map<irep_idt, irep_idt, irep_id_hash> id_mapt;
8082

83+
// Keeps functions on strings of which we have replaced the body, so that we
84+
// only do it once.
85+
std::unordered_set<irep_idt, irep_id_hash> overriden;
86+
8187
// Table mapping each java method signature to the code generating function
8288
std::unordered_map<irep_idt, conversion_functiont, irep_id_hash>
8389
conversion_table;

0 commit comments

Comments
 (0)