This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix a bug that erroneously placed function arguments on a new line despite all arguments being able to fit on the same line.
ClosedPublic

Authored by jp4a50 on Jul 25 2023, 1:08 PM.

Details

Summary

Fixes a long-standing bug: https://github.com/llvm/llvm-project/issues/44486.

The original diff that introduced the bug implemented behaviour that pushed the first argument to a function onto a new line under certain circumstances relating passing lambdas as arguments.

This behaviour was implemented in TokenAnnotator::mustBreakBefore() which meant the code lacked the necessary context to figure out whether subsequent arguments might be able to all fit on one line. As such, I've moved the implementation to ContinuationIndenter and, instead of forcing a line break at the first argument in all cases, we now allow the OptimizingLineFormatter to consider placing the first argument on the same line as the function call but don't allow further line breaks in this case.

The end result is that either the first argument must go on a new line (as before) or all arguments must be put on the current line.

Diff Detail

Event Timeline

jp4a50 created this revision.Jul 25 2023, 1:08 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptJul 25 2023, 1:08 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
jp4a50 requested review of this revision.Jul 25 2023, 1:08 PM
clang/lib/Format/ContinuationIndenter.cpp
263

Was this uninitialized?

657

Can drop outer parens.

664–666

Could move this out of the lambda.

669

This can be merged with !Current.is(tok::comment).

jp4a50 updated this revision to Diff 544308.Jul 26 2023, 4:48 AM

Address minor review comments.

jp4a50 marked 2 inline comments as done.Jul 26 2023, 4:50 AM
jp4a50 added inline comments.
clang/lib/Format/ContinuationIndenter.cpp
263

It's a new member variable I added as part of this diff.

664–666

Not sure exactly what you're referring to here. Initialization of PrevNonComment?

jp4a50 updated this revision to Diff 544310.Jul 26 2023, 4:51 AM

Format files.

clang/lib/Format/ContinuationIndenter.cpp
263

Sorry, I looked for that, but somehow missed it.

664–666

Not sure exactly what you're referring to here. Initialization of PrevNonComment?

No everything in the if before you go into the PrevNonComment. It is highlighted if you hover the comment.

jp4a50 added inline comments.Jul 27 2023, 9:13 AM
clang/lib/Format/ContinuationIndenter.cpp
664–666

Ah, I see, thanks. Technically I could move that outside the lambda. I'm just not sure how it's better? Are you making the suggestion because it would make it clearer that this logic only applies to cpp/objc and/or because it means we could avoid capturing Style?

jp4a50 marked 2 inline comments as done.Jul 27 2023, 9:14 AM
clang/lib/Format/ContinuationIndenter.cpp
664–666

Can do how you want, I think avoid capturing Style would be nice. And you wouldn't search for PrevNonComment if you don't need to. This could also be done be reordering the statements and a new return.

jp4a50 updated this revision to Diff 545594.Jul 31 2023, 4:47 AM

Refactor DisallowLineBreaksOnThisLine lambda.

jp4a50 marked 3 inline comments as done.Jul 31 2023, 4:49 AM

All comments addressed. PTAL and let me know if there are any other blockers for merge.

clang/lib/Format/ContinuationIndenter.cpp
664–666

I kept all conditions inside the lambda in the end since moving any out would have made the DisallowLineBreaksOnThisLine not reflect all conditions and I wanted to keep that.

Instead I've changed all conditions to the style of early return for consistency and ordered them in the way that I think makes them most readable.

jp4a50 updated this revision to Diff 545622.Jul 31 2023, 6:10 AM
jp4a50 marked an inline comment as done.

Formatting.

clang/lib/Format/ContinuationIndenter.cpp
657–666

I'd go for this.
But that's not necessary.

659

lambdas

HazardyKnusperkeks retitled this revision from Fix a bug that erroneously placed function arguments on a new line despite all arguments being able to fit on the same line. to [clang-format] Fix a bug that erroneously placed function arguments on a new line despite all arguments being able to fit on the same line..Jul 31 2023, 12:55 PM
jp4a50 added inline comments.Aug 1 2023, 4:11 AM
clang/lib/Format/ContinuationIndenter.cpp
657–666

Ah, yes, I see what you mean, now! Easy enough change to make so I'll do it since there's a typo to fix as well anyway.

jp4a50 updated this revision to Diff 546002.Aug 1 2023, 4:15 AM

More CR feedback.

jp4a50 marked 2 inline comments as done.Aug 1 2023, 4:16 AM
This revision is now accepted and ready to land.Aug 1 2023, 12:58 PM
jp4a50 updated this revision to Diff 546972.Aug 3 2023, 12:23 PM

Format files.

@HazardyKnusperkeks could you merge this for me assuming the build is green please? I don't have merge rights. Thanks.

@HazardyKnusperkeks could you merge this for me assuming the build is green please? I don't have merge rights. Thanks.

That will have to wait, but if no one else stepped in, I'll do it in ~2 weeks.

owenpan added inline comments.Aug 3 2023, 5:26 PM
clang/docs/ReleaseNotes.rst
167–168 ↗(On Diff #546972)

Please delete it. We only update the release note for new options.

clang/lib/Format/ContinuationIndenter.cpp
658–659
667–669
676
// Multiple lambdas in the same function call.
679
// A lambda followed by another arg.
686–692
jp4a50 updated this revision to Diff 552401.Aug 22 2023, 9:20 AM

Minor refactoring and comments.

jp4a50 updated this revision to Diff 552403.Aug 22 2023, 9:26 AM

Minor refactor.

jp4a50 marked 4 inline comments as done.Aug 22 2023, 9:26 AM

Addressed all comments. Please let me know if there's anything else required before merging.

Addressed all comments. Please let me know if there's anything else required before merging.

There are still a couple of comments unaddressed plus another that asked for changing !is() to isNot(). :)

jp4a50 updated this revision to Diff 552701.Aug 23 2023, 7:15 AM

Minor refactoring.

jp4a50 marked an inline comment as done.Aug 23 2023, 7:18 AM

@owenpan right you are! Missed those somehow. Made a further two changes. Hope I haven't missed anything else!

owenpan accepted this revision.Aug 23 2023, 10:50 AM
This revision was automatically updated to reflect the committed changes.