[libcxx-commits] [libcxx] [libc++][format] P2637R3: Member `visit` (PR #76449)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Dec 27 08:02:55 PST 2023
https://github.com/H-G-Hristov updated https://github.com/llvm/llvm-project/pull/76449
>From e1ab38976f2d989952a98487e1af114584d2bf9d Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Mon, 25 Dec 2023 11:57:12 +0200
Subject: [PATCH 1/7] [libc++][format] P2637R3: Member visit
Implements parts of: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p2637r3.html (https://eel.is/c++draft/format.arg)
---
libcxx/docs/Status/Cxx2cPapers.csv | 2 +-
libcxx/include/__config | 6 ++++
libcxx/include/__format/format_arg.h | 19 ++++++++++--
libcxx/include/format | 2 +-
.../format.args/visit.pass.cpp | 30 +++++++++++++++++++
.../generate_feature_test_macro_components.py | 1 +
6 files changed, 56 insertions(+), 4 deletions(-)
create mode 100644 libcxx/test/std/utilities/format/format.arguments/format.args/visit.pass.cpp
diff --git a/libcxx/docs/Status/Cxx2cPapers.csv b/libcxx/docs/Status/Cxx2cPapers.csv
index ff83648aa76830..b33543aad8b945 100644
--- a/libcxx/docs/Status/Cxx2cPapers.csv
+++ b/libcxx/docs/Status/Cxx2cPapers.csv
@@ -17,7 +17,7 @@
"`P0792R14 <https://wg21.link/P0792R14>`__","LWG","``function_ref``: a type-erased callable reference","Varna June 2023","","",""
"`P2874R2 <https://wg21.link/P2874R2>`__","LWG","Mandating Annex D Require No More","Varna June 2023","","",""
"`P2757R3 <https://wg21.link/P2757R3>`__","LWG","Type-checking format args","Varna June 2023","","","|format|"
-"`P2637R3 <https://wg21.link/P2637R3>`__","LWG","Member ``visit``","Varna June 2023","","","|format|"
+"`P2637R3 <https://wg21.link/P2637R3>`__","LWG","Member ``visit``","Varna June 2023","|Complete|","18.0",""
"`P2641R4 <https://wg21.link/P2641R4>`__","CWG, LWG","Checking if a ``union`` alternative is active","Varna June 2023","","",""
"`P1759R6 <https://wg21.link/P1759R6>`__","LWG","Native handles and file streams","Varna June 2023","","",""
"`P2697R1 <https://wg21.link/P2697R1>`__","LWG","Interfacing ``bitset`` with ``string_view``","Varna June 2023","|Complete|","18.0",""
diff --git a/libcxx/include/__config b/libcxx/include/__config
index adff13e714cb64..ee85b73c2e4cfe 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -956,6 +956,12 @@ typedef __char32_t char32_t;
# define _LIBCPP_DEPRECATED_IN_CXX23
# endif
+# if _LIBCPP_STD_VER >= 26
+# define _LIBCPP_DEPRECATED_IN_CXX26 _LIBCPP_DEPRECATED
+# else
+# define _LIBCPP_DEPRECATED_IN_CXX26
+# endif
+
# if !defined(_LIBCPP_HAS_NO_CHAR8_T)
# define _LIBCPP_DEPRECATED_WITH_CHAR8_T _LIBCPP_DEPRECATED
# else
diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index 280c9108241754..62684a3394d010 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -93,7 +93,7 @@ _LIBCPP_HIDE_FROM_ABI constexpr __arg_t __get_packed_type(uint64_t __types, size
} // namespace __format
-// This function is not user obervable, so it can directly use the non-standard
+// This function is not user observable, so it can directly use the non-standard
// types of the "variant". See __arg_t for more details.
template <class _Visitor, class _Context>
_LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
@@ -227,6 +227,20 @@ class _LIBCPP_TEMPLATE_VIS basic_format_arg {
_LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __type_ != __format::__arg_t::__none; }
+# if __LIBCPP_VERSION >= 26
+ template <class _Visitor>
+ _LIBCPP_HIDE_FROM_ABI decltype(auto) visit(this basic_format_arg __arg, _Visitor&& __vis) {
+ // TODO: use __visit_format_arg
+ return __arg.__value_.visit(std::forward<_Visitor>(__vis));
+ }
+
+ template <class _Rp, class _Visitor>
+ _LIBCPP_HIDE_FROM_ABI _Rp visit(this basic_format_arg __arg, _Visitor&& __vis) {
+ // TODO:: use __visit_format_arg
+ return __arg.__value_.visit<_Rp>(std::forward<_Visitor>(__vis));
+ }
+# endif
+
private:
using char_type = typename _Context::char_type;
@@ -267,7 +281,8 @@ class _LIBCPP_TEMPLATE_VIS basic_format_arg<_Context>::handle {
// This function is user facing, so it must wrap the non-standard types of
// the "variant" in a handle to stay conforming. See __arg_t for more details.
template <class _Visitor, class _Context>
-_LIBCPP_HIDE_FROM_ABI decltype(auto) visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
+_LIBCPP_DEPRECATED_IN_CXX26 _LIBCPP_HIDE_FROM_ABI decltype(auto)
+visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
switch (__arg.__type_) {
# ifndef _LIBCPP_HAS_NO_INT128
case __format::__arg_t::__i128: {
diff --git a/libcxx/include/format b/libcxx/include/format
index ab9b336d0cdabe..e1566fb4636a7b 100644
--- a/libcxx/include/format
+++ b/libcxx/include/format
@@ -170,7 +170,7 @@ namespace std {
template<class Context> class basic_format_arg;
template<class Visitor, class Context>
- see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
+ see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); // deprecated in C++26
// [format.arg.store], class template format-arg-store
template<class Context, class... Args> struct format-arg-store; // exposition only
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.args/visit.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.args/visit.pass.cpp
new file mode 100644
index 00000000000000..67f822615ce5b9
--- /dev/null
+++ b/libcxx/test/std/utilities/format/format.arguments/format.args/visit.pass.cpp
@@ -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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
+
+// <format>
+
+// class basic_format_arg;
+
+// template<class Visitor>
+// decltype(auto) visit(this basic_format_arg arg, Visitor&& vis);
+// template<class R, class Visitor>
+// R visit(this basic_format_arg arg, Visitor&& vis);
+
+template <class CharT>
+void test() {}
+
+int main(int, char**) {
+ test<char>();
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+ test<wchar_t>();
+#endif
+
+ return 0;
+}
diff --git a/libcxx/utils/generate_feature_test_macro_components.py b/libcxx/utils/generate_feature_test_macro_components.py
index 2f506f32f565cb..fbd01f8de15962 100755
--- a/libcxx/utils/generate_feature_test_macro_components.py
+++ b/libcxx/utils/generate_feature_test_macro_components.py
@@ -467,6 +467,7 @@ def add_version_header(tc):
# "c++20": 202110 Not implemented P2372R3 Fixing locale handling in chrono formatters
"c++20": 202106,
# "c++23": 202207, Not implemented P2419R2 Clarify handling of encodings in localized formatting of chrono types
+ # "c++26": 202306, # P2637R3 Member Visit
# "c++26": 202311, P2918R2 Runtime format strings II (implemented)
},
# Note these three papers are adopted at the June 2023 meeting and have sequential numbering
>From 0764fb389d46caa366ee8793e989aa6bc648caa8 Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Tue, 26 Dec 2023 11:44:47 +0200
Subject: [PATCH 2/7] Implemented member methods + WIP tests
---
libcxx/include/__format/format_arg.h | 59 ++++++++-
.../visit.pass.cpp | 0
.../visit_format_arg.deprecated.verify.cpp | 64 ++++++++++
.../format.arg/visit_format_arg.pass.cpp | 116 +++++++++---------
4 files changed, 177 insertions(+), 62 deletions(-)
rename libcxx/test/std/utilities/format/format.arguments/{format.args => format.arg}/visit.pass.cpp (100%)
create mode 100644 libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.deprecated.verify.cpp
diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index 62684a3394d010..bc9e13eb9029a5 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -144,6 +144,57 @@ _LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_
__libcpp_unreachable();
}
+# if _LIBCPP_STD_VER >= 26
+template <class _Rp, class _Visitor, class _Context>
+_LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
+ switch (__arg.__type_) {
+ case __format::__arg_t::__none:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__monostate_);
+ case __format::__arg_t::__boolean:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__boolean_);
+ case __format::__arg_t::__char_type:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__char_type_);
+ case __format::__arg_t::__int:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__int_);
+ case __format::__arg_t::__long_long:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
+ case __format::__arg_t::__i128:
+# ifndef _LIBCPP_HAS_NO_INT128
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__i128_);
+# else
+ __libcpp_unreachable();
+# endif
+ case __format::__arg_t::__unsigned:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
+ case __format::__arg_t::__unsigned_long_long:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
+ case __format::__arg_t::__u128:
+# ifndef _LIBCPP_HAS_NO_INT128
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__u128_);
+# else
+ __libcpp_unreachable();
+# endif
+ case __format::__arg_t::__float:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__float_);
+ case __format::__arg_t::__double:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__double_);
+ case __format::__arg_t::__long_double:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_double_);
+ case __format::__arg_t::__const_char_type_ptr:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__const_char_type_ptr_);
+ case __format::__arg_t::__string_view:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__string_view_);
+ case __format::__arg_t::__ptr:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__ptr_);
+ case __format::__arg_t::__handle:
+ return std::invoke_r<_Rp>(
+ std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
+ }
+
+ __libcpp_unreachable();
+}
+# endif
+
/// Contains the values used in basic_format_arg.
///
/// This is a separate type so it's possible to store the values and types in
@@ -227,17 +278,15 @@ class _LIBCPP_TEMPLATE_VIS basic_format_arg {
_LIBCPP_HIDE_FROM_ABI explicit operator bool() const noexcept { return __type_ != __format::__arg_t::__none; }
-# if __LIBCPP_VERSION >= 26
+# if _LIBCPP_STD_VER >= 26
template <class _Visitor>
_LIBCPP_HIDE_FROM_ABI decltype(auto) visit(this basic_format_arg __arg, _Visitor&& __vis) {
- // TODO: use __visit_format_arg
- return __arg.__value_.visit(std::forward<_Visitor>(__vis));
+ return std::__visit_format_arg(std::forward<_Visitor>(__vis), __arg);
}
template <class _Rp, class _Visitor>
_LIBCPP_HIDE_FROM_ABI _Rp visit(this basic_format_arg __arg, _Visitor&& __vis) {
- // TODO:: use __visit_format_arg
- return __arg.__value_.visit<_Rp>(std::forward<_Visitor>(__vis));
+ return std::__visit_format_arg<_Rp>(std::forward<_Visitor>(__vis), __arg);
}
# endif
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.args/visit.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
similarity index 100%
rename from libcxx/test/std/utilities/format/format.arguments/format.args/visit.pass.cpp
rename to libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.deprecated.verify.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.deprecated.verify.cpp
new file mode 100644
index 00000000000000..2e23b88871b681
--- /dev/null
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.deprecated.verify.cpp
@@ -0,0 +1,64 @@
+//===----------------------------------------------------------------------===//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
+
+// <format>
+
+// template<class Visitor, class Context>
+// see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
+
+#include <algorithm>
+#include <cassert>
+#include <format>
+#include <type_traits>
+
+#include "constexpr_char_traits.h"
+#include "make_string.h"
+#include "min_allocator.h"
+#include "test_macros.h"
+
+template <class Context, class To, class From>
+void test(From value) {
+ auto store = std::make_format_args<Context>(value);
+ std::basic_format_args<Context> format_args{store};
+
+ LIBCPP_ASSERT(format_args.__size() == 1);
+ assert(format_args.get(0));
+
+ // non-member
+ {
+ // expected-warning at +1 {{std::basic_format_context<char *, char>>' is deprecated}}
+ std::visit_format_arg(
+ [v = To(value)](auto a) -> To {
+ if constexpr (std::is_same_v<To, decltype(a)>) {
+ assert(v == a);
+ return a;
+ } else {
+ assert(false);
+ return {};
+ }
+ },
+ format_args.get(0));
+ }
+}
+
+template <class CharT>
+void test() {
+ using Context = std::basic_format_context<CharT*, CharT>;
+ std::basic_string<CharT> empty;
+ std::basic_string<CharT> str = MAKE_STRING(CharT, "abc");
+
+ test<Context, bool>(true);
+}
+
+int main(int, char**) {
+ test<char>();
+
+ return 0;
+}
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
index 3ddf2d0ff732a7..3a2f9afe2c5f78 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
@@ -23,6 +23,8 @@
#include "make_string.h"
#include "min_allocator.h"
+TEST_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated-declarations")
+
template <class Context, class To, class From>
void test(From value) {
auto store = std::make_format_args<Context>(value);
@@ -31,20 +33,41 @@ void test(From value) {
LIBCPP_ASSERT(format_args.__size() == 1);
assert(format_args.get(0));
- auto result = std::visit_format_arg(
- [v = To(value)](auto a) -> To {
- if constexpr (std::is_same_v<To, decltype(a)>) {
- assert(v == a);
- return a;
- } else {
- assert(false);
- return {};
- }
- },
- format_args.get(0));
+#if _LIBCPP_STD_VER >= 26
+ // member
+ {
+ auto result = format_args.get(0).visit([v = To(value)](auto a) -> To {
+ if constexpr (std::is_same_v<To, decltype(a)>) {
+ assert(v == a);
+ return a;
+ } else {
+ assert(false);
+ return {};
+ }
+ });
+
+ using ct = std::common_type_t<From, To>;
+ assert(static_cast<ct>(result) == static_cast<ct>(value));
+ }
+#endif
- using ct = std::common_type_t<From, To>;
- assert(static_cast<ct>(result) == static_cast<ct>(value));
+ // non-member
+ {
+ auto result = std::visit_format_arg(
+ [v = To(value)](auto a) -> To {
+ if constexpr (std::is_same_v<To, decltype(a)>) {
+ assert(v == a);
+ return a;
+ } else {
+ assert(false);
+ return {};
+ }
+ },
+ format_args.get(0));
+
+ using ct = std::common_type_t<From, To>;
+ assert(static_cast<ct>(result) == static_cast<ct>(value));
+ }
}
// Some types, as an extension, are stored in the variant. The Standard
@@ -75,8 +98,8 @@ void test_string_view(From value) {
assert(format_args.get(0));
using CharT = typename Context::char_type;
- using To = std::basic_string_view<CharT>;
- using V = std::basic_string<CharT>;
+ using To = std::basic_string_view<CharT>;
+ using V = std::basic_string<CharT>;
auto result = std::visit_format_arg(
[v = V(value.begin(), value.end())](auto a) -> To {
if constexpr (std::is_same_v<To, decltype(a)>) {
@@ -170,8 +193,7 @@ void test() {
test<Context, int, int>(std::numeric_limits<short>::max());
test<Context, int, int>(std::numeric_limits<int>::max());
- using LongToType =
- std::conditional_t<sizeof(long) == sizeof(int), int, long long>;
+ using LongToType = std::conditional_t<sizeof(long) == sizeof(int), int, long long>;
test<Context, LongToType, long>(std::numeric_limits<long>::min());
test<Context, LongToType, long>(std::numeric_limits<int>::min());
@@ -202,14 +224,11 @@ void test() {
// Test unsigned integer types.
test<Context, unsigned, unsigned char>(0);
- test<Context, unsigned, unsigned char>(
- std::numeric_limits<unsigned char>::max());
+ test<Context, unsigned, unsigned char>(std::numeric_limits<unsigned char>::max());
test<Context, unsigned, unsigned short>(0);
- test<Context, unsigned, unsigned short>(
- std::numeric_limits<unsigned char>::max());
- test<Context, unsigned, unsigned short>(
- std::numeric_limits<unsigned short>::max());
+ test<Context, unsigned, unsigned short>(std::numeric_limits<unsigned char>::max());
+ test<Context, unsigned, unsigned short>(std::numeric_limits<unsigned short>::max());
test<Context, unsigned, unsigned>(0);
test<Context, unsigned, unsigned>(std::numeric_limits<unsigned char>::max());
@@ -217,30 +236,20 @@ void test() {
test<Context, unsigned, unsigned>(std::numeric_limits<unsigned>::max());
using UnsignedLongToType =
- std::conditional_t<sizeof(unsigned long) == sizeof(unsigned), unsigned,
- unsigned long long>;
+ std::conditional_t<sizeof(unsigned long) == sizeof(unsigned), unsigned, unsigned long long>;
test<Context, UnsignedLongToType, unsigned long>(0);
- test<Context, UnsignedLongToType, unsigned long>(
- std::numeric_limits<unsigned char>::max());
- test<Context, UnsignedLongToType, unsigned long>(
- std::numeric_limits<unsigned short>::max());
- test<Context, UnsignedLongToType, unsigned long>(
- std::numeric_limits<unsigned>::max());
- test<Context, UnsignedLongToType, unsigned long>(
- std::numeric_limits<unsigned long>::max());
+ test<Context, UnsignedLongToType, unsigned long>(std::numeric_limits<unsigned char>::max());
+ test<Context, UnsignedLongToType, unsigned long>(std::numeric_limits<unsigned short>::max());
+ test<Context, UnsignedLongToType, unsigned long>(std::numeric_limits<unsigned>::max());
+ test<Context, UnsignedLongToType, unsigned long>(std::numeric_limits<unsigned long>::max());
test<Context, unsigned long long, unsigned long long>(0);
- test<Context, unsigned long long, unsigned long long>(
- std::numeric_limits<unsigned char>::max());
- test<Context, unsigned long long, unsigned long long>(
- std::numeric_limits<unsigned short>::max());
- test<Context, unsigned long long, unsigned long long>(
- std::numeric_limits<unsigned>::max());
- test<Context, unsigned long long, unsigned long long>(
- std::numeric_limits<unsigned long>::max());
- test<Context, unsigned long long, unsigned long long>(
- std::numeric_limits<unsigned long long>::max());
+ test<Context, unsigned long long, unsigned long long>(std::numeric_limits<unsigned char>::max());
+ test<Context, unsigned long long, unsigned long long>(std::numeric_limits<unsigned short>::max());
+ test<Context, unsigned long long, unsigned long long>(std::numeric_limits<unsigned>::max());
+ test<Context, unsigned long long, unsigned long long>(std::numeric_limits<unsigned long>::max());
+ test<Context, unsigned long long, unsigned long long>(std::numeric_limits<unsigned long long>::max());
#ifndef TEST_HAS_NO_INT128
test_handle<Context, __uint128_t>(0);
@@ -262,16 +271,12 @@ void test() {
test<Context, double, double>(std::numeric_limits<double>::min());
test<Context, double, double>(std::numeric_limits<double>::max());
- test<Context, long double, long double>(
- -std::numeric_limits<long double>::max());
- test<Context, long double, long double>(
- -std::numeric_limits<long double>::min());
+ test<Context, long double, long double>(-std::numeric_limits<long double>::max());
+ test<Context, long double, long double>(-std::numeric_limits<long double>::min());
test<Context, long double, long double>(-0.0);
test<Context, long double, long double>(0.0);
- test<Context, long double, long double>(
- std::numeric_limits<long double>::min());
- test<Context, long double, long double>(
- std::numeric_limits<long double>::max());
+ test<Context, long double, long double>(std::numeric_limits<long double>::min());
+ test<Context, long double, long double>(std::numeric_limits<long double>::max());
// Test const CharT pointer types.
@@ -307,8 +312,7 @@ void test() {
}
{
- using From = std::basic_string<CharT, constexpr_char_traits<CharT>,
- std::allocator<CharT>>;
+ using From = std::basic_string<CharT, constexpr_char_traits<CharT>, std::allocator<CharT>>;
test_string_view<Context>(From());
test_string_view<Context>(From(empty.c_str()));
@@ -316,8 +320,7 @@ void test() {
}
{
- using From =
- std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT>>;
+ using From = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT>>;
test_string_view<Context>(From());
test_string_view<Context>(From(empty.c_str()));
@@ -325,8 +328,7 @@ void test() {
}
{
- using From = std::basic_string<CharT, constexpr_char_traits<CharT>,
- min_allocator<CharT>>;
+ using From = std::basic_string<CharT, constexpr_char_traits<CharT>, min_allocator<CharT>>;
test_string_view<Context>(From());
test_string_view<Context>(From(empty.c_str()));
>From 7a70aa6a42da5d1f60a3c05e2b7e22fc08298f11 Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Tue, 26 Dec 2023 14:24:32 +0200
Subject: [PATCH 3/7] Added `visit` with return type test WIP
---
libcxx/include/__format/format_arg.h | 63 +++++++++++--
.../format.arg/visit.pass.cpp | 30 -------
.../format.arg/visit.return_type.pass.cpp | 89 +++++++++++++++++++
.../format.arg/visit_format_arg.pass.cpp | 4 +-
4 files changed, 148 insertions(+), 38 deletions(-)
delete mode 100644 libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
create mode 100644 libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index bc9e13eb9029a5..eb16326e2a72e8 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -145,6 +145,7 @@ _LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_
}
# if _LIBCPP_STD_VER >= 26
+# if 1
template <class _Rp, class _Visitor, class _Context>
_LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
switch (__arg.__type_) {
@@ -159,21 +160,21 @@ _LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<
case __format::__arg_t::__long_long:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
case __format::__arg_t::__i128:
-# ifndef _LIBCPP_HAS_NO_INT128
+# ifndef _LIBCPP_HAS_NO_INT128
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__i128_);
-# else
+# else
__libcpp_unreachable();
-# endif
+# endif
case __format::__arg_t::__unsigned:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
case __format::__arg_t::__unsigned_long_long:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
case __format::__arg_t::__u128:
-# ifndef _LIBCPP_HAS_NO_INT128
+# ifndef _LIBCPP_HAS_NO_INT128
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__u128_);
-# else
+# else
__libcpp_unreachable();
-# endif
+# endif
case __format::__arg_t::__float:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__float_);
case __format::__arg_t::__double:
@@ -193,6 +194,56 @@ _LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<
__libcpp_unreachable();
}
+# else
+template <class _Rp, class _Visitor, class _Context>
+_LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
+ switch (__arg.__type_) {
+ case __format::__arg_t::__none:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__monostate_));
+ case __format::__arg_t::__boolean:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__boolean_));
+ case __format::__arg_t::__char_type:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__char_type_));
+ case __format::__arg_t::__int:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__int_));
+ case __format::__arg_t::__long_long:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__long_long_));
+ case __format::__arg_t::__i128:
+# ifndef _LIBCPP_HAS_NO_INT128
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__i128_));
+# else
+ __libcpp_unreachable();
+# endif
+ case __format::__arg_t::__unsigned:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__unsigned_));
+ case __format::__arg_t::__unsigned_long_long:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__unsigned_long_long_));
+ case __format::__arg_t::__u128:
+# ifndef _LIBCPP_HAS_NO_INT128
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__u128_));
+# else
+ __libcpp_unreachable();
+# endif
+ case __format::__arg_t::__float:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__float_));
+ case __format::__arg_t::__double:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__double_));
+ case __format::__arg_t::__long_double:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__long_double_));
+ case __format::__arg_t::__const_char_type_ptr:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__const_char_type_ptr_));
+ case __format::__arg_t::__string_view:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__string_view_));
+ case __format::__arg_t::__ptr:
+ return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__ptr_));
+ case __format::__arg_t::__handle:
+ return std::invoke_r<_Rp>(
+ std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
+ }
+
+ __libcpp_unreachable();
+}
+# endif //
# endif
/// Contains the values used in basic_format_arg.
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
deleted file mode 100644
index 67f822615ce5b9..00000000000000
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
+++ /dev/null
@@ -1,30 +0,0 @@
-//===----------------------------------------------------------------------===//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
-// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
-
-// <format>
-
-// class basic_format_arg;
-
-// template<class Visitor>
-// decltype(auto) visit(this basic_format_arg arg, Visitor&& vis);
-// template<class R, class Visitor>
-// R visit(this basic_format_arg arg, Visitor&& vis);
-
-template <class CharT>
-void test() {}
-
-int main(int, char**) {
- test<char>();
-#ifndef TEST_HAS_NO_WIDE_CHARACTERS
- test<wchar_t>();
-#endif
-
- return 0;
-}
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
new file mode 100644
index 00000000000000..2a34b1974aa84d
--- /dev/null
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
@@ -0,0 +1,89 @@
+//===----------------------------------------------------------------------===//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
+
+// <format>
+
+// class basic_format_arg;
+
+// template<class Visitor>
+// decltype(auto) visit(this basic_format_arg arg, Visitor&& vis);
+// template<class R, class Visitor>
+// R visit(this basic_format_arg arg, Visitor&& vis);
+
+#include <algorithm>
+#include <cassert>
+#include <format>
+#include <type_traits>
+
+#include "constexpr_char_traits.h"
+#include "make_string.h"
+#include "min_allocator.h"
+#include "test_macros.h"
+
+TEST_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated-declarations")
+
+template <class Context, class To, class ExpectedResult, class From>
+void test(From value, ExpectedResult expectedValue) {
+ auto store = std::make_format_args<Context>(value);
+ std::basic_format_args<Context> format_args{store};
+
+ LIBCPP_ASSERT(format_args.__size() == 1);
+ assert(format_args.get(0));
+
+ // member
+ {
+ std::same_as<ExpectedResult> decltype(auto) result =
+ format_args.get(0).template visit<ExpectedResult>([v = To(value)](auto a) -> ExpectedResult {
+ if constexpr (std::is_same_v<To, decltype(a)>) {
+ assert(v == a);
+
+ if constexpr (std::is_same_v<ExpectedResult, bool>) {
+ return true;
+ } else if constexpr (std::is_same_v<ExpectedResult, long>) {
+ return 192812079084L;
+ } else {
+ return "visited";
+ }
+ } else {
+ assert(false);
+ return {};
+ }
+ });
+
+ assert(result == expectedValue);
+ }
+}
+
+template <class CharT>
+void test() {
+ using Context = std::basic_format_context<CharT*, CharT>;
+ std::basic_string<CharT> empty;
+ std::basic_string<CharT> str = MAKE_STRING(CharT, "abc");
+
+ // Test boolean types.
+
+ test<Context, bool, std::string>(true, "visited");
+ test<Context, bool, std::string>(false, "visited");
+
+ test<Context, bool, bool>(true, true);
+ test<Context, bool, bool>(false, true);
+
+ test<Context, bool, long>(true, 192812079084L);
+ test<Context, bool, long>(false, 192812079084L);
+}
+
+int main(int, char**) {
+ test<char>();
+#ifndef TEST_HAS_NO_WIDE_CHARACTERS
+ test<wchar_t>();
+#endif
+
+ return 0;
+}
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
index 3a2f9afe2c5f78..c8731a25267d22 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
@@ -14,14 +14,14 @@
// see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
#include <algorithm>
-#include <format>
#include <cassert>
+#include <format>
#include <type_traits>
#include "constexpr_char_traits.h"
-#include "test_macros.h"
#include "make_string.h"
#include "min_allocator.h"
+#include "test_macros.h"
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated-declarations")
>From fbdd8ac0c0d7d96cc35285e0c94a7f18594ccff7 Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Tue, 26 Dec 2023 14:32:34 +0200
Subject: [PATCH 4/7] WIP: visit.return_type.pass
---
libcxx/include/__format/format_arg.h | 51 -------------------
...sit_format_arg.pass.cpp => visit.pass.cpp} | 7 ++-
.../format.arg/visit.return_type.pass.cpp | 9 +++-
3 files changed, 13 insertions(+), 54 deletions(-)
rename libcxx/test/std/utilities/format/format.arguments/format.arg/{visit_format_arg.pass.cpp => visit.pass.cpp} (98%)
diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index eb16326e2a72e8..1724c7c1dd3c8f 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -145,7 +145,6 @@ _LIBCPP_HIDE_FROM_ABI decltype(auto) __visit_format_arg(_Visitor&& __vis, basic_
}
# if _LIBCPP_STD_VER >= 26
-# if 1
template <class _Rp, class _Visitor, class _Context>
_LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
switch (__arg.__type_) {
@@ -194,56 +193,6 @@ _LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<
__libcpp_unreachable();
}
-# else
-template <class _Rp, class _Visitor, class _Context>
-_LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<_Context> __arg) {
- switch (__arg.__type_) {
- case __format::__arg_t::__none:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__monostate_));
- case __format::__arg_t::__boolean:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__boolean_));
- case __format::__arg_t::__char_type:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__char_type_));
- case __format::__arg_t::__int:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__int_));
- case __format::__arg_t::__long_long:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__long_long_));
- case __format::__arg_t::__i128:
-# ifndef _LIBCPP_HAS_NO_INT128
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__i128_));
-# else
- __libcpp_unreachable();
-# endif
- case __format::__arg_t::__unsigned:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__unsigned_));
- case __format::__arg_t::__unsigned_long_long:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__unsigned_long_long_));
- case __format::__arg_t::__u128:
-# ifndef _LIBCPP_HAS_NO_INT128
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__u128_));
-# else
- __libcpp_unreachable();
-# endif
- case __format::__arg_t::__float:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__float_));
- case __format::__arg_t::__double:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__double_));
- case __format::__arg_t::__long_double:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__long_double_));
- case __format::__arg_t::__const_char_type_ptr:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__const_char_type_ptr_));
- case __format::__arg_t::__string_view:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__string_view_));
- case __format::__arg_t::__ptr:
- return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), std::move(__arg.__value_.__ptr_));
- case __format::__arg_t::__handle:
- return std::invoke_r<_Rp>(
- std::forward<_Visitor>(__vis), typename basic_format_arg<_Context>::handle{__arg.__value_.__handle_});
- }
-
- __libcpp_unreachable();
-}
-# endif //
# endif
/// Contains the values used in basic_format_arg.
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
similarity index 98%
rename from libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
rename to libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
index c8731a25267d22..efc52b6cffbda5 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit_format_arg.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
@@ -10,8 +10,13 @@
// <format>
+// class basic_format_arg;
+
+// template<class Visitor>
+// decltype(auto) visit(this basic_format_arg arg, Visitor&& vis); // since C++26
+
// template<class Visitor, class Context>
-// see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg);
+// see below visit_format_arg(Visitor&& vis, basic_format_arg<Context> arg); // deprecated in C++26
#include <algorithm>
#include <cassert>
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
index 2a34b1974aa84d..400f6333f7461d 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
@@ -12,8 +12,6 @@
// class basic_format_arg;
-// template<class Visitor>
-// decltype(auto) visit(this basic_format_arg arg, Visitor&& vis);
// template<class R, class Visitor>
// R visit(this basic_format_arg arg, Visitor&& vis);
@@ -77,6 +75,13 @@ void test() {
test<Context, bool, long>(true, 192812079084L);
test<Context, bool, long>(false, 192812079084L);
+
+ // Test CharT types.
+
+ test<Context, CharT, std::string, CharT>('a', "visited");
+ test<Context, CharT, std::string, CharT>('z', "visited");
+ test<Context, CharT, std::string, CharT>('0', "visited");
+ test<Context, CharT, std::string, CharT>('9', "visited");
}
int main(int, char**) {
>From 91348f1a93bff67867e483cffbc7bac5ff2560d9 Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Wed, 27 Dec 2023 13:52:44 +0200
Subject: [PATCH 5/7] WIP: visit.return_type.pass
---
.../format.arg/visit.pass.cpp | 1 +
.../format.arg/visit.return_type.pass.cpp | 272 ++++++++++++++++--
2 files changed, 253 insertions(+), 20 deletions(-)
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
index efc52b6cffbda5..c3b1609c159c8d 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.pass.cpp
@@ -28,6 +28,7 @@
#include "min_allocator.h"
#include "test_macros.h"
+// Deprecated `std::visit_format_arg` should be tested in C++26 or newer.
TEST_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated-declarations")
template <class Context, class To, class From>
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
index 400f6333f7461d..86a09fee20eebd 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
@@ -25,10 +25,23 @@
#include "min_allocator.h"
#include "test_macros.h"
-TEST_CLANG_DIAGNOSTIC_IGNORED("-Wdeprecated-declarations")
+// The expected result type shouldn't matter,therefore use a hardcoded value for simplicity.
+using ExpectedResultType = bool;
+constexpr ExpectedResultType visited{true};
-template <class Context, class To, class ExpectedResult, class From>
-void test(From value, ExpectedResult expectedValue) {
+template <class ExpectedR>
+ExpectedR make_expected_result() {
+ if constexpr (std::is_same_v<ExpectedR, bool>) {
+ return true;
+ } else if constexpr (std::is_same_v<ExpectedR, long>) {
+ return 192812079084L;
+ } else {
+ return "visited";
+ }
+}
+
+template <class Context, class To, class ExpectedR, class From>
+void test(From value, const ExpectedR& expectedValue) {
auto store = std::make_format_args<Context>(value);
std::basic_format_args<Context> format_args{store};
@@ -37,18 +50,11 @@ void test(From value, ExpectedResult expectedValue) {
// member
{
- std::same_as<ExpectedResult> decltype(auto) result =
- format_args.get(0).template visit<ExpectedResult>([v = To(value)](auto a) -> ExpectedResult {
+ std::same_as<ExpectedR> decltype(auto) result =
+ format_args.get(0).template visit<ExpectedR>([v = To(value)](auto a) -> ExpectedR {
if constexpr (std::is_same_v<To, decltype(a)>) {
assert(v == a);
-
- if constexpr (std::is_same_v<ExpectedResult, bool>) {
- return true;
- } else if constexpr (std::is_same_v<ExpectedResult, long>) {
- return 192812079084L;
- } else {
- return "visited";
- }
+ return make_expected_result<ExpectedR>();
} else {
assert(false);
return {};
@@ -59,6 +65,57 @@ void test(From value, ExpectedResult expectedValue) {
}
}
+// Some types, as an extension, are stored in the variant. The Standard
+// requires them to be observed as a handle.
+template <class Context, class T, class ExpectedR>
+void test_handle(T value, ExpectedR expectedValue) {
+ auto store = std::make_format_args<Context>(value);
+ std::basic_format_args<Context> format_args{store};
+
+ LIBCPP_ASSERT(format_args.__size() == 1);
+ assert(format_args.get(0));
+
+ std::same_as<ExpectedR> decltype(auto) result = format_args.get(0).template visit<ExpectedR>([](auto a) -> ExpectedR {
+ // TODO: This check fails
+ (void) a;
+ // assert((std::is_same_v<decltype(a), typename std::basic_format_arg<Context>::handle>));
+
+ return make_expected_result<ExpectedR>();
+ });
+
+ assert(result == expectedValue);
+}
+
+// Test specific for string and string_view.
+//
+// Since both result in a string_view there's no need to pass this as a
+// template argument.
+template <class Context, class ExpectedR, class From>
+void test_string_view(From value, ExpectedR expectedValue) {
+ auto store = std::make_format_args<Context>(value);
+ std::basic_format_args<Context> format_args{store};
+
+ LIBCPP_ASSERT(format_args.__size() == 1);
+ assert(format_args.get(0));
+
+ using CharT = typename Context::char_type;
+ using To = std::basic_string_view<CharT>;
+ using V = std::basic_string<CharT>;
+
+ std::same_as<ExpectedR> decltype(auto) result =
+ format_args.get(0).template visit<ExpectedR>([v = V(value.begin(), value.end())](auto a) -> ExpectedR {
+ if constexpr (std::is_same_v<To, decltype(a)>) {
+ assert(v == a);
+ return make_expected_result<ExpectedR>();
+ } else {
+ assert(false);
+ return {};
+ }
+ });
+
+ assert(result == expectedValue);
+}
+
template <class CharT>
void test() {
using Context = std::basic_format_context<CharT*, CharT>;
@@ -67,21 +124,196 @@ void test() {
// Test boolean types.
+ test<Context, bool, ExpectedResultType>(true, visited);
+ test<Context, bool, ExpectedResultType>(false, visited);
+
test<Context, bool, std::string>(true, "visited");
test<Context, bool, std::string>(false, "visited");
- test<Context, bool, bool>(true, true);
- test<Context, bool, bool>(false, true);
-
test<Context, bool, long>(true, 192812079084L);
test<Context, bool, long>(false, 192812079084L);
// Test CharT types.
- test<Context, CharT, std::string, CharT>('a', "visited");
- test<Context, CharT, std::string, CharT>('z', "visited");
- test<Context, CharT, std::string, CharT>('0', "visited");
- test<Context, CharT, std::string, CharT>('9', "visited");
+ test<Context, CharT, ExpectedResultType, CharT>('a', visited);
+ test<Context, CharT, ExpectedResultType, CharT>('z', visited);
+ test<Context, CharT, ExpectedResultType, CharT>('0', visited);
+ test<Context, CharT, ExpectedResultType, CharT>('9', visited);
+
+ // Test char types.
+
+ if (std::is_same_v<CharT, char>) {
+ // char to char -> char
+ test<Context, CharT, ExpectedResultType, char>('a', visited);
+ test<Context, CharT, ExpectedResultType, char>('z', visited);
+ test<Context, CharT, ExpectedResultType, char>('0', visited);
+ test<Context, CharT, ExpectedResultType, char>('9', visited);
+ } else {
+ if (std::is_same_v<CharT, wchar_t>) {
+ // char to wchar_t -> wchar_t
+ test<Context, wchar_t, ExpectedResultType, char>('a', visited);
+ test<Context, wchar_t, ExpectedResultType, char>('z', visited);
+ test<Context, wchar_t, ExpectedResultType, char>('0', visited);
+ test<Context, wchar_t, ExpectedResultType, char>('9', visited);
+ } else if (std::is_signed_v<char>) {
+ // char to CharT -> int
+ // This happens when CharT is a char8_t, char16_t, or char32_t and char
+ // is a signed type.
+ // Note if sizeof(CharT) > sizeof(int) this test fails. If there are
+ // platforms where that occurs extra tests need to be added for char32_t
+ // testing it against a long long.
+ test<Context, int, ExpectedResultType, char>('a', visited);
+ test<Context, int, ExpectedResultType, char>('z', visited);
+ test<Context, int, ExpectedResultType, char>('0', visited);
+ test<Context, int, ExpectedResultType, char>('9', visited);
+ } else {
+ // char to CharT -> unsigned
+ // This happens when CharT is a char8_t, char16_t, or char32_t and char
+ // is an unsigned type.
+ // Note if sizeof(CharT) > sizeof(unsigned) this test fails. If there are
+ // platforms where that occurs extra tests need to be added for char32_t
+ // testing it against an unsigned long long.
+ test<Context, unsigned, ExpectedResultType, char>('a', visited);
+ test<Context, unsigned, ExpectedResultType, char>('z', visited);
+ test<Context, unsigned, ExpectedResultType, char>('0', visited);
+ test<Context, unsigned, ExpectedResultType, char>('9', visited);
+ }
+ }
+
+ // Test signed integer types.
+
+ test<Context, int, ExpectedResultType, signed char>(std::numeric_limits<signed char>::min(), visited);
+ test<Context, int, ExpectedResultType, signed char>(0, visited);
+ test<Context, int, ExpectedResultType, signed char>(std::numeric_limits<signed char>::max(), visited);
+
+ test<Context, int, ExpectedResultType, short>(std::numeric_limits<short>::min(), visited);
+ test<Context, int, ExpectedResultType, short>(std::numeric_limits<signed char>::min(), visited);
+ test<Context, int, ExpectedResultType, short>(0, visited);
+ test<Context, int, ExpectedResultType, short>(std::numeric_limits<signed char>::max(), visited);
+ test<Context, int, ExpectedResultType, short>(std::numeric_limits<short>::max(), visited);
+
+ test<Context, int, ExpectedResultType, int>(std::numeric_limits<int>::min(), visited);
+ test<Context, int, ExpectedResultType, int>(std::numeric_limits<short>::min(), visited);
+ test<Context, int, ExpectedResultType, int>(std::numeric_limits<signed char>::min(), visited);
+ test<Context, int, ExpectedResultType, int>(0, visited);
+ test<Context, int, ExpectedResultType, int>(std::numeric_limits<signed char>::max(), visited);
+ test<Context, int, ExpectedResultType, int>(std::numeric_limits<short>::max(), visited);
+ test<Context, int, ExpectedResultType, int>(std::numeric_limits<int>::max(), visited);
+
+ using LongToType = std::conditional_t<sizeof(long) == sizeof(int), int, long long>;
+
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<long>::min(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<int>::min(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<short>::min(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<signed char>::min(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(0, visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<signed char>::max(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<short>::max(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<int>::max(), visited);
+ test<Context, LongToType, ExpectedResultType, long>(std::numeric_limits<long>::max(), visited);
+
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<long long>::min(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<long>::min(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<int>::min(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<short>::min(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<signed char>::min(), visited);
+ test<Context, long long, ExpectedResultType, long long>(0, visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<signed char>::max(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<short>::max(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<int>::max(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<long>::max(), visited);
+ test<Context, long long, ExpectedResultType, long long>(std::numeric_limits<long long>::max(), visited);
+
+#ifndef TEST_HAS_NO_INT128
+ test_handle<Context, __uint128_t, ExpectedResultType>(0, visited);
+#endif // TEST_HAS_NO_INT128
+
+ // Test floating point types.
+
+ test<Context, float, ExpectedResultType, float>(-std::numeric_limits<float>::max(), visited);
+ test<Context, float, ExpectedResultType, float>(-std::numeric_limits<float>::min(), visited);
+ test<Context, float, ExpectedResultType, float>(-0.0, visited);
+ test<Context, float, ExpectedResultType, float>(0.0, visited);
+ test<Context, float, ExpectedResultType, float>(std::numeric_limits<float>::min(), visited);
+ test<Context, float, ExpectedResultType, float>(std::numeric_limits<float>::max(), visited);
+
+ test<Context, double, ExpectedResultType, double>(-std::numeric_limits<double>::max(), visited);
+ test<Context, double, ExpectedResultType, double>(-std::numeric_limits<double>::min(), visited);
+ test<Context, double, ExpectedResultType, double>(-0.0, visited);
+ test<Context, double, ExpectedResultType, double>(0.0, visited);
+ test<Context, double, ExpectedResultType, double>(std::numeric_limits<double>::min(), visited);
+ test<Context, double, ExpectedResultType, double>(std::numeric_limits<double>::max(), visited);
+
+ test<Context, long double, ExpectedResultType, long double>(-std::numeric_limits<long double>::max(), visited);
+ test<Context, long double, ExpectedResultType, long double>(-std::numeric_limits<long double>::min(), visited);
+ test<Context, long double, ExpectedResultType, long double>(-0.0, visited);
+ test<Context, long double, ExpectedResultType, long double>(0.0, visited);
+ test<Context, long double, ExpectedResultType, long double>(std::numeric_limits<long double>::min(), visited);
+ test<Context, long double, ExpectedResultType, long double>(std::numeric_limits<long double>::max(), visited);
+
+ // Test const CharT pointer types.
+
+ test<Context, const CharT*, ExpectedResultType, const CharT*>(empty.c_str(), visited);
+ test<Context, const CharT*, ExpectedResultType, const CharT*>(str.c_str(), visited);
+
+ // Test string_view types.
+
+ {
+ using From = std::basic_string_view<CharT>;
+
+ test_string_view<Context, ExpectedResultType>(From(), visited);
+ test_string_view<Context, ExpectedResultType>(From(empty.c_str()), visited);
+ test_string_view<Context, ExpectedResultType>(From(str.c_str()), visited);
+ }
+ {
+ using From = std::basic_string_view<CharT, constexpr_char_traits<CharT>>;
+
+ test_string_view<Context, ExpectedResultType>(From(), visited);
+ test_string_view<Context, ExpectedResultType>(From(empty.c_str()), visited);
+ test_string_view<Context, ExpectedResultType>(From(str.c_str()), visited);
+ }
+
+ // Test string types.
+
+ {
+ using From = std::basic_string<CharT>;
+
+ test_string_view<Context, ExpectedResultType>(From(), visited);
+ test_string_view<Context, ExpectedResultType>(From(empty.c_str()), visited);
+ test_string_view<Context, ExpectedResultType>(From(str.c_str()), visited);
+ }
+
+ {
+ using From = std::basic_string<CharT, constexpr_char_traits<CharT>, std::allocator<CharT>>;
+
+ test_string_view<Context, ExpectedResultType>(From(), visited);
+ test_string_view<Context, ExpectedResultType>(From(empty.c_str()), visited);
+ test_string_view<Context, ExpectedResultType>(From(str.c_str()), visited);
+ }
+
+ {
+ using From = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT>>;
+
+ test_string_view<Context, ExpectedResultType>(From(), visited);
+ test_string_view<Context, ExpectedResultType>(From(empty.c_str()), visited);
+ test_string_view<Context, ExpectedResultType>(From(str.c_str()), visited);
+ }
+
+ {
+ using From = std::basic_string<CharT, constexpr_char_traits<CharT>, min_allocator<CharT>>;
+
+ test_string_view<Context, ExpectedResultType>(From(), visited);
+ test_string_view<Context, ExpectedResultType>(From(empty.c_str()), visited);
+ test_string_view<Context, ExpectedResultType>(From(str.c_str()), visited);
+ }
+
+ // Test pointer types.
+
+ test<Context, const void*, ExpectedResultType>(nullptr, visited);
+ int i = 0;
+ test<Context, const void*, ExpectedResultType>(static_cast<void*>(&i), visited);
+ const int ci = 0;
+ test<Context, const void*, ExpectedResultType>(static_cast<const void*>(&ci), visited);
}
int main(int, char**) {
>From ebab5b6c85632fbca725995950ac0e10f59b9d8b Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Wed, 27 Dec 2023 17:55:44 +0200
Subject: [PATCH 6/7] Fixed formatting
---
.../format.arguments/format.arg/visit.return_type.pass.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
index 86a09fee20eebd..470df35ef97196 100644
--- a/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
+++ b/libcxx/test/std/utilities/format/format.arguments/format.arg/visit.return_type.pass.cpp
@@ -77,7 +77,7 @@ void test_handle(T value, ExpectedR expectedValue) {
std::same_as<ExpectedR> decltype(auto) result = format_args.get(0).template visit<ExpectedR>([](auto a) -> ExpectedR {
// TODO: This check fails
- (void) a;
+ (void)a;
// assert((std::is_same_v<decltype(a), typename std::basic_format_arg<Context>::handle>));
return make_expected_result<ExpectedR>();
>From 1b8e407a2e4ce95ef14251ebb36b812c93cac744 Mon Sep 17 00:00:00 2001
From: Zingam <zingam at outlook.com>
Date: Wed, 27 Dec 2023 18:02:38 +0200
Subject: [PATCH 7/7] Fixed formatting
---
libcxx/include/__format/format_arg.h | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/libcxx/include/__format/format_arg.h b/libcxx/include/__format/format_arg.h
index 1724c7c1dd3c8f..bc9e13eb9029a5 100644
--- a/libcxx/include/__format/format_arg.h
+++ b/libcxx/include/__format/format_arg.h
@@ -159,21 +159,21 @@ _LIBCPP_HIDE_FROM_ABI _Rp __visit_format_arg(_Visitor&& __vis, basic_format_arg<
case __format::__arg_t::__long_long:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__long_long_);
case __format::__arg_t::__i128:
-# ifndef _LIBCPP_HAS_NO_INT128
+# ifndef _LIBCPP_HAS_NO_INT128
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__i128_);
-# else
+# else
__libcpp_unreachable();
-# endif
+# endif
case __format::__arg_t::__unsigned:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_);
case __format::__arg_t::__unsigned_long_long:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__unsigned_long_long_);
case __format::__arg_t::__u128:
-# ifndef _LIBCPP_HAS_NO_INT128
+# ifndef _LIBCPP_HAS_NO_INT128
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__u128_);
-# else
+# else
__libcpp_unreachable();
-# endif
+# endif
case __format::__arg_t::__float:
return std::invoke_r<_Rp>(std::forward<_Visitor>(__vis), __arg.__value_.__float_);
case __format::__arg_t::__double:
More information about the libcxx-commits
mailing list