From 580cc9dd7a852ecf7790ef2bb3904b176bfeea56 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Sun, 1 Jan 2023 17:17:47 +0100 Subject: [PATCH] [libc++][format] Fixes escaping string literals. D140653 has the same fix, without the extra tests. Fixes PR59763 Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D140819 --- libcxx/include/__format/formatter_string.h | 7 ++++++- .../std/utilities/format/format.tuple/format.functions.tests.h | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/libcxx/include/__format/formatter_string.h b/libcxx/include/__format/formatter_string.h index d1d13791..606fb79 100644 --- a/libcxx/include/__format/formatter_string.h +++ b/libcxx/include/__format/formatter_string.h @@ -65,6 +65,12 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx); +# if _LIBCPP_STD_VER > 20 + if (_Base::__parser_.__type_ == __format_spec::__type::__debug) + return __formatter::__format_escaped_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); +# endif + // When using a center or right alignment and the width option the length // of __str must be known to add the padding upfront. This case is handled // by the base class by converting the argument to a basic_string_view. @@ -76,7 +82,6 @@ struct _LIBCPP_TEMPLATE_VIS _LIBCPP_AVAILABILITY_FORMAT formatter __specs = _Base::__parser_.__get_parsed_std_specifications(__ctx); if (__specs.__has_width() || __specs.__has_precision()) return __formatter::__write_string(basic_string_view<_CharT>{__str}, __ctx.out(), __specs); diff --git a/libcxx/test/std/utilities/format/format.tuple/format.functions.tests.h b/libcxx/test/std/utilities/format/format.tuple/format.functions.tests.h index 64d3ce4..2ad2d87 100644 --- a/libcxx/test/std/utilities/format/format.tuple/format.functions.tests.h +++ b/libcxx/test/std/utilities/format/format.tuple/format.functions.tests.h @@ -153,6 +153,8 @@ void test_pair_int_int(TestFunction check, ExceptionTest check_exception) { template void test_pair_int_string(TestFunction check, ExceptionTest check_exception) { test_tuple_or_pair_int_string(check, check_exception, std::make_pair(42, SV("hello"))); + test_tuple_or_pair_int_string(check, check_exception, std::make_pair(42, STR("hello"))); + test_tuple_or_pair_int_string(check, check_exception, std::make_pair(42, CSTR("hello"))); } // @@ -265,6 +267,8 @@ void test_tuple_int_int(TestFunction check, ExceptionTest check_exception) { template void test_tuple_int_string(TestFunction check, ExceptionTest check_exception) { test_tuple_or_pair_int_string(check, check_exception, std::make_tuple(42, SV("hello"))); + test_tuple_or_pair_int_string(check, check_exception, std::make_tuple(42, STR("hello"))); + test_tuple_or_pair_int_string(check, check_exception, std::make_tuple(42, CSTR("hello"))); } // -- 2.7.4