[libcxx-commits] [libcxx] d30f6bc - [libc++][NFC] Refactor __libcpp_datasizeof to be a variable template (#87769)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Apr 29 02:21:31 PDT 2024
Author: Nikolas Klauser
Date: 2024-04-29T11:21:28+02:00
New Revision: d30f6bc5cd9579204864c944f127011be70b2c74
URL: https://github.com/llvm/llvm-project/commit/d30f6bc5cd9579204864c944f127011be70b2c74
DIFF: https://github.com/llvm/llvm-project/commit/d30f6bc5cd9579204864c944f127011be70b2c74.diff
LOG: [libc++][NFC] Refactor __libcpp_datasizeof to be a variable template (#87769)
This decreases memory consumption and compiles times slightly and
removes a bit of boilderplate.
Added:
Modified:
libcxx/include/__string/constexpr_c_functions.h
libcxx/include/__type_traits/datasizeof.h
libcxx/test/libcxx/type_traits/datasizeof.compile.pass.cpp
libcxx/test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp
libcxx/test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp
libcxx/test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/include/__string/constexpr_c_functions.h b/libcxx/include/__string/constexpr_c_functions.h
index 72c6ce69b60bb6..4da8542e380762 100644
--- a/libcxx/include/__string/constexpr_c_functions.h
+++ b/libcxx/include/__string/constexpr_c_functions.h
@@ -224,7 +224,7 @@ __constexpr_memmove(_Tp* __dest, _Up* __src, __element_count __n) {
std::__assign_trivially_copyable(__dest[__i], __src[__i]);
}
} else if (__count > 0) {
- ::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __libcpp_datasizeof<_Tp>::value);
+ ::__builtin_memmove(__dest, __src, (__count - 1) * sizeof(_Tp) + __datasizeof_v<_Tp>);
}
return __dest;
}
diff --git a/libcxx/include/__type_traits/datasizeof.h b/libcxx/include/__type_traits/datasizeof.h
index 3a8b1516010731..54fde242ebcde1 100644
--- a/libcxx/include/__type_traits/datasizeof.h
+++ b/libcxx/include/__type_traits/datasizeof.h
@@ -26,39 +26,38 @@
_LIBCPP_BEGIN_NAMESPACE_STD
-template <class _Tp>
-struct __libcpp_datasizeof {
#if __has_extension(datasizeof)
- static const size_t value = __datasizeof(_Tp);
+template <class _Tp>
+inline const size_t __datasizeof_v = __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_;
- };
+template <class _Tp>
+struct _FirstPaddingByte {
+ [[__no_unique_address__]] _Tp __v_;
+ char __first_padding_byte_;
+};
# else
- template <bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
- struct _FirstPaddingByte : _Tp {
- char __first_padding_byte_;
- };
+template <class _Tp, bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
+struct _FirstPaddingByte : _Tp {
+ char __first_padding_byte_;
+};
- template <>
- struct _FirstPaddingByte<true> {
- _Tp __v_;
- char __first_padding_byte_;
- };
+template <class _Tp>
+struct _FirstPaddingByte<_Tp, true> {
+ _Tp __v_;
+ char __first_padding_byte_;
+};
# 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.
- _LIBCPP_DIAGNOSTIC_PUSH
- _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
- static const size_t value = offsetof(_FirstPaddingByte<>, __first_padding_byte_);
- _LIBCPP_DIAGNOSTIC_POP
+// _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
+// the use as an extension.
+_LIBCPP_DIAGNOSTIC_PUSH
+_LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
+template <class _Tp>
+inline const size_t __datasizeof_v = offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);
+_LIBCPP_DIAGNOSTIC_POP
#endif // __has_extension(datasizeof)
-};
_LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/test/libcxx/type_traits/datasizeof.compile.pass.cpp b/libcxx/test/libcxx/type_traits/datasizeof.compile.pass.cpp
index 881b0bd8519098..03dd0f6eac53a2 100644
--- a/libcxx/test/libcxx/type_traits/datasizeof.compile.pass.cpp
+++ b/libcxx/test/libcxx/type_traits/datasizeof.compile.pass.cpp
@@ -9,10 +9,10 @@
#include <__type_traits/datasizeof.h>
#include <cstdint>
-static_assert(std::__libcpp_datasizeof<std::int8_t>::value == 1, "");
-static_assert(std::__libcpp_datasizeof<std::int16_t>::value == 2, "");
-static_assert(std::__libcpp_datasizeof<std::int32_t>::value == 4, "");
-static_assert(std::__libcpp_datasizeof<std::int64_t>::value == 8, "");
+static_assert(std::__datasizeof_v<std::int8_t> == 1, "");
+static_assert(std::__datasizeof_v<std::int16_t> == 2, "");
+static_assert(std::__datasizeof_v<std::int32_t> == 4, "");
+static_assert(std::__datasizeof_v<std::int64_t> == 8, "");
struct OneBytePadding {
OneBytePadding() {}
@@ -22,9 +22,9 @@ struct OneBytePadding {
};
#if defined(_WIN32) && !defined(__MINGW32__)
-static_assert(std::__libcpp_datasizeof<OneBytePadding>::value == 4, "");
+static_assert(std::__datasizeof_v<OneBytePadding> == 4, "");
#else
-static_assert(std::__libcpp_datasizeof<OneBytePadding>::value == 3, "");
+static_assert(std::__datasizeof_v<OneBytePadding> == 3, "");
#endif
struct InBetweenPadding {
@@ -35,4 +35,4 @@ struct InBetweenPadding {
std::int16_t c;
};
-static_assert(std::__libcpp_datasizeof<InBetweenPadding>::value == 8, "");
+static_assert(std::__datasizeof_v<InBetweenPadding> == 8, "");
diff --git a/libcxx/test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp b/libcxx/test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp
index cf1909b9287323..580c0f4ae10c7e 100644
--- a/libcxx/test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp
+++ b/libcxx/test/libcxx/utilities/expected/expected.expected/no_unique_address.compile.pass.cpp
@@ -47,28 +47,28 @@ static_assert(sizeof(std::expected<B, B>) == sizeof(B));
// Check that `expected`'s datasize is large enough for the parameter type(s).
static_assert(sizeof(std::expected<BoolWithPadding, Empty>) ==
- std::__libcpp_datasizeof<std::expected<BoolWithPadding, Empty>>::value);
+ std::__datasizeof_v<std::expected<BoolWithPadding, Empty>>);
static_assert(sizeof(std::expected<Empty, BoolWithPadding>) ==
- std::__libcpp_datasizeof<std::expected<Empty, BoolWithPadding>>::value);
+ std::__datasizeof_v<std::expected<Empty, BoolWithPadding>>);
// In this case, there should be tail padding in the `expected` because `A`
// itself does _not_ have tail padding.
-static_assert(sizeof(std::expected<A, A>) > std::__libcpp_datasizeof<std::expected<A, A>>::value);
+static_assert(sizeof(std::expected<A, A>) > std::__datasizeof_v<std::expected<A, A>>);
// Test with some real types.
static_assert(sizeof(std::expected<std::optional<int>, int>) == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<std::optional<int>, int>>::value == 8);
+static_assert(std::__datasizeof_v<std::expected<std::optional<int>, int>> == 8);
static_assert(sizeof(std::expected<int, std::optional<int>>) == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<int, std::optional<int>>>::value == 8);
+static_assert(std::__datasizeof_v<std::expected<int, std::optional<int>>> == 8);
static_assert(sizeof(std::expected<int, int>) == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<int, int>>::value == 5);
+static_assert(std::__datasizeof_v<std::expected<int, int>> == 5);
// clang-format off
-static_assert(std::__libcpp_datasizeof<int>::value == 4);
-static_assert(std::__libcpp_datasizeof<std::expected<int, int>>::value == 5);
-static_assert(std::__libcpp_datasizeof<std::expected<std::expected<int, int>, int>>::value == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<std::expected<std::expected<int, int>, int>, int>>::value == 9);
-static_assert(std::__libcpp_datasizeof<std::expected<std::expected<std::expected<std::expected<int, int>, int>, int>, int>>::value == 12);
+static_assert(std::__datasizeof_v<int> == 4);
+static_assert(std::__datasizeof_v<std::expected<int, int>> == 5);
+static_assert(std::__datasizeof_v<std::expected<std::expected<int, int>, int>> == 8);
+static_assert(std::__datasizeof_v<std::expected<std::expected<std::expected<int, int>, int>, int>> == 9);
+static_assert(std::__datasizeof_v<std::expected<std::expected<std::expected<std::expected<int, int>, int>, int>, int>> == 12);
// clang-format on
diff --git a/libcxx/test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp b/libcxx/test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp
index fdee8b71e5d983..27da03c54ac44e 100644
--- a/libcxx/test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp
+++ b/libcxx/test/libcxx/utilities/expected/expected.void/no_unique_address.compile.pass.cpp
@@ -45,23 +45,23 @@ static_assert(sizeof(std::expected<void, B>) == sizeof(B));
// Check that `expected`'s datasize is large enough for the parameter type(s).
static_assert(sizeof(std::expected<void, BoolWithPadding>) ==
- std::__libcpp_datasizeof<std::expected<void, BoolWithPadding>>::value);
+ std::__datasizeof_v<std::expected<void, BoolWithPadding>>);
// In this case, there should be tail padding in the `expected` because `A`
// itself does _not_ have tail padding.
-static_assert(sizeof(std::expected<void, A>) > std::__libcpp_datasizeof<std::expected<void, A>>::value);
+static_assert(sizeof(std::expected<void, A>) > std::__datasizeof_v<std::expected<void, A>>);
// Test with some real types.
static_assert(sizeof(std::expected<void, std::optional<int>>) == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<void, std::optional<int>>>::value == 8);
+static_assert(std::__datasizeof_v<std::expected<void, std::optional<int>>> == 8);
static_assert(sizeof(std::expected<void, int>) == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<void, int>>::value == 5);
+static_assert(std::__datasizeof_v<std::expected<void, int>> == 5);
// clang-format off
-static_assert(std::__libcpp_datasizeof<int>::value == 4);
-static_assert(std::__libcpp_datasizeof<std::expected<void, int>>::value == 5);
-static_assert(std::__libcpp_datasizeof<std::expected<void, std::expected<void, int>>>::value == 8);
-static_assert(std::__libcpp_datasizeof<std::expected<void, std::expected<void, std::expected<void, int>>>>::value == 9);
-static_assert(std::__libcpp_datasizeof<std::expected<void, std::expected<void, std::expected<void, std::expected<void, int>>>>>::value == 12);
+static_assert(std::__datasizeof_v<int> == 4);
+static_assert(std::__datasizeof_v<std::expected<void, int>> == 5);
+static_assert(std::__datasizeof_v<std::expected<void, std::expected<void, int>>> == 8);
+static_assert(std::__datasizeof_v<std::expected<void, std::expected<void, std::expected<void, int>>>> == 9);
+static_assert(std::__datasizeof_v<std::expected<void, std::expected<void, std::expected<void, std::expected<void, int>>>>> == 12);
// clang-format on
diff --git a/libcxx/test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp b/libcxx/test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp
index 209e24964807d9..7ba56577d1bb76 100644
--- a/libcxx/test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp
+++ b/libcxx/test/std/containers/sequences/array/size_and_alignment.compile.pass.cpp
@@ -46,7 +46,7 @@ void test_type() {
static_assert(!std::is_empty<Array>::value, "");
// Make sure empty arrays don't have padding bytes
- LIBCPP_STATIC_ASSERT(std::__libcpp_datasizeof<Array>::value == sizeof(Array), "");
+ LIBCPP_STATIC_ASSERT(std::__datasizeof_v<Array> == sizeof(Array), "");
}
{
More information about the libcxx-commits
mailing list