Mesa (main): util: implement STATIC_ASSERT using c++11 / c11 primitives

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jun 3 07:49:47 UTC 2022


Module: Mesa
Branch: main
Commit: 45fb815a75669b32f6317ba6d53ec9465a4b0ae0
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=45fb815a75669b32f6317ba6d53ec9465a4b0ae0

Author: Erik Faye-Lund <erik.faye-lund at collabora.com>
Date:   Mon May 23 11:53:22 2022 +0200

util: implement STATIC_ASSERT using c++11 / c11 primitives

Since we now require C11 and C++14, we can use the standard
static_asserts from the standard library instead of rolling our own
compiler-specific versions.

To avoid needing scopes around usage in switch cases, keep the
while-wrapping from before. This means it still can't be used outside of
functions, but that should be fine; we should probably just use
static_assert directly in those cases anyway.

Reviewed-by: Alyssa Rosenzweig <alyssa.rosenzweig at collabora.com>
Reviewed-by: Jesse Natalie <jenatali at microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16670>

---

 src/util/macros.h | 22 +++-------------------
 1 file changed, 3 insertions(+), 19 deletions(-)

diff --git a/src/util/macros.h b/src/util/macros.h
index b48e10667ef..22b18303826 100644
--- a/src/util/macros.h
+++ b/src/util/macros.h
@@ -69,25 +69,9 @@
 /**
  * Static (compile-time) assertion.
  */
-#if defined(_MSC_VER)
-   /* MSVC doesn't like VLA's, but it also dislikes zero length arrays
-    * (which gcc is happy with), so we have to define STATIC_ASSERT()
-    * slightly differently.
-    */
-#  define STATIC_ASSERT(COND) do {         \
-      (void) sizeof(char [(COND) != 0]);   \
-   } while (0)
-#elif defined(__GNUC__)
-   /* This version of STATIC_ASSERT() relies on VLAs.  If COND is
-    * false/zero, the array size will be -1 and we'll get a compile
-    * error
-    */
-#  define STATIC_ASSERT(COND) do {         \
-      (void) sizeof(char [1 - 2*!(COND)]); \
-   } while (0)
-#else
-#  define STATIC_ASSERT(COND) do { } while (0)
-#endif
+#define STATIC_ASSERT(cond) do { \
+   static_assert(cond, #cond); \
+} while (0)
 
 /**
  * container_of - cast a member of a structure out to the containing structure



More information about the mesa-commit mailing list