From c9fef8d83fe48f233372b890fcfd184ef68a9b69 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Thu, 2 Jan 2025 18:19:38 +0100 Subject: Add assertion failure handler --- asl/BUILD.bazel | 1 + asl/assert.cpp | 11 +++++++++++ asl/assert.hpp | 20 ++++++++++++++++++-- asl/buffer.hpp | 2 ++ asl/meta.hpp | 13 +++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 asl/assert.cpp (limited to 'asl') diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index cbfe91e..9909bc3 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -24,6 +24,7 @@ cc_library( ], srcs = [ "allocator.cpp", + "assert.cpp", "format.cpp", "format_float.cpp", "print.cpp", diff --git a/asl/assert.cpp b/asl/assert.cpp new file mode 100644 index 0000000..353232e --- /dev/null +++ b/asl/assert.cpp @@ -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"); +} diff --git a/asl/assert.hpp b/asl/assert.hpp index d95159a..bf6a795 100644 --- a/asl/assert.hpp +++ b/asl/assert.hpp @@ -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(); \ + } diff --git a/asl/buffer.hpp b/asl/buffer.hpp index 276222d..a616011 100644 --- a/asl/buffer.hpp +++ b/asl/buffer.hpp @@ -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; diff --git a/asl/meta.hpp b/asl/meta.hpp index 133d2fb..f69a4a3 100644 --- a/asl/meta.hpp +++ b/asl/meta.hpp @@ -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 struct id { using type = T; }; -- cgit