Add assertion failure handler

This commit is contained in:
2025-01-02 18:19:38 +01:00
parent 7e66d8e7e5
commit c9fef8d83f
5 changed files with 45 additions and 2 deletions

View File

@ -24,6 +24,7 @@ cc_library(
],
srcs = [
"allocator.cpp",
"assert.cpp",
"format.cpp",
"format_float.cpp",
"print.cpp",

11
asl/assert.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "asl/assert.hpp"
#include "asl/print.hpp"
void asl::report_assert_failure(const char* msg, const source_location& sl)
{
eprint("------------------------------------------------------------\n");
eprint("Assertion failure at {}, line {}:\n", sl.file, sl.line);
eprint("{}\n", msg);
eprint("------------------------------------------------------------\n");
}

View File

@ -1,6 +1,14 @@
#pragma once
#include "asl/config.hpp"
#include "asl/meta.hpp"
namespace asl
{
void report_assert_failure( const char* msg, const source_location& sl = source_location{});
} // namespace asl
#if ASL_COMPILER_CLANG_CL
#define ASL_DEBUG_BREAK() __debugbreak()
@ -10,8 +18,16 @@
#define ASL_ASSERT(...) \
if (__VA_ARGS__) {} \
else { ASL_DEBUG_BREAK(); }
else \
{ \
::asl::report_assert_failure(#__VA_ARGS__); \
ASL_DEBUG_BREAK(); \
}
#define ASL_ASSERT_RELEASE(...) \
if (__VA_ARGS__) {} \
else { ASL_DEBUG_BREAK(); }
else \
{ \
::asl::report_assert_failure(#__VA_ARGS__); \
ASL_DEBUG_BREAK(); \
}

View File

@ -114,6 +114,8 @@ private:
// NOLINTNEXTLINE(*-rvalue-reference-param-not-moved)
void move_from_other(buffer&& other)
{
ASL_ASSERT(size() == 0 && !is_on_heap());
if (other.is_on_heap())
{
m_data = other.m_data;

View File

@ -4,6 +4,19 @@
namespace asl {
struct source_location
{
const char* file;
int line;
explicit source_location(
const char* file = __builtin_FILE(),
int line = __builtin_LINE())
: file{file}
, line{line}
{}
};
struct empty {};
template<typename T> struct id { using type = T; };