[libcxx-commits] [libcxx] eee2f02 - [libc++][modules] Get rid of <cstddef> dependency in __datasizeof (#107394)

via libcxx-commits libcxx-commits at lists.llvm.org
Thu Sep 5 09:42:58 PDT 2024


Author: Louis Dionne
Date: 2024-09-05T12:42:55-04:00
New Revision: eee2f02e4e28e54e5a38a1dbbd62ea6780909e16

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

LOG: [libc++][modules] Get rid of <cstddef> dependency in __datasizeof (#107394)

All the compilers we support also provide __builtin_offsetof, so avoid
using this macro and use the builtin directly instead. This allows
removing a dependency on `<cstddef>`, which is heavier than we need.

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 b4cbd1ddfa8deb..0c1ed94f840294 100644
--- a/libcxx/include/__type_traits/datasizeof.h
+++ b/libcxx/include/__type_traits/datasizeof.h
@@ -10,9 +10,9 @@
 #define _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
 
 #include <__config>
+#include <__cstddef/size_t.h>
 #include <__type_traits/is_class.h>
 #include <__type_traits/is_final.h>
-#include <cstddef>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -37,15 +37,15 @@ struct _FirstPaddingByte {
   char __first_padding_byte_;
 };
 
-// _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
-// the use as an extension.
+// _FirstPaddingByte<> is sometimes non-standard layout.
+// It is conditionally-supported to use __builtin_offsetof in that case, but GCC and Clang allow it.
 _LIBCPP_DIAGNOSTIC_PUSH
 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
 template <class _Tp>
-inline const size_t __datasizeof_v = offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);
+inline const size_t __datasizeof_v = __builtin_offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);
 _LIBCPP_DIAGNOSTIC_POP
-#endif   // __has_extension(datasizeof)
+#endif // __has_extension(datasizeof)
 
 _LIBCPP_END_NAMESPACE_STD
 


        


More information about the libcxx-commits mailing list