5

I'm using BreakBeforeBraces: Allman in my .clang-format file, but braces in control statements (such as if, for, while, ...) are not being put on their own line.

// Currently:
void foo()
{
    while(true) {
        bar();
    }
}

// What I want:
void foo()
{
    while(true) 
    {
        bar();
    }
}

I've read that you can set nested configuration classes for braces in BraceWrapping, but I could't figure out the correct YAML syntax (and JSON syntax for the sublime text plugin), and couldn't find any existing example.

Is there any way of doing this?

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • When I run `clang-format -i -style="{BreakBeforeBraces: Allman}" test.cpp` on your example it puts all braces on their own line as you'd expect for the Allman style. Do you see something else? – Richard Viney Oct 05 '15 at 23:05
  • 2
    @RichardViney: you're right... I did some tests and turns out that `AllowShortLoopsOnASingleLine: true` is the culprit of this issue. Setting it to `false` makes the formatting work as intended. Is this a bug? – Vittorio Romeo Oct 06 '15 at 06:31
  • No I don't think this is a bug, in fact there are other style options such as `AllowShortFunctionsOnASingleLine` which when used with `BreakBeforeBraces: Allman` will also allow braces to sometimes not always have a line all to themselves under certain conditions. If you want ultra-strict Allman braces you'll need to check these other styles as well. – Richard Viney Oct 06 '15 at 07:25
  • But, as an example, the `AllowShortLoopsOnASingleLine` option is `bool` and only allows `true` or `false`. And it does affect the Allman braces *(as shown in the original question)*, even when the loop isn't actually transformed in a single line. – Vittorio Romeo Oct 06 '15 at 09:59
  • If it does indeed do that then yes I agree it's a bug, however `clang-format -i -style="{BreakBeforeBraces: Allman, AllowShortLoopsOnASingleLine: true}" test.cpp` run on both LLVM 3.6.2 and on trunk don't exhibit that behaviour in my testing. Maybe there's some interaction with another style that you're using? – Richard Viney Oct 06 '15 at 11:43
  • 1
    @RichardViney: I did more tests and figured out the combination of styles - I opened a bug report on: https://llvm.org/bugs/show_bug.cgi?id=25069 – Vittorio Romeo Oct 07 '15 at 08:29

2 Answers2

5

Achieving the desired result with a specific combination of style options is impossible at the moment. I've reported the issue as bug 25069.

Vittorio Romeo
  • 90,666
  • 33
  • 258
  • 416
  • It looks like clang-format-3.8 has individual options for breaking when BraceWrapping is set to custom now. – Trevor Hickey Jan 13 '16 at 02:40
  • This is still a bug as of clang-format 5.0.1. The key combination is `AllowShortBlocksOnASingleLine: true` and any other `AllowShort...OnASingleLine` option. Having both options set seems to negate the Allman brace style. If `AllowShortBlocksOnASingleLine: false` then you can control to your heart's content. – jwm Mar 16 '20 at 23:40
0

To work around this, I first run artistic style with the option -A10, before running clang-format

Barijaona
  • 11
  • 2