> Am 30.01.2026 um 18:00 schrieb Torbjörn SVENSSON
> <[email protected]>:
>
> Changes since v1:
>
> - Moved `#undef CONST_CAST2` to right after `#include <memory>`
> - Renamed internal copy of CONST_CAST2 to GTHR_CONST_CAST in gthr-win32.h
> and gthr-dce.h. Also undef GTHR_CONST_CAST at the end of the header
> file to avoid pollution.
>
> Ok for trunk and release/gcc-15?
Ok. Please leave others time to comment.
Thanks,
Richard
>
> --
>
> When building GCC with host=mingw, the following warning can be seen
> multiple times in the build log if the MinGW GCC version is older than
> r13-4881-g9149a5b7e0a:
>
> In file included from /build/gcc/c/c-typeck.cc:27:0:
> /build/gcc/system.h:1172:0: warning: "CONST_CAST2" redefined
> #define CONST_CAST2(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
>
> In file included from
> /usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/include/c++/x86_64-w64-mingw32/bits/gthr.h:148:0,
> from
> /usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/include/c++/ext/atomicity.h:35,
> from
> /usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/include/c++/memory:73,
> from /build/gcc/gcc/system.h:231,
> from /build/gcc/gcc/c/c-typeck.cc:27:
> /usr/lib/gcc/x86_64-w64-mingw32/7.3-win32/include/c++/x86_64-w64-mingw32/bits/gthr-default.h:33:0:
> note: this is the location of the previous definition
> #define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE _q;
> TOTYPE _nq;})(X))._nq)
>
> To ensure that the recent definition is used, always undefine any
> potential define before defining the new macro.
>
> gcc/ChangeLog:
>
> * system.h: Avoid redefinition of CONST_CAST2 macro.
>
> libgcc/ChangeLog:
>
> * config/i386/gthr-win32.h: Don't leak internal copy of
> CONST_CAST2 macro.
> * config/pa/gthr-dce.h: Likewise.
>
> Signed-off-by: Torbjörn SVENSSON <[email protected]>
> ---
> gcc/system.h | 6 ++++++
> libgcc/config/i386/gthr-win32.h | 13 ++++++++-----
> libgcc/config/pa/gthr-dce.h | 13 ++++++++-----
> 3 files changed, 22 insertions(+), 10 deletions(-)
>
> diff --git a/gcc/system.h b/gcc/system.h
> index 588e65453f5..a6233a353d6 100644
> --- a/gcc/system.h
> +++ b/gcc/system.h
> @@ -229,6 +229,12 @@ extern int fprintf_unlocked (FILE *, const char *, ...);
> # include <sstream>
> #endif
> # include <memory>
> +
> +/* Some older versions of GCC used to expose an internal copy of the
> + CONST_CAST2 macro that might be incompatible with the current version of
> the
> + GCC sources. */
> +#undef CONST_CAST2
> +
> # include <cstring>
> # include <initializer_list>
> # include <new>
> diff --git a/libgcc/config/i386/gthr-win32.h b/libgcc/config/i386/gthr-win32.h
> index 633dff7ed73..0846838e655 100644
> --- a/libgcc/config/i386/gthr-win32.h
> +++ b/libgcc/config/i386/gthr-win32.h
> @@ -86,12 +86,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.
> If not, see
> # define __GTHREAD_INLINE static inline
> #endif
>
> -/* Make sure CONST_CAST2 (origin in system.h) is declared. */
> -#ifndef CONST_CAST2
> +/* Make sure GTHR_CONST_CAST (origin in system.h) is declared. */
> +#ifndef GTHR_CONST_CAST
> #ifdef __cplusplus
> -#define CONST_CAST2(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
> +#define GTHR_CONST_CAST(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
> #else
> -#define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE _q;
> TOTYPE _nq;})(X))._nq)
> +#define GTHR_CONST_CAST(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE
> _q; TOTYPE _nq;})(X))._nq)
> #endif
> #endif
>
> @@ -720,7 +720,7 @@ __gthread_getspecific (__gthread_key_t __key)
> __GTHREAD_WIN32_INLINE int
> __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
> {
> - if (TlsSetValue (__key, CONST_CAST2(void *, const void *, __ptr)))
> + if (TlsSetValue (__key, GTHR_CONST_CAST(void *, const void *, __ptr)))
> return 0;
> else
> return (int) GetLastError ();
> @@ -886,4 +886,7 @@ __gthread_cond_wait_recursive (__gthread_cond_t *__cond,
>
> #endif /* _LIBOBJC */
>
> +/* Don't leak internal macro. */
> +#undef GTHR_CONST_CAST
> +
> #endif /* ! GCC_GTHR_WIN32_H */
> diff --git a/libgcc/config/pa/gthr-dce.h b/libgcc/config/pa/gthr-dce.h
> index b21ea8336c7..e53894ac9c7 100644
> --- a/libgcc/config/pa/gthr-dce.h
> +++ b/libgcc/config/pa/gthr-dce.h
> @@ -36,12 +36,12 @@ see the files COPYING3 and COPYING.RUNTIME respectively.
> If not, see
> DCE threads are based on POSIX threads draft 4, and many things
> have changed since then. */
>
> -/* Make sure CONST_CAST2 (original in system.h) is defined. */
> -#ifndef CONST_CAST2
> +/* Make sure GTHR_CONST_CAST (origin in system.h) is declared. */
> +#ifndef GTHR_CONST_CAST
> #ifdef __cplusplus
> -#define CONST_CAST2(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
> +#define GTHR_CONST_CAST(TOTYPE,FROMTYPE,X) (const_cast<TOTYPE> (X))
> #else
> -#define CONST_CAST2(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE _q;
> TOTYPE _nq;})(X))._nq)
> +#define GTHR_CONST_CAST(TOTYPE,FROMTYPE,X) ((__extension__(union {FROMTYPE
> _q; TOTYPE _nq;})(X))._nq)
> #endif
> #endif
>
> @@ -471,7 +471,7 @@ static inline int
> __gthread_setspecific (__gthread_key_t __key, const void *__ptr)
> {
> return __gthrw_(pthread_setspecific)
> - (__key, CONST_CAST2(void *, const void *, __ptr));
> + (__key, GTHR_CONST_CAST(void *, const void *, __ptr));
> }
>
> static inline void
> @@ -564,5 +564,8 @@ __gthread_recursive_mutex_destroy
> (__gthread_recursive_mutex_t *__mutex)
>
> #endif /* _LIBOBJC */
>
> +/* Don't leak internal macro. */
> +#undef GTHR_CONST_CAST
> +
> #endif
> #endif /* ! GCC_GTHR_DCE_H */
> --
> 2.43.0
>