summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2025-03-04 13:05:16 -0500
committerTavian Barnes <tavianator@tavianator.com>2025-03-04 13:35:07 -0500
commit3345a1fc6e87cd910602ec0f428073632d2f1ad1 (patch)
tree664083eed12b89dfd5d976894918cc71ad6dba5d
parent5c5ae3962c46cf4051bcd6d4e2355422e63de42b (diff)
downloadbfs-3345a1fc6e87cd910602ec0f428073632d2f1ad1.tar.xz
diag: Don't leave unused assertion messages in the binary
Rather than hiding them with %.0s, use a ternary to replace them with an empty string if they would be unused.
-rw-r--r--src/diag.h25
-rw-r--r--tests/tests.h16
2 files changed, 27 insertions, 14 deletions
diff --git a/src/diag.h b/src/diag.h
index a8ee911..645dbb1 100644
--- a/src/diag.h
+++ b/src/diag.h
@@ -89,17 +89,26 @@ void bfs_abortf(const char *format, ...);
#endif
/**
+ * Get the default assertion message, if no format string was specified.
+ */
+#define BFS_DIAG_MSG_(format, str) \
+ (sizeof(format) > 1 ? "" : str)
+
+/**
* Unconditional assert.
*/
#define bfs_verify(...) \
bfs_verify_(#__VA_ARGS__, __VA_ARGS__, "", )
#define bfs_verify_(str, cond, format, ...) \
- ((cond) ? (void)0 : bfs_abortf( \
+ ((cond) ? (void)0 : bfs_verify__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__))
+
+#define bfs_verify__(format, ...) \
+ bfs_abortf( \
sizeof(format) > 1 \
- ? BFS_DIAG_FORMAT_("%.0s" format "%s") \
+ ? BFS_DIAG_FORMAT_("%s" format "%s") \
: BFS_DIAG_FORMAT_("Assertion failed: `%s`"), \
- BFS_DIAG_ARGS_(str, __VA_ARGS__)))
+ BFS_DIAG_ARGS_(__VA_ARGS__))
/**
* Unconditional assert, including the last error.
@@ -107,12 +116,16 @@ void bfs_abortf(const char *format, ...);
#define bfs_everify(...) \
bfs_everify_(#__VA_ARGS__, __VA_ARGS__, "", )
+
#define bfs_everify_(str, cond, format, ...) \
- ((cond) ? (void)0 : bfs_abortf( \
+ ((cond) ? (void)0 : bfs_everify__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__))
+
+#define bfs_everify__(format, ...) \
+ bfs_abortf( \
sizeof(format) > 1 \
- ? BFS_DIAG_FORMAT_("%.0s" format "%s: %s") \
+ ? BFS_DIAG_FORMAT_("%s" format "%s: %s") \
: BFS_DIAG_FORMAT_("Assertion failed: `%s`: %s"), \
- BFS_DIAG_ARGS_(str, __VA_ARGS__ errstr(), )))
+ BFS_DIAG_ARGS_(__VA_ARGS__ errstr(), ))
/**
* Assert in debug builds; no-op in release builds.
diff --git a/tests/tests.h b/tests/tests.h
index 8b7d691..d395c7c 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -48,13 +48,13 @@ bool bfs_check_impl(bool result);
bfs_check_(#__VA_ARGS__, __VA_ARGS__, "", )
#define bfs_check_(str, cond, format, ...) \
- bfs_check_impl((cond) || (bfs_check__(str, format, __VA_ARGS__), false))
+ bfs_check_impl((cond) || (bfs_check__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__), false))
-#define bfs_check__(str, format, ...) \
+#define bfs_check__(format, ...) \
bfs_diagf(sizeof(format) > 1 \
- ? BFS_DIAG_FORMAT_("%.0s" format "%s") \
+ ? BFS_DIAG_FORMAT_("%s" format "%s") \
: BFS_DIAG_FORMAT_("Check failed: `%s`"), \
- BFS_DIAG_ARGS_(str, __VA_ARGS__))
+ BFS_DIAG_ARGS_(__VA_ARGS__))
/**
* Check a condition, logging the current error string on failure.
@@ -63,12 +63,12 @@ bool bfs_check_impl(bool result);
bfs_echeck_(#__VA_ARGS__, __VA_ARGS__, "", )
#define bfs_echeck_(str, cond, format, ...) \
- bfs_check_impl((cond) || (bfs_echeck__(str, format, __VA_ARGS__), false))
+ bfs_check_impl((cond) || (bfs_echeck__(format, BFS_DIAG_MSG_(format, str), __VA_ARGS__), false))
-#define bfs_echeck__(str, format, ...) \
+#define bfs_echeck__(format, ...) \
bfs_diagf(sizeof(format) > 1 \
- ? BFS_DIAG_FORMAT_("%.0s" format "%s: %s") \
+ ? BFS_DIAG_FORMAT_("%s" format "%s: %s") \
: BFS_DIAG_FORMAT_("Check failed: `%s`: %s"), \
- BFS_DIAG_ARGS_(str, __VA_ARGS__ errstr(), ))
+ BFS_DIAG_ARGS_(__VA_ARGS__ errstr(), ))
#endif // BFS_TESTS_H