Skip to content

Commit 895a674

Browse files
committed
Encapsulate reading and linking multiple files
We do the same work in multiple places, and can optimize the linking process when knowing about all files to be linked in one step.
1 parent f2b0b51 commit 895a674

File tree

6 files changed

+37
-70
lines changed

6 files changed

+37
-70
lines changed

jbmc/src/java_bytecode/lazy_goto_model.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,7 @@ void lazy_goto_modelt::initialize(
126126
"language");
127127
}
128128

129-
std::vector<std::string> binaries, sources;
130-
binaries.reserve(files.size());
131-
sources.reserve(files.size());
129+
std::list<std::string> binaries, sources;
132130

133131
for(const auto &file : files)
134132
{
@@ -211,18 +209,8 @@ void lazy_goto_modelt::initialize(
211209
}
212210
}
213211

214-
for(const std::string &file : binaries)
215-
{
216-
msg.status() << "Reading GOTO program from file" << messaget::eom;
217-
218-
if(read_object_and_link(file, *goto_model, message_handler))
219-
{
220-
source_locationt source_location;
221-
source_location.set_file(file);
222-
throw incorrect_goto_program_exceptiont(
223-
"failed to read/link goto model", source_location);
224-
}
225-
}
212+
if(read_objects_and_link(binaries, *goto_model, message_handler))
213+
throw incorrect_goto_program_exceptiont{"failed to read/link goto model"};
226214

227215
bool binaries_provided_start =
228216
symbol_table.has_symbol(goto_functionst::entry_point());

regression/goto-gcc/archives/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CORE
33
main.c -L. -lour_archive --verbosity
44
^EXIT=0$
55
^SIGNAL=0$
6-
^Reading: .*/foo.o$
6+
^Reading GOTO program from file .*/foo.o$
77
--
88
^warning: ignoring
99
^CONVERSION ERROR$

src/goto-cc/compile.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,11 +322,8 @@ bool compilet::link(optionalt<symbol_tablet> &&symbol_table)
322322
convert_symbols(goto_model);
323323

324324
// parse object files
325-
for(const auto &file_name : object_files)
326-
{
327-
if(read_object_and_link(file_name, goto_model, log.get_message_handler()))
328-
return true;
329-
}
325+
if(read_objects_and_link(object_files, goto_model, log.get_message_handler()))
326+
return true;
330327

331328
// produce entry point?
332329

src/goto-programs/initialize_goto_model.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,7 @@ goto_modelt initialize_goto_model(
7171
"one or more paths to program files");
7272
}
7373

74-
std::vector<std::string> binaries, sources;
75-
binaries.reserve(files.size());
76-
sources.reserve(files.size());
74+
std::list<std::string> binaries, sources;
7775

7876
for(const auto &file : files)
7977
{
@@ -135,15 +133,10 @@ goto_modelt initialize_goto_model(
135133
}
136134
}
137135

138-
for(const auto &file : binaries)
136+
if(read_objects_and_link(binaries, goto_model, message_handler))
139137
{
140-
msg.status() << "Reading GOTO program from file" << messaget::eom;
141-
142-
if(read_object_and_link(file, goto_model, message_handler))
143-
{
144-
throw invalid_source_file_exceptiont(
145-
"failed to read object or link in file '" + file + '\'');
146-
}
138+
throw invalid_source_file_exceptiont{
139+
"failed to read object or link in files"};
147140
}
148141

149142
bool binaries_provided_start=

src/goto-programs/read_goto_binary.cpp

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -271,13 +271,13 @@ bool is_goto_binary(
271271
/// \param dest: the goto model returned
272272
/// \param message_handler: for diagnostics
273273
/// \return true on error, false otherwise
274-
bool read_object_and_link(
274+
static bool read_object_and_link(
275275
const std::string &file_name,
276276
goto_modelt &dest,
277277
message_handlert &message_handler)
278278
{
279-
messaget(message_handler).statistics() << "Reading: "
280-
<< file_name << messaget::eom;
279+
messaget(message_handler).status()
280+
<< "Reading GOTO program from file " << file_name << messaget::eom;
281281

282282
// we read into a temporary model
283283
auto temp_model = read_goto_binary(file_name, message_handler);
@@ -293,36 +293,27 @@ bool read_object_and_link(
293293
return true;
294294
}
295295

296-
// reading successful, let's update config
297-
config.set_from_symbol_table(dest.symbol_table);
298-
299296
return false;
300297
}
301298

302-
/// \brief reads an object file, and also updates the config
303-
/// \param file_name: file name of the goto binary
304-
/// \param dest_symbol_table: symbol table to update
305-
/// \param dest_functions: collection of goto functions to update
306-
/// \param message_handler: for diagnostics
307-
/// \return true on error, false otherwise
308-
bool read_object_and_link(
309-
const std::string &file_name,
310-
symbol_tablet &dest_symbol_table,
311-
goto_functionst &dest_functions,
299+
bool read_objects_and_link(
300+
const std::list<std::string> &file_names,
301+
goto_modelt &dest,
312302
message_handlert &message_handler)
313303
{
314-
goto_modelt goto_model;
304+
if(file_names.empty())
305+
return false;
315306

316-
goto_model.symbol_table.swap(dest_symbol_table);
317-
goto_model.goto_functions.swap(dest_functions);
307+
for(const auto &file_name : file_names)
308+
{
309+
const bool failed = read_object_and_link(file_name, dest, message_handler);
318310

319-
bool result=read_object_and_link(
320-
file_name,
321-
goto_model,
322-
message_handler);
311+
if(failed)
312+
return true;
313+
}
323314

324-
goto_model.symbol_table.swap(dest_symbol_table);
325-
goto_model.goto_functions.swap(dest_functions);
315+
// reading successful, let's update config
316+
config.set_from_symbol_table(dest.symbol_table);
326317

327-
return result;
318+
return false;
328319
}

src/goto-programs/read_goto_binary.h

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,27 @@ Author: Daniel Kroening, [email protected]
1212
#ifndef CPROVER_GOTO_PROGRAMS_READ_GOTO_BINARY_H
1313
#define CPROVER_GOTO_PROGRAMS_READ_GOTO_BINARY_H
1414

15+
#include <list>
1516
#include <string>
1617

1718
#include <util/optional.h>
1819

19-
class goto_functionst;
2020
class goto_modelt;
2121
class message_handlert;
22-
class symbol_tablet;
2322

2423
optionalt<goto_modelt>
2524
read_goto_binary(const std::string &filename, message_handlert &);
2625

2726
bool is_goto_binary(const std::string &filename, message_handlert &);
2827

29-
bool read_object_and_link(
30-
const std::string &file_name,
31-
symbol_tablet &,
32-
goto_functionst &,
33-
message_handlert &);
34-
35-
bool read_object_and_link(
36-
const std::string &file_name,
37-
goto_modelt &,
38-
message_handlert &);
28+
/// Reads object files and updates the config if any files were read.
29+
/// \param file_names: file names of goto binaries; if empty, just returns false
30+
/// \param dest: goto model to update
31+
/// \param message_handler: for diagnostics
32+
/// \return True on error, false otherwise
33+
bool read_objects_and_link(
34+
const std::list<std::string> &file_names,
35+
goto_modelt &dest,
36+
message_handlert &message_handler);
3937

4038
#endif // CPROVER_GOTO_PROGRAMS_READ_GOTO_BINARY_H

0 commit comments

Comments
 (0)