user972014
user972014

Reputation: 3856

Using clang-format - keep empty braces on the same line

I'm trying to configure clang-format so that usually braces will start on their own line:

void func()
{
    if (...)
    {
        printf("Ta Da\n");
    }
}

But I want it to be so when braces are empty, it will be kept in a single line. (Mainly used for ctors):

Bar::Bar(int val):
    _val(val)
{}

currently it will look like this:

Bar::Bar(int val):
    _val(val)
{
}

Any ideas?

(Edited to make the situation clearer)

Upvotes: 16

Views: 6645

Answers (2)

AMA
AMA

Reputation: 4214

UPDATE: Hurray! It is now possible with Clang 5.0 or later with custom BreakBeforeBraces. See SplitEmptyFunction in the documentation.

Configuration example:

BreakBeforeBraces: Custom
BraceWrapping:
  SplitEmptyFunction: false

↓↓↓ Original answer (outdated) ↓↓↓

Unfortunately, it is not possible to achieve with Clang 4.0 the current clang-format options (as of Clang 4.0).

Source: I had the same question. After studying every documented option, and many tweaking attempts, I could not achieve this. In my experience, clang-format is just not as flexible as one would hope. As soon as you want to step out of the predefined styles and tweak things to your liking, it just does not cut it.

Upvotes: 7

Hans
Hans

Reputation: 1916

I used combination of "AllowShortFunctionsOnASingleLine": true, and "BreakBeforeBraces": "Allman", to get it to one line when declaring empty constructors etc..

Upvotes: 2

Related Questions