[libcxx-commits] [libcxx] [libc++] Work around new GCC 15 type_traits builtins that can't be used as Clang's can (PR #137871)
Roland McGrath via libcxx-commits
libcxx-commits at lists.llvm.org
Tue May 13 11:53:03 PDT 2025
https://github.com/frobtech updated https://github.com/llvm/llvm-project/pull/137871
>From 8d2d49da13e685b3a2b415bc6b433b4766a8548f Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Tue, 29 Apr 2025 12:51:53 -0700
Subject: [PATCH 1/2] [libc++] Support more GCC 15 type_traits builtins
GCC 15 has added builtins for various C++ type traits that Clang
already had. Since __has_builtin(...) now finds these, the #if
branches previously only used for Clang are now used for GCC 15.
However, GCC 15 requires that these builtins only be used in type
aliases, not in template aliases.
These changes follow the model of #81386 where a previous GCC
version added builtins for __remove_cv and __remove_cvref.
Fixed: #137704
Fixed: #117319
---
libcxx/include/__type_traits/add_lvalue_reference.h | 10 ++++++++++
libcxx/include/__type_traits/add_pointer.h | 10 ++++++++++
libcxx/include/__type_traits/add_rvalue_reference.h | 10 ++++++++++
libcxx/include/__type_traits/decay.h | 10 ++++++++++
libcxx/include/__type_traits/remove_all_extents.h | 6 ++++++
libcxx/include/__type_traits/remove_extent.h | 6 ++++++
6 files changed, 52 insertions(+)
diff --git a/libcxx/include/__type_traits/add_lvalue_reference.h b/libcxx/include/__type_traits/add_lvalue_reference.h
index 5e65477058e5f..f2ba25255fca9 100644
--- a/libcxx/include/__type_traits/add_lvalue_reference.h
+++ b/libcxx/include/__type_traits/add_lvalue_reference.h
@@ -20,8 +20,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if __has_builtin(__add_lvalue_reference)
+# if defined(_LIBCPP_COMPILER_GCC)
+template <class _Tp>
+struct __add_lvalue_reference_gcc {
+ using type = __add_lvalue_reference(_Tp);
+};
+
+template <class _Tp>
+using __add_lvalue_reference_t _LIBCPP_NODEBUG = typename __add_lvalue_reference_gcc<_Tp>::type;
+# else
template <class _Tp>
using __add_lvalue_reference_t _LIBCPP_NODEBUG = __add_lvalue_reference(_Tp);
+# endif // defined(_LIBCPP_COMPILER_GCC)
#else
diff --git a/libcxx/include/__type_traits/add_pointer.h b/libcxx/include/__type_traits/add_pointer.h
index a9a51b86abaf0..4441d15750ad7 100644
--- a/libcxx/include/__type_traits/add_pointer.h
+++ b/libcxx/include/__type_traits/add_pointer.h
@@ -22,8 +22,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if !defined(_LIBCPP_WORKAROUND_OBJCXX_COMPILER_INTRINSICS) && __has_builtin(__add_pointer)
+# if defined(_LIBCPP_COMPILER_GCC)
+template <class _Tp>
+struct __add_pointer_gcc {
+ using type = __add_pointer(_Tp);
+};
+
+template <class _Tp>
+using __add_pointer_t _LIBCPP_NODEBUG = typename __add_pointer_gcc<_Tp>::type;
+# else
template <class _Tp>
using __add_pointer_t _LIBCPP_NODEBUG = __add_pointer(_Tp);
+# endif // defined(_LIBCPP_COMPILER_GCC)
#else
template <class _Tp, bool = __is_referenceable_v<_Tp> || is_void<_Tp>::value>
diff --git a/libcxx/include/__type_traits/add_rvalue_reference.h b/libcxx/include/__type_traits/add_rvalue_reference.h
index c51dd54a76789..12b544a7555b7 100644
--- a/libcxx/include/__type_traits/add_rvalue_reference.h
+++ b/libcxx/include/__type_traits/add_rvalue_reference.h
@@ -20,8 +20,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
#if __has_builtin(__add_rvalue_reference)
+# if defined(_LIBCPP_COMPILER_GCC)
+template <class _Tp>
+struct __add_rvalue_reference_gcc {
+ using type = __add_rvalue_reference(_Tp);
+};
+
+template <class _Tp>
+using __add_rvalue_reference_t _LIBCPP_NODEBUG = typename __add_rvalue_reference_gcc<_Tp>::type;
+# else
template <class _Tp>
using __add_rvalue_reference_t _LIBCPP_NODEBUG = __add_rvalue_reference(_Tp);
+# endif // defined(_LIBCPP_COMPILER_GCC)
#else
diff --git a/libcxx/include/__type_traits/decay.h b/libcxx/include/__type_traits/decay.h
index 2e3d05d1e4871..5b821c06edf6d 100644
--- a/libcxx/include/__type_traits/decay.h
+++ b/libcxx/include/__type_traits/decay.h
@@ -26,8 +26,18 @@
_LIBCPP_BEGIN_NAMESPACE_STD
#if __has_builtin(__decay)
+# if defined(_LIBCPP_COMPILER_GCC)
+template <class _Tp>
+struct __decay_gcc {
+ using type = __decay(_Tp);
+};
+
+template <class _Tp>
+using __decay_t _LIBCPP_NODEBUG = typename __decay_gcc<_Tp>::type;
+# else
template <class _Tp>
using __decay_t _LIBCPP_NODEBUG = __decay(_Tp);
+# endif // defined(_LIBCPP_COMPILER_GCC)
template <class _Tp>
struct _LIBCPP_NO_SPECIALIZATIONS decay {
diff --git a/libcxx/include/__type_traits/remove_all_extents.h b/libcxx/include/__type_traits/remove_all_extents.h
index bd7e8060f1a55..ae688610ed968 100644
--- a/libcxx/include/__type_traits/remove_all_extents.h
+++ b/libcxx/include/__type_traits/remove_all_extents.h
@@ -24,8 +24,14 @@ struct _LIBCPP_NO_SPECIALIZATIONS remove_all_extents {
using type _LIBCPP_NODEBUG = __remove_all_extents(_Tp);
};
+# if defined(_LIBCPP_COMPILER_GCC)
+template <class _Tp>
+using __remove_all_extents_t = typename remove_all_extents<_Tp>::type;
+# else
template <class _Tp>
using __remove_all_extents_t _LIBCPP_NODEBUG = __remove_all_extents(_Tp);
+# endif // defined(_LIBCPP_COMPILER_GCC)
+
#else
template <class _Tp>
struct remove_all_extents {
diff --git a/libcxx/include/__type_traits/remove_extent.h b/libcxx/include/__type_traits/remove_extent.h
index 75bb70015b79c..518159547468e 100644
--- a/libcxx/include/__type_traits/remove_extent.h
+++ b/libcxx/include/__type_traits/remove_extent.h
@@ -24,8 +24,14 @@ struct _LIBCPP_NO_SPECIALIZATIONS remove_extent {
using type _LIBCPP_NODEBUG = __remove_extent(_Tp);
};
+# if defined(_LIBCPP_COMPILER_GCC)
+template <class _Tp>
+using __remove_extent_t = typename remove_extent<_Tp>::type;
+# else
template <class _Tp>
using __remove_extent_t _LIBCPP_NODEBUG = __remove_extent(_Tp);
+# endif // defined(_LIBCPP_COMPILER_GCC)
+
#else
template <class _Tp>
struct remove_extent {
>From b8a57c49d25939b866be698c3e8afe22d660995e Mon Sep 17 00:00:00 2001
From: Roland McGrath <mcgrathr at google.com>
Date: Thu, 8 May 2025 19:32:50 -0700
Subject: [PATCH 2/2] Just disable broken __has_builtin(...) branches for GCC
15.
---
libcxx/include/__type_traits/add_lvalue_reference.h | 12 +-----------
libcxx/include/__type_traits/add_pointer.h | 12 +-----------
libcxx/include/__type_traits/add_rvalue_reference.h | 12 +-----------
libcxx/include/__type_traits/decay.h | 12 +-----------
libcxx/include/__type_traits/remove_all_extents.h | 8 +-------
libcxx/include/__type_traits/remove_extent.h | 8 +-------
6 files changed, 6 insertions(+), 58 deletions(-)
diff --git a/libcxx/include/__type_traits/add_lvalue_reference.h b/libcxx/include/__type_traits/add_lvalue_reference.h
index f2ba25255fca9..1eadb0c2e536f 100644
--- a/libcxx/include/__type_traits/add_lvalue_reference.h
+++ b/libcxx/include/__type_traits/add_lvalue_reference.h
@@ -18,20 +18,10 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-#if __has_builtin(__add_lvalue_reference)
+#if __has_builtin(__add_lvalue_reference) && __GNUC__ < 15
-# if defined(_LIBCPP_COMPILER_GCC)
-template <class _Tp>
-struct __add_lvalue_reference_gcc {
- using type = __add_lvalue_reference(_Tp);
-};
-
-template <class _Tp>
-using __add_lvalue_reference_t _LIBCPP_NODEBUG = typename __add_lvalue_reference_gcc<_Tp>::type;
-# else
template <class _Tp>
using __add_lvalue_reference_t _LIBCPP_NODEBUG = __add_lvalue_reference(_Tp);
-# endif // defined(_LIBCPP_COMPILER_GCC)
#else
diff --git a/libcxx/include/__type_traits/add_pointer.h b/libcxx/include/__type_traits/add_pointer.h
index 4441d15750ad7..3328a2b8a43bb 100644
--- a/libcxx/include/__type_traits/add_pointer.h
+++ b/libcxx/include/__type_traits/add_pointer.h
@@ -20,20 +20,10 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-#if !defined(_LIBCPP_WORKAROUND_OBJCXX_COMPILER_INTRINSICS) && __has_builtin(__add_pointer)
+#if !defined(_LIBCPP_WORKAROUND_OBJCXX_COMPILER_INTRINSICS) && __has_builtin(__add_pointer) && __GNUC__ < 15
-# if defined(_LIBCPP_COMPILER_GCC)
-template <class _Tp>
-struct __add_pointer_gcc {
- using type = __add_pointer(_Tp);
-};
-
-template <class _Tp>
-using __add_pointer_t _LIBCPP_NODEBUG = typename __add_pointer_gcc<_Tp>::type;
-# else
template <class _Tp>
using __add_pointer_t _LIBCPP_NODEBUG = __add_pointer(_Tp);
-# endif // defined(_LIBCPP_COMPILER_GCC)
#else
template <class _Tp, bool = __is_referenceable_v<_Tp> || is_void<_Tp>::value>
diff --git a/libcxx/include/__type_traits/add_rvalue_reference.h b/libcxx/include/__type_traits/add_rvalue_reference.h
index 12b544a7555b7..e980e278f4c56 100644
--- a/libcxx/include/__type_traits/add_rvalue_reference.h
+++ b/libcxx/include/__type_traits/add_rvalue_reference.h
@@ -18,20 +18,10 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-#if __has_builtin(__add_rvalue_reference)
+#if __has_builtin(__add_rvalue_reference) && __GNUC__ < 15
-# if defined(_LIBCPP_COMPILER_GCC)
-template <class _Tp>
-struct __add_rvalue_reference_gcc {
- using type = __add_rvalue_reference(_Tp);
-};
-
-template <class _Tp>
-using __add_rvalue_reference_t _LIBCPP_NODEBUG = typename __add_rvalue_reference_gcc<_Tp>::type;
-# else
template <class _Tp>
using __add_rvalue_reference_t _LIBCPP_NODEBUG = __add_rvalue_reference(_Tp);
-# endif // defined(_LIBCPP_COMPILER_GCC)
#else
diff --git a/libcxx/include/__type_traits/decay.h b/libcxx/include/__type_traits/decay.h
index 5b821c06edf6d..4035235b6100b 100644
--- a/libcxx/include/__type_traits/decay.h
+++ b/libcxx/include/__type_traits/decay.h
@@ -25,19 +25,9 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-#if __has_builtin(__decay)
-# if defined(_LIBCPP_COMPILER_GCC)
-template <class _Tp>
-struct __decay_gcc {
- using type = __decay(_Tp);
-};
-
-template <class _Tp>
-using __decay_t _LIBCPP_NODEBUG = typename __decay_gcc<_Tp>::type;
-# else
+#if __has_builtin(__decay) && __GNUC__ < 15
template <class _Tp>
using __decay_t _LIBCPP_NODEBUG = __decay(_Tp);
-# endif // defined(_LIBCPP_COMPILER_GCC)
template <class _Tp>
struct _LIBCPP_NO_SPECIALIZATIONS decay {
diff --git a/libcxx/include/__type_traits/remove_all_extents.h b/libcxx/include/__type_traits/remove_all_extents.h
index ae688610ed968..19dc605055510 100644
--- a/libcxx/include/__type_traits/remove_all_extents.h
+++ b/libcxx/include/__type_traits/remove_all_extents.h
@@ -18,20 +18,14 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-#if __has_builtin(__remove_all_extents)
+#if __has_builtin(__remove_all_extents) && __GNUC__ < 15
template <class _Tp>
struct _LIBCPP_NO_SPECIALIZATIONS remove_all_extents {
using type _LIBCPP_NODEBUG = __remove_all_extents(_Tp);
};
-# if defined(_LIBCPP_COMPILER_GCC)
-template <class _Tp>
-using __remove_all_extents_t = typename remove_all_extents<_Tp>::type;
-# else
template <class _Tp>
using __remove_all_extents_t _LIBCPP_NODEBUG = __remove_all_extents(_Tp);
-# endif // defined(_LIBCPP_COMPILER_GCC)
-
#else
template <class _Tp>
struct remove_all_extents {
diff --git a/libcxx/include/__type_traits/remove_extent.h b/libcxx/include/__type_traits/remove_extent.h
index 518159547468e..f6ad1cc6cb9a9 100644
--- a/libcxx/include/__type_traits/remove_extent.h
+++ b/libcxx/include/__type_traits/remove_extent.h
@@ -18,20 +18,14 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-#if __has_builtin(__remove_extent)
+#if __has_builtin(__remove_extent) && __GNUC__ < 15
template <class _Tp>
struct _LIBCPP_NO_SPECIALIZATIONS remove_extent {
using type _LIBCPP_NODEBUG = __remove_extent(_Tp);
};
-# if defined(_LIBCPP_COMPILER_GCC)
-template <class _Tp>
-using __remove_extent_t = typename remove_extent<_Tp>::type;
-# else
template <class _Tp>
using __remove_extent_t _LIBCPP_NODEBUG = __remove_extent(_Tp);
-# endif // defined(_LIBCPP_COMPILER_GCC)
-
#else
template <class _Tp>
struct remove_extent {
More information about the libcxx-commits
mailing list