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>

0 commit comments

Comments
 (0)