Skip to content

Commit b9b40ed

Browse files
authored
Merge pull request #2968 from xbauch/cleanup_goto-s-z
Invariant cleanup goto programs sz
2 parents 6acae33 + cb844be commit b9b40ed

10 files changed

+99
-47
lines changed

src/goto-programs/set_properties.cpp

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

1212
#include "set_properties.h"
1313

14+
#include <util/exception_utils.h>
15+
1416
#include <algorithm>
1517
#include <unordered_set>
1618

@@ -111,7 +113,9 @@ void set_properties(
111113
set_properties(it->second.body, property_set);
112114

113115
if(!property_set.empty())
114-
throw "property "+id2string(*property_set.begin())+" not found";
116+
throw invalid_command_line_argument_exceptiont(
117+
"property " + id2string(*property_set.begin()) + " unknown",
118+
"--property id");
115119
}
116120

117121
void label_properties(goto_functionst &goto_functions)

src/goto-programs/slice_global_inits.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Date: December 2016
2121
#include <util/cprover_prefix.h>
2222
#include <util/prefix.h>
2323

24+
#include <util/invariant.h>
25+
2426
#include <goto-programs/goto_functions.h>
2527
#include <goto-programs/remove_skip.h>
2628

@@ -32,8 +34,8 @@ void slice_global_inits(goto_modelt &goto_model)
3234
const irep_idt entry_point=goto_functionst::entry_point();
3335
goto_functionst &goto_functions=goto_model.goto_functions;
3436

35-
if(!goto_functions.function_map.count(entry_point))
36-
throw "entry point not found";
37+
if(goto_functions.function_map.count(entry_point) == 0)
38+
throw user_input_error_exceptiont("entry point not found");
3739

3840
// Get the call graph restricted to functions reachable from
3941
// the entry point:
@@ -73,7 +75,8 @@ void slice_global_inits(goto_modelt &goto_model)
7375

7476
goto_functionst::function_mapt::iterator f_it;
7577
f_it=goto_functions.function_map.find(INITIALIZE_FUNCTION);
76-
assert(f_it!=goto_functions.function_map.end());
78+
if(f_it == goto_functions.function_map.end())
79+
throw incorrect_goto_program_exceptiont("initialize function not found");
7780

7881
goto_programt &goto_program=f_it->second.body;
7982

src/goto-programs/slice_global_inits.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,27 @@ Date: December 2016
1414
#ifndef CPROVER_GOTO_PROGRAMS_SLICE_GLOBAL_INITS_H
1515
#define CPROVER_GOTO_PROGRAMS_SLICE_GLOBAL_INITS_H
1616

17+
#include <util/exception_utils.h>
18+
1719
class goto_modelt;
1820

21+
class user_input_error_exceptiont : public cprover_exception_baset
22+
{
23+
public:
24+
explicit user_input_error_exceptiont(std::string message)
25+
: message(std::move(message))
26+
{
27+
}
28+
29+
std::string what() const override
30+
{
31+
return message;
32+
}
33+
34+
private:
35+
std::string message;
36+
};
37+
1938
void slice_global_inits(goto_modelt &);
2039

2140
#endif

src/goto-programs/string_instrumentation.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ Author: Daniel Kroening, [email protected]
1616
#include <util/arith_tools.h>
1717
#include <util/c_types.h>
1818
#include <util/config.h>
19+
#include <util/invariant.h>
1920
#include <util/message.h>
20-
#include <util/std_expr.h>
2121
#include <util/std_code.h>
22+
#include <util/std_expr.h>
2223
#include <util/symbol_table.h>
2324

2425
#include <goto-programs/format_strings.h>
@@ -279,9 +280,9 @@ void string_instrumentationt::do_sprintf(
279280

280281
if(arguments.size()<2)
281282
{
282-
error().source_location=target->source_location;
283-
error() << "sprintf expected to have two or more arguments" << eom;
284-
throw 0;
283+
throw incorrect_source_program_exceptiont(
284+
"sprintf expected to have two or more arguments",
285+
target->source_location);
285286
}
286287

287288
goto_programt tmp;
@@ -321,10 +322,9 @@ void string_instrumentationt::do_snprintf(
321322

322323
if(arguments.size()<3)
323324
{
324-
error().source_location=target->source_location;
325-
error() << "snprintf expected to have three or more arguments"
326-
<< eom;
327-
throw 0;
325+
throw incorrect_source_program_exceptiont(
326+
"snprintf expected to have three or more arguments",
327+
target->source_location);
328328
}
329329

330330
goto_programt tmp;
@@ -364,9 +364,8 @@ void string_instrumentationt::do_fscanf(
364364

365365
if(arguments.size()<2)
366366
{
367-
error().source_location=target->source_location;
368-
error() << "fscanf expected to have two or more arguments" << eom;
369-
throw 0;
367+
throw incorrect_source_program_exceptiont(
368+
"fscanf expected to have two or more arguments", target->source_location);
370369
}
371370

372371
goto_programt tmp;
@@ -650,9 +649,8 @@ void string_instrumentationt::do_strchr(
650649

651650
if(arguments.size()!=2)
652651
{
653-
error().source_location=target->source_location;
654-
error() << "strchr expected to have two arguments" << eom;
655-
throw 0;
652+
throw incorrect_source_program_exceptiont(
653+
"strchr expected to have two arguments", target->source_location);
656654
}
657655

658656
goto_programt tmp;
@@ -677,9 +675,8 @@ void string_instrumentationt::do_strrchr(
677675

678676
if(arguments.size()!=2)
679677
{
680-
error().source_location=target->source_location;
681-
error() << "strrchr expected to have two arguments" << eom;
682-
throw 0;
678+
throw incorrect_source_program_exceptiont(
679+
"strrchr expected to have two arguments", target->source_location);
683680
}
684681

685682
goto_programt tmp;
@@ -704,9 +701,8 @@ void string_instrumentationt::do_strstr(
704701

705702
if(arguments.size()!=2)
706703
{
707-
error().source_location=target->source_location;
708-
error() << "strstr expected to have two arguments" << eom;
709-
throw 0;
704+
throw incorrect_source_program_exceptiont(
705+
"strstr expected to have two arguments", target->source_location);
710706
}
711707

712708
goto_programt tmp;
@@ -738,9 +734,8 @@ void string_instrumentationt::do_strtok(
738734

739735
if(arguments.size()!=2)
740736
{
741-
error().source_location=target->source_location;
742-
error() << "strtok expected to have two arguments" << eom;
743-
throw 0;
737+
throw incorrect_source_program_exceptiont(
738+
"strtok expected to have two arguments", target->source_location);
744739
}
745740

746741
goto_programt tmp;

src/goto-programs/string_instrumentation.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,30 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include "goto_functions.h"
1616

17+
#include <util/exception_utils.h>
18+
1719
class message_handlert;
1820
class goto_modelt;
1921

22+
class incorrect_source_program_exceptiont : public cprover_exception_baset
23+
{
24+
public:
25+
incorrect_source_program_exceptiont(
26+
std::string message,
27+
source_locationt source_location)
28+
: message(std::move(message)), source_location(std::move(source_location))
29+
{
30+
}
31+
std::string what() const override
32+
{
33+
return message + " (at: " + source_location.as_string() + ")";
34+
}
35+
36+
private:
37+
std::string message;
38+
source_locationt source_location;
39+
};
40+
2041
void string_instrumentation(
2142
symbol_tablet &,
2243
message_handlert &,

src/goto-programs/vcd_goto_trace.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Date: June 2011
1313

1414
#include "vcd_goto_trace.h"
1515

16-
#include <cassert>
1716
#include <ctime>
1817

1918
#include <util/numbering.h>
@@ -55,8 +54,7 @@ std::string as_vcd_binary(
5554
}
5655
else if(expr.id()==ID_union)
5756
{
58-
assert(expr.operands().size()==1);
59-
return as_vcd_binary(expr.op0(), ns);
57+
return as_vcd_binary(to_union_expr(expr).op(), ns);
6058
}
6159

6260
// build "xxx"

src/goto-programs/wp.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Author: Daniel Kroening, [email protected]
1717
#include <util/std_code.h>
1818
#include <util/base_type.h>
1919

20+
#include <util/invariant.h>
21+
2022
bool has_nondet(const exprt &dest)
2123
{
2224
forall_operands(it, dest)
@@ -263,6 +265,6 @@ exprt wp(
263265
return post; // ignored
264266
else if(statement==ID_fence)
265267
return post; // ignored
266-
else
267-
throw "sorry, wp("+id2string(statement)+"...) not implemented";
268+
INVARIANT_WITH_DIAGNOSTICS(
269+
false, "sorry, wp(", id2string(statement), "...) is not implemented");
268270
}

src/goto-programs/write_goto_binary.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ Author: CM Wintersteiger
1313

1414
#include <fstream>
1515

16-
#include <util/message.h>
16+
#include <util/exception_utils.h>
17+
#include <util/invariant.h>
1718
#include <util/irep_serialization.h>
19+
#include <util/message.h>
1820
#include <util/symbol_table.h>
1921

2022
#include <goto-programs/goto_model.h>
@@ -148,22 +150,18 @@ bool write_goto_binary(
148150
irep_serializationt::ireps_containert irepc;
149151
irep_serializationt irepconverter(irepc);
150152

151-
switch(version)
152-
{
153-
case 1:
154-
case 2:
155-
case 3:
156-
throw "version no longer supported";
157-
158-
case 4:
153+
const int current_goto_version = 4;
154+
if(version < current_goto_version)
155+
throw invalid_command_line_argument_exceptiont(
156+
"version " + std::to_string(version) + " no longer supported",
157+
"supported version = " + std::to_string(current_goto_version));
158+
else if(version > current_goto_version)
159+
throw invalid_command_line_argument_exceptiont(
160+
"unknown goto binary version " + std::to_string(version),
161+
"supported version = " + std::to_string(current_goto_version));
162+
else
159163
return write_goto_binary_v4(
160164
out, symbol_table, goto_functions, irepconverter);
161-
162-
default:
163-
throw "unknown goto binary version";
164-
}
165-
166-
return false;
167165
}
168166

169167
/// Writes a goto program to disc

src/util/exception_utils.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
6464
std::string incorrect_goto_program_exceptiont::what() const
6565
{
6666
return message + " (at: " + source_location.as_string() + ")";
67+
if(source_location.is_nil())
68+
return message;
69+
else
70+
return message + " (at: " + source_location.as_string() + ")";
71+
}
72+
73+
incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
74+
std::string message)
75+
: message(std::move(message))
76+
{
77+
source_location.make_nil();
6778
}
6879

6980
unsupported_operation_exceptiont::unsupported_operation_exceptiont(

src/util/exception_utils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class incorrect_goto_program_exceptiont : public cprover_exception_baset
8686
incorrect_goto_program_exceptiont(
8787
std::string message,
8888
source_locationt source_location);
89+
explicit incorrect_goto_program_exceptiont(std::string message);
8990
std::string what() const override;
9091

9192
private:

0 commit comments

Comments
 (0)