xref: /freebsd/contrib/llvm-project/libcxx/include/__type_traits/datasizeof.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
10 #define _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
11 
12 #include <__config>
13 #include <__type_traits/is_class.h>
14 #include <__type_traits/is_final.h>
15 #include <cstddef>
16 
17 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
18 #  pragma GCC system_header
19 #endif
20 
21 // This trait provides the size of a type excluding any tail padding.
22 //
23 // It is useful in contexts where performing an operation using the full size of the class (including padding) may
24 // have unintended side effects, such as overwriting a derived class' member when writing the tail padding of a class
25 // through a pointer-to-base.
26 
27 _LIBCPP_BEGIN_NAMESPACE_STD
28 
29 #if __has_keyword(__datasizeof) || __has_extension(datasizeof)
30 template <class _Tp>
31 inline const size_t __datasizeof_v = __datasizeof(_Tp);
32 #else
33 // NOLINTNEXTLINE(readability-redundant-preprocessor) This is https://llvm.org/PR64825
34 #  if __has_cpp_attribute(__no_unique_address__)
35 template <class _Tp>
36 struct _FirstPaddingByte {
37   [[__no_unique_address__]] _Tp __v_;
38   char __first_padding_byte_;
39 };
40 #  else
41 template <class _Tp, bool = __libcpp_is_final<_Tp>::value || !is_class<_Tp>::value>
42 struct _FirstPaddingByte : _Tp {
43   char __first_padding_byte_;
44 };
45 
46 template <class _Tp>
47 struct _FirstPaddingByte<_Tp, true> {
48   _Tp __v_;
49   char __first_padding_byte_;
50 };
51 #  endif // __has_cpp_attribute(__no_unique_address__)
52 
53 // _FirstPaddingByte<> is sometimes non-standard layout. Using `offsetof` is UB in that case, but GCC and Clang allow
54 // the use as an extension.
55 _LIBCPP_DIAGNOSTIC_PUSH
56 _LIBCPP_CLANG_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
57 _LIBCPP_GCC_DIAGNOSTIC_IGNORED("-Winvalid-offsetof")
58 template <class _Tp>
59 inline const size_t __datasizeof_v = offsetof(_FirstPaddingByte<_Tp>, __first_padding_byte_);
60 _LIBCPP_DIAGNOSTIC_POP
61 #endif   // __has_extension(datasizeof)
62 
63 _LIBCPP_END_NAMESPACE_STD
64 
65 #endif // _LIBCPP___TYPE_TRAITS_DATASIZEOF_H
66