Skip to content

Commit

Permalink
options/internal: fix static asserts for pre-C++04
Browse files Browse the repository at this point in the history
This commit correctly checks whether the C++ standard is new enough, and
if so, uses the standard-defined `static_assert`. In the case of C, gcc
and clang implement `_Static_assert` as an extension irrespective of the
chosen C standard. Otherwise, we fall back to not checking the
condition. We need to do the `extern int` hack in order to not produce
errors in pedantic mode, as a single semicolon or `(void)0` at file
scope give warnings or errors then.
  • Loading branch information
no92 committed Oct 26, 2024
1 parent 1a53ea2 commit 1fac289
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions options/internal/include/bits/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,13 @@ typedef __SIZE_TYPE__ __mlibc_size;
/* Sanity checking. Make sure that we agree with the compiler's ABI. */
/* ---------------------------------------------------------------------------- */

#if defined(__cpp_static_assert)
#if defined(__cplusplus) && defined(__cpp_static_assert) && __cpp_static_assert >= 200410L
# define __MLIBC_STATIC_ASSERT(c, text) static_assert(c, text)
#else
#elif !defined(__cplusplus)
/* _Static_assert is an extension in C89/C99. */
# define __MLIBC_STATIC_ASSERT(c, text) __extension__ _Static_assert(c, text)
#else
# define __MLIBC_STATIC_ASSERT(c, text) extern int __static_assert_unavailable
#endif

#define __MLIBC_CHECK_TYPE(T1, T2) __MLIBC_STATIC_ASSERT(sizeof(T1) == sizeof(T2),\
Expand Down

0 comments on commit 1fac289

Please sign in to comment.