Skip to content

Commit 5dde4be

Browse files
authored
Merge pull request #2996 from hannes-steffenhagen-diffblue/feature-new_exception_types
Add additional exception classes and introduce base exception
2 parents 0c21f1c + fb1be5d commit 5dde4be

File tree

4 files changed

+154
-13
lines changed

4 files changed

+154
-13
lines changed

src/goto-cc/goto_cc_mode.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Author: CM Wintersteiger, 2006
2222
#include <sysexits.h>
2323
#endif
2424

25+
#include <util/exception_utils.h>
2526
#include <util/parse_options.h>
2627
#include <util/version.h>
2728

@@ -106,6 +107,11 @@ int goto_cc_modet::main(int argc, const char **argv)
106107
error() << "Out of memory" << eom;
107108
return EX_SOFTWARE;
108109
}
110+
catch(const cprover_exception_baset &e)
111+
{
112+
error() << e.what() << eom;
113+
return EX_SOFTWARE;
114+
}
109115
}
110116

111117
/// prints a message informing the user about incorrect options

src/util/exception_utils.cpp

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,71 @@ Author: Fotis Koutoulakis, [email protected]
77
\*******************************************************************/
88

99
#include "exception_utils.h"
10+
#include <utility>
1011

11-
std::string invalid_user_input_exceptiont::what() const noexcept
12+
std::string invalid_user_input_exceptiont::what() const
1213
{
1314
std::string res;
14-
res += "\nInvalid User Input\n";
15-
res += "Option: " + option + "\n";
16-
res += "Reason: " + reason;
15+
res += "Invalid User Input";
16+
res += "\nOption: " + option;
17+
res += "\nReason: " + reason;
1718
// Print an optional correct usage message assuming correct input parameters have been passed
18-
res += correct_input + "\n";
19+
if(!correct_input.empty())
20+
{
21+
res += "\nSuggestion: " + correct_input;
22+
}
1923
return res;
2024
}
25+
26+
invalid_user_input_exceptiont::invalid_user_input_exceptiont(
27+
std::string reason,
28+
std::string option,
29+
std::string correct_input)
30+
: reason(std::move(reason)),
31+
option(std::move(option)),
32+
correct_input(std::move(correct_input))
33+
{
34+
}
35+
36+
system_exceptiont::system_exceptiont(std::string message)
37+
: message(std::move(message))
38+
{
39+
}
40+
41+
std::string system_exceptiont::what() const
42+
{
43+
return message;
44+
}
45+
46+
deserialization_exceptiont::deserialization_exceptiont(std::string message)
47+
: message(std::move(message))
48+
{
49+
}
50+
51+
std::string deserialization_exceptiont::what() const
52+
{
53+
return message;
54+
}
55+
56+
incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
57+
std::string message,
58+
source_locationt source_location)
59+
: message(std::move(message)), source_location(std::move(source_location))
60+
{
61+
}
62+
63+
unsupported_operation_exceptiont::unsupported_operation_exceptiont(
64+
std::string message)
65+
: message(std::move(message))
66+
{
67+
}
68+
69+
std::string incorrect_goto_program_exceptiont::what() const
70+
{
71+
return message + " (at: " + source_location.as_string() + ")";
72+
}
73+
74+
std::string unsupported_operation_exceptiont::what() const
75+
{
76+
return message;
77+
}

src/util/exception_utils.h

Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,24 @@ Author: Fotis Koutoulakis, [email protected]
1111

1212
#include <string>
1313

14-
class invalid_user_input_exceptiont
14+
#include "source_location.h"
15+
16+
/// Base class for exceptions thrown in the cprover project.
17+
/// Intended to be used as a convenient way to have a
18+
/// "catch all and report errors" from application entry points.
19+
class cprover_exception_baset
20+
{
21+
public:
22+
/// A human readable description of what went wrong.
23+
/// For readability, implementors should not add a leading
24+
/// or trailing newline to this description.
25+
virtual std::string what() const = 0;
26+
};
27+
28+
/// Thrown when users pass incorrect command line arguments,
29+
/// for example passing no files to analysis or setting
30+
/// two mutually exclusive flags
31+
class invalid_user_input_exceptiont : public cprover_exception_baset
1532
{
1633
/// The reason this exception was generated.
1734
std::string reason;
@@ -25,12 +42,69 @@ class invalid_user_input_exceptiont
2542
invalid_user_input_exceptiont(
2643
std::string reason,
2744
std::string option,
28-
std::string correct_input = "")
29-
: reason(reason), option(option), correct_input(correct_input)
30-
{
31-
}
45+
std::string correct_input = "");
46+
47+
std::string what() const override;
48+
};
49+
50+
/// Thrown when some external system fails unexpectedly.
51+
/// Examples are IO exceptions (files not present, or we don't
52+
/// have the right permissions to interact with them), timeouts for
53+
/// external processes etc
54+
class system_exceptiont : public cprover_exception_baset
55+
{
56+
public:
57+
explicit system_exceptiont(std::string message);
58+
std::string what() const override;
59+
60+
private:
61+
std::string message;
62+
};
63+
64+
/// Thrown when failing to deserialize a value from some
65+
/// low level format, like JSON or raw bytes
66+
class deserialization_exceptiont : public cprover_exception_baset
67+
{
68+
public:
69+
explicit deserialization_exceptiont(std::string message);
70+
71+
std::string what() const override;
72+
73+
private:
74+
std::string message;
75+
};
76+
77+
/// Thrown when a goto program that's being processed is in an invalid format,
78+
/// for example passing the wrong number of arguments to functions.
79+
/// Note that this only applies to goto programs that are user provided,
80+
/// that internal transformations on goto programs don't produce invalid
81+
/// programs should be guarded by invariants instead.
82+
/// \see invariant.h
83+
class incorrect_goto_program_exceptiont : public cprover_exception_baset
84+
{
85+
public:
86+
incorrect_goto_program_exceptiont(
87+
std::string message,
88+
source_locationt source_location);
89+
std::string what() const override;
90+
91+
private:
92+
std::string message;
93+
source_locationt source_location;
94+
};
95+
96+
/// Thrown when we encounter an instruction, parameters to an instruction etc.
97+
/// in a goto program that has some theoretically valid semantics,
98+
/// but that we don't presently have any support for.
99+
class unsupported_operation_exceptiont : public cprover_exception_baset
100+
{
101+
public:
102+
explicit unsupported_operation_exceptiont(std::string message);
103+
std::string what() const override;
32104

33-
std::string what() const noexcept;
105+
private:
106+
/// The unsupported operation causing this fault to occur.
107+
std::string message;
34108
};
35109

36110
#endif // CPROVER_UTIL_EXCEPTION_UTILS_H

src/util/parse_options.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,16 @@ int parse_options_baset::main()
7171

7272
return doit();
7373
}
74-
catch(invalid_user_input_exceptiont &e)
74+
catch(const invalid_user_input_exceptiont &e)
7575
{
7676
std::cerr << e.what() << "\n";
7777
return CPROVER_EXIT_USAGE_ERROR;
7878
}
79-
return CPROVER_EXIT_SUCCESS;
79+
catch(const cprover_exception_baset &e)
80+
{
81+
std::cerr << e.what() << '\n';
82+
return CPROVER_EXIT_EXCEPTION;
83+
}
8084
}
8185

8286
std::string

0 commit comments

Comments
 (0)