Skip to content

Commit

Permalink
Remove nonfatal assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-rifkin committed Dec 5, 2023
1 parent cfcc198 commit 402e8dc
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 27 deletions.
17 changes: 3 additions & 14 deletions include/assert/assert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@
#endif

namespace libassert {
enum class ASSERTION {
NONFATAL, FATAL
};

enum class assert_type {
debug_assertion,
assertion,
Expand All @@ -116,7 +112,7 @@ namespace libassert {
#ifndef LIBASSERT_FAIL
LIBASSERT_EXPORT
#endif
void LIBASSERT_FAIL(libassert::assert_type type, libassert::ASSERTION fatal, const libassert::assertion_printer& printer);
void LIBASSERT_FAIL(libassert::assert_type type, const libassert::assertion_printer& printer);

// always_false is just convenient to use here
#define LIBASSERT_PHONY_USE(E) ((void)libassert::detail::always_false<decltype(E)>)
Expand Down Expand Up @@ -858,7 +854,6 @@ namespace libassert::detail {
#undef LIBASSERT_X

struct LIBASSERT_EXPORT extra_diagnostics {
ASSERTION fatality = ASSERTION::FATAL;
std::string message;
std::vector<std::pair<std::string, std::string>> entries;
const char* pretty_function = "<error>";
Expand All @@ -879,9 +874,7 @@ namespace libassert::detail {
// TODO
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
void process_arg(extra_diagnostics& entry, size_t i, const char* const* const args_strings, const T& t) {
if constexpr(isa<T, ASSERTION>) {
entry.fatality = t;
} else if constexpr(isa<T, pretty_function_name_wrapper>) {
if constexpr(isa<T, pretty_function_name_wrapper>) {
entry.pretty_function = t.pretty_function;
} else {
// three cases to handle: assert message, errno, and regular diagnostics
Expand Down Expand Up @@ -1065,7 +1058,6 @@ namespace libassert::detail {
);
// process_args needs to be called as soon as possible in case errno needs to be read
const auto processed_args = process_args(args_strings, args...);
const auto fatal = processed_args.fatality;
opaque_trace raw_trace = get_stacktrace_opaque();
// generate header
binary_diagnostics_descriptor binary_diagnostics;
Expand Down Expand Up @@ -1103,7 +1095,7 @@ namespace libassert::detail {
trace,
sizeof_extra_diagnostics
};
::LIBASSERT_FAIL(params->type, fatal, printer);
::LIBASSERT_FAIL(params->type, printer);
}

template<typename A, typename B, typename C, typename... Args>
Expand Down Expand Up @@ -1150,9 +1142,6 @@ inline void ERROR_ASSERTION_FAILURE_IN_CONSTEXPR_CONTEXT() {
// intentionally a no-op.
}

// NOLINTNEXTLINE(misc-unused-using-decls)
using libassert::ASSERTION;

#if LIBASSERT_IS_CLANG || LIBASSERT_IS_GCC || (defined(_MSVC_TRADITIONAL) && _MSVC_TRADITIONAL == 0)
// Macro mapping utility by William Swanson https://github.com/swansontec/map-macro/blob/master/map.h
#define LIBASSERT_EVAL0(...) __VA_ARGS__
Expand Down
15 changes: 5 additions & 10 deletions src/assert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,7 @@ namespace libassert {

LIBASSERT_ATTR_COLD std::string assertion_printer::operator()(int width) const {
const auto& [ name, type, expr_str, location, args_strings ] = *params;
const auto& [ fatal, message, extra_diagnostics, pretty_function ] = processed_args;
const auto& [ message, extra_diagnostics, pretty_function ] = processed_args;
std::string output;
// generate header
const auto function = prettify_type(pretty_function);
Expand Down Expand Up @@ -1017,7 +1017,6 @@ namespace libassert::utility {
LIBASSERT_ATTR_COLD
void libassert_default_fail_action(
libassert::assert_type type,
ASSERTION fatal,
const libassert::assertion_printer& printer
) {
// TODO: Just throw instead of all of this?
Expand All @@ -1034,18 +1033,14 @@ void libassert_default_fail_action(
switch(type) {
case libassert::assert_type::debug_assertion:
case libassert::assert_type::assertion:
if(fatal == ASSERTION::FATAL) {
case libassert::assert_type::assumption: // switch-if-case, cursed!
(void)fflush(stderr);
std::abort();
}
case libassert::assert_type::assumption: // switch-if-case, cursed!
(void)fflush(stderr);
std::abort();
// Breaking here as debug CRT allows aborts to be ignored, if someone wants to make a debug build of
// this library (on top of preventing fallthrough from nonfatal libassert)
break;
case libassert::assert_type::verification:
if(fatal == ASSERTION::FATAL) {
throw libassert::verification_failure();
}
throw libassert::verification_failure();
break;
default:
LIBASSERT_PRIMITIVE_ASSERT(false);
Expand Down
2 changes: 1 addition & 1 deletion tests/demo/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
void qux();
void wubble();

void custom_fail(libassert::assert_type, libassert::ASSERTION, const libassert::assertion_printer& printer) {
void custom_fail(libassert::assert_type, const libassert::assertion_printer& printer) {
std::cerr<<printer(libassert::utility::terminal_width(STDERR_FILENO))<<std::endl<<std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/integration/integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using namespace std::literals;

void test_path_differentiation();

void custom_fail(libassert::assert_type, libassert::ASSERTION, const libassert::assertion_printer& printer) {
void custom_fail(libassert::assert_type, const libassert::assertion_printer& printer) {
std::cout<<libassert::utility::strip_colors(printer(0))<<std::endl<<std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/type_handling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

using namespace libassert::detail;

void custom_fail(libassert::assert_type, libassert::ASSERTION, const libassert::assertion_printer& printer) {
void custom_fail(libassert::assert_type, const libassert::assertion_printer& printer) {
std::cerr<<printer(libassert::utility::terminal_width(2))<<std::endl<<std::endl;
abort();
}
Expand Down

0 comments on commit 402e8dc

Please sign in to comment.