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@collabora.com>
Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16670>
This commit is contained in:
Erik Faye-Lund 2022-05-23 11:53:22 +02:00 committed by Marge Bot
parent f1023571e8
commit 45fb815a75
1 changed files with 3 additions and 19 deletions

View File

@ -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