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

This removes the similar tags used in the chrono tzdb implementation.

Fixes: https://github.com/llvm/llvm-project/issues/85432
This commit is contained in:
Mark de Wever
2024-04-10 20:34:58 +02:00
committed by GitHub
parent 2b00a73f62
commit 0a1317564a
20 changed files with 79 additions and 103 deletions

View File

@@ -863,6 +863,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

View File

@@ -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
@@ -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;

View File

@@ -18,6 +18,7 @@
# include <__compare/strong_order.h>
# include <__config>
# include <__utility/private_constructor_tag.h>
# include <string>
# include <string_view>
@@ -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;

View File

@@ -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>
@@ -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();
@@ -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

View File

@@ -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
@@ -49,13 +50,13 @@ public:
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_) {
@@ -74,10 +75,8 @@ private:
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

View File

@@ -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

View File

@@ -2090,18 +2090,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" }

View File

@@ -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

View File

@@ -1,27 +0,0 @@
// -*- 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
//
//===----------------------------------------------------------------------===//
// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
#ifndef _LIBCPP_SRC_INCLUDE_TZDB_LEAP_SECOND_PRIVATE_H
#define _LIBCPP_SRC_INCLUDE_TZDB_LEAP_SECOND_PRIVATE_H
#include <chrono>
_LIBCPP_BEGIN_NAMESPACE_STD
namespace chrono {
struct leap_second::__constructor_tag {};
} // namespace chrono
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_SRC_INCLUDE_TZDB_LEAP_SECOND_PRIVATE_H

View File

@@ -1,27 +0,0 @@
// -*- 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
//
//===----------------------------------------------------------------------===//
// For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
#ifndef _LIBCPP_SRC_INCLUDE_TZDB_TIME_ZONE_LINK_PRIVATE_H
#define _LIBCPP_SRC_INCLUDE_TZDB_TIME_ZONE_LINK_PRIVATE_H
#include <chrono>
_LIBCPP_BEGIN_NAMESPACE_STD
namespace chrono {
struct time_zone_link::__constructor_tag {};
} // namespace chrono
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP_SRC_INCLUDE_TZDB_TIME_ZONE_LINK_PRIVATE_H

View File

@@ -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();

View File

@@ -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"
@@ -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) {
@@ -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);
}
}

View File

@@ -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, "");

View File

@@ -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() {

View File

@@ -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() {

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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

View File

@@ -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