This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Extend SpaceBeforeParens for requires
ClosedPublic

Authored by HazardyKnusperkeks on Nov 7 2021, 1:06 PM.

Details

Summary

We can now configure the space between requires and the following paren,separate for clauses and expressions.

Diff Detail

Event Timeline

HazardyKnusperkeks requested review of this revision.Nov 7 2021, 1:06 PM
HazardyKnusperkeks created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptNov 7 2021, 1:06 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
curdeius added inline comments.
clang/docs/ClangFormatStyleOptions.rst
3756

You meant "if there is one", right?

clang/unittests/Format/FormatTest.cpp
14306

I'd like to see some tests for mixed true/false, false/true values of these parameters. You only test false/false and true/true combinations so far.

22636

This test seems redundant. Does it test something else than the above added ones?

HazardyKnusperkeks marked 2 inline comments as done.

Addressed comments.

clang/docs/ClangFormatStyleOptions.rst
3756

Yeah

clang/unittests/Format/FormatTest.cpp
22636

No it doesn't, I just thought I would put the requires stuff into the requires test case. I for one work with the gtest filter on single test cases if I work on some issue.

Now the new diff.

MyDeveloperDay accepted this revision.Nov 9 2021, 11:32 AM

LGTM (nit the clang-format check)

This revision is now accepted and ready to land.Nov 9 2021, 11:32 AM
owenpan added inline comments.Nov 9 2021, 5:03 PM
clang/lib/Format/TokenAnnotator.cpp
3145–3148

Nit: remove else.

HazardyKnusperkeks planned changes to this revision.Dec 3 2021, 12:14 PM
HazardyKnusperkeks marked an inline comment as done.

Delayed until D113319 is resolved.

Quuxplusone added inline comments.
clang/docs/ClangFormatStyleOptions.rst
3756

IMO these options should be named InRequiresClause and InRequiresExpression: that's where the space is going. The space doesn't go after the requires-clause. The space doesn't go after the requires-expression.
It occurs to me that the name of this option could be analogous to the name of the option that controls []() {} versus [] () {}... except that it looks like there is no such option (and I'm happy about that). :) Also, the name of that option would probably just be AfterCaptureList, which doesn't help us in this case.

clang/include/clang/Format/Format.h
3371

Here and line 3380, and possibly elsewhere: s/parentheses/parenthesis/

clang/unittests/Format/FormatTest.cpp
14369

Throughout, I'd like to see simpler test cases and more of them. E.g. I think this would suffice, in lieu of lines 14305–14369:

SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresClause = true;
SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresExpression = true;
verifyFormat("void f(auto x) requires (requires (int i) { x+i; }) { }", SpaceAfterRequires);
verifyFormat("if (requires (int i) { x+i; }) return;", SpaceAfterRequires);
verifyFormat("bool b = requires (int i) { x+i; };", SpaceAfterRequires);

SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresClause = true;
SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresExpression = false;
verifyFormat("void f(auto x) requires (requires(int i) { x+i; }) { }", SpaceAfterRequires);
verifyFormat("if (requires(int i) { x+i; }) return;", SpaceAfterRequires);
verifyFormat("bool b = requires(int i) { x+i; };", SpaceAfterRequires);

SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresClause = false;
SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresExpression = true;
verifyFormat("void f(auto x) requires(requires (int i) { x+i; }) { }", SpaceAfterRequires);
verifyFormat("if (requires (int i) { x+i; }) return;", SpaceAfterRequires);
verifyFormat("bool b = requires (int i) { x+i; };", SpaceAfterRequires);

SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresClause = false;
SpaceAfterRequires.SpaceBeforeParensOptions.InRequiresExpression = false;
verifyFormat("void f(auto x) requires(requires(int i) { x+i; }) { }", SpaceAfterRequires);
verifyFormat("if (requires(int i) { x+i; }) return;", SpaceAfterRequires);
verifyFormat("bool b = requires(int i) { x+i; };", SpaceAfterRequires);
clang/docs/ClangFormatStyleOptions.rst
3756

I get your point, but the space does not go anywhere in the clause/expression, so AfterRequiresForClauses?

Quuxplusone added inline comments.Dec 4 2021, 8:20 AM
clang/docs/ClangFormatStyleOptions.rst
3756

(I'd avoid the word For, because of keyword for.)
A requires-expression is the whole expression, requires + param-list + body: https://eel.is/c++draft/expr.prim.req#nt:requires-expression
A requires-clause is the whole clause, requires + logical-or-expr: https://eel.is/c++draft/temp.pre#nt:requires-clause
Does that resolve your concern about the word In?

HazardyKnusperkeks marked 4 inline comments as done.
HazardyKnusperkeks added a reviewer: curdeius.
HazardyKnusperkeks removed a subscriber: curdeius.
  • Rebased
  • Updated
  • Tests changed
This revision is now accepted and ready to land.Feb 7 2022, 7:07 AM
HazardyKnusperkeks requested review of this revision.Feb 7 2022, 7:07 AM
HazardyKnusperkeks added inline comments.
clang/docs/ClangFormatStyleOptions.rst
3756

My concern is: In may refer to anywhere in the clause, but we are talking about one very specific point per clause. (Analogues for expression.)

The current version is very explicit, and I don't have a problem with such a long name.

clang/include/clang/Format/Format.h
3371

Non native here, but I think one could argue both were okay. I just copied what the other options state.

Quuxplusone added inline comments.Feb 7 2022, 7:17 AM
clang/include/clang/Format/Format.h
3371

It's definitely not grammatical to talk about "opening parentheses, if there is one". But if this is just copying pre-existing wording, then OK, no objection to someone handling the grammar in a followup PR.

I need a response here. :) @Quuxplusone are the names okay for you?

curdeius accepted this revision.Feb 14 2022, 1:29 AM

LGTM. My naming suggestion is not binding. I have no strong opinion on this, but a shorter name would get my 👍 :).
So please sync with other reviewers.

clang/docs/ClangFormatStyleOptions.rst
3755

Wouldn't AfterRequiresInClause/AfterRequiresInExpression be meaningful enough?

This revision is now accepted and ready to land.Feb 14 2022, 1:29 AM
clang/docs/ClangFormatStyleOptions.rst
3755

+1, I have no problem with AfterRequiresInClause and AfterRequiresInExpression!

HazardyKnusperkeks marked 2 inline comments as done.Feb 14 2022, 9:01 AM
HazardyKnusperkeks added inline comments.
clang/docs/ClangFormatStyleOptions.rst
3755

I will change the names accordingly before the commit.

They are much better.

HazardyKnusperkeks marked 2 inline comments as done.Feb 15 2022, 12:07 PM
This revision was landed with ongoing or failed builds.Feb 15 2022, 12:38 PM
This revision was automatically updated to reflect the committed changes.