summaryrefslogtreecommitdiff
path: root/asl
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-02 18:19:38 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2025-01-02 18:19:38 +0100
commitc9fef8d83fe48f233372b890fcfd184ef68a9b69 (patch)
tree0c1825a180288c38a11ba4e644bd1a6b3151f906 /asl
parent7e66d8e7e50060553be837b021aef1d83d0252bd (diff)
Add assertion failure handler
Diffstat (limited to 'asl')
-rw-r--r--asl/BUILD.bazel1
-rw-r--r--asl/assert.cpp11
-rw-r--r--asl/assert.hpp20
-rw-r--r--asl/buffer.hpp2
-rw-r--r--asl/meta.hpp13
5 files changed, 45 insertions, 2 deletions
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<typename T> struct id { using type = T; };