Debug.h: Implement a real STATIC_ASSERT on non-GCC2.

This commit is contained in:
Augustin Cavalier 2018-08-22 00:32:30 -04:00
parent e0d28cdccf
commit 47b079afc9
2 changed files with 26 additions and 14 deletions

View File

@ -90,12 +90,18 @@ extern "C" {
/* STATIC_ASSERT is a compile-time check that can be used to */ /* STATIC_ASSERT is a compile-time check that can be used to */
/* verify static expressions such as: STATIC_ASSERT(sizeof(int64) == 8); */ /* verify static expressions such as: STATIC_ASSERT(sizeof(int64) == 8); */
#if __GNUC__ >= 5 && !defined(__cplusplus)
# define STATIC_ASSERT(x) _Static_assert(x, "static assert failed!")
#elif defined(__cplusplus) && __cplusplus >= 201103L
# define STATIC_ASSERT(x) static_assert(x, "static assert failed!")
#else
# define STATIC_ASSERT(x) \ # define STATIC_ASSERT(x) \
do { \ do { \
struct __staticAssertStruct__ { \ struct __staticAssertStruct__ { \
char __static_assert_failed__[2*(x) - 1]; \ char __static_assert_failed__[2*(x) - 1]; \
}; \ }; \
} while (false) } while (false)
#endif
#endif /* _DEBUG_H */ #endif /* _DEBUG_H */

View File

@ -69,12 +69,18 @@
# define ASSERT_PRINT(x, format, args...) do { } while(0) # define ASSERT_PRINT(x, format, args...) do { } while(0)
#endif #endif
#if __GNUC__ >= 5 && !defined(__cplusplus)
# define STATIC_ASSERT(x) _Static_assert(x, "static assert failed!")
#elif defined(__cplusplus) && __cplusplus >= 201103L
# define STATIC_ASSERT(x) static_assert(x, "static assert failed!")
#else
# define STATIC_ASSERT(x) \ # define STATIC_ASSERT(x) \
do { \ do { \
struct __staticAssertStruct__ { \ struct __staticAssertStruct__ { \
char __static_assert_failed__[2*(x) - 1]; \ char __static_assert_failed__[2*(x) - 1]; \
}; \ }; \
} while (false) } while (false)
#endif
#if KDEBUG #if KDEBUG
# define KDEBUG_ONLY(x) x # define KDEBUG_ONLY(x) x