diff options
| author | WormHeamer | 2025-12-18 19:19:44 -0500 |
|---|---|---|
| committer | WormHeamer | 2025-12-18 19:19:44 -0500 |
| commit | ce66eebb6ca4588224cd2cf0a38c0e6ffbfcee01 (patch) | |
| tree | 82e634530cdc45eed59eb2b5603a5bf791810c74 | |
| parent | 75b0f38f8ec0cb70250e05ff24e052360f7c114f (diff) | |
add configurable PUT_ERROR_MSG
| -rw-r--r-- | wrmr.h | 27 |
1 files changed, 21 insertions, 6 deletions
@@ -6,8 +6,6 @@ #include <stdint.h> #include <stddef.h> -#include <stdlib.h> -#include <stdio.h> typedef int8_t i8; typedef int16_t i16; @@ -27,8 +25,27 @@ typedef uintptr_t uptr; #define BESTRING_(x) #x #define BESTRING(x) BESTRING_(x) +#if defined(__GNUC__) +# define TRAP() __builtin_trap() +#else +# define TRAP() (*(volatile int *)NULL = 0) +#endif + +#ifndef PUT_ERROR_MSG +# include <stdio.h> +# define PUT_ERROR_MSG(msg) fputs(msg, stderr) +#endif + #define FILE_LINE_MSG(msg) (__FILE__ ":" BESTRING(__LINE__) ": " msg "\n") -#define FAIL_WITH_MSG(msg) do { fputs(FILE_LINE_MSG(msg), stderr); abort(); } while(0) +#define FAIL_WITH_MSG(msg) do { PUT_ERROR_MSG(FILE_LINE_MSG(msg)); TRAP(); } while(0) + +#ifdef NDEBUG +# define ASSERT(...) do {} while(0) +#else +# define ASSERT(...) do {\ + if (!(__VA_ARGS__)) FAIL_WITH_MSG("Assertion failed: " #__VA_ARGS__);\ + } while(0) +#endif #ifndef NDEBUG # define UNREACHABLE_MSG(msg) FAIL_WITH_MSG(msg) @@ -54,9 +71,7 @@ typedef uintptr_t uptr; # elif defined(__clang_version__) # define ASSUME(...) (__builtin_assume(__VA_ARGS__)) # endif -#endif - -#ifndef ASSUME +#else # define ASSUME(...) do if (!(__VA_ARGS__)) UNREACHABLE_MSG("assumption failed: " #__VA_ARGS__); while(0); #endif |
