From: Ondřej Surý Date: Mon, 4 Oct 2021 15:14:53 +0000 (+0200) Subject: Improve STATIC_ASSERT macro for older compilers X-Git-Tag: v9.17.19~8^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=804ec1bcaa413ec066f687a099c105d38025198b;p=thirdparty%2Fbind9.git Improve STATIC_ASSERT macro for older compilers 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 --- diff --git a/lib/isc/include/isc/util.h b/lib/isc/include/isc/util.h index 07e7af71ea3..0c240ea5012 100644 --- a/lib/isc/include/isc/util.h +++ b/lib/isc/include/isc/util.h @@ -228,7 +228,12 @@ #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