From e75e612900b8652f15bc7d84241c5190183add51 Mon Sep 17 00:00:00 2001 From: Joshua Root Date: Tue, 12 Jul 2022 13:27:19 +1000 Subject: [PATCH 1/9] gh-99069: Consolidate checks for static_assert Several platforms don't define the static_assert macro despite having compiler support for the _Static_assert keyword. The macro needs to be defined since it is used unconditionally in the Python code. So it should always be safe to define it if undefined and not in C++11 (or later) mode. Hence, remove the checks for particular platforms or libc versions, and just define static_assert anytime it needs to be defined but isn't. That way, all platforms that need the fix will get it, regardless of whether someone specifically thought of them. Also document that certain macOS versions are among the platforms that need this. --- Include/pymacro.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index e37cda44c5ebf1..b58e6dd2e64c12 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -3,20 +3,18 @@ // gh-91782: On FreeBSD 12, if the _POSIX_C_SOURCE and _XOPEN_SOURCE macros are // defined, disables C11 support and does not define -// the static_assert() macro. Define the static_assert() macro in Python until -// suports C11: +// the static_assert() macro. // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255290 -#if defined(__FreeBSD__) && !defined(static_assert) -# define static_assert _Static_assert -#endif -// static_assert is defined in glibc from version 2.16. Before it requires -// compiler support (gcc >= 4.6) and is called _Static_assert. +// macOS <= 10.10 doesn't define static_assert in assert.h at all despite +// having C11 compiler support. + +// static_assert is defined in glibc from version 2.16. Compiler support for +// the C11 _Static_assert keyword is in gcc >= 4.6. + // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. -#if (defined(__GLIBC__) \ - && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ <= 16)) \ - && !(defined(__cplusplus) && __cplusplus >= 201103L) \ - && !defined(static_assert)) +#if !defined(static_assert) \ + && !(defined(__cplusplus) && __cplusplus >= 201103L) # define static_assert _Static_assert #endif From 783604d4f8d8a8d397c3d09d37808bcb7be28c73 Mon Sep 17 00:00:00 2001 From: Joshua Root Date: Tue, 12 Jul 2022 20:12:44 +1000 Subject: [PATCH 2/9] Workaround for MSVC --- Include/pymacro.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index b58e6dd2e64c12..940ce0e5dc5fea 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -12,8 +12,10 @@ // static_assert is defined in glibc from version 2.16. Compiler support for // the C11 _Static_assert keyword is in gcc >= 4.6. +// MSVC makes static_assert a keyword, contrary to the C standard. + // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. -#if !defined(static_assert) \ +#if !defined(static_assert) && !defined(_MSC_VER) \ && !(defined(__cplusplus) && __cplusplus >= 201103L) # define static_assert _Static_assert #endif From 08106d79788005eee02574639b5f64182ce13627 Mon Sep 17 00:00:00 2001 From: Joshua Root Date: Mon, 7 Nov 2022 22:53:47 +1100 Subject: [PATCH 3/9] Indicate blank lines are part of the comment --- Include/pymacro.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index 940ce0e5dc5fea..e1c230ce874a0a 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -5,15 +5,15 @@ // defined, disables C11 support and does not define // the static_assert() macro. // https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255290 - +// // macOS <= 10.10 doesn't define static_assert in assert.h at all despite // having C11 compiler support. - +// // static_assert is defined in glibc from version 2.16. Compiler support for // the C11 _Static_assert keyword is in gcc >= 4.6. - +// // MSVC makes static_assert a keyword, contrary to the C standard. - +// // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. #if !defined(static_assert) && !defined(_MSC_VER) \ && !(defined(__cplusplus) && __cplusplus >= 201103L) From 6c36647eeffc94e48d94e10ed2f899d2948f67c5 Mon Sep 17 00:00:00 2001 From: Joshua Root Date: Mon, 21 Nov 2022 22:59:50 +1100 Subject: [PATCH 4/9] Restrict static_assert define to gcc/clang C11+ --- Include/pymacro.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index e1c230ce874a0a..951b21235b87d1 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -15,8 +15,8 @@ // MSVC makes static_assert a keyword, contrary to the C standard. // // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. -#if !defined(static_assert) && !defined(_MSC_VER) \ - && !(defined(__cplusplus) && __cplusplus >= 201103L) +#if !defined(static_assert) && defined(__GNUC__) \ + && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L # define static_assert _Static_assert #endif From ce67d71353af538122eea1e69cb7021b77fcfdcc Mon Sep 17 00:00:00 2001 From: Joshua Root Date: Sat, 3 Dec 2022 08:53:52 +1100 Subject: [PATCH 5/9] Add more explanation of __STDC_VERSION__ check --- Include/pymacro.h | 1 + 1 file changed, 1 insertion(+) diff --git a/Include/pymacro.h b/Include/pymacro.h index 951b21235b87d1..6a8a179862ca2a 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -15,6 +15,7 @@ // MSVC makes static_assert a keyword, contrary to the C standard. // // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. +// So only define if building as C, not C++. #if !defined(static_assert) && defined(__GNUC__) \ && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L # define static_assert _Static_assert From cdd52ce3f7427f94e9199dda72da56f8da7c9e6b Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sat, 3 Dec 2022 14:43:41 +0100 Subject: [PATCH 6/9] Update Include/pymacro.h --- Include/pymacro.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index 6a8a179862ca2a..1d0fc72951fab5 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -15,7 +15,7 @@ // MSVC makes static_assert a keyword, contrary to the C standard. // // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. -// So only define if building as C, not C++. +// So only define if building as C (if __STDC_VERSION__ is defined), not C++. #if !defined(static_assert) && defined(__GNUC__) \ && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L # define static_assert _Static_assert From a5ceb00aae327b2196c5de74c8afbc2b161db16c Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Mon, 5 Dec 2022 12:59:49 +0100 Subject: [PATCH 7/9] Update Include/pymacro.h --- Include/pymacro.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index 1d0fc72951fab5..f8d65b6394ab85 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -16,7 +16,7 @@ // // In C++ 11 static_assert is a keyword, redefining is undefined behaviour. // So only define if building as C (if __STDC_VERSION__ is defined), not C++. -#if !defined(static_assert) && defined(__GNUC__) \ +#if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \ && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L # define static_assert _Static_assert #endif From 57950a37e87302514d32316d9648e1f4a006512a Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 11 Feb 2023 05:31:07 +0000 Subject: [PATCH 8/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Build/2023-02-11-05-31-05.gh-issue-99069.X4LDvY.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Build/2023-02-11-05-31-05.gh-issue-99069.X4LDvY.rst diff --git a/Misc/NEWS.d/next/Build/2023-02-11-05-31-05.gh-issue-99069.X4LDvY.rst b/Misc/NEWS.d/next/Build/2023-02-11-05-31-05.gh-issue-99069.X4LDvY.rst new file mode 100644 index 00000000000000..ae9b4d59ca8cec --- /dev/null +++ b/Misc/NEWS.d/next/Build/2023-02-11-05-31-05.gh-issue-99069.X4LDvY.rst @@ -0,0 +1 @@ +Extended workaround defining ``static_assert`` when missing from the libc headers to all clang and gcc builds. In particular, this fixes building on macOS <= 10.10. From 3ca0a69a7e8a588dafecb0eb3b46d7e47ec77ef3 Mon Sep 17 00:00:00 2001 From: Joshua Root Date: Thu, 16 Mar 2023 06:25:08 +1100 Subject: [PATCH 9/9] C2x compatibility for static_assert The C2x draft (currently expected to become C23) makes static_assert a keyword to match C++. So only define the macro for up to C17. --- Include/pymacro.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Include/pymacro.h b/Include/pymacro.h index f8d65b6394ab85..342d2a7b844adf 100644 --- a/Include/pymacro.h +++ b/Include/pymacro.h @@ -12,12 +12,14 @@ // static_assert is defined in glibc from version 2.16. Compiler support for // the C11 _Static_assert keyword is in gcc >= 4.6. // -// MSVC makes static_assert a keyword, contrary to the C standard. +// MSVC makes static_assert a keyword in C11-17, contrary to the standards. // -// In C++ 11 static_assert is a keyword, redefining is undefined behaviour. -// So only define if building as C (if __STDC_VERSION__ is defined), not C++. +// In C++11 and C2x, static_assert is a keyword, redefining is undefined +// behaviour. So only define if building as C (if __STDC_VERSION__ is defined), +// not C++, and only for C11-17. #if !defined(static_assert) && (defined(__GNUC__) || defined(__clang__)) \ - && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L \ + && __STDC_VERSION__ <= 201710L # define static_assert _Static_assert #endif