summaryrefslogtreecommitdiffstats
path: root/Compat.h
diff options
context:
space:
mode:
authorIvan Shapovalov <intelfx@intelfx.name>2023-03-12 20:08:40 +0400
committerBenBE <BenBE@geshi.org>2023-04-23 16:00:17 +0200
commit1b640dff5759d73458204c87e217dab8759badd9 (patch)
tree860d2dea7e1c204a7c339337ca28fad4bce24d7b /Compat.h
parented7eac5dfe8239f7942102f9d1a4f9ef5210f4d7 (diff)
Compat: implement static_assert() shim for pre-C11
Diffstat (limited to 'Compat.h')
-rw-r--r--Compat.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/Compat.h b/Compat.h
index 1c4794e6..b83fc3d8 100644
--- a/Compat.h
+++ b/Compat.h
@@ -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>
@@ -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 */

© 2014-2024 Faster IT GmbH | imprint | privacy policy