aboutsummaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/std
AgeCommit message (Collapse)AuthorFilesLines
2025-08-04libstdc++: Fix dereferencing of std::indirect xvalues [PR121128]Tomasz Kamiński2-0/+111
Forr rvalues the _Self parameter deduces a non-reference type. Consequently, ((_Self)__self) moved the object to a temporary, which then destroyed on function exit. This patch fixes this by using a C-style cast __self to (const indirect&). This not only resolves the above issue but also correctly handles types that are derived (publicly and privately) from indirect. Allocator requirements in [allocator.requirements.general] p22 guarantee that dereferencing const _M_objp works with equivalent semantics to dereferencing _M_objp. PR libstdc++/121128 libstdc++-v3/ChangeLog: * include/bits/indirect.h (indirect::operator*): Cast __self to approparietly qualified indirect. * testsuite/std/memory/indirect/access.cc: New test. * testsuite/std/memory/polymorphic/access.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-07-30libstdc++: Fix test when dual abi disabledFrançois Dumont1-2/+2
When !_GLIBCXX_USE_DUAL_ABI the old COW std::string implementation is being used which do not generate the expected error diagnostics. libstdc++-v3/ChangeLog: * testsuite/std/time/format/data_not_present_neg.cc: Remove _GLIBCXX_USE_DUAL_ABI check. Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-07-29libsdc++: Test using range_format::map as format_kind.Tomasz Kamiński1-1/+3
This adderess TODO from the test file. libstdc++-v3/ChangeLog: * testsuite/std/format/ranges/format_kind.cc: New test. Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-07-25c++: Unwrap type traits defined in terms of builtins within diagnostics ↵Nathaniel Shead2-0/+3
[PR117294] Currently, concept failures of standard type traits just report 'expression X<T> evaluates to false'. However, many type traits are actually defined in terms of compiler builtins; we can do better here. For instance, 'is_constructible_v' could go on to explain why the type is not constructible, or 'is_invocable_v' could list potential candidates. Apart from concept diagnostics, this is also useful when using such traits in a 'static_assert' directly, so this patch also adjusts the diagnostics in that context. As a first step to supporting that we need to be able to map the standard type traits to the builtins that they use. Rather than adding another list that would need to be kept up-to-date whenever a builtin is added, this patch instead tries to detect any variable template defined directly in terms of a TRAIT_EXPR. This patch also adjusts 'diagnose_trait_expr' to provide more helpful diagnostics for these cases. Not all type traits have yet been updated, this patch just updates those that seem particularly valuable or straight-forward. The function also gets moved to cp/semantics.cc to be closer to 'trait_expr_value'. Various other parts of the compiler are also adjusted here to assist in making clear diagnostics, such as making more use of 'is_stub_object' to refer to a type directly rather than in terms of 'std::declval<T>()'. Additionally, since there are now more cases of nesting within a 'static_assert'ion I felt it was helpful for the experimental-nesting mode to nest here as well. PR c++/117294 PR c++/113854 gcc/cp/ChangeLog: * call.cc (implicit_conversion_error): Hide label when printing a stub object. (convert_like_internal): Likewise, and nest candidate diagnostics. * constexpr.cc (diagnose_failing_condition): Nest diagnostics, attempt to provide more helpful diagnostics for traits. * constraint.cc (satisfy_atom): Pass result before constant evaluation to diagnose_atomic_constraint. (diagnose_trait_expr): Adjust diagnostics for clarity and detail. (maybe_diagnose_standard_trait): New function. (diagnose_atomic_constraint): Attempt to provide more helpful diagnostics for more traits. * cp-tree.h (explain_not_noexcept): Declare new function. (is_trivially_xible): Add parameter. (is_nothrow_xible): Likewise. (is_xible): Likewise. (is_convertible): Likewise. (is_nothrow_convertible): Likewise. (diagnose_trait_expr): Declare new function. (maybe_diagnose_standard_trait): Declare new function. * error.cc (dump_type) <case TREE_VEC>: Handle trait types. * except.cc (explain_not_noexcept): New function. * method.cc (build_trait_object): Add complain parameter. (build_invoke): Propagate complain parameter. (assignable_expr): Add explain parameter to show diagnostics. (constructible_expr): Likewise. (destructible_expr): Likewise. (is_xible_helper): Replace trivial flag with explain flag, add diagnostics. (is_trivially_xible): New explain flag. (is_nothrow_xible): Likewise. (is_xible): Likewise. (is_convertible_helper): Add complain flag. (is_convertible): New explain flag. (is_nothrow_convertible): Likewise. * typeck.cc (cp_build_function_call_vec): Add handling for stub objects. (convert_arguments): Always return -1 on error. * typeck2.cc (cxx_readonly_error): Add handling for stub objects. libstdc++-v3/ChangeLog: * testsuite/20_util/any/misc/any_cast_neg.cc: Adjust diagnostics. * testsuite/20_util/expected/illformed_neg.cc: Likewise. * testsuite/20_util/optional/monadic/or_else_neg.cc: Likewise. * testsuite/23_containers/array/creation/3_neg.cc: Likewise. * testsuite/24_iterators/range_generators/lwg3900.cc: Likewise. * testsuite/29_atomics/atomic/requirements/types_neg.cc: Likewise. * testsuite/30_threads/stop_token/stop_callback/invocable_neg.cc: Likewise. * testsuite/30_threads/stop_token/stop_callback/destructible_neg.cc: Likewise. * testsuite/std/format/arguments/args_neg.cc: Likewise. * testsuite/std/format/string_neg.cc: Likewise. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/concepts-traits3.C: Adjust diagnostics. * g++.dg/cpp2a/concepts-traits4.C: New test. * g++.dg/diagnostic/static_assert5.C: New test. * g++.dg/ext/has_virtual_destructor2.C: New test. * g++.dg/ext/is_assignable2.C: New test. * g++.dg/ext/is_constructible9.C: New test. * g++.dg/ext/is_convertible7.C: New test. * g++.dg/ext/is_destructible3.C: New test. * g++.dg/ext/is_invocable6.C: New test. * g++.dg/ext/is_virtual_base_of_diagnostic2.C: New test. Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com> Reviewed-by: Patrick Palka <ppalka@redhat.com> Reviewed-by: Jason Merrill <jason@redhat.com>
2025-07-18libstdc++: Fixed localized empty-spec formatting for months/weekdays [PR121154]Tomasz Kamiński2-0/+9
Previously for localized output, if _M_debug option was set, the _M_check_ok completed succesfully and _M_locale_fmt was called for months/weekdays that are !ok(). This patch lifts debug checks from each conversion function into _M_check_ok, that in case of !ok() values return a string_view containing the kind of calendar data, to be included after "is not a valid" string. The localized output (_M_locale_fmt) is not used if string is non-empty. Emitting of this message is now handled in _M_format_to, further reducing each specifier function. To handle weekday (%a,%A) and month (%b,%B), _M_check_ok now accepts a mutable reference to conversion specifier, and updates it to corresponding numeric value (%w, %m). Extra care needs to be taken to handle a month(0) that needs to be printed as single digit in debug format. Finally, the _M_time_point is replaced with _M_needs_ok_check member, that indicates if input contains any user-suplied values that are checked for being ok() and these values are referenced in chrono-specs. PR libstdc++/121154 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (_ChronoSpec::_M_time_point): Remove. (_ChronoSpec::_M_needs_ok_check): Define (__formatter_chrono::_M_parse): Set _M_needs_ok_check. (__formatter_chrono::_M_check_ok): Check values also for debug mode, and return __string_view. (__formatter_chrono::_M_format_to): Handle results of _M_check_ok. (__formatter_chrono::_M_wi, __formatter_chrono::_M_a_A) (__formatter_chrono::_M_b_B, __formatter_chrono::_M_C_y_Y) (__formatter_chrono::_M_d_e, __formatter_chrono::_M_F): Removed handling of _M_debug. (__formatter_chrono::__M_m): Print zero unpadded in _M_debug mode. (__formatter_duration::_S_spec_for): Remove _M_time_point refernce. (__formatter_duration::_M_parse): Override _M_needs_ok_check. * testsuite/std/time/month/io.cc: Test for localized !ok() values. * testsuite/std/time/weekday/io.cc: Test for localized !ok() values.
2025-07-15libstdc++: Add missing initializers for __maybe_present_t members [PR119962]Patrick Palka3-0/+24
Data members of type __maybe_present_t where the conditionally present type might be an aggregate or fundamental type need to be explicitly value-initialized (rather than implicitly default-initialized), so that default-initialization of the containing class always results in an completely initialized object. PR libstdc++/119962 libstdc++-v3/ChangeLog: * include/std/ranges (join_view::_Iterator::_M_outer): Initialize. (lazy_split_view::_OuterIter::_M_current): Initialize. (join_with_view::_Iterator::_M_outer_it): Initialize. * testsuite/std/ranges/adaptors/join.cc (test15): New test. * testsuite/std/ranges/adaptors/join_with/1.cc (test05): New test. * testsuite/std/ranges/adaptors/lazy_split.cc (test13): New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-07-09libstdc++: Added missing members to numeric_limits specializations for ↵Mateusz Zych1-0/+31
integer-class types [iterator.concept.winc]/11 says that std::numeric_limits should be specialized for integer-class types, with each member defined appropriately. libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (numeric_limits<__max_size_type>): New members. (numeric_limits<__max_diff_type>): Likewise. * testsuite/std/ranges/iota/max_size_type.cc: New test cases. Signed-off-by: Mateusz Zych <mte.zych@gmail.com>
2025-07-08libstdc++: Do not expose set_brackets/set_separator for formatter with ↵Tomasz Kamiński1-0/+52
format_kind other than sequence [PR119861] The standard defines separate specializations of range-default-formatter, out of which only one for range_format::sequence provide the set_brackets and set_separator methods. We implemented it as one specialization and exposed this method for range_format other than string or debug_string, i.e. when range_formatter was used as underlying formatter. PR libstdc++/119861 libstdc++-v3/ChangeLog: * include/std/format (formatter<_Rg, _CharT>::set_separator) (formatter<_Rg, _CharT>::set_brackets): Constrain with (format_kind<_Rg> == range_format::sequence). * testsuite/std/format/ranges/pr119861_neg.cc: New test.
2025-07-07libstdc++: Format chrono %a/%A/%b/%h/%B/%p using locale's time_put [PR117214]XU Kailiang2-0/+44
C++ formatting locale could have a custom time_put that performs differently from the C locale, so do not use __timepunct directly, instead all of above specifiers use _M_locale_fmt. For %a/%A/%b/%h/%B, the code handling the exception is now moved to the _M_check_ok function, that is invoked before handling of the conversion specifier. For time_points the values of months/weekday are computed, and thus are always ok(), this information is indicated by new _M_time_point member of the _ChronoSpec. The different behavior of j specifier for durations and time_points/calendar types, is now handled using only _ChronoParts, and _M_time_only in _ChronoSpec is no longer needed, thus it was removed. PR libstdc++/117214 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (_ChronoSpec::_M_time_only): Remove. (_ChronoSpec::_M_time_point): Define. (__formatter_chrono::_M_parse): Use __parts to determine interpretation of j. (__formatter_chrono::_M_check_ok): Define. (__formatter_chrono::_M_format_to): Invoke _M_check_ok. (__formatter_chrono::_M_a_A, __formatter_chrono::_M_b_B): Move exception throwing to _M_check_ok. (__formatter_chrono::_M_j): Use _M_needs to define interpretation. (__formatter_duration::_S_spec_for): Set _M_time_point. * testsuite/std/time/format/format.cc: Test for exception for !ok() months/weekday. * testsuite/std/time/format/pr117214_custom_timeput.cc: New test. Co-authored-by: Tomasz Kaminski <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: XU Kailiang <xu2k3l4@outlook.com> Signed-off-by: Tomasz Kaminski <tkaminsk@redhat.com>
2025-07-03libstdc++: Update LWG 4166 changes to concat_view::end() [PR120934]Patrick Palka1-0/+13
In r15-4555-gf191c830154565 we proactively implemented the initial proposed resolution for LWG 4166 which later turned out to be insufficient, since we must also require equality_comparable of the underlying iterators before concat_view could be a common range. This patch implements the updated P/R, requiring all underlying iterators to be forward (which implies equality_comparable) before making concat_view common, which fixes the testcase from this PR. PR libstdc++/120934 libstdc++-v3/ChangeLog: * include/std/ranges (concat_view::end): Refine condition for returning an iterator instead of default_sentinel as per the updated P/R for LWG 4166. * testsuite/std/ranges/concat/1.cc (test05): New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-27libstdc++: Fix warnings introduced by type-erasing for chrono commits [PR110739]Tomasz Kamiński1-17/+26
The r16-1709-g4b3cefed1a08344495fedec4982d85168bd8173f caused `-Woverflow` in empty_spec.cc file. This warning is not cause by any issue in shipping code, and results in taking to much shortcut when implementing a test-only custom representation type Rep, where long was always used to store a value. In particular common type for Rep and long long int, was de-facto long. This is addressed by adding Under template parameter, that controls the type of stored value, and handling it properly in common_type specializations. No changes to shipping code are necessary. Secondly, extracting _M_locale_fmt calls in r16-1712-gcaac94, resulted in __ctx format parameter no longer being used. This patch removes such parameter entirely, and replace _FormatContext template parameter, with _OutIter parameter for __out. For consistency type of the __out is decoupled from _FormatContext, for functions that still need context: * to extract locale (_M_A_a, _M_B_b, _M_c, _M_p, _M_r, _M_subsecs) * perform formatting for duration/subseconds (_M_Q, _M_T, _M_S, _M_subsecs) PR libstdc++/110739 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono::_M_format_to): Rename _Out to _OutIter for consistency, and update calls to specifier functions. (__formatter_chrono::_M_wi, __formatter_chrono::_M_C_y_Y) (__formatter_chrono::_M_D_x, __formatter_chrono::_M_d_e) (__formatter_chrono::_M_F, __formatter_chrono::_M_g_G) (__formatter_chrono::_M_H_I, __formatter_chrono::_M_j) (__formatter_chrono::_M_m, __formatter_chrono::_M_M) (__formatter_chrono::_M_q, __formatter_chrono::_M_R_X) (__formatter_chrono::_M_u_w, __formatter_chrono::_M_U_V_W) (__formatter_chrono::_M_z, __formatter_chrono::_M_z): Remove _FormatContext parameter, and introduce _OutIter for __out type. (__formatter_chrono::_M_a_A, __formatter_chrono::_M_B_b) (__formatter_chrono::_M_p, __formatter_chrono::_M_Q) (__formatter_chrono::_M_r, __formatter_chrono::_M_S) (__formatter_chrono::_M_subsecs, __formatter_chrono::_M_T): Introduce separate _OutIter template parameter for __out. (__formatter_chrono::_M_c, __formatter_chrono::_M_T): Likewise, and adjust calls to specifiers functions. * testsuite/std/time/format/empty_spec.cc: Make underlying type for Rep configurable.
2025-06-26libstdc++: Type-erase chrono-data for formatting [PR110739]Tomasz Kamiński1-8/+8
This patch reworks the formatting for the chrono types, such that they are all formatted in terms of _ChronoData class, that includes all required fields. Populating each required field is performed in formatter for specific type, based on the chrono-spec used. To facilitate above, the _ChronoSpec now includes additional _M_needed field, that represnts the chrono data that is referenced by format spec (this value is also configured for __defSpec). This value differs from the value of __parts passed to _M_parse, which does include all fields that can be computed from input (e.g. weekday_indexed can be computed for year_month_day). Later it is used to fill _ChronoData, in particular _M_fill_* family of functions, to determine if given field needs to be set, and thus its value needs to be computed. In consequence _ChronoParts enum was extended with additional values, that allows more fine grained identification: * _TimeOfDay is separated into _HoursMinutesSeconds and _Subseconds, * _TimeZone is separated into _ZoneAbbrev and _ZoneOffset, * _LocalDays, _WeekdayIndex are defined and in included in _Date, * _Duration is removed, and instead _EpochUnits and _UnitSuffix are introduced. Furthermore, to avoid name conflicts _ChonoParts is now defined as enum class, with additional operators that simplify uses. In addition to fields that can be printed using chrono-spec, _ChronoData stores: * Total days in wall time (_M_ldays), day of year (_M_day_of_year) - used by struct tm construction, and for ISO calendar computation. * Total seconds in wall time (_M_lseconds) - this value may be different from sum of days, hours, minutes, seconds (e.g. see utc_time below). Included to allow future extension, like printing total minutes. * Total seconds since epoch - due offset different from above. Again to be used with future extension (e.g. %s as proposed in P2945R1). * Subseconds - count of attoseconds (10^(-18)), in addition to printing can be used to compute fractional hours, minutes. The both total seconds fields use single _TotalSeconds enumerator in _ChronoParts, that when present in combination with _EpochUnits or _LocalDays indicates that _M_eseconds (_EpochSeconds) or _M_lseconds (_LocalSeconds) are provided/required. To handle type formatting of time since epoch ('%Q'|_EpochUnits), we use the format_args mechanism, where the result of +d.count() (see LWG4118) is erased into make_format_args to local __arg_store, that is later referenced by _M_ereps (_M_ereps.get(0)). To handle precision values, and in prepartion to allow user to configure ones, we store the precision as third element of _M_ereps (_M_ereps.get(2)), this allows duration with precision to be printed using "{0:{2}}". For subseconds the precision is handled differently depending on the representation: * for integral reps, _M_subseconds value is used to determine fractional value, precision is trimmed to 18 digits; * for floating-points, _M_ereps stores duration<Rep> initialized with only fractional seconds, that is later formatted with precision. Always using _M_subseconds fields for integral duration, means that we do not use formattter for user-defined durations that are considered to be integral (see empty_spec.cc file change). To avoid potentially expensive computation of _M_subseconds, we make sure that _ChronoParts::_Subseconds is set only if _Subseconds are needed. In particular we remove this flag for localized ouput in _M_parse. Construction of the _M_ereps as described above is handled by __formatter_duration, that is then used to format duration, hh_mm_ss and time_points specializations. This class also handles _UnitSuffix, the _M_units_suffix field is populated either with predefined suffix (chrono::__detail::__units_suffix) or one produced locally. Finally, formatters for types listed below contains type specific logic: * hh_mm_ss - we do not compute total duration and seconds, unless explicitly requested, as such computation may overflow; * utc_time - for time during leap second insertion, the _M_seconds field is increased to 60; * __local_time_fmt - exception is thrown if zone offset (_ZoneOffset) or abbrevation (_ZoneAbbrev) is requsted, but corresponding pointer is null, futhermore conversion from `char` to `wchar_t` for abbreviation is performed if needed. PR libstdc++/110739 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__format::__no_timezone_available): Removed, replaced with separate throws in formatter for __local_time_fmt (__format::_ChronoParts): Defined additional enumertors and declared as enum class. (__format::operator&(_ChronoParts, _ChronoParts)) (__format::operator&=(_ChronoParts&, _ChronoParts)) (__format::operator-(_ChronoParts, _ChronoParts)) (__format::operator-=(_ChronoParts&, _ChronoParts)) (__format::operator==(_ChronoParts, decltype(nullptr))) (_ChronoSpec::_M_time_only, _ChronoSpec::_M_floating_point_rep) (_ChronoSpec::_M_custom_rep, _ChronoSpec::_M_needed) (_ChronoSpec::_M_needs, __format::_ChronoData): Define. (__format::__formatter_chrono): Redefine to accept _ChronoData. (__formatter_chrono::_M_format_to_ostream): Moved to __formatter_duration. (__format::__formatter_duration): Define. (__formatter_chrono_info::format): Pass value-constructed _ChronoData. (std::formatter<chrono::day, _CharT>) (std::formatter<chrono::month, _CharT>) (std::formatter<chrono::year, _CharT>) (std::formatter<chrono::weekday, _CharT>) (std::formatter<chrono::weekday_indexed, _CharT>) (std::formatter<chrono::weekday_last, _CharT>) (std::formatter<chrono::month_day, _CharT>) (std::formatter<chrono::month_day_last, _CharT>) (std::formatter<chrono::month_weekday, _CharT>) (std::formatter<chrono::month_weekday_indexed, _CharT>) (std::formatter<chrono::month_weekday_last, _CharT>) (std::formatter<chrono::year_month, _CharT>) (std::formatter<chrono::year_month_day, _CharT>) (std::formatter<chrono::year_month_day_last, _CharT>) (std::formatter<chrono::year_month_weekday, _CharT>) (std::formatter<chrono::year_month_weekday_indexed, _CharT>) (std::formatter<chrono::year_month_weekday_last, _CharT>): Construct _ChronoData in format, and configure _M_needed in _ChronoSpec. (std::formatter<chrono::duration<_Rep, _Period>, _CharT>) (std::formatter<chrono::hh_mm_ss<_Duration>, _CharT>) (std::formatter<chrono::sys_time<_Duration>, _CharT>) (std::formatter<chrono::utc_time<_Duration>, _CharT>) (std::formatter<chrono::tai_time<_Duration>, _CharT>) (std::formatter<chrono::gps_time<_Duration>, _CharT>) (std::formatter<chrono::file_time<_Duration>, _CharT>) (std::formatter<chrono::local_time<_Duration>, _CharT>) (std::formatter<chrono::_detail::__local_time_fmt<_Duration>, _CharT>): Reworked in terms of __formatter_duration and _ChronoData. (std::formatter<chrono::_detail::__utc_leap_second<_Duration>, _CharT>): Removed. (_Parser<_Duration>::operator()): Adjusted for _ChronoParts being enum class. * include/std/chrono (__detail::__utc_leap_second): Removed, replaced with simply bumping _M_seconds in _ChronoData. * testsuite/std/time/format/empty_spec.cc: Updated %S integral ouput. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-25libstdc++: Test for %S precision for durations with integral representation.Tomasz Kamiński1-5/+99
Existing test are extented to cover cases where not precision is specified, or it is specified to zero. The precision value is ignored in all cases. libstdc++-v3/ChangeLog: * testsuite/std/time/format/precision.cc: New tests.
2025-06-25libstdc++: Report compilation error on formatting "%d" from month_last ↵Tomasz Kamiński1-0/+164
[PR120650] For month_day we incorrectly reported day information to be available, which lead to format_error being thrown from the call to formatter::format at runtime, instead of making call to format ill-formed. The included test cover most of the combinations of _ChronoParts and format specifiers. PR libstdc++/120650 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (formatter<chrono::month_day_last,_CharT>::parse): Call _M_parse with only Month being available. * testsuite/std/time/format/data_not_present_neg.cc: New test.
2025-06-13libstdc++: Format %r, %x and %X using locale's time_put facet [PR120648]Tomasz Kamiński1-5/+38
Similarly to issue reported for %c in PR117214, the format string for locale specific time (%r, %X) and date (%x) representations may contain specifiers not accepted by chrono-spec, leading to exception being thrown. This happened for following conversion specifier and locale combinations: * %r, %X for aa_DJ.UTF-8, ar_SA.UTF-8 * %x for ca_AD.UTF-8, my_MM.UTF-8 This fix follows approach from r15-8490-gc24a1d5, and uses time_put to emit localized date format. The existing _M_c is reworked to handle all locale dependent conversion specifies, by accepting them as argument. It is also renamed to _M_c_r_x_X. PR libstdc++/120648 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono::_M_format_to): Handle %c, %r, %x and %X by passing them to _M_c_r_x_X. (__formatter_chrono::_M_c_r_x_X): Reworked from _M_c. (__formatter_chrono::_M_c): Renamed into above. (__formatter_chrono::_M_r, __formatter_chrono::_M_x) (__formatter_chrono::_M_X): Removed. * testsuite/std/time/format/pr117214.cc: New tests for %r, %x, %X with date, time and durations.
2025-06-13libstdc++: Replace _CharT template parameter with CharT in format tests.Tomasz Kamiński11-168/+166
As pointed out by Daniel Krügler we do not need to use reserved name in tests. libstdc++-v3/ChangeLog: * testsuite/23_containers/vector/bool/format.cc: Replaced _CharT with CharT. * testsuite/std/format/debug.cc: Likewise. * testsuite/std/format/ranges/adaptors.cc: Likewise. * testsuite/std/format/ranges/formatter.cc: Likewise. * testsuite/std/format/ranges/map.cc: Likewise. * testsuite/std/format/ranges/sequence.cc: Likewise. * testsuite/std/format/ranges/string.cc: Likewise. * testsuite/std/format/tuple.cc: Likewise. * testsuite/std/time/format/empty_spec.cc: Likewise. * testsuite/std/time/format/pr120114.cc: Likewise. * testsuite/std/time/format/pr120481.cc: Likewise. * testsuite/std/time/format/precision.cc: Likewise.
2025-06-13libstdc++: Test chrono-spec containing only whitespaces.Tomasz Kamiński1-0/+56
libstdc++-v3/ChangeLog: * testsuite/std/time/format/whitespace.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-12libstdc++: Do not specialize std::formatter for incomplete type [PR120625]Jonathan Wakely1-0/+19
Using an incomplete type as the template argument for std::formatter specializations causes problems for program-defined specializations of std::formatter which have constraints. When the compiler has to find which specialization of std::formatter to use for the incomplete type it considers the program-defined specializations and checks to see if their constraints are satisfied, which can give errors if the constraints cannot be checked for incomplete types. This replaces the base class of the disabled specializations with a concrete class __formatter_disabled, so there is no need to match a specialization and no more incomplete type. libstdc++-v3/ChangeLog: PR libstdc++/120625 * include/std/format (__format::__disabled): Remove. (__formatter_disabled): New type. (formatter<char*, wchar_t>, formatter<const char*, wchar_t>) (formatter<char[N], wchar_t>, formatter<string, wchar_t>) (formatter<string_view, wchar_t>): Use __formatter_disabled as base class instead of formatter<__disabled, wchar_t>. * testsuite/std/format/formatter/120625.cc: New test. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-11libstdc++: Test for precision and floting point durations.Tomasz Kamiński2-0/+158
libstdc++-v3/ChangeLog: * testsuite/std/time/format/empty_spec.cc: New tests. * testsuite/std/time/format/precision.cc: New test.
2025-06-10libstdc++: Make __max_size_type and __max_diff_type structuralPatrick Palka1-0/+7
This patch makes these integer-class types structural types by public-izing their data members so that they could be used as NTTP types. I don't think this is required by the standard, but it seems like a useful extension. libstdc++-v3/ChangeLog: * include/bits/max_size_type.h (__max_size_type::_M_val): Make public instead of private. (__max_size_type::_M_msb): Likewise. (__max_diff_type::_M_rep): Likewise. * testsuite/std/ranges/iota/max_size_type.cc: Verify __max_diff_type and __max_size_type are structural. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Jonathan Wakely <jwakely@redhat.com>
2025-06-06libstdc++: Support wide characters output for sys_info and local_info [PR120565]Tomasz Kamiński1-4/+4
Formatting sys_info as wchar_t require widening of the abbrev (zone) member. To support that we reuse the existing code in support for '%Z' specifier, for local_time_format, and produce output using singe format call with "[{0:%F %T},{1:%F %T},{2:%T},{3:%Q%q},{0:%Z}]" format string. As noted in the comment, produced output is locale independed, as it does not contain decimal separtors. For sys_info, the outputed literals are widended using _GLIBCXX_WIDEN, except opening and closing brackets, that are fetched from __format::_Separators. PR libstdc++/120565 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (operator<<(basic_ostream<_CharT, _Traits>&, const sys_info&)) (operator<<(basic_ostream<_CharT, _Traits>&, const local_info&)): Support wchar_t as _CharT. * testsuite/std/time/format/empty_spec.cc: Instantiated test_infos for wchar_t and increase timeout. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-06libstdc++: Test for formatting with empty spec for local_info and sys_info.Tomasz Kamiński1-3/+108
We do not test with wchar_t currently, as operator<< (and format) are ill-formed in such case, because we do not widen abbrev member of local_info. Adding a tests for behavior of the ostream operator and the formatting with empty chrono-spec for the chrono types. The coverage is now complete: * sys_info, local_info in this commit, * time point, zoned_time and local_time_format in r16-1107-g3cfa53aa95a19c, * duration and hh_mm_ss in r16-1099-gac0a04b7a254fb, * calendar types in r16-1016-g28a17985dd34b7. Finally, the timeout for the test was increased again and preprocessor checks for zoned_time where changed to _GLIBCXX_USE_CXX11_ABI || !_GLIBCXX_USE_DUAL_ABI to match ones from bits/chrono_io.h file. libstdc++-v3/ChangeLog: * testsuite/std/time/format/empty_spec.cc: New tests and increased timeout. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-05libstdc++: Fix std::format thousands separators when sign present [PR120548]Jonathan Wakely1-0/+10
The leading sign character should be skipped when deciding whether to insert thousands separators into a floating-point format. libstdc++-v3/ChangeLog: PR libstdc++/120548 * include/std/format (__formatter_fp::_M_localize): Do not include a leading sign character in the string to be grouped. * testsuite/std/format/functions/format.cc: Check grouping when sign is present in the output. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-05libstdc++: Fix formatting of 3-digits months,day,weekday and hour [PR120481]Tomasz Kamiński2-8/+328
This patch fixes the handle multiple digits values for the month, day, weekday and hour, when used with the %m, %d, %e, %m, %u, %w, %H, and %D, %F specifiers. The values are now printed unmodified. This patch also fixes printing negative year with %F, where the values was not padded to four digits. Furthemore, the %I,%p are adjusted to handle input with hours values set to over 24 hours. In the case the values is interpretd modulo 24. This was already the case for %r (locale's 12-hour clock), as we convert the input into seconds. In case of %u, %w we print values unchanged, this makes the behavior of this specifiers equivalent to printing the iso_encoding and c_encoding respectively. As constructing weekday from value 7, initializes it with 0, the !ok() weekdays values are always greater of equal eight, so they are clearly distinguishable. The months, weekday, day values that can have 3 decimal digit as maximum (range [0, 255]), we are using new _S_str_d1, _S_str_d2 that return string_view containing textual representation, without padding or padded to two digits. This function accepts are 3 character buffer, that are used for 3 digits number. In other cases, we return _S_digit and _S_two_digits result directly. The former is changed to return string_view to facilitate this. For %F and %D when at least one component have more digits that expected (2 for month and day, 4 for year), we produce output using format_to with appropriate format string. Otherwise the representation is produced in local char buffer. Two simply fill this buffer, _S_fill_two_digits function was added. We also make sure that minus is not included in year width for %F. The handling of %C, %Y, %y was adjusted to use similar pattern, for years with more than two digits. To support that the order of characters in _S_chars was adjusted so it contain "-{}" string. For handling of %H, we print 3 or more digits values using format_to. The handling for large hours values in %T and %R was changed, so they printed using format_to, and otherwise we use same stack buffer as for minutes to print them. PR libstdc++/120481 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__format::_S_chars): Reorder so it contains "-{}". (__format::_S_colon, __format::_S_slash, __format::_S_space) (__format::_S_plus_minus): Updated starting indicies. (__format::_S_minus_empty_spec): Define. (__formatter_chrono::_M_C_y_Y, __formatter_chrono::_M_R_T): Rework implementation. (__formatter_chrono::_M_d_e, __formatter_chrono::_M_F) (__formatter_chrono::_M_m, __formatter_chrono::_M_u_w) (__formatter_chrono::_M_H_I, __formatter_chrono::_M_p): Handle multi digits values. (__formatter_chrono::_S_digit): Return string view. (__formatter_chrono::_S_str_d1, __formatter_chrono::_S_str_d2) (__formatter_chrono::_S_fill_two_digits): Define. * testsuite/std/time/format/empty_spec.cc: Update test for year_month_day, that uses '%F'. * testsuite/std/time/format/pr120481.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-04libstdc++: Skip time zone format testing for COW std::stringJonathan Wakely1-0/+4
This is needed when testing with -D_GLIBCXX_USE_CXX11_ABI=0 to fix: FAIL: std/time/format/empty_spec.cc -std=gnu++20 (test for excess errors) libstdc++-v3/ChangeLog: * testsuite/std/time/format/empty_spec.cc: Only test time zones for cxx11 string ABI.
2025-06-04libstdc++: Test for formatting with empty spec for time points.Tomasz Kamiński1-14/+194
Adding a tests for behavior of the ostream operator and the formatting with empty chrono-spec for the chrono types. Current coverage is: * time point, zoned_time and local_time_format in this commit, * duration and hh_mm_ss in r16-1099-gac0a04b7a254fb, * calendar types in r16-1016-g28a17985dd34b7. libstdc++-v3/ChangeLog: * testsuite/std/time/format/empty_spec.cc: New tests. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-04libstdc++: Fix format call and test formatting with empty specs for durations.Tomasz Kamiński1-0/+271
This patches fixes an obvious error, where the output iterator argument was missing for call to format_to, when duration with custom representation types are used. It's also adding the test for behavior of ostream operator and the formatting with empty chron-spec for the chrono types. Current coverage is: * duration and hh_mm_ss in this commit, * calendar types in r16-1016-g28a17985dd34b7. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono:_M_s): Add missing __out argument to format_to call. * testsuite/std/time/format/empty_spec.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-06-02libstdc++: Implement C++26 std::polymorphic [PR119152]Jonathan Wakely8-0/+1394
This patch implements C++26 std::polymorphic as specified in P3019 with amendment to move assignment from LWG 4251. The implementation always allocate stored object on the heap. The manager function (_M_manager) is similary keep with the object (polymorphic::_Obj), which reduces the size of the polymorphic to size of the single pointer plus allocator (that is declared with [[no_unique_address]]). The implementation does not not use small-object optimization (SSO). We may consider adding this in the future, as SSO is allowed by the standard. However, storing any polimorphic object will require providing space for two pointers (manager function and vtable pointer) and user-declared data members. PR libstdc++/119152 libstdc++-v3/ChangeLog: * include/bits/indirect.h (std::polymorphic, pmr::polymorphic) [__glibcxx_polymorphic]: Define. * include/bits/version.def (polymorphic): Define. * include/bits/version.h: Regenerate. * include/std/memory: Define __cpp_lib_polymorphic. * testsuite/std/memory/polymorphic/copy.cc: New test. * testsuite/std/memory/polymorphic/copy_alloc.cc: New test. * testsuite/std/memory/polymorphic/ctor.cc: New test. * testsuite/std/memory/polymorphic/ctor_poly.cc: New test. * testsuite/std/memory/polymorphic/incomplete.cc: New test. * testsuite/std/memory/polymorphic/invalid_neg.cc: New test. * testsuite/std/memory/polymorphic/move.cc: New test. * testsuite/std/memory/polymorphic/move_alloc.cc: New test. Co-authored-by: Tomasz Kamiński <tkaminsk@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-05-30libstdc++: Test for formatting with empty spec for calendar types.Tomasz Kamiński1-0/+301
Adding a test for behavior of the ostream operator and the formatting with empty chron-spec for the chrono types. This commit covers calendar types. libstdc++-v3/ChangeLog: * testsuite/std/time/format/empty_spec.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-05-27libstdc++: Fix test failures for 32-bit AIXJonathan Wakely2-0/+4
With -maix32 (the default) we only have 16-bit wchar_t so these tests fail. The debug.cc one is because we use -fwide-exec-charset=UTF-32BE which tries to encode each wide character as four bytes in a 2-byte wchar_t. The format.cc one is because the clown face character can't be encoded in a single 16-bit wchar_t. libstdc++-v3/ChangeLog: * testsuite/std/format/debug.cc: Disable for targets with 16-bit wchar_t. * testsuite/std/format/functions/format.cc: Use -DUNICODE for targets with 32-bit wchar_t. (test_unicode) [UNICODE]: Only run checks when UNICODE is defined.
2025-05-26libstdc++: Run in_place constructor test for std::indirect [PR119152]Tomasz Kamiński1-1/+2
In indirect/ctor.cc test_inplace_ctor function was defined, but never called. PR libstdc++/119152 libstdc++-v3/ChangeLog: * testsuite/std/memory/indirect/ctor.cc: Run test_inplace_ctor.
2025-05-26libstdc++: Implement C++26 std::indirect [PR119152]Jonathan Wakely8-0/+1140
This patch implements C++26 std::indirect as specified in P3019 with amendment to move assignment from LWG 4251. PR libstdc++/119152 libstdc++-v3/ChangeLog: * doc/doxygen/stdheader.cc: Added indirect.h file. * include/Makefile.am: Add new header. * include/Makefile.in: Regenerate. * include/bits/indirect.h: New file. * include/bits/version.def (indirect): Define. * include/bits/version.h: Regenerate. * include/std/memory: Include new header. * testsuite/std/memory/indirect/copy.cc * testsuite/std/memory/indirect/copy_alloc.cc * testsuite/std/memory/indirect/ctor.cc * testsuite/std/memory/indirect/incomplete.cc * testsuite/std/memory/indirect/invalid_neg.cc * testsuite/std/memory/indirect/move.cc * testsuite/std/memory/indirect/move_alloc.cc * testsuite/std/memory/indirect/relops.cc Co-authored-by: Tomasz Kamiński <tkaminsk@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-05-23testsuite: Fix typo in parse_ctx.ccStefan Schulze Frielinghaus1-1/+1
libstdc++-v3/ChangeLog: * testsuite/std/format/parse_ctx.cc: Fix typo for bfloat16 guard.
2025-05-19libstdc++: Fix std::format of chrono::local_days with {} [PR120293]Jonathan Wakely1-0/+3
Formatting of chrono::local_days with an empty chrono-specs should be equivalent to inserting it into an ostream, which should use the overload for inserting chrono::sys_days into an ostream. The implementation of empty chrono-specs in _M_format_to_ostream takes some short cuts, and that wasn't being done correctly for chrono::local_days. libstdc++-v3/ChangeLog: PR libstdc++/120293 * include/bits/chrono_io.h (_M_format_to_ostream): Add special case for local_time convertible to local_days. * testsuite/std/time/clock/local/io.cc: Check formatting of chrono::local_days. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-05-15libstdc++: Fix std::format_kind primary template for Clang [PR120190]Jonathan Wakely1-5/+10
Although Clang trunk has been adjusted to handle our std::format_kind definition (because they need to be able to compile the GCC 15.1.0 release), it's probably better to not rely on something that they might start diagnosing again in future. Define the primary template in terms of an immediately invoked function expression, so that we can put a static_assert(false) in the body. libstdc++-v3/ChangeLog: PR libstdc++/120190 * include/std/format (format_kind): Adjust primary template to not depend on itself. * testsuite/std/format/ranges/format_kind_neg.cc: Adjust expected errors. Check more invalid specializations. Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com> Reviewed-by: Daniel Krügler <daniel.kruegler@gmail.com>
2025-05-14libstdc++: Preserve the argument type in basic_format_args [PR119246]Tomasz Kamiński2-36/+81
This commits adjust the way how the arguments are stored in the _Arg_value (and thus basic_format_args), by preserving the types of fixed width floating-point types, that were previously converted to float, double, long double. The _Arg_value union now contains alternatives with std::bfloat16_t, std::float16_t, std::float32_t, std::float64_t that use pre-existing _Arg_bf16, _Arg_f16, _Arg_f32, _Arg_f32 argument types. This does not affect formatting, as specialization of formatters for fixed width floating-point types formats them by casting to the corresponding standard floating point type. For the 128bit floating we need to handle the ppc64 architecture, (_GLIBCXX_LONG_DOUBLE_ALT128_COMPAT) for which the long double may (per TU basis) designate either __ibm128 and __ieee128 type, we need to store both types in the _Arg_value and have two _Arg_types (_Arg_ibm128, _Arg_ieee128). On other architectures we use extra enumerator value to store __float128, that is different from long double and _Float128. This is consistent with ppc64, for which __float128, if present, is same type as __ieee128. We use _Arg_float128 _M_float128 names that deviate from _Arg_fN naming scheme, to emphasize that this flag is not used for std::float128_t (_Float128) type, that is consistenly formatted via handle. The __format::__float128_t type is renamed to __format::__flt128_t, to mitigate visual confusion between this type and __float128. We also introduce __bflt16_t typedef instead of using of decltype. We add new alternative for the _Arg_value and allow them to be accessed via _S_get, when the types are available. However, we produce and handle corresponding _Arg_type, only when we can format them. See also r14-3329-g27d0cfcb2b33de. The formatter<_Float128, _CharT> that formats via __format::__flt128_t is always provided, when type is available. It is still correct when __format::__flt128_t is _Float128. We also provide formatter<__float128, _CharT> that formats via __flt128_t. As this type may be disabled (-mno-float128), extra care needs to be taken, for situation when __float128 is same as long double. If the formatter would be defined in such case, the formatter<long double, _CharT> would be generated from different specializations, and have different mangling: * formatter<__float128, _CharT> if __float128 is present, * formatter<__format::__formattable_float, _CharT> otherwise. To best of my knowledge this happens only on ppc64 for __ieee128 and __float128, so the formatter is not defined in this case. static_assert is added to detect other configurations like that. In such case we should replace it with constraint. PR libstdc++/119246 libstdc++-v3/ChangeLog: * include/std/format (__format::__bflt16_t): Define. (_GLIBCXX_FORMAT_F128): Separate value for cases where _Float128 is used. (__format::__float128_t): Renamed to __format::__flt128_t. (std::formatter<_Float128, _CharT>): Define always if there is formattable 128bit float. (std::formatter<__float128, _CharT>): Define. (_Arg_type::_Arg_f128): Rename to _Arg_float128 and adjust value. (_Arg_type::_Arg_ibm128): Change value to _Arg_ldbl. (_Arg_type::_Arg_ieee128): Define as alias to _Arg_float128. (_Arg_value::_M_f128): Replaced with _M_ieee128 and _M_float128. (_Arg_value::_M_ieee128, _Arg_value::_M_float128) (_Arg_value::_M_bf16, _Arg_value::_M_f16, _Arg_value::_M_f32) (_Arg_value::_M_f64): Define. (_Arg_value::_S_get, basic_format_arg::_S_to_enum): Handle __bflt16, _Float16, _Float32, _Float64, and __float128 types. (basic_format_arg::_S_to_arg_type): Preserve _bflt16, _Float16, _Float32, _Float64 and __float128 types. (basic_format_arg::_M_visit): Handle _Arg_float128, _Arg_ieee128, _Arg_b16, _Arg_f16, _Arg_f32, _Arg_f64. * testsuite/std/format/arguments/args.cc: Updated to illustrate that extended floating point types use handles now. Added test for __float128. * testsuite/std/format/parse_ctx.cc: Extended test to cover class to check_dynamic_spec with floating point types and handles. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-05-07libstdc++: Fix width computation for the chrono formatting [PR120114]Tomasz Kamiński1-0/+125
Use `__unicode::_field_width` to compute the field width of the output when writting the formatted output for std::chrono::types. This applies both to characters copied from format string, and one produced by localized formatting. We also use _Str_sink::view() instead of get(), which avoids copying the content of the buffer to std::string in case of small output. PR libstdc++/120114 libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__formatter_chrono::_M_format): Use __field_width. * testsuite/std/time/format/pr120114.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-30libstdc++: Fix _Padding_sink in case when predicted width is between ↵Tomasz Kamiński2-0/+18
padwidth and maxwidth [PR109162] The _Padding_sink was behaving incorrectly, when the predicted width (based on code units count) was higher than _M_maxwidth, but lower than _M_padwidth. In this case _M_update() returned without calling _M_force_update() and computing field width for Unicode encoding, because _M_buffering() returned 'true'. As a consequence we switched to _M_ignoring() mode, while storing a sequence with more code units but smaller field width than _M_maxwidth. We now call _M_force_update() if predicted width is greater or equal to either _M_padwidth or _M_maxwidth. This happened for existing test case on 32bit architecture. PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/std/format (_Padding_sink::_M_update): Fixed condition for calling _M_force_update. * testsuite/std/format/debug.cc: Add test that reproduces this issue on 64bit architecture. * testsuite/std/format/ranges/sequence.cc: Another edge value test.
2025-04-25libstdc++: Minimalize temporary allocations when width is specified [PR109162]Tomasz Kamiński4-5/+655
When width parameter is specified for formatting range, tuple or escaped presentation of string, we used to format characters to temporary string, and write produce sequence padded according to the spec. However, once the estimated width of formatted representation of input is larger than the value of spec width, it can be written directly to the output. This limits size of required allocation, especially for large ranges. Similarly, if precision (maximum) width is provided for string presentation, only a prefix of sequence with estimated width not greater than precision, needs to be buffered. To realize above, this commit implements a new _Padding_sink specialization. This sink holds an output iterator, a value of padding width, (optionally) maximum width and a string buffer inherited from _Str_sink. Then any incoming characters are treated in one of following ways, depending of estimated width W of written sequence: * written to string if W is smaller than padding width and maximum width (if present) * ignored, if W is greater than maximum width * written to output iterator, if W is greater than padding width The padding sink is used instead of _Str_sink in __format::__format_padded, __formatter_str::_M_format_escaped functions. Furthermore __formatter_str::_M_format implementation was reworked, to: * reduce number of instantiations by delegating to _Rg& and const _Rg& overloads, * non-debug presentation is written to _Out directly or via _Padding_sink * if maximum width is specified for debug format with non-unicode encoding, string size is limited to that number. PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/bits/formatfwd.h (__simply_formattable_range): Moved from std/format. * include/std/format (__formatter_str::_format): Extracted escaped string handling to separate method... (__formatter_str::_M_format_escaped): Use __Padding_sink. (__formatter_str::_M_format): Adjusted implementation. (__formatter_str::_S_trunc): Extracted as namespace function... (__format::_truncate): Extracted from __formatter_str::_S_trunc. (__format::_Seq_sink): Removed forward declarations, made members protected and non-final. (_Seq_sink::_M_trim): Define. (_Seq_sink::_M_span): Renamed from view. (_Seq_sink::view): Returns string_view instead of span. (__format::_Str_sink): Moved after _Seq_sink. (__format::__format_padded): Use _Padding_sink. * testsuite/std/format/debug.cc: Add timeout and new tests. * testsuite/std/format/ranges/sequence.cc: Specify unicode as encoding and new tests. * testsuite/std/format/ranges/string.cc: Likewise. * testsuite/std/format/tuple.cc: Likewise. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-25libstdc++: Replace leftover std::queue with Adaptor in ranges/adaptors.cc.Tomasz Kamiński1-1/+1
This was leftover from work-in-progress state, where only std::queue was tested. libstdc++-v3/ChangeLog: * testsuite/std/format/ranges/adaptors.cc: Updated test.
2025-04-25libstdc++: Define __cpp_lib_format_ranges in format header [PR109162]Tomasz Kamiński4-3/+12
As P2286R8 and P2585R1 as now fully implemented, we now define __cpp_lib_format_ranges feature test macro with __cpp_lib_format_ranges. This macro is provided only in <format>. Uses of internal __glibcxx_format_ranges are also updated. PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/bits/version.def (format_ranges): Remove no_stdname and update value. * include/bits/version.h: Regenerate. * src/c++23/std.cc.in: Replace __glibcxx_format_ranges with __cpp_lib_format_ranges. * testsuite/std/format/formatter/lwg3944.cc: Likewise. * testsuite/std/format/parse_ctx.cc: Likewise. * testsuite/std/format/string.cc: Likewise. * testsuite/std/format/ranges/feature_test.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-25libstdc++: Implement formatters for queue, priority_queue and stack [PR109162]Tomasz Kamiński1-0/+156
This patch implements formatter specializations for standard container adaptors (queue, priority_queue and stack) from P2286R8. To be able to access the protected `c` member, the adaptors befriend corresponding formatter specializations. Note that such specialization may be disable if the container is formattable, in such case specializations are unharmful. As in the case of previous commits, the signatures of the user-facing parse and format methods of the provided formatters deviate from the standard by constraining types of parameters: * _CharT is constrained __formatter::__char * basic_format_parse_context<_CharT> for parse argument * basic_format_context<_Out, _CharT> for format second argument The standard specifies all above as unconstrained types. In particular _CharT constrain, allow us to befriend all allowed specializations. Furthermore the standard specifies these formatters as delegating to formatter<ranges::ref_view<const? _Container>, charT>, which in turn delegates to range_formatter. This patch avoids one level of indirection, and dependency of ranges::ref_view. This is technically observable if user specializes formatter<std::ref_view<PD>> where PD is program defined container, but I do not think this is the case worth extra indirection. This patch also moves the formattable and it's dependencies to the formatfwd.h, so it can be used in adapters formatters, without including format header. The definition of _Iter_for is changed from alias to denoting back_insert_iterator<basic_string<_CharT>>, to struct with type nested typedef that points to same type, that is forward declared. PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/bits/formatfwd.h (__format::__parsable_with) (__format::__formattable_with, __format::__formattable_impl) (__format::__has_debug_format, __format::__const_formattable_range) (__format::__maybe_const_range, __format::__maybe_const) (std::formattable): Moved from std/format. (__format::Iter_for, std::range_formatter): Forward declare. * include/bits/stl_queue.h (std::formatter): Forward declare. (std::queue, std::priority_queue): Befriend formatter specializations. * include/bits/stl_stack.h (std::formatter): Forward declare. (std::stack): Befriend formatter specializations. * include/std/format (__format::_Iter_for): Define as struct with (__format::__parsable_with, __format::__formattable_with) (__format::__formattable_impl, __format::__has_debug_format) (_format::__const_formattable_range, __format::__maybe_const_range) (__format::__maybe_const, std::formattable): Moved to bits/formatfwd.h. (std::range_formatter): Remove default argument specified in declaration in bits/formatfwd.h. * include/std/queue: Include bits/version.h before bits/stl_queue.h. (formatter<queue<_Tp, _Container, _Compare>, _CharT>) (formatter<priority_queue<_Tp, _Container, _Compare>, _CharT>): Define. * include/std/stack: Include bits/version.h before bits/stl_stack.h (formatter<stack<_Tp, _Container, _Compare>, _CharT>): Define. * testsuite/std/format/ranges/adaptors.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-22libstdc++: Increase timeouts for format and flat_maps testsTomasz Kamiński3-0/+3
Test for format parse format string and compile time, flat_maps ones test all functionality in single test file. libstdc++-v3/ChangeLog: * testsuite/23_containers/flat_map/1.cc: Add dg-timeout-factor 2. * testsuite/23_containers/flat_multimap/1.cc: Likewise. * testsuite/std/format/ranges/map.cc: Likewise. * testsuite/std/format/ranges/sequence.cc: Likewise. * testsuite/std/format/ranges/string.cc: Likewise.
2025-04-17libstdc++: Do not use 'not' alternative token in <format>Jonathan Wakely1-0/+13
This fixes: FAIL: 17_intro/headers/c++1998/operator_names.cc -std=gnu++23 (test for excess errors) FAIL: 17_intro/headers/c++1998/operator_names.cc -std=gnu++26 (test for excess errors) The purpose of 'not defined<format_kind<R>>' is to be ill-formed (as required by [format.range.fmtkind]) and to give an error that includes the string "not defined<format_kind<R>>". That was intended to tell you that format_kind<R> is not defined, just like it says! But user code can use -fno-operator-names so we can't use 'not' here, and "! defined" in the diagnostic doesn't seem as user-friendly. It also raises questions about whether it was intended to be the preprocessor token 'defined' (it's not) or where 'defined' is defined (it's not). Replace it with __primary_template_not_defined<format_kind<R>> and a comment, which seems to give a fairly clear diagnostic with both GCC and Clang. The diagnostic now looks like: .../include/c++/15.0.1/format:5165:7: error: use of 'std::format_kind<int>' before deduction of 'auto' 5165 | format_kind<_Rg> // you can specialize this for non-const input ranges | ^~~~~~~~~~~~~~~~ .../include/c++/15.0.1/format:5164:35: error: '__primary_template_not_defined' was not declared in this scope 5164 | __primary_template_not_defined( | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^ 5165 | format_kind<_Rg> // you can specialize this for non-const input ranges | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5166 | ); | ~ libstdc++-v3/ChangeLog: * include/std/format (format_kind): Do not use 'not' alternative token to make the primary template ill-formed. Use the undeclared identifier __primary_template_not_defined and a comment that will appear in diagnostics. * testsuite/std/format/ranges/format_kind_neg.cc: New test.
2025-04-16libstdc++: Fix constification in range_formatter::format [PR109162]Tomasz Kamiński1-0/+22
The _Rg is deduced to lvalue reference for the lvalue arguments, and in such case __format::__maybe_const_range<_Rg, _CharT> is always _Rg (adding const to reference does not change behavior). Now we correctly check if _Range = remove_reference_t<_Rg> is const formattable range, furthermore as range_formatter<T> can only format ranges of values of type (possibly const) _Tp, we additional check if the remove_cvref_t<range_reference_t<const _Range>> is _Tp. The range_reference_t<R> and range_reference_t<const R> have different types (modulo remove_cvref_t) for std::vector<bool> (::reference and bool) or flat_map<T, U> (pair<const T&, U&> and pair<const T&, const U&>). PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/std/format (range_formatter::format): Format const range, only if reference type is not changed. * testsuite/std/format/ranges/formatter.cc: New tests. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-16libstdc++: Implement formatters for pair and tuple [PR109162]Tomasz Kamiński4-19/+507
This patch implements formatter specializations for pair and tuple form P2286R8. In addition using 'm` and range_format::map (from P2585R1) for ranges are now supported. The formatters for pairs and tuples whose corresponding elements are the same (after applying remove_cvref_t) derive from the same __tuple_formatter class. This reduce the code duplication, as most of the parsing and formatting is the same in such cases. We use a custom reduced implementation of the tuple (__formatters_storage) to store the elements formatters. Handling of the padding (width and fill) options, is extracted to __format::__format_padded function, that is used both by __tuple_formatter and range_formatter. To reduce number of instantations range_formatter::format triggers, we cast incoming range to __format::__maybe_const_range<_Rg, _CharT>&, before formatting it. As in the case of previous commits, the signatures of the user-facing parse and format methods of the provided formatters deviate from the standard by constraining types of parameters: * _CharT is constrained __formatter::__char * basic_format_parse_context<_CharT> for parse argument * basic_format_context<_Out, _CharT> for format second argument The standard specifies last three of above as unconstrained types. Finally, test for tuple-like std::array and std::ranges::subrange, that illustrate that they remain formatted as ranges. PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/std/format (__formatter_int::_M_format_character_escaped) (__formatter_str::format): Use __sink.out() to produce _Sink_iter. (__format::__const_formattable_range): Moved closer to range_formatter. (__format::__maybe_const_range): Use `__conditional_t` and moved closer to range_formatter. (__format::__format_padded, __format::maybe_const) (__format::__indexed_formatter_storage, __format::__tuple_formatter) (std::formatter<pair<_Fp, _Sp>, _CharT>>) (std::formatter<tuple<_Tps...>, _CharT): Define. (std::formatter<_Rg, _CharT>::format): Cast incoming range to __format::__maybe_const_range<_Rg, _CharT>&. (std::formatter<_Rg, _CharT>::_M_format): Extracted from format, and use __format_padded. (std::formatter<_Rg, _CharT>::_M_format_no_padding): Rename... (std::formatter<_Rg, _CharT>::_M_format_elems): ...to this. (std::formatter<_Rg, _CharT>::_M_format_with_padding): Extracted as __format_padded. * testsuite/util/testsuite_iterators.h (test_input_range_nocopy): Define. * testsuite/std/format/ranges/formatter.cc: Tests for `m` specifier. * testsuite/std/format/ranges/sequence.cc: Tests for array and subrange. * testsuite/std/format/ranges/map.cc: New test. * testsuite/std/format/tuple.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-15libstdc++: Implement formatter for ranges and range_formatter [PR109162]Tomasz Kamiński6-8/+665
This patch implements formatter specialization for input_ranges and range_formatter class from P2286R8, as adjusted by P2585R1. The formatter for pair/tuple is not yet provided, making maps not formattable. This introduces an new _M_format_range member to internal __formatter_str, that formats range as _CharT as string, according to the format spec. This function transform any contiguous range into basic_string_view directly, by computing size if necessary. Otherwise, for ranges for which size can be computed (forward_range or sized_range) we use a stack buffer, if they are sufficiently small. Finally, we create a basic_string<_CharT> from the range, and format its content. In case when padding is specified, this is handled by firstly formatting the content of the range to the temporary string object. However, this can be only implemented if the iterator of the basic_format_context is internal type-erased iterator used by implementation. Otherwise a new basic_format_context would need to be created, which would require rebinding of handles stored in the arguments: note that format spec for element type could retrieve any format argument from format context, visit and use handle to format it. As basic_format_context provide no user-facing constructor, the user are not able to construct object of that type with arbitrary iterators. The signatures of the user-facing parse and format methods of the provided formatters deviate from the standard by constraining types of params: * _CharT is constrained __formatter::__char * basic_format_parse_context<_CharT> for parse argument * basic_format_context<_Out, _CharT> for format second argument The standard specifies last three of above as unconstrained types. These types are later passed to possibly user-provided formatter specializations, that are required via formattable concept to only accept above types. Finally, the formatter<input_range, _CharT> specialization is implemented without using specialization of range-default-formatter exposition only template as base class, while providing same functionality. PR libstdc++/109162 libstdc++-v3/ChangeLog: * include/std/format (__format::__has_debug_format, _Pres_type::_Pres_seq) (_Pres_type::_Pres_str, __format::__Stackbuf_size): Define. (_Separators::_S_squares, _Separators::_S_parens, _Separators::_S_comma) (_Separators::_S_colon): Define additional constants. (_Spec::_M_parse_fill_and_align): Define overload accepting list of excluded characters for fill, and forward existing overload. (__formatter_str::_M_format_range): Define. (__format::_Buf_sink) Use __Stackbuf_size for size of array. (__format::__is_map_formattable, std::range_formatter) (std::formatter<_Rg, _CharT>): Define. * src/c++23/std.cc.in (std::format_kind, std::range_format) (std::range_formatter): Export. * testsuite/std/format/formatter/lwg3944.cc: Guarded tests with __glibcxx_format_ranges. * testsuite/std/format/formatter/requirements.cc: Adjusted for standard behavior. * testsuite/23_containers/vector/bool/format.cc: Test vector<bool> formatting. * testsuite/std/format/ranges/format_kind.cc: New test. * testsuite/std/format/ranges/formatter.cc: New test. * testsuite/std/format/ranges/sequence.cc: New test. * testsuite/std/format/ranges/string.cc: New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-14libstdc++: Use UTF-32BE as wide encoding for big-endian machines [PR119725]Tomasz Kamiński2-2/+3
This changes the `dg-options` line so UTF-32 with byte order native to the machine is used as wide encoding. We still do not handle mismatch in the byte order of the Unicode encodings (UTF32-BE on little-endian machines). This would require larger changes, as for example `unicode-data.h` tables are encoded with native byte order. PR libstdc++/119725 libstdc++-v3/ChangeLog: * testsuite/std/format/debug.cc: Updated dg-options. * testsuite/std/format/debug_nonunicode.cc: Updated dg-options. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-11libstdc++: Implement debug format for strings and characters formatters ↵Tomasz Kamiński4-2/+461
[PR109162] This patch implements part P2286R8 that specified debug (escaped) format for the strings and characters sequences. This include both handling of the '?' format specifier and set_debug_format member. To indicate partial support we define __glibcxx_format_ranges macro value 1, without defining __cpp_lib_format_ranges. We provide two separate escaping routines depending on the literal encoding for the corresponding character types. If the character encoding is Unicode, we follow the specification for the standard (__format::__write_escaped_unicode). For other encodings, we escape only characters in range [0x00, 0x80), interpreting them as ASCII values: [0x00, 0x20), 0x7f and '\t', '\r', '\n', '\\', '"', '\'' are escaped. We assume every character outside this range is printable (__format::_write_escaped_ascii). In particular we do not yet implement special handling of shift sequences. For Unicode escaping a new __unicode::__escape_edges table is introduced, that encodes information if character belongs to General_Category that is escaped by the standard (Control or Other). This table is generated from DerivedGeneralCategory.txt provided by Unicode. Only boolean flag is preserved to reduce the number of entries. The additional rules for escaping are handled by __format::__should_escape_unicode. When width or precision is specified, we emit escaped string to the temporary buffer and format the resulting string according to the format spec. For characters use a fixed size stack buffer, for which a new _Fixedbuf_sink is introduced. For strings, we use _Str_sink and to avoid allocations, we compute the estimated size of (possibly truncated) input, and if it is larger than width field we print directly. PR libstdc++/109162 contrib/ChangeLog: * unicode/README: Mentioned DerivedGeneralCategory.txt. * unicode/gen_libstdcxx_unicode_data.py: Generation __escape_edges table from DerivedGeneralCategory.txt. Update file name in comments. * unicode/DerivedGeneralCategory.txt: Copy of file distributed by Unicode Consortium. libstdc++-v3/ChangeLog: * include/bits/chrono_io.h (__detail::_Widen): Moved to std/format file. * include/bits/unicode-data.h: Regnerate. * include/bits/unicode.h (__unicode::_Utf_iterator::_M_units) (__unicode::__should_escape_category): Define. * include/std/format (_GLIBCXX_WIDEN_, _GLIBCXX_WIDEN): Copied from include/bits/chrono_io.h. (__format::_Widen): Moved from include/bits/chrono_io.h. (__format::_Term_char, __format::_Escapes, __format::_Separators) (__format::__should_escape_ascii, __format::__should_escape_unicode) (__format::__write_escape_seq, __format::__write_escaped_char) (__format::__write_escaped_acii, __format::__write_escaped_unicode) (__format::__write_escaped): Define. (__formatter_str::_S_trunc): Extracted truncation of character sequences. (__formatter_str::format): Handle _Pres_esc. (__formatter_int::_M_do_parse) [__glibcxx_format_ranges]: Parse '?'. (__formatter_int::_M_format_character_escaped): Define. (formatter<_CharT, _CharT>::format, formatter<char, wchar_t>::format): Handle _Pres_esc. (__formatter_str::set_debug_format, formatter<...>::set_debug_format) Guard with __glibcxx_format_ranges. (__format::_Fixedbuf_sink): Define. * testsuite/23_containers/vector/bool/format.cc: Use __format::_Widen and remove unnecessary <chrono> include. * testsuite/std/format/debug.cc: New test. * testsuite/std/format/debug_nonunicode.cc: New test. * testsuite/std/format/parse_ctx.cc (escaped_strings_supported): Define to true if __glibcxx_format_ranges is defined. * testsuite/std/format/string.cc (escaped_strings_supported): Define to true if __glibcxx_format_ranges is defined. Reviewed-by: Jonathan Wakely <jwakely@redhat.com> Signed-off-by: Tomasz Kamiński <tkaminsk@redhat.com>
2025-04-09libstdc++: Fix constraint recursion in basic_const_iterator operator- [PR115046]Patrick Palka1-0/+13
It was proposed in PR112490 to also adjust basic_const_iterator's friend operator-(sent, iter) overload alongside the r15-7757-g4342c50ca84ae5 adjustments to its comparison operators, but we lacked a concrete testcase demonstrating fixable constraint recursion there. It turns out Hewill Kang's PR115046 is such a testcase! So this patch makes the same adjustments to that overload as well, fixing PR115046. The LWG 4218 P/R will need to get adjusted too. PR libstdc++/115046 PR libstdc++/112490 libstdc++-v3/ChangeLog: * include/bits/stl_iterator.h (basic_const_iterator::operator-): Replace non-dependent basic_const_iterator function parameter with a dependent one of type basic_const_iterator<_It2> where _It2 matches _It. * testsuite/std/ranges/adaptors/as_const/1.cc (test04): New test. Reviewed-by: Jonathan Wakely <jwakely@redhat.com>