Skip to content

Commit 1d9584e

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 e8c0f2b commit 1d9584e

File tree

6 files changed

+40
-70
lines changed

6 files changed

+40
-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
@@ -123,9 +123,7 @@ void lazy_goto_modelt::initialize(
123123
"language");
124124
}
125125

126-
std::vector<std::string> binaries, sources;
127-
binaries.reserve(files.size());
128-
sources.reserve(files.size());
126+
std::list<std::string> binaries, sources;
129127

130128
for(const auto &file : files)
131129
{
@@ -208,18 +206,8 @@ void lazy_goto_modelt::initialize(
208206
}
209207
}
210208

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-
}
222-
}
209+
if(read_objects_and_link(binaries, *goto_model, message_handler))
210+
throw incorrect_goto_program_exceptiont{"failed to read/link goto model"};
223211

224212
bool binaries_provided_start =
225213
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: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ bool compilet::link(optionalt<symbol_tablet> &&symbol_table)
320320
convert_symbols(goto_model);
321321

322322
// parse object files
323-
for(const auto &file_name : object_files)
324-
{
325-
if(read_object_and_link(file_name, goto_model, log.get_message_handler()))
326-
return true;
327-
}
323+
const bool read_link_failed =
324+
read_objects_and_link(object_files, goto_model, log.get_message_handler());
325+
326+
if(read_link_failed)
327+
return true;
328328

329329
// produce entry point?
330330

src/goto-programs/initialize_goto_model.cpp

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

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

7775
for(const auto &file : files)
7876
{
@@ -134,15 +132,10 @@ goto_modelt initialize_goto_model(
134132
}
135133
}
136134

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

148141
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
@@ -270,13 +270,13 @@ bool is_goto_binary(
270270
/// \param dest: the goto model returned
271271
/// \param message_handler: for diagnostics
272272
/// \return true on error, false otherwise
273-
bool read_object_and_link(
273+
static bool read_object_and_link(
274274
const std::string &file_name,
275275
goto_modelt &dest,
276276
message_handlert &message_handler)
277277
{
278-
messaget(message_handler).statistics() << "Reading: "
279-
<< file_name << messaget::eom;
278+
messaget(message_handler).status()
279+
<< "Reading GOTO program from file " << file_name << messaget::eom;
280280

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

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

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

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

318-
bool result=read_object_and_link(
319-
file_name,
320-
goto_model,
321-
message_handler);
310+
if(failed)
311+
return true;
312+
}
322313

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

326-
return result;
317+
return false;
327318
}

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)