[Bf-blender-cvs] [2aec7410fd4] experimental-build: Add non-gcc static assert macro.
Bastien Montagne
noreply at git.blender.org
Thu Nov 23 12:53:02 CET 2017
Commit: 2aec7410fd49df11f18471e11ee1b55468abd37e
Author: Bastien Montagne
Date: Thu Nov 23 12:50:29 2017 +0100
Branches: experimental-build
https://developer.blender.org/rB2aec7410fd49df11f18471e11ee1b55468abd37e
Add non-gcc static assert macro.
Adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html
===================================================================
M source/blender/blenlib/BLI_utildefines.h
===================================================================
diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h
index 8f8d7cc3b7f..ca5cdf31de5 100644
--- a/source/blender/blenlib/BLI_utildefines.h
+++ b/source/blender/blenlib/BLI_utildefines.h
@@ -655,15 +655,32 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
/* C++ can't use _Static_assert, expects static_assert() but c++0x only,
* Coverity also errors out. */
-#if (!defined(__cplusplus)) && \
- (!defined(__COVERITY__)) && \
- (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */
+#if (defined(__cplusplus))
+# define BLI_STATIC_ASSERT(a, msg)
+#elif (!defined(__COVERITY__)) && \
+ (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 406)) /* gcc4.6+ only */
# define BLI_STATIC_ASSERT(a, msg) __extension__ _Static_assert(a, msg);
#else
- /* TODO msvc, clang */
-# define BLI_STATIC_ASSERT(a, msg)
+/* Code adapted from http://www.pixelbeat.org/programming/gcc/static_assert.html */
+/* 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. */
+# ifdef (__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
+
#define BLI_STATIC_ASSERT_ALIGN(st, align) \
BLI_STATIC_ASSERT((sizeof(st) % (align) == 0), "Structure must be strictly aligned")
More information about the Bf-blender-cvs
mailing list