summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2023-05-18 12:16:30 -0400
committerTavian Barnes <tavianator@tavianator.com>2023-05-18 16:30:25 -0400
commitded8567215afa498295660d123159f26210e066a (patch)
treee73f5602063ffc2a69658392c9497b61d73d6dd3 /src
parent873e6bf836b2dd2d7dedc905c0386b7cd66c0d85 (diff)
downloadbfs-ded8567215afa498295660d123159f26210e066a.tar.xz
diag: New bfs_abort() and bfs_bug() macros
Diffstat (limited to 'src')
-rw-r--r--src/diag.c10
-rw-r--r--src/diag.h24
2 files changed, 33 insertions, 1 deletions
diff --git a/src/diag.c b/src/diag.c
index 53db98e..d7ffaa6 100644
--- a/src/diag.c
+++ b/src/diag.c
@@ -9,7 +9,15 @@
#include <assert.h>
#include <errno.h>
#include <stdarg.h>
-#include <stddef.h>
+#include <stdlib.h>
+
+noreturn void bfs_abortf(const char *format, ...) {
+ va_list args;
+ va_start(args, format);
+ vfprintf(stderr, format, args);
+ va_end(args);
+ abort();
+}
void bfs_perror(const struct bfs_ctx *ctx, const char *str) {
bfs_error(ctx, "%s: %m.\n", str);
diff --git a/src/diag.h b/src/diag.h
index a086942..a3e0e1d 100644
--- a/src/diag.h
+++ b/src/diag.h
@@ -22,6 +22,30 @@
# define BFS_STATIC_ASSERT(expr, msg, ...) _Static_assert(expr, msg)
#endif
+/**
+ * Print a message to standard error and abort.
+ */
+BFS_FORMATTER(1, 2)
+noreturn void bfs_abortf(const char *format, ...);
+
+/**
+ * Unconditional abort with a message.
+ */
+#define bfs_abort(...) \
+ BFS_ABORT(__VA_ARGS__, "\n")
+
+#define BFS_ABORT(format, ...) \
+ bfs_abortf((format) ? "%s: %s:%d:%s(): " format "%s" : "", BFS_COMMAND, __FILE__, __LINE__, __func__, __VA_ARGS__)
+
+/**
+ * Abort in debug builds; no-op in release builds.
+ */
+#ifdef NDEBUG
+# define bfs_bug(...) ((void)0)
+#else
+# define bfs_bug bfs_abort
+#endif
+
struct bfs_expr;
/**