This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Allow break after return keyword
AbandonedPublic

Authored by gedare on Jun 14 2023, 3:31 PM.

Details

Summary

Currently new line breaks are not added between the return keyword
and the return value. With long, singleton return values, it can be
preferred to break before the return value. An example of this would be
a lengthy string return value.

Adds a new style option PenaltyBreakReturn to control when breaks are
preferred. With the current setting of 100, most existing unit tests pass.
One unit test needed to be tweaked, as it assumes very long return values
do not get broken from the return keyword. Added a new unit test to exercise
the long return value.

Diff Detail

Event Timeline

gedare created this revision.Jun 14 2023, 3:31 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJun 14 2023, 3:31 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
gedare requested review of this revision.Jun 14 2023, 3:31 PM
NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to clang/include/clang/Format/Format.h but does not contain an update to ClangFormatStyleOptions.rst

ClangFormatStyleOptions.rst is generated via clang/docs/tools/dump_format_style.py, please run this to regenerate the .rst

You can validate that the rst is valid by running.

./docs/tools/dump_format_style.py
mkdir -p html
/usr/bin/sphinx-build -n ./docs ./html
gedare updated this revision to Diff 531557.Jun 14 2023, 3:40 PM

Regenerate docs

NOTE: Clang-Format Team Automated Review Comment

Your review contains a change to ClangFormatStyleOptions.rst but not a change to clang/include/clang/Format/Format.h

ClangFormatStyleOptions.rst is auto generated from Format.h via clang/docs/tools/dump_format_style.py, please run this to regenerate the .rst

You can validate that the rst is valid by running.

./docs/tools/dump_format_style.py
mkdir -p html
/usr/bin/sphinx-build -n ./docs ./html
NOTE: Clang-Format Team Automated Review Comment

It looks like your clang-format review does not contain any unit tests, please try to ensure all code changes have a unit test (unless this is an NFC or refactoring, adding documentation etc..)

Add your unit tests in clang/unittests/Format and you can build with ninja FormatTests. We recommend using the verifyFormat(xxx) format of unit tests rather than EXPECT_EQ as this will ensure you change is tolerant to random whitespace changes (see FormatTest.cpp as an example)

For situations where your change is altering the TokenAnnotator.cpp which can happen if you are trying to improve the annotation phase to ensure we are correctly identifying the type of a token, please add a token annotator test in TokenAnnotatorTest.cpp

gedare updated this revision to Diff 531561.Jun 14 2023, 3:47 PM
  • Regenerate ClangFormatStyleOptions.rst

I'd like more tests (and examples how it is before the change), some operations (+, *, etc.), your stated string literal with and without many spaces, call chains.

I have nothing against the introduction, I'm just not sure about the default value and the position of the check in splitPenalty.

And please add a remark in the changelog.

gedare added a comment.EditedJun 17 2023, 11:23 AM

I'd like more tests (and examples how it is before the change), some operations (+, *, etc.), your stated string literal with and without many spaces, call chains.

Ok. Since it applies by default, all the existing unittests use it. I will see about fabricating some more interesting tests for it.

I have nothing against the introduction, I'm just not sure about the default value and the position of the check in splitPenalty.

Yes the default was chosen arbitrarily. Adding more tests to exercise return value lengths should help tune defaults.

And please add a remark in the changelog.

Is there a separate changelog file? I don't understand this request. Edit: I think this refers to ./clang/docs/ReleaseNotes.rst

And please add a remark in the changelog.

Is there a separate changelog file? I don't understand this request. Edit: I think this refers to ./clang/docs/ReleaseNotes.rst

Yep that's the file.

MyDeveloperDay added inline comments.Jun 21 2023, 8:09 AM
clang/lib/Format/Format.cpp
1484

is 100 the current penalty?

clang/unittests/Format/FormatTest.cpp
22187

the fails the beyonce rule doesn't it, unless you are assuming the test is a bug?

gedare abandoned this revision.Jun 22 2023, 11:12 AM

I don't like the approach I took. It will be hard (impossible) to tune a default return penalty that ensures backward compatibility. The only way I see to make this work is to add an option to enable breaks after the return keyword. This is low on my priorities however, so I am abandoning this.