Skip to content

[NFC][libc++] Adds (multi|)(map|set) forward declarations. #131541

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
Mar 20, 2025

Conversation

mordante
Copy link
Member

This removes duplicated forward declarations of these classes.

closes: #131518

@mordante mordante requested a review from a team as a code owner March 16, 2025 20:10
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 16, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 16, 2025

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

This removes duplicated forward declarations of these classes.

closes: #131518


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

9 Files Affected:

  • (modified) libcxx/include/CMakeLists.txt (+2)
  • (modified) libcxx/include/__functional/operations.h (+1-4)
  • (modified) libcxx/include/__fwd/functional.h (+7)
  • (added) libcxx/include/__fwd/map.h (+31)
  • (added) libcxx/include/__fwd/set.h (+30)
  • (modified) libcxx/include/__tree (+2-9)
  • (modified) libcxx/include/map (+3-6)
  • (modified) libcxx/include/module.modulemap (+3-1)
  • (modified) libcxx/include/set (+2-4)
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index d7c36d6b438fb..443e58fffe0d4 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -439,12 +439,14 @@ set(files
   __fwd/get.h
   __fwd/ios.h
   __fwd/istream.h
+  __fwd/map.h
   __fwd/mdspan.h
   __fwd/memory.h
   __fwd/memory_resource.h
   __fwd/ostream.h
   __fwd/pair.h
   __fwd/queue.h
+  __fwd/set.h
   __fwd/span.h
   __fwd/sstream.h
   __fwd/stack.h
diff --git a/libcxx/include/__functional/operations.h b/libcxx/include/__functional/operations.h
index 67d9da289aead..14357ef0a3ca5 100644
--- a/libcxx/include/__functional/operations.h
+++ b/libcxx/include/__functional/operations.h
@@ -13,6 +13,7 @@
 #include <__config>
 #include <__functional/binary_function.h>
 #include <__functional/unary_function.h>
+#include <__fwd/functional.h>
 #include <__type_traits/desugars_to.h>
 #include <__type_traits/is_integral.h>
 #include <__utility/forward.h>
@@ -349,11 +350,7 @@ struct _LIBCPP_TEMPLATE_VIS not_equal_to<void> {
 };
 #endif
 
-#if _LIBCPP_STD_VER >= 14
-template <class _Tp = void>
-#else
 template <class _Tp>
-#endif
 struct _LIBCPP_TEMPLATE_VIS less : __binary_function<_Tp, _Tp, bool> {
   typedef bool __result_type; // used by valarray
   _LIBCPP_CONSTEXPR_SINCE_CXX14 _LIBCPP_HIDE_FROM_ABI bool operator()(const _Tp& __x, const _Tp& __y) const {
diff --git a/libcxx/include/__fwd/functional.h b/libcxx/include/__fwd/functional.h
index 32c9ef33e453b..343a8dc22b8d3 100644
--- a/libcxx/include/__fwd/functional.h
+++ b/libcxx/include/__fwd/functional.h
@@ -17,6 +17,13 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+#if _LIBCPP_STD_VER >= 14
+template <class _Tp = void>
+#else
+template <class _Tp>
+#endif
+struct _LIBCPP_TEMPLATE_VIS less;
+
 template <class>
 struct _LIBCPP_TEMPLATE_VIS hash;
 
diff --git a/libcxx/include/__fwd/map.h b/libcxx/include/__fwd/map.h
new file mode 100644
index 0000000000000..aad404ca12b8c
--- /dev/null
+++ b/libcxx/include/__fwd/map.h
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___FWD_MAP_H
+#define _LIBCPP___FWD_MAP_H
+
+#include <__config>
+#include <__fwd/functional.h>
+#include <__fwd/memory.h>
+#include <__fwd/pair.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+class _LIBCPP_TEMPLATE_VIS map;
+
+template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+class _LIBCPP_TEMPLATE_VIS multimap;
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___FWD_MAP_H
diff --git a/libcxx/include/__fwd/set.h b/libcxx/include/__fwd/set.h
new file mode 100644
index 0000000000000..0ae2c4109c19d
--- /dev/null
+++ b/libcxx/include/__fwd/set.h
@@ -0,0 +1,30 @@
+//===----------------------------------------------------------------------===//
+//
+// 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___FWD_SET_H
+#define _LIBCPP___FWD_SET_H
+
+#include <__config>
+#include <__fwd/functional.h>
+#include <__fwd/memory.h>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
+class _LIBCPP_TEMPLATE_VIS set;
+
+template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
+class _LIBCPP_TEMPLATE_VIS multiset;
+
+_LIBCPP_END_NAMESPACE_STD
+
+#endif // _LIBCPP___FWD_SET_H
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index c627641d5d86f..08ae8996f8f7d 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -13,6 +13,8 @@
 #include <__algorithm/min.h>
 #include <__assert>
 #include <__config>
+#include <__fwd/map.h>
+#include <__fwd/set.h>
 #include <__iterator/distance.h>
 #include <__iterator/iterator_traits.h>
 #include <__iterator/next.h>
@@ -48,15 +50,6 @@ _LIBCPP_PUSH_MACROS
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
-template <class, class, class, class>
-class _LIBCPP_TEMPLATE_VIS map;
-template <class, class, class, class>
-class _LIBCPP_TEMPLATE_VIS multimap;
-template <class, class, class>
-class _LIBCPP_TEMPLATE_VIS set;
-template <class, class, class>
-class _LIBCPP_TEMPLATE_VIS multiset;
-
 template <class _Tp, class _Compare, class _Allocator>
 class __tree;
 template <class _Tp, class _NodePtr, class _DiffType>
diff --git a/libcxx/include/map b/libcxx/include/map
index 37a8ec91b8f1f..e7e0c14e36999 100644
--- a/libcxx/include/map
+++ b/libcxx/include/map
@@ -582,6 +582,7 @@ erase_if(multimap<Key, T, Compare, Allocator>& c, Predicate pred);  // C++20
 #  include <__functional/binary_function.h>
 #  include <__functional/is_transparent.h>
 #  include <__functional/operations.h>
+#  include <__fwd/map.h>
 #  include <__iterator/erase_if_container.h>
 #  include <__iterator/iterator_traits.h>
 #  include <__iterator/ranges_iterator_traits.h>
@@ -756,10 +757,6 @@ public:
   }
 };
 
-template <class _Key, class _Tp, class _Compare, class _Allocator>
-class map;
-template <class _Key, class _Tp, class _Compare, class _Allocator>
-class multimap;
 template <class _TreeIterator>
 class __map_const_iterator;
 
@@ -971,7 +968,7 @@ public:
   friend class _LIBCPP_TEMPLATE_VIS __tree_const_iterator;
 };
 
-template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+template <class _Key, class _Tp, class _Compare, class _Allocator>
 class _LIBCPP_TEMPLATE_VIS map {
 public:
   // types:
@@ -1656,7 +1653,7 @@ struct __container_traits<map<_Key, _Tp, _Compare, _Allocator> > {
   static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
 };
 
-template <class _Key, class _Tp, class _Compare = less<_Key>, class _Allocator = allocator<pair<const _Key, _Tp> > >
+template <class _Key, class _Tp, class _Compare, class _Allocator>
 class _LIBCPP_TEMPLATE_VIS multimap {
 public:
   // types:
diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap
index b9964dac84acd..57361947de08b 100644
--- a/libcxx/include/module.modulemap
+++ b/libcxx/include/module.modulemap
@@ -1548,6 +1548,7 @@ module std [system] {
   }
 
   module map {
+    module fwd { header "__fwd/map.h" }
     header "map"
     export *
   }
@@ -1905,6 +1906,7 @@ module std [system] {
   }
 
   module set {
+    module fwd { header "__fwd/set.h" }
     header "set"
     export *
   }
@@ -1972,7 +1974,7 @@ module std [system] {
       export std.utility.element_count // used as part of the constexpr C function's API
     }
     module extern_template_lists    { header "__string/extern_template_lists.h" }
-    module fwd                      {  header "__fwd/string.h" }
+    module fwd                      { header "__fwd/string.h" }
 
     header "string"
     export *
diff --git a/libcxx/include/set b/libcxx/include/set
index bd7bfef1f3e29..1f60dc7c45bd8 100644
--- a/libcxx/include/set
+++ b/libcxx/include/set
@@ -522,6 +522,7 @@ erase_if(multiset<Key, Compare, Allocator>& c, Predicate pred);  // C++20
 #  include <__config>
 #  include <__functional/is_transparent.h>
 #  include <__functional/operations.h>
+#  include <__fwd/set.h>
 #  include <__iterator/erase_if_container.h>
 #  include <__iterator/iterator_traits.h>
 #  include <__iterator/ranges_iterator_traits.h>
@@ -570,9 +571,6 @@ _LIBCPP_PUSH_MACROS
 _LIBCPP_BEGIN_NAMESPACE_STD
 
 template <class _Key, class _Compare, class _Allocator>
-class multiset;
-
-template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
 class _LIBCPP_TEMPLATE_VIS set {
 public:
   // types:
@@ -1034,7 +1032,7 @@ struct __container_traits<set<_Key, _Compare, _Allocator> > {
   static _LIBCPP_CONSTEXPR const bool __emplacement_has_strong_exception_safety_guarantee = true;
 };
 
-template <class _Key, class _Compare = less<_Key>, class _Allocator = allocator<_Key> >
+template <class _Key, class _Compare, class _Allocator>
 class _LIBCPP_TEMPLATE_VIS multiset {
 public:
   // types:

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

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

I really like this patch, thanks for cleaning this up!

LGTM assuming fixing the CI doesn't require much changes. It looks like you might be hitting another instance of #131814.

You could try re-exporting like this:

// under std.functional
module operations {
  header "__functional/operations.h"
  export std.fwd.functional
}

This removes duplicated forward declarations of these classes.

closes: llvm#131518
@mordante mordante force-pushed the review/add_map_set_forward_headers branch from 45523c6 to 5ab4f47 Compare March 19, 2025 17:15
@mordante mordante merged commit 73cd84a into llvm:main Mar 20, 2025
86 checks passed
@mordante mordante deleted the review/add_map_set_forward_headers branch March 20, 2025 17:00
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.

[libc++] Properly forward declare std::(|multi_)(map|set)
3 participants