From 8eb18cdb65e3fd6111fc50ac5a69d3f7094dd54c Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Mon, 4 Nov 2024 11:53:18 -0500 Subject: diag: New bfs_unreachable() and bfs_assume() macros --- src/diag.h | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/diag.h b/src/diag.h index 3bea9b2..bc741cf 100644 --- a/src/diag.h +++ b/src/diag.h @@ -12,6 +12,7 @@ #include "bfstd.h" #include +#include /** * A source code location. @@ -126,6 +127,37 @@ void bfs_abortf(const struct bfs_loc *loc, const char *format, ...); # define bfs_eassert bfs_everify #endif +/** @internal Wrapper for unreachable(). */ +#ifdef unreachable +# define bfs_unreachable_ unreachable +#elif __has_builtin(__builtin_unreachable) +# define bfs_unreachable_ __builtin_unreachable +#else +_noreturn +static inline void bfs_unreachable_(void) { +} +#endif + +/** + * Abort in debug builds; unreachable in release builds. + */ +#ifdef NDEBUG +# define bfs_unreachable(...) bfs_unreachable_() +#else +# define bfs_unreachable bfs_abort +#endif + +/** + * Assert in debug builds; assume in release builds. + */ +#ifdef NDEBUG +# define bfs_assume(...) bfs_assume_(__VA_ARGS__, ) +# define bfs_assume_(cond, ...) \ + ((cond) ? (void)0 : bfs_unreachable_()) +#else +# define bfs_assume bfs_assert +#endif + struct bfs_ctx; struct bfs_expr; -- cgit v1.2.3