Introduce a check() method (like assert() but always on)

This commit is contained in:
apio 2022-11-15 19:36:50 +01:00
parent c319336e3d
commit c9feb11366
4 changed files with 35 additions and 8 deletions

View File

@ -27,6 +27,8 @@ add_executable(moon ${SOURCES})
target_link_libraries(moon moon-asm) target_link_libraries(moon moon-asm)
target_compile_definitions(moon PRIVATE IN_MOON)
target_compile_options(moon PRIVATE -Os) target_compile_options(moon PRIVATE -Os)
target_compile_options(moon PRIVATE -pedantic -Wall -Wextra -Werror -Wvla) target_compile_options(moon PRIVATE -pedantic -Wall -Wextra -Werror -Wvla)

View File

@ -84,5 +84,7 @@ extern "C" [[noreturn]] void _start()
Serial::println("Successfully unmapped address"); Serial::println("Successfully unmapped address");
check(false);
CPU::efficient_halt(); CPU::efficient_halt();
} }

22
luna/Check.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#ifdef IN_MOON
#include "arch/Serial.h"
[[noreturn]] inline bool __check_failed(const char* string)
{
Serial::print("CHECK FAILED: ");
Serial::println(string);
for (;;)
;
}
#else
[[noreturn]] inline bool __check_failed(const char*)
{
__builtin_trap();
}
#endif
#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
#define check(expr) (expr) || __check_failed("at " __FILE__ ":" STRINGIZE_VALUE_OF(__LINE__) ": " #expr)

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <Check.h>
#include <Move.h> #include <Move.h>
#include <PlacementNew.h> #include <PlacementNew.h>
#include <Types.h> #include <Types.h>
@ -81,19 +82,19 @@ template <typename T> class Result
int error() int error()
{ {
// ensure(has_error()); check(has_error());
return m_error; return m_error;
} }
Error release_error() Error release_error()
{ {
// ensure(has_error()); check(has_error());
return {m_error}; return {m_error};
} }
T value() T value()
{ {
// ensure(has_value()); check(has_value());
return m_storage.fetch_reference(); return m_storage.fetch_reference();
} }
@ -105,7 +106,7 @@ template <typename T> class Result
T release_value() T release_value()
{ {
// ensure(has_value()); check(has_value());
T item = m_storage.fetch_reference(); T item = m_storage.fetch_reference();
m_has_value = false; m_has_value = false;
m_storage.destroy(); m_storage.destroy();
@ -206,25 +207,25 @@ template <> class Result<void>
int error() int error()
{ {
// ensure(has_error()); check(has_error());
return m_error; return m_error;
} }
Error release_error() Error release_error()
{ {
// ensure(has_error()); check(has_error());
return {m_error}; return {m_error};
} }
void value() void value()
{ {
// ensure(has_value()); check(has_value());
return; return;
} }
void release_value() void release_value()
{ {
// ensure(has_value()); check(has_value());
return; return;
} }