This is an archive of the discontinued LLVM Phabricator instance.

[Clang-Format] Add AlwaysBreakBeforeElse and AlwaysBreakBeforeCatch Style to avoid cuddled else/catch
AbandonedPublic

Authored by MyDeveloperDay on Aug 31 2015, 6:04 AM.

Details

Summary

Implementation of Jarkko Hietaniemi original AlwaysBreakBeforeElse and AlwaysBreakBeforeCatch to avoid cuddled braces on an "else/catch" statement.

WebKit Style + Linux BreakBeforeBraces style gives cuddled else/catch brackets (e.g. "} else {") , this patch adds an AlwaysBreakBeforeElse and AlwaysBreakBeforeCatch style to allow required brace style such that the else/catch is placed on a newline after the closing brace of the if/try

Patch by: Jarkko Hietaniemi

Diff Detail

Event Timeline

MyDeveloperDay retitled this revision from to [Clang-Format] Add AlwaysBreakBeforeElse Style to avoid cuddled else.
MyDeveloperDay updated this object.
MyDeveloperDay added a subscriber: cfe-commits.
MyDeveloperDay retitled this revision from [Clang-Format] Add AlwaysBreakBeforeElse Style to avoid cuddled else to [Clang-Format] Add AlwaysBreakBeforeElse and AlwaysBreakBeforeCatch Style to avoid cuddled else/catch.
MyDeveloperDay updated this object.

As this patch has not been reviewed yet, and in using the tool in my own environment I noticed the same is true for catch statments, I want to see
try {
}
catch (exception &e) {
}

not

try {
} catch (exception &e) {
}

Handle subsequent catch statements

nikola resigned from this revision.Sep 16 2015, 4:53 PM
nikola removed a reviewer: nikola.
djasper edited edge metadata.Sep 17 2015, 2:06 AM

Please upload a patch with full file context.

docs/ClangFormatStyleOptions.rst
247

Hm, I think these should be grouped in some way and interact with BreakBeforeBraces.

I theory, I'd like to have a lot of different flags and make BreakBeforeBraces select specific presents for them. However, maybe we can put all the brace breaking options together in some nice way (don't have a very good idea yet).

Not saying that you need to do all of this, but pulling these two options out from the other BreakBeforeBraces options seems a little undesirable. They essentially also just define what we do around braces.

MyDeveloperDay edited edge metadata.

full file diff

MyDeveloperDay added inline comments.Sep 17 2015, 2:50 AM
docs/ClangFormatStyleOptions.rst
247

Well I agree, ideally the BreakBeforeBraces styles should be implemented by setting the finite control on each element

Then code like this

if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||

Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup ||
Style.AlwaysBreakBeforeCatch) {
 ...
}

could become

if (Style.AlwaysBreakBeforeCatch) {

...

}

This would simply require some code in the initializer that says

if (Style.BreakBeforeBraces == FormatStyle::BS_Allman ||

Style.BreakBeforeBraces == FormatStyle::BS_GNU ||
Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)

{

Style.AlwaysBreakBeforeCatch=true;

}

if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
{

Style.AlwaysBreakBeforeElse=true;

}

This would allow those of us not using one of the 5 chosen styles to build our teams style guide via the individual capabilities

Removing all the BS_Stroustrup/Allman etc.. from the UnwrappedLineParser will greatly improve its readability

The question is how all the other options should be called and how we can
group them nicely so that we don't flood the options configuration page too
much. Maybe it's time for nesting in the configuration class?

If that's ok for you, I'd like to try and restructure the existing options first. I'll try to complete that this week. Will update this review once I am done.

@djasper I'm more than happy with that, just ping me if you need me the rework the change afterwards to fit

I think r248802 should make this mostly obsolete, but feel free to add the tests :-).

I don't have permission to commit

This looks good though.

What do you mean by "this looks good"?

MyDeveloperDay abandoned this revision.Sep 30 2015, 8:00 AM