Skip to content

[libc++] Remove <stddef.h> includes from the granularized <cstddef> headers #114788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

philnik777
Copy link
Contributor

We can define some of these aliases without having to include the system <stddef.h> and there doesn't seem to be much of a reason we shouldn't do it this way.

@philnik777 philnik777 force-pushed the remove_stddef_h_includes branch 3 times, most recently from 3b7dcf6 to fb81469 Compare November 4, 2024 16:52
@philnik777 philnik777 marked this pull request as ready for review November 5, 2024 13:00
@philnik777 philnik777 requested a review from a team as a code owner November 5, 2024 13:00
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Nov 5, 2024
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2024

@llvm/pr-subscribers-libcxx

Author: Nikolas Klauser (philnik777)

Changes

We can define some of these aliases without having to include the system <stddef.h> and there doesn't seem to be much of a reason we shouldn't do it this way.


Full diff: https://github.com/llvm/llvm-project/pull/114788.diff

12 Files Affected:

  • (modified) libcxx/include/__cstddef/nullptr_t.h (+1-2)
  • (modified) libcxx/include/__cstddef/ptrdiff_t.h (+1-2)
  • (modified) libcxx/include/__cstddef/size_t.h (+1-2)
  • (modified) libcxx/include/__exception/exception_ptr.h (+1)
  • (modified) libcxx/include/__functional/function.h (+1)
  • (modified) libcxx/include/__functional/hash.h (+1)
  • (modified) libcxx/include/__memory/shared_ptr.h (+1)
  • (modified) libcxx/include/forward_list (+1)
  • (modified) libcxx/include/new (+1)
  • (modified) libcxx/include/string_view (+1)
  • (modified) libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp (+2-1)
  • (modified) libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp (+2-2)
diff --git a/libcxx/include/__cstddef/nullptr_t.h b/libcxx/include/__cstddef/nullptr_t.h
index de3f7d4ab5fa75..7eaae017539652 100644
--- a/libcxx/include/__cstddef/nullptr_t.h
+++ b/libcxx/include/__cstddef/nullptr_t.h
@@ -10,7 +10,6 @@
 #define _LIBCPP___CSTDDEF_NULLPTR_T_H
 
 #include <__config>
-#include <stddef.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -18,7 +17,7 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-using ::nullptr_t;
+using nullptr_t = decltype(nullptr);
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/include/__cstddef/ptrdiff_t.h b/libcxx/include/__cstddef/ptrdiff_t.h
index f8b5cdaaff01c9..c53e999d73b019 100644
--- a/libcxx/include/__cstddef/ptrdiff_t.h
+++ b/libcxx/include/__cstddef/ptrdiff_t.h
@@ -10,7 +10,6 @@
 #define _LIBCPP___CSTDDEF_PTRDIFF_T_H
 
 #include <__config>
-#include <stddef.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -18,7 +17,7 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-using ::ptrdiff_t _LIBCPP_USING_IF_EXISTS;
+using ptrdiff_t = __PTRDIFF_TYPE__;
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/include/__cstddef/size_t.h b/libcxx/include/__cstddef/size_t.h
index 91abbf01318953..9a6d921d0d1f2f 100644
--- a/libcxx/include/__cstddef/size_t.h
+++ b/libcxx/include/__cstddef/size_t.h
@@ -10,7 +10,6 @@
 #define _LIBCPP___CSTDDEF_SIZE_T_H
 
 #include <__config>
-#include <stddef.h>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header
@@ -18,7 +17,7 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-using ::size_t _LIBCPP_USING_IF_EXISTS;
+using size_t = __SIZE_TYPE__;
 
 _LIBCPP_END_NAMESPACE_STD
 
diff --git a/libcxx/include/__exception/exception_ptr.h b/libcxx/include/__exception/exception_ptr.h
index 32c56fc5cc1263..c43c005603dd7a 100644
--- a/libcxx/include/__exception/exception_ptr.h
+++ b/libcxx/include/__exception/exception_ptr.h
@@ -10,6 +10,7 @@
 #define _LIBCPP___EXCEPTION_EXCEPTION_PTR_H
 
 #include <__config>
+#include <__cstddef/nullptr_t.h>
 #include <__exception/operations.h>
 #include <__memory/addressof.h>
 #include <__memory/construct_at.h>
diff --git a/libcxx/include/__functional/function.h b/libcxx/include/__functional/function.h
index e3844f86dbd3f5..543be05f10f330 100644
--- a/libcxx/include/__functional/function.h
+++ b/libcxx/include/__functional/function.h
@@ -12,6 +12,7 @@
 
 #include <__assert>
 #include <__config>
+#include <__cstddef/nullptr_t.h>
 #include <__exception/exception.h>
 #include <__functional/binary_function.h>
 #include <__functional/invoke.h>
diff --git a/libcxx/include/__functional/hash.h b/libcxx/include/__functional/hash.h
index 87009dfa62ef59..a33cba53c9309c 100644
--- a/libcxx/include/__functional/hash.h
+++ b/libcxx/include/__functional/hash.h
@@ -10,6 +10,7 @@
 #define _LIBCPP___FUNCTIONAL_HASH_H
 
 #include <__config>
+#include <__cstddef/nullptr_t.h>
 #include <__functional/unary_function.h>
 #include <__fwd/functional.h>
 #include <__type_traits/conjunction.h>
diff --git a/libcxx/include/__memory/shared_ptr.h b/libcxx/include/__memory/shared_ptr.h
index 0722b3f3e6d543..f50dc272f64ae1 100644
--- a/libcxx/include/__memory/shared_ptr.h
+++ b/libcxx/include/__memory/shared_ptr.h
@@ -13,6 +13,7 @@
 #include <__compare/compare_three_way.h>
 #include <__compare/ordering.h>
 #include <__config>
+#include <__cstddef/nullptr_t.h>
 #include <__cstddef/ptrdiff_t.h>
 #include <__exception/exception.h>
 #include <__functional/binary_function.h>
diff --git a/libcxx/include/forward_list b/libcxx/include/forward_list
index 04466d9a673fc6..a511fef31bc5d1 100644
--- a/libcxx/include/forward_list
+++ b/libcxx/include/forward_list
@@ -200,6 +200,7 @@ template <class T, class Allocator, class Predicate>
 #include <__algorithm/lexicographical_compare_three_way.h>
 #include <__algorithm/min.h>
 #include <__config>
+#include <__cstddef/nullptr_t.h>
 #include <__iterator/distance.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/move_iterator.h>
diff --git a/libcxx/include/new b/libcxx/include/new
index 290ad9e97f8ded..dc5aabe6a97ef3 100644
--- a/libcxx/include/new
+++ b/libcxx/include/new
@@ -87,6 +87,7 @@ void  operator delete[](void* ptr, void*) noexcept;
 */
 
 #include <__config>
+#include <__cstddef/max_align_t.h>
 #include <__cstddef/size_t.h>
 #include <__exception/exception.h>
 #include <__type_traits/is_function.h>
diff --git a/libcxx/include/string_view b/libcxx/include/string_view
index 0edda7aeb1a77c..db1f42523a13a3 100644
--- a/libcxx/include/string_view
+++ b/libcxx/include/string_view
@@ -208,6 +208,7 @@ namespace std {
 #include <__algorithm/min.h>
 #include <__assert>
 #include <__config>
+#include <__cstddef/nullptr_t.h>
 #include <__cstddef/ptrdiff_t.h>
 #include <__cstddef/size_t.h>
 #include <__functional/hash.h>
diff --git a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
index 4ca83ddff78e5b..e0aea76561e3a2 100644
--- a/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
+++ b/libcxx/test/std/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
@@ -37,8 +37,9 @@
 // template<class T>
 //     strong_ordering operator<=>(shared_ptr<T> const& x, nullptr_t) noexcept;   // C++20
 
-#include <memory>
 #include <cassert>
+#include <cstddef>
+#include <memory>
 
 #include "test_macros.h"
 #include "test_comparisons.h"
diff --git a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
index 6f0ba2e756196c..5ef9c4b044c681 100644
--- a/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
+++ b/libcxx/test/std/utilities/smartptr/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
@@ -39,9 +39,9 @@
 //   constexpr compare_three_way_result_t<typename unique_ptr<T, D>::pointer>
 //     operator<=>(const unique_ptr<T, D>& x, nullptr_t);                        // C++20
 
-#include <memory>
 #include <cassert>
-#include <type_traits>
+#include <cstddef>
+#include <memory>
 
 #include "test_macros.h"
 #include "test_comparisons.h"


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

_LIBCPP_BEGIN_NAMESPACE_STD

using ::nullptr_t;
using nullptr_t = decltype(nullptr);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The big difference is that if the system headers and the compiler disagree about what those types should be, we'll now favour the compiler's definitions whereas we would have previously favoured the system headers (or the Clang builtin headers, depending on which one comes first).

I can't imagine why it would make sense for the compiler and the system library to disagree, but I'm fairly certain there are setups in which they disagree (those setups are probably brittle to the point of being arguably wrong).

So TLDR, it's probably possible for this patch to introduce a behavior change in some weird setups, but I don't think that's something we should take into account beyond recognizing that possibility.

@philnik777 philnik777 merged commit 5acc4a3 into llvm:main Nov 6, 2024
63 checks passed
@philnik777 philnik777 deleted the remove_stddef_h_includes branch March 29, 2025 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants