This is an archive of the discontinued LLVM Phabricator instance.

[libc++][format] Disable default formatter.
ClosedPublic

Authored by Mordante on Dec 18 2021, 4:14 AM.

Details

Reviewers
ldionne
vitaut
Group Reviewers
Restricted Project
Commits
rG2b8b48c5a0c4: [libc++][format] Disable default formatter.
Summary

[format.formatter.spec]/5 lists the requirements for the default
formatter. The original implementation didn't implement this. This
implements the default formatter according to the Standard.

This adds additional test to validate the default formatter is disabled
and the required standard formatters are enabled.

While adding the tests it seems the formatters needed a constraint for the
character types they were valid for.

Implements parts of:

  • P0645 Text Formatting

Depends on D115988

Diff Detail

Event Timeline

Mordante requested review of this revision.Dec 18 2021, 4:14 AM
Mordante created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptDec 18 2021, 4:14 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript
Mordante updated this revision to Diff 395282.Dec 18 2021, 4:43 AM

Qualify std::nullptr_t to fix a CI error.

Mordante updated this revision to Diff 395287.Dec 18 2021, 5:54 AM

Attempt to fix CI issues. They are due to not all headers available in all compilation modi.

Mordante updated this revision to Diff 395289.Dec 18 2021, 6:39 AM

Adds some missing includes in the tests.

Test nits. I assume the code is correct.

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
68–69

Would it be useful to verify here that std::as_const(f).format(u, fc) is also OK?
(Recall our earlier discussion of how standard formatters should be const-friendly because eventually they'll be required to be constexpr-friendly. This could be tucked behind an #ifdef _LIBCPP_VER.)

82

Isn't it equally easy to

#define TEST_ENABLED_AFTER_CXX23(T, CharT) disabled<std::formatter<T, CharT>>

?

113–115

This (non-template-dependent) test seems out of place, but whatever.
As long as it's not a typo for assert_formatter_is_enabled<CharT, wchar_t>();.

250

For all of these, once the int versions are enabled, I assume you'll also need to check that the e.g. struct Widget versions are still disabled:

assert_formatter_is_disabled<std::array<Widget, 42>, CharT>();

etc.

269
assert_formatter_is_disabled<const int*, CharT>();
assert_formatter_is_disabled<c, CharT>();
assert_formatter_is_disabled<c*, CharT>();
assert_formatter_is_disabled<const c*, CharT>();
assert_formatter_is_disabled<const char*, wchar_t>();
assert_formatter_is_disabled<const char*, char8_t>();  // ?
Mordante marked 2 inline comments as done.Dec 18 2021, 11:50 AM

Thanks for the reviews!

libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
68–69

No that's not useful since at the moment those functions aren't const and can't be const.
When the width is formatting argument its value is resolved in format and modifies the object. This obviously needs to be resolved when your LWG3636 gets accepted. But even if it doesn't get accepted that change probably is useful when implementing P2286 "Formatting ranges".

So I've already investigated how I want to resolve this. However I first like to know the final resolution for LWG3576 "Clarifying fill character in std::format". When implementing its resolution it's a good time to make sure these functions can be const.

82

That's also an option. For now I just want to explain why I've taken the current approach for disable. How I'm doing the final macro is for a later review. (Basically the current approach makes it easy to test for enabled based on the language Standard used.)

113–115

Guess some comment would be good ;-) It's intended and executes twice. This formatter converts a char to a wchar_t. It's a one of a kind and specified in the Standard. Since it's part of P0645 I wanted to keep it here.

250

Yes there can be a lot more tests. But I think you have a good point about Widget and it should still be disabled. I only prefer not to add them here. I think test_disabled is a better place.

Mordante marked 2 inline comments as done.Dec 18 2021, 12:26 PM
Mordante added inline comments.
libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
68–69

@Quuxplusone Fun fact I just see P2286R4 already cites and assumes LWG3636 will be accepted and requires the format function to be const qualified.

vitaut added inline comments.Dec 19 2021, 8:55 AM
libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
269

Maybe also test volatile int* for completeness?

Mordante marked 4 inline comments as done.Dec 19 2021, 12:06 PM
Mordante added inline comments.
libcxx/test/std/utilities/format/format.formatter/format.formatter.spec/types.compile.pass.cpp
269

I added these tests and some more.

Mordante updated this revision to Diff 395350.Dec 19 2021, 12:13 PM
Mordante marked an inline comment as done.

Addresses review comments
Adds an additional test testing the proper template default argument.

ldionne accepted this revision.Jan 24 2022, 7:31 AM
This revision is now accepted and ready to land.Jan 24 2022, 7:31 AM
This revision was automatically updated to reflect the committed changes.