Skip to content

Commit

Permalink
Compat: implement static_assert() shim for pre-C11
Browse files Browse the repository at this point in the history
  • Loading branch information
intelfx authored and BenBE committed Apr 23, 2023
1 parent ed7eac5 commit 1b640df
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ in the source distribution for its full text.

#include "config.h" // IWYU pragma: keep

#include <assert.h> // IWYU pragma: keep
#include <fcntl.h>
#include <stddef.h>
#include <unistd.h>
Expand Down Expand Up @@ -61,4 +62,26 @@ ssize_t Compat_readlink(openat_arg_t dirfd,
char* buf,
size_t bufsize);

/*
* static_assert() hack for pre-C11
* TODO: drop after moving to -std=c11 or newer
*/

/* C11 guarantees _Static_assert is a keyword */
#if (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 201112L
# if !defined(_Static_assert)
# define _Static_assert(expr, msg) \
extern int (*__Static_assert_function (void)) \
[!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
# endif
#endif

/* C23 will guarantee static_assert is a keyword or a macro */
/* FIXME: replace 202300L with proper value once C23 is published */
#if (defined __STDC_VERSION__ ? __STDC_VERSION__ : 0) < 202300L
# if !defined(static_assert)
# define static_assert(expr, msg) _Static_assert(expr, msg)
# endif
#endif

#endif /* HEADER_Compat */

0 comments on commit 1b640df

Please sign in to comment.