This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Add SpacesInParens with SpacesInParensOptions
ClosedPublic

Authored by gedare on Jul 13 2023, 3:13 PM.

Details

Summary

This is a refactoring of:

  • SpacesInConditionalStatement
  • SpacesInCStyleCastParentheses
  • SpaceInEmptyParentheses
  • SpacesInParentheses

These are now options under the new Style Option: SpacesInParens. The existing
options are maintained for backward compatibility.

Within SpacesInParens, there are currently options for:

  • Never
  • Custom

The currently available options for Custom are:

  • InConditionalStatements
  • InCStyleCasts
  • InEmptyParentheses
  • Other

Setting InConditionalStatements and Other to true enables the same space additions as SpacesInParentheses.

This refactoring does not add or remove any existing features, but it makes it
possible to more easily extend and maintain the addition of spaces within
parentheses.

This rev is related to Github Issue #55428.
https://github.com/llvm/llvm-project/issues/55428

Diff Detail

Event Timeline

gedare created this revision.Jul 13 2023, 3:13 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJul 13 2023, 3:13 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gedare requested review of this revision.Jul 13 2023, 3:13 PM
gedare edited the summary of this revision. (Show Details)Jul 13 2023, 3:15 PM

After doing this, I'm not too sure about the value of keeping the higher-level CStyleCasts, ConditionalStatements, and EmptyParentheses as options to SpacesInParens. However, the behavior of Always is actually "Always except for CStyleCasts and EmptyParentheses, which is consistent with how SpacesInParentheses currently works. I think that is a bit buggy/brittle, but I kept the behavior as-is, and modeled the design of this after SpaceBeforeParens.

I might prefer to make Always really mean every single parens, and simply have
Never, Always, and Custom. Inside Custom, there can be a Other option to catch anything that isn't explicitly controlled.

I could make this work, mapping the previous behavior of SpacesInParenthesis to Custom with Options = {ConditionalStatements: true, Other: true}. I'll sit on this a bit, but if I don't hear objections (to the entire point of this rev, or to my next step), I will redo the options like this.

clang/include/clang/Format/Format.h
4207–4275

The deprecated options should be removed from the struct, see AllowAllConstructorInitializersOnNextLine for an example.

You also need to adapt the parsing logic a bit.

clang/lib/Format/Format.cpp
735

Since the option is new, there is no backward. You can drop this.

MyDeveloperDay added inline comments.Jul 16 2023, 7:22 AM
clang/lib/Format/TokenAnnotator.cpp
4044

isn't SpacesInParentheses mapped to Style.SpacesInParens == FormatStyle::SIPO_Always?

gedare updated this revision to Diff 541219.Jul 17 2023, 1:39 PM
gedare marked 3 inline comments as done.

Properly deprecate old options, and simplify top-level to Never and Custom

I addressed the comments, and I have redesigned this option to simplify it further. Now there are only two options at the top for SpacesInParens to be either Never or Custom. Then within Custom the individual behavior of the spaces can be controlled. This allows, for example, someone to actually set every option true and really get a space in every parens that is supported. Meanwhile, to get the behavior of what was previously called SpacesInParentheses a user would set the spaces in parens options for InConditionalStatements and Other to be true. If Other gets further divided in the future, the previous behavior can be retained by simply setting its options to true as well.

clang/include/clang/Format/Format.h
4207–4275

Got it, thanks for the hint

clang/lib/Format/TokenAnnotator.cpp
4044

Yes it was, but I misunderstood how to deprecate options. This is fixed now that I removed the deprecated SpacesInParentheses, but it's also been changed to map SpacesInParentheses to Style.SpacesInParens == FormatStyle::SIPO_Custom.

gedare edited the summary of this revision. (Show Details)Jul 17 2023, 1:51 PM
clang/lib/Format/Format.cpp
1035

By removing the old options don’t you break everyone’s clang format file

If you limit it to Never I don't see any value in the differentiation. You could just always use Custom (by dropping the custom and only having the nested options).

But I think having at least the Always option would be nice. If you want always to have a space and you set everything by hand to true, someone comes along and adds a new option (which then is defaulted to false) you don't get what you want.

clang/lib/Format/Format.cpp
1035

You need to parse all of the old options, and map them to the new one, if and only if the old one(s) is/are set and the new is not! See below for the other deprecated options.

MyDeveloperDay requested changes to this revision.Jul 18 2023, 4:58 AM
This revision now requires changes to proceed.Jul 18 2023, 4:58 AM
gedare updated this revision to Diff 541770.Jul 18 2023, 3:47 PM

Parse deprecated options and map to new ones.

If you limit it to Never I don't see any value in the differentiation. You could just always use Custom (by dropping the custom and only having the nested options).

But I think having at least the Always option would be nice. If you want always to have a space and you set everything by hand to true, someone comes along and adds a new option (which then is defaulted to false) you don't get what you want.

Having Custom simplifies detecting if the new options are being used. It is possible to add an Always option if someone wants it, but that option has not existed yet for clang-format.

clang/lib/Format/Format.cpp
1035

Got it, I misunderstood how to handle deprecated options (twice). I think I have that sorted now.

gedare marked 2 inline comments as done.Jul 18 2023, 3:50 PM
MyDeveloperDay accepted this revision.Jul 18 2023, 10:56 PM

This looks good can you wait for one of the others to accept too

This revision is now accepted and ready to land.Jul 18 2023, 10:56 PM
HazardyKnusperkeks requested changes to this revision.Jul 19 2023, 12:06 PM

D155529#inline-1505573
But what does clang-format without this patch format? I just want to make sure, that we don't have a regression when this lands, but D155529 not.

clang/unittests/Format/ConfigParseTest.cpp
411–425

You need to write similar tests to show that your backwards compatible parsing works as intended.

This revision now requires changes to proceed.Jul 19 2023, 12:06 PM
gedare updated this revision to Diff 542249.Jul 19 2023, 5:01 PM

Add config parse tests for deprecated options, and add tests for attribute

gedare marked an inline comment as done.Jul 19 2023, 5:20 PM

Everything is fine, I just need to know how the attribute stuff is formatted with plain LLVM style.

gedare added inline comments.Jul 20 2023, 12:32 PM
clang/unittests/Format/FormatTest.cpp
16778

and here

16822

Everything is fine, I just need to know how the attribute stuff is formatted with plain LLVM style.

oh, sorry. plain llvm style. I will look for a good place to add that.

Everything is fine, I just need to know how the attribute stuff is formatted with plain LLVM style.

oh, sorry. plain llvm style. I will look for a good place to add that.

You can put it in the same test case in front to build the baseline.

gedare updated this revision to Diff 542649.Jul 20 2023, 1:18 PM

Add more attribute test cases.

Everything is fine, I just need to know how the attribute stuff is formatted with plain LLVM style.

__attribute__ is formatted with plain LLVM style in 5-10 other test cases. For plain llvm style probably the best is UnderstandsAttributes. I did add the test cases at the start of ConfigurableSpacesInParens with plain llvm style to establish a baseline.

In reviewing those test cases I did see a good reason to add another test of attribute placement. I have now covered function decl attributes before and after the function name, and variable attributes.

This revision is now accepted and ready to land.Jul 20 2023, 1:38 PM
gedare updated this revision to Diff 543727.Jul 24 2023, 3:41 PM

Add a release note.

owenpan added inline comments.Jul 24 2023, 7:12 PM
clang/docs/ReleaseNotes.rst
830
clang/include/clang/Format/Format.h
4245
4247–4248
4254–4255
This revision was landed with ongoing or failed builds.Jul 24 2023, 7:27 PM
This revision was automatically updated to reflect the committed changes.