Skip to content

Commit

Permalink
Nix unconditional fatal errors. (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
dabrahams authored Aug 19, 2024
1 parent 9aa9af0 commit 7d8e8bc
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions include/adobe/contract_checks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@ class contract_violation final : public ::std::logic_error
using kind_t = int;

// The predefined kinds of contract violations provided by this library.
enum predefined_kind : kind_t {
precondition = 1,
postcondition,
invariant,
unconditional_fatal_error
};
enum predefined_kind : kind_t { precondition = 1, postcondition, invariant };

private:
// A string representation of the condition whose falsity caused this violation to be detected.
Expand Down Expand Up @@ -57,29 +52,19 @@ class contract_violation final : public ::std::logic_error

void print_report() const
{
if (_kind == predefined_kind::unconditional_fatal_error) {
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
std::fprintf(stderr,
"%s:%d: %s: %s\n",
_file,
static_cast<int>(_line),
"Unconditional fatal error",
what());
} else {
const char *const description =
_kind == predefined_kind::precondition ? "Precondition violated"
: _kind == predefined_kind::postcondition ? "Postcondition not upheld"
: _kind == predefined_kind::invariant ? "Invariant not upheld"
: "Unknown category kind";
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
std::fprintf(stderr,
"%s:%d: %s (%s). %s\n",
_file,
static_cast<int>(_line),
description,
_condition,
what());
}
const char *const description = _kind == predefined_kind::precondition ? "Precondition violated"
: _kind == predefined_kind::postcondition
? "Postcondition not upheld"
: _kind == predefined_kind::invariant ? "Invariant not upheld"
: "Unknown category kind";
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
std::fprintf(stderr,
"%s:%d: %s (%s). %s\n",
_file,
static_cast<int>(_line),
description,
_condition,
what());
(void)std::fflush(stderr);
}
};
Expand Down

0 comments on commit 7d8e8bc

Please sign in to comment.