-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++] Implements Runtime format strings II. #72543
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
libcxx/test/std/utilities/format/format.fmt.string/ctor.runtime-format-string.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <format> | ||
|
||
// template<class charT, class... Args> | ||
// class basic_format_string<charT, type_identity_t<Args>...> | ||
// | ||
// basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {} | ||
// | ||
// Additional testing is done in | ||
// - libcxx/test/std/utilities/format/format.functions/format.runtime_format.pass.cpp | ||
// - libcxx/test/std/utilities/format/format.functions/format.locale.runtime_format.pass.cpp | ||
|
||
#include <format> | ||
#include <cassert> | ||
|
||
#include "test_macros.h" | ||
|
||
int main(int, char**) { | ||
static_assert(noexcept(std::format_string<>{std::runtime_format(std::string_view{})})); | ||
{ | ||
std::format_string<> s = std::runtime_format("}{invalid format string}{"); | ||
assert(s.get() == "}{invalid format string}{"); | ||
} | ||
|
||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
static_assert(noexcept(std::wformat_string<>{std::runtime_format(std::wstring_view{})})); | ||
{ | ||
std::wformat_string<> s = std::runtime_format(L"}{invalid format string}{"); | ||
assert(s.get() == L"}{invalid format string}{"); | ||
} | ||
#endif // TEST_HAS_NO_WIDE_CHARACTERS | ||
|
||
return 0; | ||
} |
85 changes: 85 additions & 0 deletions
85
libcxx/test/std/utilities/format/format.functions/format.locale.runtime_format.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
//===----------------------------------------------------------------------===// | ||
// 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: no-localization | ||
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME | ||
|
||
// XFAIL: availability-fp_to_chars-missing | ||
|
||
// <format> | ||
|
||
// Tests the behavior of | ||
// | ||
// runtime-format-string<char> runtime_format(string_view fmt) noexcept; | ||
// runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept; | ||
// | ||
// and | ||
// | ||
// template<class charT, class... Args> | ||
mordante marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// struct basic_format_string { | ||
// ... | ||
// basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {} | ||
// ... | ||
// } | ||
// | ||
// This is done by testing it in the top-level functions: | ||
// | ||
// template<class... Args> | ||
// string format(const locale& loc, format_string<Args...> fmt, Args&&... args); | ||
// template<class... Args> | ||
// wstring format(const locale& loc, wformat_string<Args...> fmt, Args&&... args); | ||
// | ||
// The basics of runtime_format and basic_format_string's constructor are tested in | ||
// - libcxx/test/std/utilities/format/format.syn/runtime_format_string.pass.cpp | ||
// - libcxx/test/std/utilities/format/format.fmt.string/ctor.runtime-format-string.pass.cpp | ||
|
||
#include <format> | ||
#include <cassert> | ||
#include <locale> | ||
#include <vector> | ||
|
||
#include "test_macros.h" | ||
#include "format_tests.h" | ||
#include "string_literal.h" | ||
#include "assert_macros.h" | ||
#include "concat_macros.h" | ||
|
||
auto test = []<class CharT, class... Args>( | ||
std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr { | ||
std::basic_string<CharT> out = std::format(std::locale(), std::runtime_format(fmt), std::forward<Args>(args)...); | ||
TEST_REQUIRE(out == expected, | ||
TEST_WRITE_CONCATENATED( | ||
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n')); | ||
}; | ||
|
||
auto test_exception = | ||
[]<class CharT, class... Args>( | ||
[[maybe_unused]] std::string_view what, | ||
[[maybe_unused]] std::basic_string_view<CharT> fmt, | ||
[[maybe_unused]] Args&&... args) { | ||
TEST_VALIDATE_EXCEPTION( | ||
std::format_error, | ||
[&]([[maybe_unused]] const std::format_error& e) { | ||
TEST_LIBCPP_REQUIRE( | ||
e.what() == what, | ||
TEST_WRITE_CONCATENATED( | ||
"\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); | ||
}, | ||
TEST_IGNORE_NODISCARD std::format(std::locale(), std::runtime_format(fmt), std::forward<Args>(args)...)); | ||
}; | ||
|
||
int main(int, char**) { | ||
format_tests<char, execution_modus::partial>(test, test_exception); | ||
|
||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
format_tests_char_to_wchar_t(test); | ||
format_tests<wchar_t, execution_modus::partial>(test, test_exception); | ||
#endif | ||
|
||
return 0; | ||
} |
83 changes: 83 additions & 0 deletions
83
libcxx/test/std/utilities/format/format.functions/format.runtime_format.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//===----------------------------------------------------------------------===// | ||
// 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 | ||
|
||
// XFAIL: availability-fp_to_chars-missing | ||
|
||
// <format> | ||
|
||
// Tests the behavior of | ||
// | ||
// runtime-format-string<char> runtime_format(string_view fmt) noexcept; | ||
// runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept; | ||
// | ||
// and | ||
// | ||
// template<class charT, class... Args> | ||
mordante marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// struct basic_format_string { | ||
// ... | ||
// basic_format_string(runtime-format-string<charT> s) noexcept : str(s.str) {} | ||
// ... | ||
// } | ||
// | ||
// This is done by testing it in the top-level functions: | ||
// | ||
// template<class... Args> | ||
// string format(format_string<Args...> fmt, Args&&... args); | ||
// template<class... Args> | ||
// wstring format(wformat_string<Args...> fmt, Args&&... args); | ||
// | ||
// The basics of runtime_format and basic_format_string's constructor are tested in | ||
// - libcxx/test/std/utilities/format/format.syn/runtime_format_string.pass.cpp | ||
// - libcxx/test/std/utilities/format/format.fmt.string/ctor.runtime-format-string.pass.cpp | ||
|
||
#include <format> | ||
#include <cassert> | ||
#include <vector> | ||
|
||
#include "test_macros.h" | ||
#include "format_tests.h" | ||
#include "string_literal.h" | ||
#include "assert_macros.h" | ||
#include "concat_macros.h" | ||
|
||
auto test = []<class CharT, class... Args>( | ||
std::basic_string_view<CharT> expected, std::basic_string_view<CharT> fmt, Args&&... args) constexpr { | ||
std::basic_string<CharT> out = std::format(std::runtime_format(fmt), std::forward<Args>(args)...); | ||
mordante marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TEST_REQUIRE(out == expected, | ||
TEST_WRITE_CONCATENATED( | ||
"\nFormat string ", fmt, "\nExpected output ", expected, "\nActual output ", out, '\n')); | ||
}; | ||
|
||
auto test_exception = | ||
[]<class CharT, class... Args>( | ||
[[maybe_unused]] std::string_view what, | ||
[[maybe_unused]] std::basic_string_view<CharT> fmt, | ||
[[maybe_unused]] Args&&... args) { | ||
TEST_VALIDATE_EXCEPTION( | ||
std::format_error, | ||
[&]([[maybe_unused]] const std::format_error& e) { | ||
TEST_LIBCPP_REQUIRE( | ||
e.what() == what, | ||
TEST_WRITE_CONCATENATED( | ||
"\nFormat string ", fmt, "\nExpected exception ", what, "\nActual exception ", e.what(), '\n')); | ||
}, | ||
TEST_IGNORE_NODISCARD std::format(std::runtime_format(fmt), std::forward<Args>(args)...)); | ||
}; | ||
|
||
int main(int, char**) { | ||
format_tests<char, execution_modus::partial>(test, test_exception); | ||
|
||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
format_tests_char_to_wchar_t(test); | ||
format_tests<wchar_t, execution_modus::partial>(test, test_exception); | ||
#endif | ||
|
||
return 0; | ||
} |
70 changes: 70 additions & 0 deletions
70
libcxx/test/std/utilities/format/format.syn/runtime_format_string.pass.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
|
||
// <format> | ||
|
||
// template<class charT> struct runtime-format-string { // exposition-only | ||
// private: | ||
// basic_string_view<charT> str; // exposition-only | ||
// | ||
// public: | ||
// runtime-format-string(basic_string_view<charT> s) noexcept : str(s) {} | ||
// | ||
// runtime-format-string(const runtime-format-string&) = delete; | ||
// runtime-format-string& operator=(const runtime-format-string&) = delete; | ||
// }; | ||
// | ||
// runtime-format-string<char> runtime_format(string_view fmt) noexcept; | ||
// runtime-format-string<wchar_t> runtime_format(wstring_view fmt) noexcept; | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Additional testing is done in | ||
// - libcxx/test/std/utilities/format/format.functions/format.runtime_format.pass.cpp | ||
// - libcxx/test/std/utilities/format/format.functions/format.locale.runtime_format.pass.cpp | ||
|
||
#include <format> | ||
|
||
#include <cassert> | ||
#include <concepts> | ||
#include <string_view> | ||
#include <type_traits> | ||
|
||
#include "test_macros.h" | ||
|
||
template <class T, class CharT> | ||
static void test_properties() { | ||
static_assert(std::is_nothrow_convertible_v<std::basic_string_view<CharT>, T>); | ||
static_assert(std::is_nothrow_constructible_v<T, std::basic_string_view<CharT>>); | ||
|
||
static_assert(!std::copy_constructible<T>); | ||
static_assert(!std::is_copy_assignable_v<T>); | ||
|
||
static_assert(!std::move_constructible<T>); | ||
static_assert(!std::is_move_assignable_v<T>); | ||
} | ||
|
||
int main(int, char**) { | ||
ldionne marked this conversation as resolved.
Show resolved
Hide resolved
|
||
static_assert(noexcept(std::runtime_format(std::string_view{}))); | ||
auto format_string = std::runtime_format(std::string_view{}); | ||
|
||
using FormatString = decltype(format_string); | ||
LIBCPP_ASSERT((std::same_as<FormatString, std::__runtime_format_string<char>>)); | ||
test_properties<FormatString, char>(); | ||
|
||
#ifndef TEST_HAS_NO_WIDE_CHARACTERS | ||
static_assert(noexcept(std::runtime_format(std::wstring_view{}))); | ||
auto wformat_string = std::runtime_format(std::wstring_view{}); | ||
|
||
using WFormatString = decltype(wformat_string); | ||
LIBCPP_ASSERT((std::same_as<WFormatString, std::__runtime_format_string<wchar_t>>)); | ||
test_properties<WFormatString, wchar_t>(); | ||
#endif // TEST_HAS_NO_WIDE_CHARACTERS | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.