Skip to content

Commit ffb9095

Browse files
committed
De-duplicate initialize_goto_model's code
lazy_goto_modelt used almost the same code. Refactor initialize_goto_model's implementation to enable re-use from lazy_goto_modelt.
1 parent 0a2766b commit ffb9095

File tree

5 files changed

+170
-167
lines changed

5 files changed

+170
-167
lines changed

jbmc/src/java_bytecode/lazy_goto_model.cpp

Lines changed: 11 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
#include "java_bytecode_language.h"
99

10+
#include <goto-programs/initialize_goto_model.h>
1011
#include <goto-programs/read_goto_binary.h>
11-
#include <goto-programs/rebuild_goto_start_function.h>
1212

1313
#include <langapi/mode.h>
1414

@@ -163,103 +163,19 @@ void lazy_goto_modelt::initialize(
163163
}
164164
else
165165
{
166-
for(const auto &filename : sources)
167-
{
168-
#ifdef _MSC_VER
169-
std::ifstream infile(widen(filename));
170-
#else
171-
std::ifstream infile(filename);
172-
#endif
173-
174-
if(!infile)
175-
{
176-
throw system_exceptiont(
177-
"failed to open input file '" + filename + '\'');
178-
}
179-
180-
language_filet &lf = add_language_file(filename);
181-
lf.language = get_language_from_filename(filename);
182-
183-
if(lf.language == nullptr)
184-
{
185-
throw invalid_source_file_exceptiont(
186-
"failed to figure out type of file '" + filename + '\'');
187-
}
188-
189-
languaget &language = *lf.language;
190-
language.set_message_handler(message_handler);
191-
language.set_language_options(options);
192-
193-
msg.status() << "Parsing " << filename << messaget::eom;
194-
195-
if(language.parse(infile, filename))
196-
{
197-
throw invalid_source_file_exceptiont("PARSING ERROR");
198-
}
199-
200-
lf.get_modules();
201-
}
202-
203-
msg.status() << "Converting" << messaget::eom;
204-
205-
if(language_files.typecheck(symbol_table))
206-
{
207-
throw invalid_source_file_exceptiont("CONVERSION ERROR");
208-
}
209-
}
210-
211-
for(const std::string &file : binaries)
212-
{
213-
msg.status() << "Reading GOTO program from file" << messaget::eom;
214-
215-
if(read_object_and_link(file, *goto_model, message_handler))
216-
{
217-
source_locationt source_location;
218-
source_location.set_file(file);
219-
throw incorrect_goto_program_exceptiont(
220-
"failed to read/link goto model", source_location);
221-
}
166+
initialize_from_source_files(
167+
sources, options, language_files, symbol_table, message_handler);
222168
}
223169

224-
bool binaries_provided_start =
225-
symbol_table.has_symbol(goto_functionst::entry_point());
226-
227-
bool entry_point_generation_failed = false;
228-
229-
if(binaries_provided_start && options.is_set("function"))
230-
{
231-
// The goto binaries provided already contain a __CPROVER_start
232-
// function that may be tied to a different entry point `function`.
233-
// Hence, we will rebuild the __CPROVER_start function.
234-
235-
// Get the language annotation of the existing __CPROVER_start function.
236-
std::unique_ptr<languaget> language =
237-
get_entry_point_language(symbol_table, options, message_handler);
238-
239-
// To create a new entry point we must first remove the old one
240-
remove_existing_entry_point(symbol_table);
241-
242-
// Create the new entry-point
243-
entry_point_generation_failed =
244-
language->generate_support_functions(symbol_table);
170+
read_goto_binaries_and_link(binaries, *goto_model, message_handler);
245171

246-
// Remove the function from the goto functions so it is copied back in
247-
// from the symbol table during goto_convert
248-
if(!entry_point_generation_failed)
249-
unload(goto_functionst::entry_point());
250-
}
251-
else if(!binaries_provided_start)
252-
{
253-
// Allow all language front-ends to try to provide the user-specified
254-
// (--function) entry-point, or some language-specific default:
255-
entry_point_generation_failed =
256-
language_files.generate_support_functions(symbol_table);
257-
}
258-
259-
if(entry_point_generation_failed)
260-
{
261-
throw invalid_source_file_exceptiont("SUPPORT FUNCTION GENERATION ERROR");
262-
}
172+
set_up_custom_entry_point(
173+
language_files,
174+
symbol_table,
175+
[this](const irep_idt &id) { goto_functions.unload(id); },
176+
options,
177+
false,
178+
message_handler);
263179

264180
// stupid hack
265181
config.set_object_bits_from_symbol_table(symbol_table);

jbmc/src/java_bytecode/lazy_goto_model.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,6 @@ class lazy_goto_modelt : public abstract_goto_modelt
183183
/// Eagerly loads all functions from the symbol table.
184184
void load_all_functions() const;
185185

186-
void unload(const irep_idt &name) const
187-
{
188-
goto_functions.unload(name);
189-
}
190-
191186
language_filet &add_language_file(const std::string &filename)
192187
{
193188
return language_files.add_file(filename);

src/ansi-c/ansi_c_entry_point.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,16 +480,16 @@ bool generate_ansi_c_start_function(
480480
const std::string main_signature = type2c(symbol.type, ns);
481481
messaget message(message_handler);
482482
message.error().source_location = symbol.location;
483-
message.error()
484-
<< "'main' with signature '" << main_signature
485-
<< "' found,"
486-
<< " but expecting one of:\n"
487-
<< " int main(void)\n"
488-
<< " int main(int argc, char *argv[])\n"
489-
<< " int main(int argc, char *argv[], char *envp[])\n"
490-
<< "If this is a non-standard main entry point please provide a custom\n"
491-
<< "entry function and use --function instead"
492-
<< messaget::eom;
483+
message.error() << "'main' with signature '" << main_signature
484+
<< "' found,"
485+
<< " but expecting one of:\n"
486+
<< " int main(void)\n"
487+
<< " int main(int argc, char *argv[])\n"
488+
<< " int main(int argc, char *argv[], char *envp[])\n"
489+
<< "If this is a non-standard main entry point please "
490+
"provide a custom\n"
491+
<< "entry function and use --function instead"
492+
<< messaget::eom;
493493
return true;
494494
}
495495
}

0 commit comments

Comments
 (0)