Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 830
user972014 Avatar asked Oct 04 '16 17:10

user972014


2 Answers

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.

like image 157
AMA Avatar answered Oct 09 '22 00:10

AMA


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

like image 2
Hans Avatar answered Oct 09 '22 01:10

Hans