Skip to content

[libc++] Support more GCC 15 type_traits builtins #137871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

frobtech
Copy link
Contributor

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

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 llvm#81386 where a previous GCC
version added builtins for __remove_cv and __remove_cvref.

Fixed: llvm#137704
Fixed: llvm#117319
@frobtech frobtech marked this pull request as ready for review April 29, 2025 20:00
@frobtech frobtech requested a review from a team as a code owner April 29, 2025 20:00
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 29, 2025

@llvm/pr-subscribers-libcxx

Author: Roland McGrath (frobtech)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/137871.diff

6 Files Affected:

  • (modified) libcxx/include/__type_traits/add_lvalue_reference.h (+10)
  • (modified) libcxx/include/__type_traits/add_pointer.h (+10)
  • (modified) libcxx/include/__type_traits/add_rvalue_reference.h (+10)
  • (modified) libcxx/include/__type_traits/decay.h (+10)
  • (modified) libcxx/include/__type_traits/remove_all_extents.h (+6)
  • (modified) libcxx/include/__type_traits/remove_extent.h (+6)
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 {

Comment on lines +24 to +27
template <class _Tp>
struct __add_lvalue_reference_gcc {
using type = __add_lvalue_reference(_Tp);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just use add_lvalue_reference in the GCC branch in the end. I think it would be better to just disable this branch for the GCC upgrade and do a proper refactoring afterwards. You've probably just copy-pasted what I've done for remove_cvref, but that's a bit special, since we've got an internal type trait for versions before remove_cvref was added.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GCC 15 has only just been released and GCC 14 is still in wide use and does not have the builtin.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only support the latest version of GCC.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no window where both 14 and 15 work, then you make it impossible for people to upgrade. I need to upgrade to a fixed libc++ today so that I can upgrade to GCC 15 tomorrow. I can't upgrade to GCC 15 today because I need first land an upgraded libc++.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's been the policy for years and nobody had a problem with it. Also, this is not the PR to discuss policy changes. If you think we should change our support policy feel free to write an RFC.

@frobtech frobtech requested a review from a team April 29, 2025 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

libc++ headers vs GCC 15 type traits builtins [libc++] libc++ doesn't compile with gcc 15
3 participants