Skip to content

[libc++] Adds a global private constructor tag. #87920

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
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ set(files
__utility/pair.h
__utility/piecewise_construct.h
__utility/priority_tag.h
__utility/private_constructor_tag.h
__utility/rel_ops.h
__utility/small_buffer.h
__utility/swap.h
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__chrono/leap_second.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
# include <__compare/ordering.h>
# include <__compare/three_way_comparable.h>
# include <__config>
# include <__utility/private_constructor_tag.h>

# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand All @@ -35,9 +36,8 @@ namespace chrono {

class leap_second {
public:
struct __constructor_tag;
[[nodiscard]]
_LIBCPP_HIDE_FROM_ABI explicit constexpr leap_second(__constructor_tag&&, sys_seconds __date, seconds __value)
_LIBCPP_HIDE_FROM_ABI explicit constexpr leap_second(__private_constructor_tag, sys_seconds __date, seconds __value)
: __date_(__date), __value_(__value) {}

_LIBCPP_HIDE_FROM_ABI leap_second(const leap_second&) = default;
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/__chrono/time_zone_link.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

# include <__compare/strong_order.h>
# include <__config>
# include <__utility/private_constructor_tag.h>
# include <string>
# include <string_view>

Expand All @@ -37,9 +38,8 @@ namespace chrono {

class time_zone_link {
public:
struct __constructor_tag;
_LIBCPP_NODISCARD_EXT
_LIBCPP_HIDE_FROM_ABI explicit time_zone_link(__constructor_tag&&, string_view __name, string_view __target)
_LIBCPP_HIDE_FROM_ABI explicit time_zone_link(__private_constructor_tag, string_view __name, string_view __target)
: __name_{__name}, __target_{__target} {}

_LIBCPP_HIDE_FROM_ABI time_zone_link(time_zone_link&&) = default;
Expand Down
12 changes: 6 additions & 6 deletions libcxx/include/__locale
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <__mutex/once_flag.h>
#include <__type_traits/make_unsigned.h>
#include <__utility/no_destroy.h>
#include <__utility/private_constructor_tag.h>
#include <cctype>
#include <clocale>
#include <cstdint>
Expand Down Expand Up @@ -97,8 +98,7 @@ private:

template <class>
friend struct __no_destroy;
struct __private_tag {};
_LIBCPP_HIDE_FROM_ABI explicit locale(__private_tag, __imp* __loc) : __locale_(__loc) {}
_LIBCPP_HIDE_FROM_ABI explicit locale(__private_constructor_tag, __imp* __loc) : __locale_(__loc) {}

void __install_ctor(const locale&, facet*, long);
static locale& __global();
Expand Down Expand Up @@ -1248,10 +1248,10 @@ extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char, char
#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<wchar_t, char, mbstate_t>;
#endif
extern template class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
codecvt_byname<char16_t, char, mbstate_t>; // deprecated in C++20
extern template class _LIBCPP_DEPRECATED_IN_CXX20 _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS
codecvt_byname<char32_t, char, mbstate_t>; // deprecated in C++20
extern template class _LIBCPP_DEPRECATED_IN_CXX20
_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char, mbstate_t>; // deprecated in C++20
extern template class _LIBCPP_DEPRECATED_IN_CXX20
_LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char, mbstate_t>; // deprecated in C++20
#ifndef _LIBCPP_HAS_NO_CHAR8_T
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char16_t, char8_t, mbstate_t>; // C++20
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS codecvt_byname<char32_t, char8_t, mbstate_t>; // C++20
Expand Down
9 changes: 4 additions & 5 deletions libcxx/include/__stop_token/stop_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <__type_traits/is_nothrow_constructible.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/private_constructor_tag.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
Expand Down Expand Up @@ -49,13 +50,13 @@ class _LIBCPP_AVAILABILITY_SYNC stop_callback : private __stop_callback_base {
requires constructible_from<_Callback, _Cb>
_LIBCPP_HIDE_FROM_ABI explicit stop_callback(const stop_token& __st,
_Cb&& __cb) noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
: stop_callback(__private_tag{}, __st.__state_, std::forward<_Cb>(__cb)) {}
: stop_callback(__private_constructor_tag{}, __st.__state_, std::forward<_Cb>(__cb)) {}

template <class _Cb>
requires constructible_from<_Callback, _Cb>
_LIBCPP_HIDE_FROM_ABI explicit stop_callback(stop_token&& __st,
_Cb&& __cb) noexcept(is_nothrow_constructible_v<_Callback, _Cb>)
: stop_callback(__private_tag{}, std::move(__st.__state_), std::forward<_Cb>(__cb)) {}
: stop_callback(__private_constructor_tag{}, std::move(__st.__state_), std::forward<_Cb>(__cb)) {}

_LIBCPP_HIDE_FROM_ABI ~stop_callback() {
if (__state_) {
Expand All @@ -74,10 +75,8 @@ class _LIBCPP_AVAILABILITY_SYNC stop_callback : private __stop_callback_base {

friend __stop_callback_base;

struct __private_tag {};

template <class _StatePtr, class _Cb>
_LIBCPP_HIDE_FROM_ABI explicit stop_callback(__private_tag, _StatePtr&& __state, _Cb&& __cb) noexcept(
_LIBCPP_HIDE_FROM_ABI explicit stop_callback(__private_constructor_tag, _StatePtr&& __state, _Cb&& __cb) noexcept(
is_nothrow_constructible_v<_Callback, _Cb>)
: __stop_callback_base([](__stop_callback_base* __cb_base) noexcept {
// stop callback is supposed to only be called once
Expand Down
28 changes: 28 additions & 0 deletions libcxx/include/__utility/private_constructor_tag.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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__UTILITY_PRIVATE_CONSTRUCTOR_TAG_H
#define _LIBCPP__UTILITY_PRIVATE_CONSTRUCTOR_TAG_H

#include <__config>

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

_LIBCPP_BEGIN_NAMESPACE_STD

// This tag allows defining non-standard exposition-only constructors while
// preventing users from being able to use them, since this reserved-name tag
// needs to be used.
struct __private_constructor_tag {};

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP__UTILITY_PRIVATE_CONSTRUCTOR_TAG_H
21 changes: 11 additions & 10 deletions libcxx/include/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -2087,18 +2087,19 @@ module std_private_utility_pair [system] {
export std_private_type_traits_is_nothrow_move_assignable
export std_private_utility_pair_fwd
}
module std_private_utility_pair_fwd [system] { header "__fwd/pair.h" }
module std_private_utility_piecewise_construct [system] { header "__utility/piecewise_construct.h" }
module std_private_utility_priority_tag [system] { header "__utility/priority_tag.h" }
module std_private_utility_rel_ops [system] { header "__utility/rel_ops.h" }
module std_private_utility_small_buffer [system] { header "__utility/small_buffer.h" }
module std_private_utility_swap [system] {
module std_private_utility_pair_fwd [system] { header "__fwd/pair.h" }
module std_private_utility_piecewise_construct [system] { header "__utility/piecewise_construct.h" }
module std_private_utility_priority_tag [system] { header "__utility/priority_tag.h" }
module std_private_utility_private_constructor_tag [system] { header "__utility/private_constructor_tag.h" }
module std_private_utility_rel_ops [system] { header "__utility/rel_ops.h" }
module std_private_utility_small_buffer [system] { header "__utility/small_buffer.h" }
module std_private_utility_swap [system] {
header "__utility/swap.h"
export std_private_type_traits_is_swappable
}
module std_private_utility_to_underlying [system] { header "__utility/to_underlying.h" }
module std_private_utility_unreachable [system] { header "__utility/unreachable.h" }
module std_private_utility_to_underlying [system] { header "__utility/to_underlying.h" }
module std_private_utility_unreachable [system] { header "__utility/unreachable.h" }

module std_private_variant_monostate [system] { header "__variant/monostate.h" }
module std_private_variant_monostate [system] { header "__variant/monostate.h" }

module std_private_vector_fwd [system] { header "__fwd/vector.h" }
module std_private_vector_fwd [system] { header "__fwd/vector.h" }
2 changes: 0 additions & 2 deletions libcxx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,6 @@ endif()

if (LIBCXX_ENABLE_LOCALIZATION AND LIBCXX_ENABLE_FILESYSTEM AND LIBCXX_ENABLE_TIME_ZONE_DATABASE)
list(APPEND LIBCXX_EXPERIMENTAL_SOURCES
include/tzdb/leap_second_private.h
include/tzdb/time_zone_link_private.h
include/tzdb/time_zone_private.h
include/tzdb/types_private.h
include/tzdb/tzdb_list_private.h
Expand Down
27 changes: 0 additions & 27 deletions libcxx/src/include/tzdb/leap_second_private.h

This file was deleted.

27 changes: 0 additions & 27 deletions libcxx/src/include/tzdb/time_zone_link_private.h

This file was deleted.

2 changes: 1 addition & 1 deletion libcxx/src/locale.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ constinit __no_destroy<locale::__imp>
locale::__imp::classic_locale_imp_(__uninitialized_tag{}); // initialized below in classic()

const locale& locale::classic() {
static const __no_destroy<locale> classic_locale(__private_tag{}, [] {
static const __no_destroy<locale> classic_locale(__private_constructor_tag{}, [] {
// executed exactly once on first initialization of `classic_locale`
locale::__imp::classic_locale_imp_.__emplace(1u);
return &locale::__imp::classic_locale_imp_.__get();
Expand Down
6 changes: 2 additions & 4 deletions libcxx/src/tzdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
#include <stdexcept>
#include <string>

#include "include/tzdb/leap_second_private.h"
#include "include/tzdb/time_zone_link_private.h"
#include "include/tzdb/time_zone_private.h"
#include "include/tzdb/types_private.h"
#include "include/tzdb/tzdb_list_private.h"
Expand Down Expand Up @@ -582,7 +580,7 @@ static void __parse_link(tzdb& __tzdb, istream& __input) {
string __name = chrono::__parse_string(__input);
chrono::__skip_line(__input);

__tzdb.links.emplace_back(time_zone_link::__constructor_tag{}, std::move(__name), std::move(__target));
__tzdb.links.emplace_back(std::__private_constructor_tag{}, std::move(__name), std::move(__target));
}

static void __parse_tzdata(tzdb& __db, __tz::__rules_storage_type& __rules, istream& __input) {
Expand Down Expand Up @@ -649,7 +647,7 @@ static void __parse_leap_seconds(vector<leap_second>& __leap_seconds, istream&&
seconds __value{chrono::__parse_integral(__input, false)};
chrono::__skip_line(__input);

__leap_seconds.emplace_back(leap_second::__constructor_tag{}, __date, __value);
__leap_seconds.emplace_back(std::__private_constructor_tag{}, __date, __value);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// struct __private_constructor_tag{};

// The private constructor tag is intended to be a trivial type that can easily
// be used to mark a constructor exposition-only.
//
// Tests whether the type is trivial.

#include <__utility/private_constructor_tag.h>
#include <type_traits>

static_assert(std::is_trivial<std::__private_constructor_tag>::value, "");
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
#include <type_traits>
#include <cassert>

// Add the include path required by test_chrono_leap_second.h when using libc++.
// ADDITIONAL_COMPILE_FLAGS(stdlib=libc++): -I %{libcxx-dir}/src/include
#include "test_chrono_leap_second.h"

constexpr bool test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <concepts>
#include <cassert>

// Add the include path required by test_chrono_leap_second.h when using libc++.
// ADDITIONAL_COMPILE_FLAGS(stdlib=libc++): -I %{libcxx-dir}/src/include
#include "test_chrono_leap_second.h"

constexpr bool test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#include "test_macros.h"

// Add the include path required by test_chrono_leap_second.h when using libc++.
// ADDITIONAL_COMPILE_FLAGS(stdlib=libc++): -I %{libcxx-dir}/src/include
#include "test_chrono_leap_second.h"

constexpr void test(const std::chrono::leap_second leap_second, std::chrono::sys_seconds expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#include "test_macros.h"

// Add the include path required by test_chrono_leap_second.h when using libc++.
// ADDITIONAL_COMPILE_FLAGS(stdlib=libc++): -I %{libcxx-dir}/src/include
#include "test_chrono_leap_second.h"

constexpr void test(const std::chrono::leap_second leap_second, std::chrono::seconds expected) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
#include "test_macros.h"
#include "test_comparisons.h"

// Add the include path required by test_chrono_leap_second.h when using libc++.
// ADDITIONAL_COMPILE_FLAGS(stdlib=libc++): -I %{libcxx-dir}/src/include
#include "test_chrono_leap_second.h"

constexpr void test_comparison(const std::chrono::leap_second lhs, const std::chrono::leap_second rhs) {
Expand Down
9 changes: 2 additions & 7 deletions libcxx/test/support/test_chrono_leap_second.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,11 @@

#ifdef _LIBCPP_VERSION

// In order to find this include the calling test needs to provide this path in
// the search path. Typically this looks like:
// ADDITIONAL_COMPILE_FLAGS(stdlib=libc++): -I %{libcxx-dir}/src/include
// where the number of `../` sequences depends on the subdirectory level of the
// test.
# include "tzdb/leap_second_private.h" // Header in the dylib
# include <__utility/private_constructor_tag.h>

inline constexpr std::chrono::leap_second
test_leap_second_create(const std::chrono::sys_seconds& date, const std::chrono::seconds& value) {
return std::chrono::leap_second{std::chrono::leap_second::__constructor_tag{}, date, value};
return std::chrono::leap_second{std::__private_constructor_tag{}, date, value};
}

#else // _LIBCPP_VERSION
Expand Down
1 change: 1 addition & 0 deletions libcxx/utils/generate_iwyu_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def IWYU_mapping(header: str) -> typing.Optional[typing.List[str]]:
"__debug_utils/.+",
"__fwd/get[.]h",
"__support/.+",
"__utility/private_constructor_tag.h",
]
if any(re.match(pattern, header) for pattern in ignore):
return None
Expand Down