Skip to content

Commit d6832a6

Browse files
authored
[libc++][modules] Modularize <cstddef> (#107254)
Many headers include `<cstddef>` just for size_t, and pulling in additional content (e.g. the traits used for std::byte) is unnecessary. To solve this problem, this patch splits up `<cstddef>` into subcomponents so that headers can include only the parts that they actually require. This has the added benefit of making the modules build a lot stricter with respect to IWYU, and also providing a canonical location where we define `std::size_t` and friends (which were previously defined in multiple headers like `<cstddef>` and `<ctime>`). After this patch, there's still many places in the codebase where we include `<cstddef>` when `<__cstddef/size_t.h>` would be sufficient. This patch focuses on removing `<cstddef>` includes from __type_traits to make these headers non-circular with `<cstddef>`. Additional refactorings can be tackled separately.
1 parent 84cf3a5 commit d6832a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+306
-124
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ set(files
324324
__coroutine/coroutine_traits.h
325325
__coroutine/noop_coroutine_handle.h
326326
__coroutine/trivial_awaitables.h
327+
__cstddef/byte.h
328+
__cstddef/max_align_t.h
329+
__cstddef/nullptr_t.h
330+
__cstddef/ptrdiff_t.h
331+
__cstddef/size_t.h
327332
__debug_utils/randomize_range.h
328333
__debug_utils/sanitizers.h
329334
__debug_utils/strict_weak_ordering_check.h

libcxx/include/__algorithm/ranges_minmax.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <__ranges/access.h>
2525
#include <__ranges/concepts.h>
2626
#include <__type_traits/desugars_to.h>
27+
#include <__type_traits/is_integral.h>
2728
#include <__type_traits/is_reference.h>
2829
#include <__type_traits/is_trivially_copyable.h>
2930
#include <__type_traits/remove_cvref.h>

libcxx/include/__atomic/atomic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
#include <__config>
1717
#include <__functional/operations.h>
1818
#include <__memory/addressof.h>
19+
#include <__type_traits/enable_if.h>
1920
#include <__type_traits/is_floating_point.h>
2021
#include <__type_traits/is_function.h>
22+
#include <__type_traits/is_integral.h>
2123
#include <__type_traits/is_same.h>
2224
#include <__type_traits/remove_const.h>
2325
#include <__type_traits/remove_pointer.h>

libcxx/include/__charconv/to_chars_integral.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <__system_error/errc.h>
2222
#include <__type_traits/enable_if.h>
2323
#include <__type_traits/integral_constant.h>
24+
#include <__type_traits/is_integral.h>
2425
#include <__type_traits/is_same.h>
2526
#include <__type_traits/make_32_64_or_128_bit.h>
2627
#include <__type_traits/make_unsigned.h>

libcxx/include/__cstddef/byte.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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___CSTDDEF_BYTE_H
10+
#define _LIBCPP___CSTDDEF_BYTE_H
11+
12+
#include <__config>
13+
#include <__type_traits/enable_if.h>
14+
#include <__type_traits/is_integral.h>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
# pragma GCC system_header
18+
#endif
19+
20+
#if _LIBCPP_STD_VER >= 17
21+
namespace std { // purposefully not versioned
22+
23+
enum class byte : unsigned char {};
24+
25+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator|(byte __lhs, byte __rhs) noexcept {
26+
return static_cast<byte>(
27+
static_cast<unsigned char>(static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)));
28+
}
29+
30+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept {
31+
return __lhs = __lhs | __rhs;
32+
}
33+
34+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator&(byte __lhs, byte __rhs) noexcept {
35+
return static_cast<byte>(
36+
static_cast<unsigned char>(static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)));
37+
}
38+
39+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept {
40+
return __lhs = __lhs & __rhs;
41+
}
42+
43+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator^(byte __lhs, byte __rhs) noexcept {
44+
return static_cast<byte>(
45+
static_cast<unsigned char>(static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)));
46+
}
47+
48+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept {
49+
return __lhs = __lhs ^ __rhs;
50+
}
51+
52+
_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator~(byte __b) noexcept {
53+
return static_cast<byte>(static_cast<unsigned char>(~static_cast<unsigned int>(__b)));
54+
}
55+
56+
template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
57+
_LIBCPP_HIDE_FROM_ABI constexpr byte& operator<<=(byte& __lhs, _Integer __shift) noexcept {
58+
return __lhs = __lhs << __shift;
59+
}
60+
61+
template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
62+
_LIBCPP_HIDE_FROM_ABI constexpr byte operator<<(byte __lhs, _Integer __shift) noexcept {
63+
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift));
64+
}
65+
66+
template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
67+
_LIBCPP_HIDE_FROM_ABI constexpr byte& operator>>=(byte& __lhs, _Integer __shift) noexcept {
68+
return __lhs = __lhs >> __shift;
69+
}
70+
71+
template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
72+
_LIBCPP_HIDE_FROM_ABI constexpr byte operator>>(byte __lhs, _Integer __shift) noexcept {
73+
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift));
74+
}
75+
76+
template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
77+
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Integer to_integer(byte __b) noexcept {
78+
return static_cast<_Integer>(__b);
79+
}
80+
81+
} // namespace std
82+
#endif // _LIBCPP_STD_VER >= 17
83+
84+
#endif // _LIBCPP___CSTDDEF_BYTE_H
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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___CSTDDEF_MAX_ALIGN_T_H
10+
#define _LIBCPP___CSTDDEF_MAX_ALIGN_T_H
11+
12+
#include <__config>
13+
#include <stddef.h>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
#if !defined(_LIBCPP_CXX03_LANG)
22+
using ::max_align_t _LIBCPP_USING_IF_EXISTS;
23+
#endif
24+
25+
_LIBCPP_END_NAMESPACE_STD
26+
27+
#endif // _LIBCPP___CSTDDEF_MAX_ALIGN_T_H

libcxx/include/__cstddef/nullptr_t.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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___CSTDDEF_NULLPTR_T_H
10+
#define _LIBCPP___CSTDDEF_NULLPTR_T_H
11+
12+
#include <__config>
13+
#include <stddef.h>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
using ::nullptr_t;
22+
23+
_LIBCPP_END_NAMESPACE_STD
24+
25+
#endif // _LIBCPP___CSTDDEF_NULLPTR_T_H

libcxx/include/__cstddef/ptrdiff_t.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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___CSTDDEF_PTRDIFF_T_H
10+
#define _LIBCPP___CSTDDEF_PTRDIFF_T_H
11+
12+
#include <__config>
13+
#include <stddef.h>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
22+
23+
_LIBCPP_END_NAMESPACE_STD
24+
25+
#endif // _LIBCPP___CSTDDEF_PTRDIFF_T_H

libcxx/include/__cstddef/size_t.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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___CSTDDEF_SIZE_T_H
10+
#define _LIBCPP___CSTDDEF_SIZE_T_H
11+
12+
#include <__config>
13+
#include <stddef.h>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
using ::size_t _LIBCPP_USING_IF_EXISTS;
22+
23+
_LIBCPP_END_NAMESPACE_STD
24+
25+
#endif // _LIBCPP___CSTDDEF_SIZE_T_H

libcxx/include/__exception/nested_exception.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <__exception/exception_ptr.h>
1414
#include <__memory/addressof.h>
1515
#include <__type_traits/decay.h>
16+
#include <__type_traits/enable_if.h>
17+
#include <__type_traits/integral_constant.h>
1618
#include <__type_traits/is_base_of.h>
1719
#include <__type_traits/is_class.h>
1820
#include <__type_traits/is_constructible.h>

libcxx/include/__fwd/array.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
#define _LIBCPP___FWD_ARRAY_H
1111

1212
#include <__config>
13-
#include <cstddef>
13+
#include <__cstddef/size_t.h>
14+
#include <__type_traits/integral_constant.h>
1415

1516
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1617
# pragma GCC system_header

libcxx/include/__fwd/complex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define _LIBCPP___FWD_COMPLEX_H
1111

1212
#include <__config>
13-
#include <cstddef>
13+
#include <__cstddef/size_t.h>
1414

1515
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1616
# pragma GCC system_header

libcxx/include/__fwd/pair.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#define _LIBCPP___FWD_PAIR_H
1111

1212
#include <__config>
13+
#include <__cstddef/size_t.h>
1314
#include <__fwd/tuple.h>
14-
#include <cstddef>
1515

1616
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1717
# pragma GCC system_header

libcxx/include/__fwd/span.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define _LIBCPP___FWD_SPAN_H
1212

1313
#include <__config>
14-
#include <cstddef>
14+
#include <__cstddef/size_t.h>
1515
#include <limits>
1616

1717
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)

libcxx/include/__fwd/subrange.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include <__concepts/copyable.h>
1313
#include <__config>
14+
#include <__cstddef/size_t.h>
1415
#include <__iterator/concepts.h>
15-
#include <cstddef>
1616

1717
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1818
# pragma GCC system_header

libcxx/include/__fwd/tuple.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define _LIBCPP___FWD_TUPLE_H
1111

1212
#include <__config>
13-
#include <cstddef>
13+
#include <__cstddef/size_t.h>
1414

1515
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
1616
# pragma GCC system_header

libcxx/include/__iterator/concepts.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <__memory/pointer_traits.h>
3535
#include <__type_traits/add_pointer.h>
3636
#include <__type_traits/common_reference.h>
37+
#include <__type_traits/integral_constant.h>
3738
#include <__type_traits/is_pointer.h>
3839
#include <__type_traits/is_primary_template.h>
3940
#include <__type_traits/is_reference.h>

libcxx/include/__iterator/iterator_traits.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <__type_traits/common_reference.h>
2525
#include <__type_traits/conditional.h>
2626
#include <__type_traits/disjunction.h>
27+
#include <__type_traits/enable_if.h>
28+
#include <__type_traits/integral_constant.h>
2729
#include <__type_traits/is_convertible.h>
2830
#include <__type_traits/is_object.h>
2931
#include <__type_traits/is_primary_template.h>

libcxx/include/__iterator/wrap_iter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <__memory/addressof.h>
1818
#include <__memory/pointer_traits.h>
1919
#include <__type_traits/enable_if.h>
20+
#include <__type_traits/integral_constant.h>
2021
#include <__type_traits/is_convertible.h>
2122
#include <cstddef>
2223

libcxx/include/__mdspan/layout_stride.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <__type_traits/common_type.h>
2626
#include <__type_traits/is_constructible.h>
2727
#include <__type_traits/is_convertible.h>
28+
#include <__type_traits/is_integral.h>
2829
#include <__type_traits/is_nothrow_constructible.h>
2930
#include <__type_traits/is_same.h>
3031
#include <__utility/as_const.h>

libcxx/include/__memory/pointer_traits.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <__type_traits/conditional.h>
1616
#include <__type_traits/conjunction.h>
1717
#include <__type_traits/decay.h>
18+
#include <__type_traits/enable_if.h>
1819
#include <__type_traits/is_class.h>
1920
#include <__type_traits/is_function.h>
2021
#include <__type_traits/is_void.h>

libcxx/include/__memory/shared_ptr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include <__type_traits/conditional.h>
3535
#include <__type_traits/conjunction.h>
3636
#include <__type_traits/disjunction.h>
37+
#include <__type_traits/enable_if.h>
38+
#include <__type_traits/integral_constant.h>
3739
#include <__type_traits/is_array.h>
3840
#include <__type_traits/is_bounded_array.h>
3941
#include <__type_traits/is_constructible.h>

libcxx/include/__memory/unique_ptr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <__type_traits/common_type.h>
2424
#include <__type_traits/conditional.h>
2525
#include <__type_traits/dependent_type.h>
26+
#include <__type_traits/enable_if.h>
2627
#include <__type_traits/integral_constant.h>
2728
#include <__type_traits/is_array.h>
2829
#include <__type_traits/is_assignable.h>

libcxx/include/__memory/uses_allocator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___MEMORY_USES_ALLOCATOR_H
1212

1313
#include <__config>
14+
#include <__type_traits/integral_constant.h>
1415
#include <__type_traits/is_convertible.h>
1516
#include <cstddef>
1617

libcxx/include/__random/mersenne_twister_engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <__algorithm/min.h>
1414
#include <__config>
1515
#include <__random/is_seed_sequence.h>
16+
#include <__type_traits/enable_if.h>
1617
#include <cstddef>
1718
#include <cstdint>
1819
#include <iosfwd>

libcxx/include/__random/seed_seq.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <__algorithm/max.h>
1515
#include <__config>
1616
#include <__iterator/iterator_traits.h>
17+
#include <__type_traits/enable_if.h>
18+
#include <__type_traits/is_integral.h>
1719
#include <__type_traits/is_unsigned.h>
1820
#include <cstdint>
1921
#include <initializer_list>

libcxx/include/__random/subtract_with_carry_engine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <__config>
1515
#include <__random/is_seed_sequence.h>
1616
#include <__random/linear_congruential_engine.h>
17+
#include <__type_traits/enable_if.h>
1718
#include <cstddef>
1819
#include <cstdint>
1920
#include <iosfwd>

libcxx/include/__ranges/subrange.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <__tuple/tuple_size.h>
3434
#include <__type_traits/conditional.h>
3535
#include <__type_traits/decay.h>
36+
#include <__type_traits/integral_constant.h>
3637
#include <__type_traits/is_pointer.h>
3738
#include <__type_traits/is_reference.h>
3839
#include <__type_traits/make_unsigned.h>

0 commit comments

Comments
 (0)