public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Do not define static_assert or thread_local in headers for C2x
@ 2022-09-07 15:15 Joseph Myers
  2022-09-07 18:15 ` Florian Weimer
  0 siblings, 1 reply; 2+ messages in thread
From: Joseph Myers @ 2022-09-07 15:15 UTC (permalink / raw)
  To: libc-alpha

C2x makes static_assert and thread_local into keywords, removing the
definitions as macros in assert.h and threads.h.  Thus, disable those
macros in those glibc headers for C2x.

The disabling is done based on a combination of language version and
__GNUC_PREREQ, *not* based on __GLIBC_USE (ISOC2X), on the principle
that users of the header (when requesting C11 or later APIs - not
assert.h for C99 and older API versions) should always have the names
static_assert or thread_local available after inclusion of the header,
whether as a keyword or as a macro.  Thus, when using a compiler
without the keywords (whether an older compiler, possibly in C2x mode,
or _GNU_SOURCE with any compiler but in an older language mode, for
example) the macros should be defined, even when C2x APIs have been
requested.  The __GNUC_PREREQ conditionals here may well need updating
with the versions of other compilers that gained support for these
keywords in C2x mode.

Tested for x86_64.

diff --git a/assert/assert.h b/assert/assert.h
index 67f1cce5e9..b438d8eeb7 100644
--- a/assert/assert.h
+++ b/assert/assert.h
@@ -135,7 +135,11 @@ __END_DECLS
 #endif /* NDEBUG.  */
 
 
-#if defined __USE_ISOC11 && !defined __cplusplus
+#if (defined __USE_ISOC11			\
+     && (!defined __STDC_VERSION__		\
+	 || __STDC_VERSION__ <= 201710L		\
+	 || !__GNUC_PREREQ (13, 0))		\
+     && !defined __cplusplus)
 # undef static_assert
 # define static_assert _Static_assert
 #endif
diff --git a/sysdeps/pthread/threads.h b/sysdeps/pthread/threads.h
index 687b45c610..13c8f35768 100644
--- a/sysdeps/pthread/threads.h
+++ b/sysdeps/pthread/threads.h
@@ -27,7 +27,9 @@ __BEGIN_DECLS
 #include <bits/thread-shared-types.h>
 #include <bits/types/struct_timespec.h>
 
-#ifndef __cplusplus
+#if (!defined __STDC_VERSION__				\
+     || __STDC_VERSION__ <= 201710L			\
+     || !__GNUC_PREREQ (13, 0)) && !defined __cplusplus
 # define thread_local _Thread_local
 #endif
 

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Do not define static_assert or thread_local in headers for C2x
  2022-09-07 15:15 Do not define static_assert or thread_local in headers for C2x Joseph Myers
@ 2022-09-07 18:15 ` Florian Weimer
  0 siblings, 0 replies; 2+ messages in thread
From: Florian Weimer @ 2022-09-07 18:15 UTC (permalink / raw)
  To: Joseph Myers; +Cc: libc-alpha

* Joseph Myers:

> C2x makes static_assert and thread_local into keywords, removing the
> definitions as macros in assert.h and threads.h.  Thus, disable those
> macros in those glibc headers for C2x.
>
> The disabling is done based on a combination of language version and
> __GNUC_PREREQ, *not* based on __GLIBC_USE (ISOC2X), on the principle
> that users of the header (when requesting C11 or later APIs - not
> assert.h for C99 and older API versions) should always have the names
> static_assert or thread_local available after inclusion of the header,
> whether as a keyword or as a macro.  Thus, when using a compiler
> without the keywords (whether an older compiler, possibly in C2x mode,
> or _GNU_SOURCE with any compiler but in an older language mode, for
> example) the macros should be defined, even when C2x APIs have been
> requested.  The __GNUC_PREREQ conditionals here may well need updating
> with the versions of other compilers that gained support for these
> keywords in C2x mode.

Looks reasonable, thanks.

The internal definition of _Static_assert in include/sys/cdefs.h is not
affected.

Florian


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-09-07 18:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-07 15:15 Do not define static_assert or thread_local in headers for C2x Joseph Myers
2022-09-07 18:15 ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).