Add non-gcc variant of static assert macro.

Adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html.

Note that this macro just discards error message, so error when building
is much less nice than with gcc's _Static_assert... But error log will
point to right place in code, so should still be OK.
This commit is contained in:
2017-11-23 20:15:15 +01:00
parent 5e13097dc3
commit d423e66d34

View File

@@ -660,10 +660,26 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
(defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */ (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */
# define BLI_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg); # define BLI_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg);
#else #else
/* TODO msvc, clang */ /* Code adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html */
# define BLI_STATIC_ASSERT(a, msg) /* Note we need the two concats below because arguments to ## are not expanded, so we need to
* expand __LINE__ with one indirection before doing the actual concatenation. */
# define ASSERT_CONCAT_(a, b) a##b
# define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
/* These can't be used after statements in c89. */
# if defined(__COUNTER__) /* MSVC */
# define BLI_STATIC_ASSERT(a, msg) \
; enum { ASSERT_CONCAT(static_assert_, __COUNTER__) = 1 / (int)(!!(a)) };
# else /* older gcc, clang... */
/* This can't be used twice on the same line so ensure if using in headers
* that the headers are not included twice (by wrapping in #ifndef...#endif)
* Note it doesn't cause an issue when used on same line of separate modules
* compiled with gcc -combine -fwhole-program. */
# define BLI_STATIC_ASSERT(a, msg) \
; enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1 / (int)(!!(a)) };
# endif
#endif #endif
#define BLI_STATIC_ASSERT_ALIGN(st, align) \ #define BLI_STATIC_ASSERT_ALIGN(st, align) \
BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned") BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")