Skip to content

Commit 8996b67

Browse files
committed
Reading/writing goto binaries: consistently use return values
Always use the return value to communicate errors, not a mix of return values and exceptions.
1 parent f8380e5 commit 8996b67

File tree

8 files changed

+40
-18
lines changed

8 files changed

+40
-18
lines changed

src/goto-analyzer/static_simplifier.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ bool static_simplifier(
190190
goto_model.goto_functions.update();
191191

192192
m.status() << "Writing goto binary" << messaget::eom;
193-
return write_goto_binary(out,
194-
ns.get_symbol_table(),
195-
goto_model.goto_functions);
193+
return write_goto_binary(
194+
out, ns.get_symbol_table(), goto_model.goto_functions, message_handler);
196195
}

src/goto-cc/compile.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ bool compilet::write_bin_object_file(
601601
return true;
602602
}
603603

604-
if(write_goto_binary(outfile, src_goto_model))
604+
if(write_goto_binary(outfile, src_goto_model, message_handler))
605605
return true;
606606

607607
const auto cnt = function_body_count(src_goto_model.goto_functions);

src/goto-harness/goto_harness_parse_options.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,13 @@ int goto_harness_parse_optionst::doit()
163163
}
164164
else
165165
{
166-
write_goto_binary(
167-
got_harness_config.out_file, goto_model, log.get_message_handler());
166+
if(write_goto_binary(
167+
got_harness_config.out_file, goto_model, log.get_message_handler()))
168+
{
169+
throw deserialization_exceptiont{
170+
"failed to write goto program to file '" + got_harness_config.out_file +
171+
"'"};
172+
}
168173
}
169174

170175
return CPROVER_EXIT_SUCCESS;

src/goto-programs/read_bin_goto_object.h

+3
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ Date: May 2007
1414
#ifndef CPROVER_GOTO_PROGRAMS_READ_BIN_GOTO_OBJECT_H
1515
#define CPROVER_GOTO_PROGRAMS_READ_BIN_GOTO_OBJECT_H
1616

17+
#include <util/nodiscard.h>
18+
1719
#include <iosfwd>
1820
#include <string>
1921

2022
class symbol_table_baset;
2123
class goto_functionst;
2224
class message_handlert;
2325

26+
NODISCARD
2427
bool read_bin_goto_object(
2528
std::istream &in,
2629
const std::string &filename,

src/goto-programs/read_goto_binary.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ 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 <util/nodiscard.h>
16+
#include <util/optional.h>
17+
1518
#include <list>
1619
#include <string>
1720

18-
#include <util/optional.h>
19-
2021
class goto_modelt;
2122
class message_handlert;
2223

@@ -30,6 +31,7 @@ bool is_goto_binary(const std::string &filename, message_handlert &);
3031
/// \param [out] dest: GOTO model to update.
3132
/// \param message_handler: for diagnostics
3233
/// \return True on error, false otherwise
34+
NODISCARD
3335
bool read_objects_and_link(
3436
const std::list<std::string> &file_names,
3537
goto_modelt &dest,

src/goto-programs/write_goto_binary.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Author: CM Wintersteiger
1313

1414
#include <fstream>
1515

16-
#include <util/exception_utils.h>
1716
#include <util/irep_serialization.h>
1817
#include <util/message.h>
1918

@@ -144,12 +143,14 @@ static void write_goto_binary(
144143
bool write_goto_binary(
145144
std::ostream &out,
146145
const goto_modelt &goto_model,
146+
message_handlert &message_handler,
147147
int version)
148148
{
149149
return write_goto_binary(
150150
out,
151151
goto_model.symbol_table,
152152
goto_model.goto_functions,
153+
message_handler,
153154
version);
154155
}
155156

@@ -158,6 +159,7 @@ bool write_goto_binary(
158159
std::ostream &out,
159160
const symbol_table_baset &symbol_table,
160161
const goto_functionst &goto_functions,
162+
message_handlert &message_handler,
161163
int version)
162164
{
163165
// header
@@ -169,15 +171,19 @@ bool write_goto_binary(
169171

170172
if(version < GOTO_BINARY_VERSION)
171173
{
172-
throw invalid_command_line_argument_exceptiont(
173-
"version " + std::to_string(version) + " no longer supported",
174-
"supported version = " + std::to_string(GOTO_BINARY_VERSION));
174+
messaget message{message_handler};
175+
message.error() << "version " << version << " no longer supported; "
176+
<< "supported version = " << GOTO_BINARY_VERSION
177+
<< messaget::eom;
178+
return true;
175179
}
176180
if(version > GOTO_BINARY_VERSION)
177181
{
178-
throw invalid_command_line_argument_exceptiont(
179-
"unknown goto binary version " + std::to_string(version),
180-
"supported version = " + std::to_string(GOTO_BINARY_VERSION));
182+
messaget message{message_handler};
183+
message.error() << "unknown goto binary version " << version << "; "
184+
<< "supported version = " << GOTO_BINARY_VERSION
185+
<< messaget::eom;
186+
return true;
181187
}
182188
write_goto_binary(out, symbol_table, goto_functions, irepconverter);
183189
return false;
@@ -198,5 +204,5 @@ bool write_goto_binary(
198204
return true;
199205
}
200206

201-
return write_goto_binary(out, goto_model);
207+
return write_goto_binary(out, goto_model, message_handler);
202208
}

src/goto-programs/write_goto_binary.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Author: CM Wintersteiger
1414

1515
#define GOTO_BINARY_VERSION 5
1616

17+
#include <util/nodiscard.h>
18+
1719
#include <iosfwd>
1820
#include <string>
1921

@@ -22,17 +24,22 @@ class goto_modelt;
2224
class message_handlert;
2325
class symbol_table_baset;
2426

27+
NODISCARD
2528
bool write_goto_binary(
2629
std::ostream &out,
2730
const goto_modelt &,
28-
int version=GOTO_BINARY_VERSION);
31+
message_handlert &,
32+
int version = GOTO_BINARY_VERSION);
2933

34+
NODISCARD
3035
bool write_goto_binary(
3136
std::ostream &out,
3237
const symbol_table_baset &,
3338
const goto_functionst &,
39+
message_handlert &,
3440
int version = GOTO_BINARY_VERSION);
3541

42+
NODISCARD
3643
bool write_goto_binary(
3744
const std::string &filename,
3845
const goto_modelt &,

src/symtab2gb/symtab2gb_parse_options.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static void run_symtab2gb(
106106
linked_goto_model.symbol_table.swap(linked_symbol_table);
107107
goto_convert(linked_goto_model, message_handler);
108108

109-
if(failed(write_goto_binary(out_file, linked_goto_model)))
109+
if(failed(write_goto_binary(out_file, linked_goto_model, message_handler)))
110110
{
111111
throw system_exceptiont{"failed to write goto binary to " + gb_filename};
112112
}

0 commit comments

Comments
 (0)