This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Don't remove braces if a 1-statement body would wrap
ClosedPublic

Authored by owenpan on May 6 2022, 3:27 PM.

Details

Summary

Reimplement the RemoveBracesLLVM feature which handles a single-statement block that would get wrapped.

Fixes https://github.com/llvm/llvm-project/issues/53543.

Diff Detail

Event Timeline

owenpan created this revision.May 6 2022, 3:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2022, 3:27 PM
owenpan requested review of this revision.May 6 2022, 3:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 6 2022, 3:27 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
Jeroen added a subscriber: Jeroen.May 6 2022, 10:22 PM

Will it also add braces if they where not there yet?

clang/unittests/Format/FormatTest.cpp
25374

Will it also add braces if they where not there yet?

owenpan added inline comments.May 6 2022, 10:37 PM
clang/unittests/Format/FormatTest.cpp
25374

No, but you can insert braces by setting InsertBraces to true and RemoveBracesLLVM to false.

clang/lib/Format/UnwrappedLineParser.cpp
465

A bit explanation what it means that something might fit in one line would be nice.

479

Especially this one is not clear to me, why do we return true here?

481

Is a FormatToken "big", or expensive to copy? If not I'd save them directly, otherwise I'd prefer a unique_ptr.

owenpan planned changes to this revision.May 7 2022, 12:46 PM
owenpan added inline comments.
clang/lib/Format/UnwrappedLineParser.cpp
465

Will do.

479

We will remove braces only if the previous line (i.e. the line before the closing brace) ends with a semi or comment. I should move this check to the caller or rename the function.

481

We have to make a copy of the FormatToken here so that we can restore it after the TokenAnnotator modifies it directly.

484

We must save Token.Children as well.

owenpan updated this revision to Diff 428468.May 10 2022, 12:56 PM

Added saving/restoring the children of UnwrappedLineNode and addressed review comments.

owenpan marked 3 inline comments as done.May 10 2022, 12:58 PM
curdeius accepted this revision.May 11 2022, 12:35 PM

LGTM with nits.

clang/lib/Format/UnwrappedLineParser.cpp
810

It might be a matter of taste but adding this variable makes the code harder to read to me.

821

So the token's children are modified (moved)? Is it done so that children be not considered by the annotator?

831

Why not like this?

This revision is now accepted and ready to land.May 11 2022, 12:35 PM
owenpan added inline comments.May 11 2022, 1:05 PM
clang/lib/Format/UnwrappedLineParser.cpp
810

The last token of ParsedLine is of interest here. We want the annotator to compute its TotalLength to determine whether the line might fit on a single line. I'm open to renaming the variable if you have a better suggestion.

821

Both the constructor and the destructor of AnnotatedLine clear the children of UnwrappedLineNode, so we save them beforehand and restore them afterward.

831

Why not like this?

See the assertion on line 781 above. We are computing the TotalLength of LastToken via Line. Either way works, but I prefer the simpler expression. I can change it though if you insist.

curdeius added inline comments.May 11 2022, 1:16 PM
clang/lib/Format/UnwrappedLineParser.cpp
831

Yeah, what I meant is to just get rid of LastToken variable and write the suggested code.
But well, both ways work. No strong feelings.

This revision was landed with ongoing or failed builds.May 12 2022, 3:54 AM
This revision was automatically updated to reflect the committed changes.