0

i have a very strange behavior with my current .clang-format file.

here is the file:

---
BasedOnStyle: LLVM
ColumnLimit: 200
IndentWidth: 4
UseTab: Always
TabWidth: 4
BreakBeforeBraces: Linux

considering this example C code it will format it as expected:

struct foo {
    char a;
    int b;
};

static const struct foo data[] = {
    {'a', 1},
    {'b', 2},
    {'c', 3},
};

however if i add an empty {} element in my array, clang-format will put the complete array initialization in one line:

struct foo {
    char a;
    int b;
};

static const struct foo data[] = {{'a', 1}, {'b', 2}, {'c', 3}, {}};

is there a way to tell clang-format to allow an empty element in a line and to correctly format the code as in the first example?

EDIT1:

as Nullndr suggested adding a traling , will get clang-format to create the right formatting.

however lets rephrase the question: is there a way to tell clang-format to do the formatting correct even without trailing ,?

EDIT2: it seems this is a bug in clang-format. i reported it here: https://github.com/llvm/llvm-project/issues/61585

eddy
  • 488
  • 5
  • 18

1 Answers1

1

Sadly, even with version 15 this is still not possible and there is nothing about this on the docs.

This is not a problem just with arrays, but with structs too, take a look:

static const struct foo data = {'a', 1}; // see the missing `,`

If you put a comma, it correctly indent the struct:

static const struct foo data = {
  'a',
  1, // see the trailing `,` 
};

Here on SO there are a lot of responses about this, see here for example.

Another trick is to put a comment at the end of the row:

static const struct foo data[] = {
  {'a', 1}, //
  {'b', 2}, //
  {'c', 3}  //
};

I never used this since I find it ugly

Ian Abbott
  • 15,083
  • 19
  • 33
Nullndr
  • 1,624
  • 1
  • 6
  • 24