Skip to content

Commit 86b13e9

Browse files
committed
Exception utils: remove redundant what() implementations
Make a `std::string reason` a member of the base class, and make `what()` default to returning `reason`. This avoids duplicating the same implementation across several child classes. While at it, also fix the missing init-to-nil in one of incorrect_goto_program_exceptiont's constructors.
1 parent 3f175c9 commit 86b13e9

File tree

7 files changed

+42
-89
lines changed

7 files changed

+42
-89
lines changed

src/goto-programs/restrict_function_pointers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ static void restrict_function_pointer(
8787
invalid_restriction_exceptiont::invalid_restriction_exceptiont(
8888
std::string reason,
8989
std::string correct_format)
90-
: reason(std::move(reason)), correct_format(std::move(correct_format))
90+
: cprover_exception_baset(std::move(reason)),
91+
correct_format(std::move(correct_format))
9192
{
9293
}
9394

src/goto-programs/slice_global_inits.h

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,9 @@ class user_input_error_exceptiont : public cprover_exception_baset
2323
{
2424
public:
2525
explicit user_input_error_exceptiont(std::string message)
26-
: message(std::move(message))
26+
: cprover_exception_baset(std::move(message))
2727
{
2828
}
29-
30-
std::string what() const override
31-
{
32-
return message;
33-
}
34-
35-
private:
36-
std::string message;
3729
};
3830

3931
/// Remove initialization of global variables that are not used in any function

src/goto-programs/string_instrumentation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@ class incorrect_source_program_exceptiont : public cprover_exception_baset
2626
incorrect_source_program_exceptiont(
2727
std::string message,
2828
source_locationt source_location)
29-
: message(std::move(message)), source_location(std::move(source_location))
29+
: cprover_exception_baset(std::move(message)),
30+
source_location(std::move(source_location))
3031
{
3132
}
3233
std::string what() const override
3334
{
34-
return message + " (at: " + source_location.as_string() + ")";
35+
return reason + " (at: " + source_location.as_string() + ")";
3536
}
3637

3738
private:
38-
std::string message;
3939
source_locationt source_location;
4040
};
4141

src/memory-analyzer/gdb_api.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,10 @@ class gdb_apit
229229
class gdb_interaction_exceptiont : public cprover_exception_baset
230230
{
231231
public:
232-
explicit gdb_interaction_exceptiont(std::string reason) : reason(reason)
232+
explicit gdb_interaction_exceptiont(std::string reason)
233+
: cprover_exception_baset(std::move(reason))
233234
{
234235
}
235-
std::string what() const override
236-
{
237-
return reason;
238-
}
239-
240-
private:
241-
std::string reason;
242236
};
243237

244238
#endif // CPROVER_MEMORY_ANALYZER_GDB_API_H

src/solvers/smt2/smt2_tokenizer.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ Author: Daniel Kroening, [email protected]
99
#ifndef CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
1010
#define CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
1111

12-
#include <util/exception_utils.h>
13-
1412
#include <sstream>
1513
#include <string>
1614

@@ -23,7 +21,7 @@ class smt2_tokenizert
2321
line_no=1;
2422
}
2523

26-
class smt2_errort : public cprover_exception_baset
24+
class smt2_errort
2725
{
2826
public:
2927
smt2_errort(const std::string &_message, unsigned _line_no)
@@ -36,7 +34,7 @@ class smt2_tokenizert
3634
{
3735
}
3836

39-
std::string what() const override
37+
std::string what() const
4038
{
4139
return message.str();
4240
}

src/util/exception_utils.cpp

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ Author: Fotis Koutoulakis, [email protected]
99
#include "exception_utils.h"
1010
#include <utility>
1111

12+
std::string cprover_exception_baset::what() const
13+
{
14+
return reason;
15+
}
16+
1217
std::string invalid_command_line_argument_exceptiont::what() const
1318
{
1419
std::string res;
@@ -28,42 +33,32 @@ invalid_command_line_argument_exceptiont::
2833
std::string reason,
2934
std::string option,
3035
std::string correct_input)
31-
: reason(std::move(reason)),
36+
: cprover_exception_baset(std::move(reason)),
3237
option(std::move(option)),
3338
correct_input(std::move(correct_input))
3439
{
3540
}
3641

3742
system_exceptiont::system_exceptiont(std::string message)
38-
: message(std::move(message))
43+
: cprover_exception_baset(std::move(message))
3944
{
4045
}
4146

42-
std::string system_exceptiont::what() const
43-
{
44-
return message;
45-
}
46-
4747
deserialization_exceptiont::deserialization_exceptiont(std::string message)
48-
: message(std::move(message))
48+
: cprover_exception_baset(std::move(message))
4949
{
5050
}
5151

52-
std::string deserialization_exceptiont::what() const
53-
{
54-
return message;
55-
}
56-
5752
incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
5853
std::string message)
59-
: message(std::move(message))
54+
: cprover_exception_baset(std::move(message)),
55+
source_location(source_locationt::nil())
6056
{
61-
source_location.make_nil();
6257
}
6358

6459
std::string incorrect_goto_program_exceptiont::what() const
6560
{
66-
std::string ret(message);
61+
std::string ret(reason);
6762

6863
if(!source_location.is_nil())
6964
ret += " (at: " + source_location.as_string() + ")";
@@ -76,32 +71,17 @@ std::string incorrect_goto_program_exceptiont::what() const
7671

7772
unsupported_operation_exceptiont::unsupported_operation_exceptiont(
7873
std::string message)
79-
: message(std::move(message))
74+
: cprover_exception_baset(std::move(message))
8075
{
8176
}
8277

83-
std::string unsupported_operation_exceptiont::what() const
84-
{
85-
return message;
86-
}
87-
8878
analysis_exceptiont::analysis_exceptiont(std::string reason)
89-
: reason(std::move(reason))
90-
{
91-
}
92-
93-
std::string analysis_exceptiont::what() const
79+
: cprover_exception_baset(std::move(reason))
9480
{
95-
return reason;
9681
}
9782

9883
invalid_source_file_exceptiont::invalid_source_file_exceptiont(
9984
std::string reason)
100-
: reason(std::move(reason))
85+
: cprover_exception_baset(std::move(reason))
10186
{
10287
}
103-
104-
std::string invalid_source_file_exceptiont::what() const
105-
{
106-
return reason;
107-
}

src/util/exception_utils.h

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,28 @@ class cprover_exception_baset
2727
/// A human readable description of what went wrong.
2828
/// For readability, implementors should not add a leading
2929
/// or trailing newline to this description.
30-
virtual std::string what() const = 0;
30+
virtual std::string what() const;
3131
virtual ~cprover_exception_baset() = default;
32+
33+
protected:
34+
/// This constructor is marked protected to ensure this class isn't used
35+
/// directly. Deriving classes should be used to more precisely describe the
36+
/// problem that occurred.
37+
explicit cprover_exception_baset(std::string reason)
38+
: reason(std::move(reason))
39+
{
40+
}
41+
42+
/// The reason this exception was generated. This is the string returned by
43+
/// `what()` unless that method is overridden
44+
std::string reason;
3245
};
3346

3447
/// Thrown when users pass incorrect command line arguments,
3548
/// for example passing no files to analysis or setting
3649
/// two mutually exclusive flags
3750
class invalid_command_line_argument_exceptiont : public cprover_exception_baset
3851
{
39-
/// The reason this exception was generated.
40-
std::string reason;
4152
/// The full command line option (not the argument) that got
4253
/// erroneous input.
4354
std::string option;
@@ -61,10 +72,6 @@ class system_exceptiont : public cprover_exception_baset
6172
{
6273
public:
6374
explicit system_exceptiont(std::string message);
64-
std::string what() const override;
65-
66-
private:
67-
std::string message;
6875
};
6976

7077
/// Thrown when failing to deserialize a value from some
@@ -73,11 +80,6 @@ class deserialization_exceptiont : public cprover_exception_baset
7380
{
7481
public:
7582
explicit deserialization_exceptiont(std::string message);
76-
77-
std::string what() const override;
78-
79-
private:
80-
std::string message;
8183
};
8284

8385
/// Thrown when a goto program that's being processed is in an invalid format,
@@ -106,7 +108,6 @@ class incorrect_goto_program_exceptiont : public cprover_exception_baset
106108
std::string what() const override;
107109

108110
private:
109-
std::string message;
110111
source_locationt source_location;
111112

112113
std::string diagnostics;
@@ -117,8 +118,8 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
117118
std::string message,
118119
Diagnostic &&diagnostic,
119120
Diagnostics &&... diagnostics)
120-
: message(std::move(message)),
121-
source_location(),
121+
: cprover_exception_baset(std::move(message)),
122+
source_location(source_locationt::nil()),
122123
diagnostics(detail::assemble_diagnostics(
123124
std::forward<Diagnostic>(diagnostic),
124125
std::forward<Diagnostics>(diagnostics)...))
@@ -130,7 +131,7 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
130131
std::string message,
131132
source_locationt source_location,
132133
Diagnostics &&... diagnostics)
133-
: message(std::move(message)),
134+
: cprover_exception_baset(std::move(message)),
134135
source_location(std::move(source_location)),
135136
diagnostics(
136137
detail::assemble_diagnostics(std::forward<Diagnostics>(diagnostics)...))
@@ -143,12 +144,8 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont(
143144
class unsupported_operation_exceptiont : public cprover_exception_baset
144145
{
145146
public:
147+
/// \p message is the unsupported operation causing this fault to occur.
146148
explicit unsupported_operation_exceptiont(std::string message);
147-
std::string what() const override;
148-
149-
private:
150-
/// The unsupported operation causing this fault to occur.
151-
std::string message;
152149
};
153150

154151
/// Thrown when an unexpected error occurs during the analysis (e.g., when the
@@ -157,11 +154,6 @@ class analysis_exceptiont : public cprover_exception_baset
157154
{
158155
public:
159156
explicit analysis_exceptiont(std::string reason);
160-
std::string what() const override;
161-
162-
private:
163-
/// The reason this exception was generated.
164-
std::string reason;
165157
};
166158

167159
/// Thrown when we can't handle something in an input source file.
@@ -171,10 +163,6 @@ class invalid_source_file_exceptiont : public cprover_exception_baset
171163
{
172164
public:
173165
explicit invalid_source_file_exceptiont(std::string reason);
174-
std::string what() const override;
175-
176-
private:
177-
std::string reason;
178166
};
179167

180168
#endif // CPROVER_UTIL_EXCEPTION_UTILS_H

0 commit comments

Comments
 (0)