简体   繁体   中英

clang-format: brace on new line except for empty blocks

I'm trying to setup the following formatting:

struct no_member {}; // single line

struct one_member
{
    int a;
};

struct multiple_members
{
    int a;
    int b;
};

Sadly, clang-format doesn't seem to be able to conditionally break before braces, it's only always or never, and I get stuck with the following formatting:

struct empty
{};

which I really don't like. There's the same problem with functions or control blocks. You can't have empty brace on a single line and break before in other cases.

That doesn't seem like a very exotic or unusual formatting, is it doable?

I'm trying to setup the following formatting:

struct no_member {}; // single line

struct one_member
{
    int a;
};

struct multiple_members
{
    int a;
    int b;
};

Sadly, clang-format doesn't seem to be able to conditionaly break before braces, it's only always or never, and I get stuck with the following formatting:

struct empty
{};

wich I realy don't like. There's the same problem with functions or constrol blocks. You can't have empty brace on a single line and break before in other cases.

That doesn't seem like a very exotic or unusual formatting, is it doable?

I'm trying to setup the following formatting:

struct no_member {}; // single line

struct one_member
{
    int a;
};

struct multiple_members
{
    int a;
    int b;
};

Sadly, clang-format doesn't seem to be able to conditionaly break before braces, it's only always or never, and I get stuck with the following formatting:

struct empty
{};

wich I realy don't like. There's the same problem with functions or constrol blocks. You can't have empty brace on a single line and break before in other cases.

That doesn't seem like a very exotic or unusual formatting, is it doable?

struct one_member
{
    int a;
}

Here you forget ; at close brace;

see:

#include <stdio.h>

struct no_member
{}; // single line

struct one_member
{
    int a;
};

struct multiple_members
{
    int a;
    int b;
};

int main(void) {
  printf("Hello World\n");
  return 0;
}

Nothing error here, program will print Hello World

I found something similar which may help you.

AllowShortFunctionsOnASingleLine: Empty
AllowShortBlocksOnASingleLine: Empty

So the result is:

void emptyfn() {}

void myfn() 
{
    //some code
}

But this work only for functions, there is option like this:这样的选项:

AllowShortStructsOnASingleLine: Empty

Here is my.clang-format file https://pastebin.com/ZHmJxQ7g

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM