[libcxx-commits] [libcxx] eeeb963 - [libc++] Use __datasizeof for __libcpp_datasizeof if available (#72104)

via libcxx-commits libcxx-commits at lists.llvm.org
Sat Dec 23 23:45:28 PST 2023


Author: Nikolas Klauser
Date: 2023-12-24T08:45:25+01:00
New Revision: eeeb963841e05e3d53d730a1a46fd9fa9996d409

URL: https://github.com/llvm/llvm-project/commit/eeeb963841e05e3d53d730a1a46fd9fa9996d409
DIFF: https://github.com/llvm/llvm-project/commit/eeeb963841e05e3d53d730a1a46fd9fa9996d409.diff

LOG: [libc++] Use __datasizeof for __libcpp_datasizeof if available (#72104)

This avoids the UB and makes things a bit cheaper in terms of
compile-times.

Added: 
    

Modified: 
    libcxx/include/__type_traits/datasizeof.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__type_traits/datasizeof.h b/libcxx/include/__type_traits/datasizeof.h
index 5688e3293a69eb..3a8b1516010731 100644
--- a/libcxx/include/__type_traits/datasizeof.h
+++ b/libcxx/include/__type_traits/datasizeof.h
@@ -28,13 +28,17 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Tp>
 struct __libcpp_datasizeof {
-#if __has_cpp_attribute(__no_unique_address__)
+#if __has_extension(datasizeof)
+  static const size_t value = __datasizeof(_Tp);
+#else
+// NOLINTNEXTLINE(readability-redundant-preprocessor) This is https://llvm.org/PR64825
+#  if __has_cpp_attribute(__no_unique_address__)
   template <class = char>
   struct _FirstPaddingByte {
     [[__no_unique_address__]] _Tp __v_;
     char __first_padding_byte_;
   };
-#else
+#  else
   template <bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
   struct _FirstPaddingByte : _Tp {
     char __first_padding_byte_;
@@ -45,7 +49,7 @@ struct __libcpp_datasizeof {
     _Tp __v_;
     char __first_padding_byte_;
   };
-#endif
+#  endif // __has_cpp_attribute(__no_unique_address__)
 
   // _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
   // the use as an extension.
@@ -53,6 +57,7 @@ struct __libcpp_datasizeof {
   _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
   static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
   _LIBCPP_DIAGNOSTIC_POP
+#endif   // __has_extension(datasizeof)
 };
 
 _LIBCPP_END_NAMESPACE_STD


        


More information about the libcxx-commits mailing list