Skip to content

Exception utils: remove redundant what() implementations #6608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/goto-programs/restrict_function_pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
}

Expand Down
10 changes: 1 addition & 9 deletions src/goto-programs/slice_global_inits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/goto-programs/string_instrumentation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
10 changes: 2 additions & 8 deletions src/memory-analyzer/gdb_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 2 additions & 4 deletions src/solvers/smt2/smt2_tokenizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ Author: Daniel Kroening, [email protected]
#ifndef CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H
#define CPROVER_SOLVERS_SMT2_SMT2_TOKENIZER_H

#include <util/exception_utils.h>

#include <sstream>
#include <string>

Expand All @@ -23,7 +21,7 @@ class smt2_tokenizert
line_no=1;
}

class smt2_errort : public cprover_exception_baset
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason this was removed from the inheritance chain? Would it be better if message was removed and substituted with reason?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this class, message is a std::ostringstream, making it really different from the parent class. Really, there is no meaningful commonality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only benefit would be to catch all these exceptions at once, which could be done by another 'base class with reason' above the existing base, but that's minor IMO.

class smt2_errort
{
public:
smt2_errort(const std::string &_message, unsigned _line_no)
Expand All @@ -36,7 +34,7 @@ class smt2_tokenizert
{
}

std::string what() const override
std::string what() const
{
return message.str();
}
Expand Down
48 changes: 14 additions & 34 deletions src/util/exception_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ Author: Fotis Koutoulakis, [email protected]
#include "exception_utils.h"
#include <utility>

std::string cprover_exception_baset::what() const
{
return reason;
}

std::string invalid_command_line_argument_exceptiont::what() const
{
std::string res;
Expand All @@ -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() + ")";
Expand All @@ -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;
}
48 changes: 18 additions & 30 deletions src/util/exception_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,28 @@ 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,
/// for example passing no files to analysis or setting
/// 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;
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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>(diagnostic),
std::forward<Diagnostics>(diagnostics)...))
Expand All @@ -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>(diagnostics)...))
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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