Skip to content

Commit

Permalink
[libc++][modules] Modularize <cstddef> (llvm#107254)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ldionne authored and VitaNuo committed Sep 12, 2024
1 parent 23b9bff commit b8c345c
Show file tree
Hide file tree
Showing 84 changed files with 306 additions and 124 deletions.
5 changes: 5 additions & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ set(files
__coroutine/coroutine_traits.h
__coroutine/noop_coroutine_handle.h
__coroutine/trivial_awaitables.h
__cstddef/byte.h
__cstddef/max_align_t.h
__cstddef/nullptr_t.h
__cstddef/ptrdiff_t.h
__cstddef/size_t.h
__debug_utils/randomize_range.h
__debug_utils/sanitizers.h
__debug_utils/strict_weak_ordering_check.h
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__algorithm/ranges_minmax.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <__ranges/access.h>
#include <__ranges/concepts.h>
#include <__type_traits/desugars_to.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_reference.h>
#include <__type_traits/is_trivially_copyable.h>
#include <__type_traits/remove_cvref.h>
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/__atomic/atomic.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include <__config>
#include <__functional/operations.h>
#include <__memory/addressof.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_floating_point.h>
#include <__type_traits/is_function.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_same.h>
#include <__type_traits/remove_const.h>
#include <__type_traits/remove_pointer.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__charconv/to_chars_integral.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <__system_error/errc.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_same.h>
#include <__type_traits/make_32_64_or_128_bit.h>
#include <__type_traits/make_unsigned.h>
Expand Down
84 changes: 84 additions & 0 deletions libcxx/include/__cstddef/byte.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//===---------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef _LIBCPP___CSTDDEF_BYTE_H
#define _LIBCPP___CSTDDEF_BYTE_H

#include <__config>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_integral.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

#if _LIBCPP_STD_VER >= 17
namespace std { // purposefully not versioned

enum class byte : unsigned char {};

_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator|(byte __lhs, byte __rhs) noexcept {
return static_cast<byte>(
static_cast<unsigned char>(static_cast<unsigned int>(__lhs) | static_cast<unsigned int>(__rhs)));
}

_LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator|=(byte& __lhs, byte __rhs) noexcept {
return __lhs = __lhs | __rhs;
}

_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator&(byte __lhs, byte __rhs) noexcept {
return static_cast<byte>(
static_cast<unsigned char>(static_cast<unsigned int>(__lhs) & static_cast<unsigned int>(__rhs)));
}

_LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator&=(byte& __lhs, byte __rhs) noexcept {
return __lhs = __lhs & __rhs;
}

_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator^(byte __lhs, byte __rhs) noexcept {
return static_cast<byte>(
static_cast<unsigned char>(static_cast<unsigned int>(__lhs) ^ static_cast<unsigned int>(__rhs)));
}

_LIBCPP_HIDE_FROM_ABI inline constexpr byte& operator^=(byte& __lhs, byte __rhs) noexcept {
return __lhs = __lhs ^ __rhs;
}

_LIBCPP_HIDE_FROM_ABI inline constexpr byte operator~(byte __b) noexcept {
return static_cast<byte>(static_cast<unsigned char>(~static_cast<unsigned int>(__b)));
}

template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI constexpr byte& operator<<=(byte& __lhs, _Integer __shift) noexcept {
return __lhs = __lhs << __shift;
}

template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI constexpr byte operator<<(byte __lhs, _Integer __shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) << __shift));
}

template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI constexpr byte& operator>>=(byte& __lhs, _Integer __shift) noexcept {
return __lhs = __lhs >> __shift;
}

template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
_LIBCPP_HIDE_FROM_ABI constexpr byte operator>>(byte __lhs, _Integer __shift) noexcept {
return static_cast<byte>(static_cast<unsigned char>(static_cast<unsigned int>(__lhs) >> __shift));
}

template <class _Integer, __enable_if_t<is_integral<_Integer>::value, int> = 0>
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr _Integer to_integer(byte __b) noexcept {
return static_cast<_Integer>(__b);
}

} // namespace std
#endif // _LIBCPP_STD_VER >= 17

#endif // _LIBCPP___CSTDDEF_BYTE_H
27 changes: 27 additions & 0 deletions libcxx/include/__cstddef/max_align_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//===---------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef _LIBCPP___CSTDDEF_MAX_ALIGN_T_H
#define _LIBCPP___CSTDDEF_MAX_ALIGN_T_H

#include <__config>
#include <stddef.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if !defined(_LIBCPP_CXX03_LANG)
using ::max_align_t _LIBCPP_USING_IF_EXISTS;
#endif

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___CSTDDEF_MAX_ALIGN_T_H
25 changes: 25 additions & 0 deletions libcxx/include/__cstddef/nullptr_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===---------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef _LIBCPP___CSTDDEF_NULLPTR_T_H
#define _LIBCPP___CSTDDEF_NULLPTR_T_H

#include <__config>
#include <stddef.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

using ::nullptr_t;

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___CSTDDEF_NULLPTR_T_H
25 changes: 25 additions & 0 deletions libcxx/include/__cstddef/ptrdiff_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===---------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef _LIBCPP___CSTDDEF_PTRDIFF_T_H
#define _LIBCPP___CSTDDEF_PTRDIFF_T_H

#include <__config>
#include <stddef.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___CSTDDEF_PTRDIFF_T_H
25 changes: 25 additions & 0 deletions libcxx/include/__cstddef/size_t.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===---------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===---------------------------------------------------------------------===//

#ifndef _LIBCPP___CSTDDEF_SIZE_T_H
#define _LIBCPP___CSTDDEF_SIZE_T_H

#include <__config>
#include <stddef.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

using ::size_t _LIBCPP_USING_IF_EXISTS;

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___CSTDDEF_SIZE_T_H
2 changes: 2 additions & 0 deletions libcxx/include/__exception/nested_exception.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <__exception/exception_ptr.h>
#include <__memory/addressof.h>
#include <__type_traits/decay.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_base_of.h>
#include <__type_traits/is_class.h>
#include <__type_traits/is_constructible.h>
Expand Down
3 changes: 2 additions & 1 deletion libcxx/include/__fwd/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#define _LIBCPP___FWD_ARRAY_H

#include <__config>
#include <cstddef>
#include <__cstddef/size_t.h>
#include <__type_traits/integral_constant.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__fwd/complex.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define _LIBCPP___FWD_COMPLEX_H

#include <__config>
#include <cstddef>
#include <__cstddef/size_t.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__fwd/pair.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#define _LIBCPP___FWD_PAIR_H

#include <__config>
#include <__cstddef/size_t.h>
#include <__fwd/tuple.h>
#include <cstddef>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__fwd/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define _LIBCPP___FWD_SPAN_H

#include <__config>
#include <cstddef>
#include <__cstddef/size_t.h>
#include <limits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__fwd/subrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

#include <__concepts/copyable.h>
#include <__config>
#include <__cstddef/size_t.h>
#include <__iterator/concepts.h>
#include <cstddef>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/__fwd/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#define _LIBCPP___FWD_TUPLE_H

#include <__config>
#include <cstddef>
#include <__cstddef/size_t.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__iterator/concepts.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <__memory/pointer_traits.h>
#include <__type_traits/add_pointer.h>
#include <__type_traits/common_reference.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_pointer.h>
#include <__type_traits/is_primary_template.h>
#include <__type_traits/is_reference.h>
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/__iterator/iterator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <__type_traits/common_reference.h>
#include <__type_traits/conditional.h>
#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_convertible.h>
#include <__type_traits/is_object.h>
#include <__type_traits/is_primary_template.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__iterator/wrap_iter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <__memory/addressof.h>
#include <__memory/pointer_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_convertible.h>
#include <cstddef>

Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__mdspan/layout_stride.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <__type_traits/common_type.h>
#include <__type_traits/is_constructible.h>
#include <__type_traits/is_convertible.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_nothrow_constructible.h>
#include <__type_traits/is_same.h>
#include <__utility/as_const.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__memory/pointer_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <__type_traits/conditional.h>
#include <__type_traits/conjunction.h>
#include <__type_traits/decay.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_class.h>
#include <__type_traits/is_function.h>
#include <__type_traits/is_void.h>
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/__memory/shared_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include <__type_traits/conditional.h>
#include <__type_traits/conjunction.h>
#include <__type_traits/disjunction.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_array.h>
#include <__type_traits/is_bounded_array.h>
#include <__type_traits/is_constructible.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__memory/unique_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <__type_traits/common_type.h>
#include <__type_traits/conditional.h>
#include <__type_traits/dependent_type.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_array.h>
#include <__type_traits/is_assignable.h>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__memory/uses_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#define _LIBCPP___MEMORY_USES_ALLOCATOR_H

#include <__config>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_convertible.h>
#include <cstddef>

Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__random/mersenne_twister_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <__algorithm/min.h>
#include <__config>
#include <__random/is_seed_sequence.h>
#include <__type_traits/enable_if.h>
#include <cstddef>
#include <cstdint>
#include <iosfwd>
Expand Down
2 changes: 2 additions & 0 deletions libcxx/include/__random/seed_seq.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <__algorithm/max.h>
#include <__config>
#include <__iterator/iterator_traits.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_integral.h>
#include <__type_traits/is_unsigned.h>
#include <cstdint>
#include <initializer_list>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__random/subtract_with_carry_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <__config>
#include <__random/is_seed_sequence.h>
#include <__random/linear_congruential_engine.h>
#include <__type_traits/enable_if.h>
#include <cstddef>
#include <cstdint>
#include <iosfwd>
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/__ranges/subrange.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <__tuple/tuple_size.h>
#include <__type_traits/conditional.h>
#include <__type_traits/decay.h>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_pointer.h>
#include <__type_traits/is_reference.h>
#include <__type_traits/make_unsigned.h>
Expand Down
Loading

0 comments on commit b8c345c

Please sign in to comment.