This is an archive of the discontinued LLVM Phabricator instance.

clang-format: Fixed various brace wrapping and block merging bugs
Needs ReviewPublic

Authored by mxbOctasic on Apr 13 2016, 12:02 PM.

Details

Reviewers
djasper
Summary

Multiple brace wrapping flag combination were broken.

First, IndentBraces + !AfterControlStatement caused the whole If-Else construct to be indented.

Currently, only function blocks can be merged into one line when the opening brace wrapped.
The short block merging logic checks the AfterFunction flag for all block types.
With AllowShortFunctions (All), !BraceWrapping.AfterFunction and BraceWrapping.AfterControlStatement I ended up with this:

if(true)
{}

This happened with all other non-function blocks with the opening brace wrapped on its own line.

Diff Detail

Event Timeline

mxbOctasic retitled this revision from to clang-format: Fixed various brace wrapping and block merging bugs.
mxbOctasic updated this object.
mxbOctasic added a reviewer: djasper.
mxbOctasic added subscribers: cameron314, cfe-commits.
mxbOctasic added inline comments.Apr 13 2016, 12:17 PM
lib/Format/UnwrappedLineParser.cpp
789–790

We may not want this.

1388–1390

IndentBraces can only work properly when both AfterControlStatement and BeforeElse are active.
The opening brace can only be indented when it is wrapped on its own line, and the else keyword has to be on another line too.
We have to decide which option has precedence over the others.
Right now I'm overriding BeforeElse when the opening brace is wrapped.

unittests/Format/FormatTest.cpp
2454–2456

This test probably should not have been changed.
However, it's strange to have the try on one line but not the catch.

djasper added inline comments.Apr 13 2016, 12:56 PM
lib/Format/UnwrappedLineParser.cpp
1388–1390

Maybe we should put logic like this into expandPresets (possibly renaming it)?

unittests/Format/FormatTest.cpp
427

I think we need a better way to structure these tests. And reusing a style with the name "AllowSimpleBracedStatements" doesn't make sense here.

474

I'd remove all the line breaks and "\n"s here.

2454–2456

Yeah, I agree.

mxbOctasic added inline comments.Apr 14 2016, 7:48 AM
lib/Format/UnwrappedLineParser.cpp
789–790

Type is set to TT_CompoundStatementLBrace by the CompoundStatementIndenter.

Should it be TT_ObjCBlockLBrace instead?

Should the braces be affected by BraceWrapping.IndentBraces?

1388–1390

Something like "applyBraceWrappingFlags"?
The preset styles do not set incompatible flags (yet?).
Do we want to perform fix-ups like this only on custom styles?

I will update the \brief in Format.h to mention that some flags can be overridden in certain edge cases.

unittests/Format/FormatTest.cpp
474

Should we consider the BeforeElse flag when merging this statement?