Skip to content

Commit 3123dba

Browse files
Merge pull request #2967 from hannes-steffenhagen-diffblue/invariant_cleanup-goto_programs-jr
Invariant cleanup goto programs jr
2 parents 160410b + 365bb72 commit 3123dba

17 files changed

+148
-120
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CORE
22
main.c
33
--replace-calls f:g
4-
Functions f and g are not type-compatible
5-
^EXIT=11$
4+
functions f and g are not type-compatible
5+
^EXIT=1$
66
^SIGNAL=0$
77
--
88
^warning: ignoring
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CORE
22
main.c
33
--replace-calls f:g
4-
Functions f and g are not type-compatible
5-
^EXIT=11$
4+
functions f and g are not type-compatible
5+
^EXIT=1$
66
^SIGNAL=0$
77
--
88
^warning: ignoring
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CORE
22
main.c
33
--replace-calls f:g --replace-calls h:f
4-
Function f cannot both be replaced and be a replacement
5-
^EXIT=11$
4+
function f cannot both be replaced and be a replacement
5+
^EXIT=1$
66
^SIGNAL=0$
77
--
88
^warning: ignoring

src/goto-programs/lazy_goto_model.cpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <util/cmdline.h>
1313
#include <util/config.h>
14+
#include <util/exception_utils.h>
1415
#include <util/journalling_symbol_table.h>
1516
#include <util/unicode.h>
1617

@@ -96,8 +97,11 @@ void lazy_goto_modelt::initialize(
9697
const std::vector<std::string> &files=cmdline.args;
9798
if(files.empty())
9899
{
99-
msg.error() << "Please provide a program" << messaget::eom;
100-
throw 0;
100+
throw invalid_command_line_argument_exceptiont(
101+
"no program provided",
102+
"source file names",
103+
"one or more paths to a goto binary or a source file in a supported "
104+
"language");
101105
}
102106

103107
std::vector<std::string> binaries, sources;
@@ -124,21 +128,17 @@ void lazy_goto_modelt::initialize(
124128

125129
if(!infile)
126130
{
127-
msg.error() << "failed to open input file `" << filename
128-
<< '\'' << messaget::eom;
129-
throw 0;
131+
throw system_exceptiont(
132+
"failed to open input file `" + filename + '\'');
130133
}
131134

132135
language_filet &lf=add_language_file(filename);
133136
lf.language=get_language_from_filename(filename);
134137

135138
if(lf.language==nullptr)
136139
{
137-
source_locationt location;
138-
location.set_file(filename);
139-
msg.error().source_location=location;
140-
msg.error() << "failed to figure out type of file" << messaget::eom;
141-
throw 0;
140+
throw invalid_source_file_exceptiont(
141+
"failed to figure out type of file `" + filename + '\'');
142142
}
143143

144144
languaget &language=*lf.language;
@@ -149,8 +149,7 @@ void lazy_goto_modelt::initialize(
149149

150150
if(language.parse(infile, filename))
151151
{
152-
msg.error() << "PARSING ERROR" << messaget::eom;
153-
throw 0;
152+
throw invalid_source_file_exceptiont("PARSING ERROR");
154153
}
155154

156155
lf.get_modules();
@@ -160,8 +159,7 @@ void lazy_goto_modelt::initialize(
160159

161160
if(language_files.typecheck(symbol_table))
162161
{
163-
msg.error() << "CONVERSION ERROR" << messaget::eom;
164-
throw 0;
162+
throw invalid_source_file_exceptiont("CONVERSION ERROR");
165163
}
166164
}
167165

@@ -170,7 +168,12 @@ void lazy_goto_modelt::initialize(
170168
msg.status() << "Reading GOTO program from file" << messaget::eom;
171169

172170
if(read_object_and_link(file, *goto_model, message_handler))
173-
throw 0;
171+
{
172+
source_locationt source_location;
173+
source_location.set_file(file);
174+
throw incorrect_goto_program_exceptiont(
175+
"failed to read/link goto model", source_location);
176+
}
174177
}
175178

176179
bool binaries_provided_start =
@@ -205,8 +208,7 @@ void lazy_goto_modelt::initialize(
205208

206209
if(entry_point_generation_failed)
207210
{
208-
msg.error() << "SUPPORT FUNCTION GENERATION ERROR" << messaget::eom;
209-
throw 0;
211+
throw invalid_source_file_exceptiont("SUPPORT FUNCTION GENERATION ERROR");
210212
}
211213

212214
// stupid hack

src/goto-programs/link_goto_model.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Michael Tautschnig, Daniel Kroening
1818
#include <util/rename_symbol.h>
1919

2020
#include <linking/linking_class.h>
21+
#include <util/exception_utils.h>
2122

2223
#include "goto_model.h"
2324

@@ -171,15 +172,18 @@ void link_goto_model(
171172
message_handler);
172173

173174
if(linking.typecheck_main())
174-
throw 0;
175-
175+
{
176+
throw invalid_source_file_exceptiont("typechecking main failed");
177+
}
176178
if(link_functions(
177-
dest.symbol_table,
178-
dest.goto_functions,
179-
src.symbol_table,
180-
src.goto_functions,
181-
linking.rename_symbol,
182-
weak_symbols,
183-
linking.object_type_updates))
184-
throw 0;
179+
dest.symbol_table,
180+
dest.goto_functions,
181+
src.symbol_table,
182+
src.goto_functions,
183+
linking.rename_symbol,
184+
weak_symbols,
185+
linking.object_type_updates))
186+
{
187+
throw invalid_source_file_exceptiont("linking failed");
188+
}
185189
}

src/goto-programs/loop_ids.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void show_loop_ids_json(
7979
const goto_programt &goto_program,
8080
json_arrayt &loops)
8181
{
82-
assert(ui==ui_message_handlert::uit::JSON_UI); // use function above
82+
PRECONDITION(ui == ui_message_handlert::uit::JSON_UI); // use function above
8383

8484
forall_goto_program_instructions(it, goto_program)
8585
{

src/goto-programs/osx_fat_reader.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Module: Read Mach-O
1111

1212
#include "osx_fat_reader.h"
1313

14-
#include <cassert>
1514
#include <cstdlib>
15+
#include <util/exception_utils.h>
16+
#include <util/invariant.h>
1617

1718
#ifdef __APPLE__
1819
#include <mach-o/fat.h>
@@ -46,12 +47,13 @@ osx_fat_readert::osx_fat_readert(std::ifstream &in) :
4647
in.read(reinterpret_cast<char*>(&fh), sizeof(struct fat_header));
4748

4849
if(!in)
49-
throw "failed to read OSX fat header";
50+
throw system_exceptiont("failed to read OSX fat header");
5051

5152
if(!is_osx_fat_magic(reinterpret_cast<char*>(&(fh.magic))))
52-
throw "OSX fat header malformed (magic)"; // NOLINT(readability/throw)
53+
throw deserialization_exceptiont("OSX fat header malformed (magic)");
5354

54-
assert(sizeof(fh.nfat_arch)==4);
55+
static_assert(
56+
sizeof(fh.nfat_arch) == 4, "fat_header::nfat_arch is of type uint32_t");
5557
unsigned narch=__builtin_bswap32(fh.nfat_arch);
5658

5759
for(unsigned i=0; !has_gb_arch && i<narch; ++i)
@@ -61,9 +63,10 @@ osx_fat_readert::osx_fat_readert(std::ifstream &in) :
6163
// NOLINTNEXTLINE(readability/identifiers)
6264
in.read(reinterpret_cast<char*>(&fa), sizeof(struct fat_arch));
6365

64-
assert(sizeof(fa.cputype)==4 &&
65-
sizeof(fa.cpusubtype)==4 &&
66-
sizeof(fa.size)==4);
66+
static_assert(
67+
sizeof(fa.cputype) == 4 && sizeof(fa.cpusubtype) == 4 &&
68+
sizeof(fa.size) == 4,
69+
"This requires a specific fat architecture");
6770
int cputype=__builtin_bswap32(fa.cputype);
6871
int cpusubtype=__builtin_bswap32(fa.cpusubtype);
6972
unsigned size=__builtin_bswap32(fa.size);
@@ -79,7 +82,7 @@ bool osx_fat_readert::extract_gb(
7982
const std::string &source,
8083
const std::string &dest) const
8184
{
82-
assert(has_gb_arch);
85+
PRECONDITION(has_gb_arch);
8386

8487
return run("lipo", {"lipo", "-thin", "hppa7100LC", "-output", dest, source});
8588
}

src/goto-programs/parameter_assignments.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void parameter_assignmentst::do_function_calls(
4646

4747
// add x=y for f(y) where x is the parameter
4848

49-
assert(function_call.function().id()==ID_symbol);
49+
PRECONDITION(function_call.function().id() == ID_symbol);
5050

5151
const irep_idt &identifier=
5252
to_symbol_expr(function_call.function()).get_identifier();

src/goto-programs/pointer_arithmetic.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,32 +32,33 @@ void pointer_arithmetict::read(const exprt &src)
3232
}
3333
else if(src.id()==ID_minus)
3434
{
35-
assert(src.operands().size()==2);
36-
read(src.op0());
37-
const unary_minus_exprt o(src.op1(), src.op1().type());
38-
add_to_offset(o);
35+
auto const &minus_src = to_minus_expr(src);
36+
read(minus_src.op0());
37+
const unary_minus_exprt unary_minus(
38+
minus_src.op1(), minus_src.op1().type());
39+
add_to_offset(unary_minus);
3940
}
4041
else if(src.id()==ID_address_of)
4142
{
42-
assert(src.operands().size()==1);
43-
if(src.op0().id()==ID_index)
43+
auto const &address_of_src = to_address_of_expr(src);
44+
if(address_of_src.op().id() == ID_index)
4445
{
45-
const index_exprt &index_expr=
46-
to_index_expr(src.op0());
46+
const index_exprt &index_expr = to_index_expr(address_of_src.op());
4747

4848
if(index_expr.index().is_zero())
49-
make_pointer(src);
49+
make_pointer(address_of_src);
5050
else
5151
{
5252
add_to_offset(index_expr.index());
5353
// produce &x[0] + i instead of &x[i]
54-
exprt new_src=src;
55-
new_src.op0().op1()=from_integer(0, index_expr.index().type());
56-
make_pointer(new_src);
54+
auto new_address_of_src = address_of_src;
55+
new_address_of_src.op().op1() =
56+
from_integer(0, index_expr.index().type());
57+
make_pointer(new_address_of_src);
5758
}
5859
}
5960
else
60-
make_pointer(src);
61+
make_pointer(address_of_src);
6162
}
6263
else
6364
make_pointer(src);

src/goto-programs/printf_formatter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ Author: Daniel Kroening, [email protected]
1111

1212
#include "printf_formatter.h"
1313

14-
#include <cassert>
1514
#include <sstream>
1615

1716
#include <util/c_types.h>

src/goto-programs/read_bin_goto_object.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ static bool read_bin_goto_object_v4(
145145
{
146146
unsigned n=*nit;
147147
rev_target_mapt::const_iterator entry=rev_target_map.find(n);
148-
assert(entry!=rev_target_map.end());
148+
INVARIANT(
149+
entry != rev_target_map.end(),
150+
"something from the target map should also be in the reverse target "
151+
"map");
149152
ins->targets.push_back(entry->second);
150153
}
151154
}

0 commit comments

Comments
 (0)