diff --git a/src/goto-programs/restrict_function_pointers.cpp b/src/goto-programs/restrict_function_pointers.cpp index db57e4c3826..56b29c33365 100644 --- a/src/goto-programs/restrict_function_pointers.cpp +++ b/src/goto-programs/restrict_function_pointers.cpp @@ -87,7 +87,8 @@ static void restrict_function_pointer( invalid_restriction_exceptiont::invalid_restriction_exceptiont( std::string reason, std::string correct_format) - : reason(std::move(reason)), correct_format(std::move(correct_format)) + : cprover_exception_baset(std::move(reason)), + correct_format(std::move(correct_format)) { } diff --git a/src/goto-programs/slice_global_inits.h b/src/goto-programs/slice_global_inits.h index 10deb27e474..78a6fad9d5d 100644 --- a/src/goto-programs/slice_global_inits.h +++ b/src/goto-programs/slice_global_inits.h @@ -23,17 +23,9 @@ class user_input_error_exceptiont : public cprover_exception_baset { public: explicit user_input_error_exceptiont(std::string message) - : message(std::move(message)) + : cprover_exception_baset(std::move(message)) { } - - std::string what() const override - { - return message; - } - -private: - std::string message; }; /// Remove initialization of global variables that are not used in any function diff --git a/src/goto-programs/string_instrumentation.h b/src/goto-programs/string_instrumentation.h index bc89b779bf3..153f8fe3505 100644 --- a/src/goto-programs/string_instrumentation.h +++ b/src/goto-programs/string_instrumentation.h @@ -26,16 +26,16 @@ class incorrect_source_program_exceptiont : public cprover_exception_baset incorrect_source_program_exceptiont( std::string message, source_locationt source_location) - : message(std::move(message)), source_location(std::move(source_location)) + : cprover_exception_baset(std::move(message)), + source_location(std::move(source_location)) { } std::string what() const override { - return message + " (at: " + source_location.as_string() + ")"; + return reason + " (at: " + source_location.as_string() + ")"; } private: - std::string message; source_locationt source_location; }; diff --git a/src/memory-analyzer/gdb_api.h b/src/memory-analyzer/gdb_api.h index fd68143ebd9..2190731c0d6 100644 --- a/src/memory-analyzer/gdb_api.h +++ b/src/memory-analyzer/gdb_api.h @@ -229,16 +229,10 @@ class gdb_apit class gdb_interaction_exceptiont : public cprover_exception_baset { public: - explicit gdb_interaction_exceptiont(std::string reason) : reason(reason) + explicit gdb_interaction_exceptiont(std::string reason) + : cprover_exception_baset(std::move(reason)) { } - std::string what() const override - { - return reason; - } - -private: - std::string reason; }; #endif // CPROVER_MEMORY_ANALYZER_GDB_API_H diff --git a/src/solvers/smt2/smt2_tokenizer.h b/src/solvers/smt2/smt2_tokenizer.h index 71f4e9acdae..41c38ca09d9 100644 --- a/src/solvers/smt2/smt2_tokenizer.h +++ b/src/solvers/smt2/smt2_tokenizer.h @@ -9,8 +9,6 @@ Author: Daniel Kroening, kroening@kroening.com #ifndef CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H #define CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H -#include - #include #include @@ -23,7 +21,7 @@ class smt2_tokenizert line_no=1; } - class smt2_errort : public cprover_exception_baset + class smt2_errort { public: smt2_errort(const std::string &_message, unsigned _line_no) @@ -36,7 +34,7 @@ class smt2_tokenizert { } - std::string what() const override + std::string what() const { return message.str(); } diff --git a/src/util/exception_utils.cpp b/src/util/exception_utils.cpp index 29f850c1a7f..624f9d0f167 100644 --- a/src/util/exception_utils.cpp +++ b/src/util/exception_utils.cpp @@ -9,6 +9,11 @@ Author: Fotis Koutoulakis, fotis.koutoulakis@diffblue.com #include "exception_utils.h" #include +std::string cprover_exception_baset::what() const +{ + return reason; +} + std::string invalid_command_line_argument_exceptiont::what() const { std::string res; @@ -28,42 +33,32 @@ invalid_command_line_argument_exceptiont:: std::string reason, std::string option, std::string correct_input) - : reason(std::move(reason)), + : cprover_exception_baset(std::move(reason)), option(std::move(option)), correct_input(std::move(correct_input)) { } system_exceptiont::system_exceptiont(std::string message) - : message(std::move(message)) + : cprover_exception_baset(std::move(message)) { } -std::string system_exceptiont::what() const -{ - return message; -} - deserialization_exceptiont::deserialization_exceptiont(std::string message) - : message(std::move(message)) + : cprover_exception_baset(std::move(message)) { } -std::string deserialization_exceptiont::what() const -{ - return message; -} - incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont( std::string message) - : message(std::move(message)) + : cprover_exception_baset(std::move(message)), + source_location(source_locationt::nil()) { - source_location.make_nil(); } std::string incorrect_goto_program_exceptiont::what() const { - std::string ret(message); + std::string ret(reason); if(!source_location.is_nil()) ret += " (at: " + source_location.as_string() + ")"; @@ -76,32 +71,17 @@ std::string incorrect_goto_program_exceptiont::what() const unsupported_operation_exceptiont::unsupported_operation_exceptiont( std::string message) - : message(std::move(message)) + : cprover_exception_baset(std::move(message)) { } -std::string unsupported_operation_exceptiont::what() const -{ - return message; -} - analysis_exceptiont::analysis_exceptiont(std::string reason) - : reason(std::move(reason)) -{ -} - -std::string analysis_exceptiont::what() const + : cprover_exception_baset(std::move(reason)) { - return reason; } invalid_source_file_exceptiont::invalid_source_file_exceptiont( std::string reason) - : reason(std::move(reason)) + : cprover_exception_baset(std::move(reason)) { } - -std::string invalid_source_file_exceptiont::what() const -{ - return reason; -} diff --git a/src/util/exception_utils.h b/src/util/exception_utils.h index 90a15e51886..f10e2b0689a 100644 --- a/src/util/exception_utils.h +++ b/src/util/exception_utils.h @@ -27,8 +27,21 @@ class cprover_exception_baset /// A human readable description of what went wrong. /// For readability, implementors should not add a leading /// or trailing newline to this description. - virtual std::string what() const = 0; + virtual std::string what() const; virtual ~cprover_exception_baset() = default; + +protected: + /// This constructor is marked protected to ensure this class isn't used + /// directly. Deriving classes should be used to more precisely describe the + /// problem that occurred. + explicit cprover_exception_baset(std::string reason) + : reason(std::move(reason)) + { + } + + /// The reason this exception was generated. This is the string returned by + /// `what()` unless that method is overridden + std::string reason; }; /// Thrown when users pass incorrect command line arguments, @@ -36,8 +49,6 @@ class cprover_exception_baset /// two mutually exclusive flags class invalid_command_line_argument_exceptiont : public cprover_exception_baset { - /// The reason this exception was generated. - std::string reason; /// The full command line option (not the argument) that got /// erroneous input. std::string option; @@ -61,10 +72,6 @@ class system_exceptiont : public cprover_exception_baset { public: explicit system_exceptiont(std::string message); - std::string what() const override; - -private: - std::string message; }; /// Thrown when failing to deserialize a value from some @@ -73,11 +80,6 @@ class deserialization_exceptiont : public cprover_exception_baset { public: explicit deserialization_exceptiont(std::string message); - - std::string what() const override; - -private: - std::string message; }; /// 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 std::string what() const override; private: - std::string message; source_locationt source_location; std::string diagnostics; @@ -117,8 +118,8 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont( std::string message, Diagnostic &&diagnostic, Diagnostics &&... diagnostics) - : message(std::move(message)), - source_location(), + : cprover_exception_baset(std::move(message)), + source_location(source_locationt::nil()), diagnostics(detail::assemble_diagnostics( std::forward(diagnostic), std::forward(diagnostics)...)) @@ -130,7 +131,7 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont( std::string message, source_locationt source_location, Diagnostics &&... diagnostics) - : message(std::move(message)), + : cprover_exception_baset(std::move(message)), source_location(std::move(source_location)), diagnostics( detail::assemble_diagnostics(std::forward(diagnostics)...)) @@ -143,12 +144,8 @@ incorrect_goto_program_exceptiont::incorrect_goto_program_exceptiont( class unsupported_operation_exceptiont : public cprover_exception_baset { public: + /// \p message is the unsupported operation causing this fault to occur. explicit unsupported_operation_exceptiont(std::string message); - std::string what() const override; - -private: - /// The unsupported operation causing this fault to occur. - std::string message; }; /// Thrown when an unexpected error occurs during the analysis (e.g., when the @@ -157,11 +154,6 @@ class analysis_exceptiont : public cprover_exception_baset { public: explicit analysis_exceptiont(std::string reason); - std::string what() const override; - -private: - /// The reason this exception was generated. - std::string reason; }; /// 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 { public: explicit invalid_source_file_exceptiont(std::string reason); - std::string what() const override; - -private: - std::string reason; }; #endif // CPROVER_UTIL_EXCEPTION_UTILS_H