summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2024-11-04 11:53:18 -0500
committerTavian Barnes <tavianator@tavianator.com>2024-11-04 11:59:16 -0500
commit8eb18cdb65e3fd6111fc50ac5a69d3f7094dd54c (patch)
treec953d6560def39b85bd685629689fbf219478b7c /src
parent80cf88bcb7d3a5a1dc27887d7280e50b1dd89929 (diff)
downloadbfs-8eb18cdb65e3fd6111fc50ac5a69d3f7094dd54c.tar.xz
diag: New bfs_unreachable() and bfs_assume() macros
Diffstat (limited to 'src')
-rw-r--r--src/diag.h32
1 files changed, 32 insertions, 0 deletions
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 <stdarg.h>
+#include <stddef.h>
/**
* 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;