This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix lambda formatting in conditional
ClosedPublic

Authored by HazardyKnusperkeks on Oct 13 2022, 2:19 PM.

Details

Summary

Without the patch UnwrappedLineFormatter::analyzeSolutionSpace just ran out of possible formattings and would put everything just on one line. The problem was that the the line break was forbidden, but putting the conditional colon on the same line is also forbidden.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptOct 13 2022, 2:19 PM
HazardyKnusperkeks requested review of this revision.Oct 13 2022, 2:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 13 2022, 2:19 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Fun fact, there is one instance of such a case within the clang-format code. I can't remember where (I stumbled upon this in January, and tried to fix it ever since), should that be reformatted with this patch, or with a follow up?

clang/lib/Format/ContinuationIndenter.cpp
1412–1417

Someone put that in on purpose, but is that only intended for java script, then we could add the language check.
As one can see I also had to disable it for requires clauses. Because I think it is a very hard thing to disable line breaks for all the scopes.

1423

Or should we limit that to lambdas in a conditional? That would be even messier to detect.

MyDeveloperDay accepted this revision.Oct 14 2022, 9:22 AM
This revision is now accepted and ready to land.Oct 14 2022, 9:22 AM

Fun fact, there is one instance of such a case within the clang-format code. I can't remember where (I stumbled upon this in January, and tried to fix it ever since), should that be reformatted with this patch, or with a follow up?

Perhaps with a follow-up in which we can reformat the entire directory.

clang/lib/Format/ContinuationIndenter.cpp
333

Return true if Current is a conditional colon preceded by a lambda r_brace that is followed by a pair of (possibly empty) parentheses.

1412–1417

Someone put that in on purpose, but is that only intended for java script, then we could add the language check.

It started as a JavaScript-only behavior but was later extended to cover all languages.

1423

Or should we limit that to lambdas in a conditional?

IMO we should, and it might not be too bad if you override NoLineBreak in canBreak() instead. See line 333 above.

HazardyKnusperkeks marked 3 inline comments as done.

Override in canBreak().

owenpan accepted this revision.Oct 24 2022, 8:39 PM
owenpan added inline comments.
clang/lib/Format/ContinuationIndenter.cpp
336

We can delete this line.

HazardyKnusperkeks marked an inline comment as done.
HazardyKnusperkeks added inline comments.
clang/lib/Format/ContinuationIndenter.cpp
336

Added an assert, just to be safe.

owenpan accepted this revision.Oct 26 2022, 9:14 PM
This revision was landed with ongoing or failed builds.Nov 3 2022, 5:09 AM
This revision was automatically updated to reflect the committed changes.