Skip to content

Commit 8941567

Browse files
authored
Merge pull request diffblue#2124 from smowton/smowton/feature/fallback-function-provider
Lazy methods: allow driver program to provide stubs
2 parents da61186 + cd04e70 commit 8941567

9 files changed

+472
-31
lines changed

src/goto-programs/lazy_goto_functions_map.h

+45-10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ class lazy_goto_functions_mapt final
5454
goto_functionst::goto_functiont &function,
5555
journalling_symbol_tablet &function_symbols)>
5656
post_process_functiont;
57+
typedef std::function<bool(const irep_idt &name)>
58+
can_generate_function_bodyt;
59+
typedef std::function<
60+
bool(
61+
const irep_idt &function_name,
62+
symbol_table_baset &symbol_table,
63+
goto_functiont &function,
64+
bool body_available)>
65+
generate_function_bodyt;
5766

5867
private:
5968
typedef std::map<key_type, goto_functiont> underlying_mapt;
@@ -66,6 +75,8 @@ class lazy_goto_functions_mapt final
6675
language_filest &language_files;
6776
symbol_tablet &symbol_table;
6877
const post_process_functiont post_process_function;
78+
const can_generate_function_bodyt driver_program_can_generate_function_body;
79+
const generate_function_bodyt driver_program_generate_function_body;
6980
message_handlert &message_handler;
7081

7182
public:
@@ -75,11 +86,17 @@ class lazy_goto_functions_mapt final
7586
language_filest &language_files,
7687
symbol_tablet &symbol_table,
7788
post_process_functiont post_process_function,
89+
can_generate_function_bodyt driver_program_can_generate_function_body,
90+
generate_function_bodyt driver_program_generate_function_body,
7891
message_handlert &message_handler)
7992
: goto_functions(goto_functions),
8093
language_files(language_files),
8194
symbol_table(symbol_table),
82-
post_process_function(std::move(post_process_function)),
95+
post_process_function(post_process_function),
96+
driver_program_can_generate_function_body(
97+
driver_program_can_generate_function_body),
98+
driver_program_generate_function_body(
99+
driver_program_generate_function_body),
83100
message_handler(message_handler)
84101
{
85102
}
@@ -107,7 +124,9 @@ class lazy_goto_functions_mapt final
107124
/// it a bodyless stub.
108125
bool can_produce_function(const key_type &name) const
109126
{
110-
return language_files.can_convert_lazy_method(name);
127+
return
128+
language_files.can_convert_lazy_method(name) ||
129+
driver_program_can_generate_function_body(name);
111130
}
112131

113132
void unload(const key_type &name) const { goto_functions.erase(name); }
@@ -153,14 +172,30 @@ class lazy_goto_functions_mapt final
153172
underlying_mapt::iterator it=goto_functions.find(name);
154173
if(it!=goto_functions.end())
155174
return *it;
156-
// Fill in symbol table entry body if not already done
157-
// If this returns false then it's a stub
158-
language_files.convert_lazy_method(name, function_symbol_table);
159-
// Create goto_functiont
160-
goto_functionst::goto_functiont function;
161-
goto_convert_functionst convert_functions(
162-
function_symbol_table, message_handler);
163-
convert_functions.convert_function(name, function);
175+
176+
goto_functiont function;
177+
178+
// First chance: see if the driver program wants to provide a replacement:
179+
bool body_provided =
180+
driver_program_generate_function_body(
181+
name,
182+
function_symbol_table,
183+
function,
184+
language_files.can_convert_lazy_method(name));
185+
186+
// Second chance: see if language_filest can provide a body:
187+
if(!body_provided)
188+
{
189+
// Fill in symbol table entry body if not already done
190+
language_files.convert_lazy_method(name, function_symbol_table);
191+
body_provided = function_symbol_table.lookup_ref(name).value.is_not_nil();
192+
193+
// Create goto_functiont
194+
goto_convert_functionst convert_functions(
195+
function_symbol_table, message_handler);
196+
convert_functions.convert_function(name, function);
197+
}
198+
164199
// Add to map
165200
return *goto_functions.emplace(name, std::move(function)).first;
166201
}

src/goto-programs/lazy_goto_model.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
lazy_goto_modelt::lazy_goto_modelt(
2323
post_process_functiont post_process_function,
2424
post_process_functionst post_process_functions,
25+
can_generate_function_bodyt driver_program_can_generate_function_body,
26+
generate_function_bodyt driver_program_generate_function_body,
2527
message_handlert &message_handler)
2628
: goto_model(new goto_modelt()),
2729
symbol_table(goto_model->symbol_table),
@@ -41,9 +43,15 @@ lazy_goto_modelt::lazy_goto_modelt(
4143
function);
4244
this->post_process_function(model_function, *this);
4345
},
46+
driver_program_can_generate_function_body,
47+
driver_program_generate_function_body,
4448
message_handler),
45-
post_process_function(std::move(post_process_function)),
46-
post_process_functions(std::move(post_process_functions)),
49+
post_process_function(post_process_function),
50+
post_process_functions(post_process_functions),
51+
driver_program_can_generate_function_body(
52+
driver_program_can_generate_function_body),
53+
driver_program_generate_function_body(
54+
driver_program_generate_function_body),
4755
message_handler(message_handler)
4856
{
4957
language_files.set_message_handler(message_handler);
@@ -68,10 +76,12 @@ lazy_goto_modelt::lazy_goto_modelt(lazy_goto_modelt &&other)
6876
function);
6977
this->post_process_function(model_function, *this);
7078
},
79+
other.driver_program_can_generate_function_body,
80+
other.driver_program_generate_function_body,
7181
other.message_handler),
7282
language_files(std::move(other.language_files)),
73-
post_process_function(std::move(other.post_process_function)),
74-
post_process_functions(std::move(other.post_process_functions)),
83+
post_process_function(other.post_process_function),
84+
post_process_functions(other.post_process_functions),
7585
message_handler(other.message_handler)
7686
{
7787
}

src/goto-programs/lazy_goto_model.h

+21
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,16 @@ class lazy_goto_modelt : public abstract_goto_modelt
2424
void(goto_model_functiont &function, const abstract_goto_modelt &)>
2525
post_process_functiont;
2626
typedef std::function<bool(goto_modelt &goto_model)> post_process_functionst;
27+
typedef lazy_goto_functions_mapt::can_generate_function_bodyt
28+
can_generate_function_bodyt;
29+
typedef lazy_goto_functions_mapt::generate_function_bodyt
30+
generate_function_bodyt;
2731

2832
explicit lazy_goto_modelt(
2933
post_process_functiont post_process_function,
3034
post_process_functionst post_process_functions,
35+
can_generate_function_bodyt driver_program_can_generate_function_body,
36+
generate_function_bodyt driver_program_generate_function_body,
3137
message_handlert &message_handler);
3238

3339
lazy_goto_modelt(lazy_goto_modelt &&other);
@@ -60,6 +66,19 @@ class lazy_goto_modelt : public abstract_goto_modelt
6066
[&handler, &options](goto_modelt &goto_model) -> bool {
6167
return handler.process_goto_functions(goto_model, options);
6268
},
69+
[&handler](const irep_idt &name) -> bool {
70+
return handler.can_generate_function_body(name);
71+
},
72+
[&handler]
73+
(const irep_idt &function_name,
74+
symbol_table_baset &symbol_table,
75+
goto_functiont &function,
76+
bool is_first_chance)
77+
{
78+
return
79+
handler.generate_function_body(
80+
function_name, symbol_table, function, is_first_chance);
81+
},
6382
message_handler);
6483
}
6584

@@ -127,6 +146,8 @@ class lazy_goto_modelt : public abstract_goto_modelt
127146
// Function/module processing functions
128147
const post_process_functiont post_process_function;
129148
const post_process_functionst post_process_functions;
149+
const can_generate_function_bodyt driver_program_can_generate_function_body;
150+
const generate_function_bodyt driver_program_generate_function_body;
130151

131152
/// Logging helper field
132153
message_handlert &message_handler;

src/java_bytecode/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ SRC = bytecode_info.cpp \
3737
remove_java_new.cpp \
3838
replace_java_nondet.cpp \
3939
select_pointer_type.cpp \
40+
simple_method_stubbing.cpp \
4041
# Empty last line
4142

4243
INCLUDES= -I ..

0 commit comments

Comments
 (0)