From 93d8e7d066ec89a235630907164b07ada6ee5c3c Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 15 Oct 2024 12:34:59 -0500 Subject: Change asssertions to abort. --- source/fud_assert.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'source/fud_assert.cpp') diff --git a/source/fud_assert.cpp b/source/fud_assert.cpp index 9c2ec77..749aa6b 100644 --- a/source/fud_assert.cpp +++ b/source/fud_assert.cpp @@ -10,16 +10,16 @@ namespace fud { -void assertFail(const char* assertion, const char* file, unsigned int line, const char* function) noexcept(false) +[[noreturn]] void assertFail(const char* assertion, const std::source_location sourceLocation) { constexpr size_t MAX_FILE_CHARS = 256; constexpr size_t MAX_FUNCTION_CHARS = 256; constexpr size_t BITS_PER_OCTAL = 3; - constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(line)) + 3; + constexpr auto MAX_LINE_CHARS = BITS_PER_OCTAL * sizeof(decltype(sourceLocation.line())) + 3; constexpr size_t MAX_ASSERT_CHARS = 512 - MAX_LINE_CHARS; constexpr size_t ASSERT_MSG_SIZE = MAX_FILE_CHARS + MAX_LINE_CHARS + MAX_FUNCTION_CHARS + MAX_ASSERT_CHARS; - auto lengthResult = cStringLength(file); + auto lengthResult = cStringLength(sourceLocation.file_name()); size_t filenameLength = 0; auto badLength = lengthResult < 1 || lengthResult > SSIZE_MAX; if (!badLength) { @@ -32,7 +32,7 @@ void assertFail(const char* assertion, const char* file, unsigned int line, cons filenameLength = sizeof(invalidFile); filename = invalidFile; } else if (filenameLength > MAX_LINE_CHARS) { - filename = file + filenameLength - MAX_LINE_CHARS; + filename = sourceLocation.file_name() + filenameLength - MAX_LINE_CHARS; filenameLength = MAX_FILE_CHARS; } @@ -40,14 +40,15 @@ void assertFail(const char* assertion, const char* file, unsigned int line, cons // clang-format off static_cast(std::format_to_n( buffer.data(), buffer.size() - 1U, - "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed", + "{:.{}s}:{}: {:.{}s}: Assertion `{:.{}s}` failed\n", filename, filenameLength, - line, - function, MAX_FUNCTION_CHARS, + sourceLocation.line(), + sourceLocation.function_name(), MAX_FUNCTION_CHARS, assertion, MAX_ASSERT_CHARS)); // clang-format on - - throw std::runtime_error(buffer.data()); + buffer[buffer.size() - 1] = '\0'; + fputs(buffer.data(), stderr); + std::abort(); } } // namespace fud -- cgit v1.2.3