]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Improve STATIC_ASSERT macro for older compilers
authorOndřej Surý <ondrej@sury.org>
Mon, 4 Oct 2021 15:14:53 +0000 (17:14 +0200)
committerOndřej Surý <ondrej@sury.org>
Tue, 5 Oct 2021 20:13:29 +0000 (22:13 +0200)
Previously, when using compiler without support for static assertions,
the STATIC_ASSERT() macro would be replaced with runtime assertion.
Change the STATIC_ASSERT() macro to a version that's compile time
assertion even when using pre-C11 compilers.

Courtesy of Joseph Quinsey: https://godbolt.org/z/K9RvWS

lib/isc/include/isc/util.h

index 07e7af71ea3aa1dd043ab225916f319fdf34d46c..0c240ea50121751f6a97e36236611d5b96431560 100644 (file)
 #elif __has_feature(c_static_assert)
 #define STATIC_ASSERT(cond, msg) _Static_assert(cond, msg)
 #else /* if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR >= 6) */
-#define STATIC_ASSERT(cond, msg) INSIST(cond)
+
+/* Courtesy of Joseph Quinsey: https://godbolt.org/z/K9RvWS */
+#define TOKENPASTE(a, b)       a##b /* "##" is the "Token Pasting Operator" */
+#define EXPAND_THEN_PASTE(a, b) TOKENPASTE(a, b) /* expand then paste */
+#define STATIC_ASSERT(x, msg) \
+       enum { EXPAND_THEN_PASTE(ASSERT_line_, __LINE__) = 1 / ((msg) && (x)) }
 #endif /* if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR >= 6) */
 
 #ifdef UNIT_TESTING