Issue |
108726
|
Summary |
Trailing commas in static initializer completely disables formatting for that initializer
|
Labels |
new issue
|
Assignees |
|
Reporter |
detly
|
Take the following C code:
```c
typedef struct {
int one;
int extras[3];
} input_extras_foobar_t;
static input_extras_foobar_t input_extras_from_level(
unsigned int level, int really_long_arg)
{
input_extras_foobar_t val = {
._one_ = 1001, .extras = { [2] = !!((level - 1) & 1U) != !really_long_arg, [1] = !!((level - 1) & 2U) != !really_long_arg, [0] = !!((level - 1) & 4U) != !really_long_arg,},
};
return val;
}
```
And this `.clang-format`:
```yml
BasedOnStyle: WebKit
Language: Cpp
ColumnLimit: 88
```
Running `clang-format -i test.c` leaves the overly long initializer line completely untouched.
If I remove the outermost trailing comma, it _does_ get formatted as:
```c
typedef struct {
int one;
int extras[3];
} input_extras_foobar_t;
static input_extras_foobar_t input_extras_from_level(
unsigned int level, int really_long_arg)
{
input_extras_foobar_t val = { ._one_ = 1001,
.extras = {
[2] = !!((level - 1) & 1U) != !really_long_arg,
[1] = !!((level - 1) & 2U) != !really_long_arg,
[0] = !!((level - 1) & 4U) != !really_long_arg,
} };
return val;
}
```
If I remove the trailing comma only for the `.extras` structure, I get a different result:
```c
typedef struct {
int one;
int extras[3];
} input_extras_foobar_t;
static input_extras_foobar_t input_extras_from_level(
unsigned int level, int really_long_arg)
{
input_extras_foobar_t val = {
._one_ = 1001,
.extras = { [2] = !!((level - 1) & 1U) != !really_long_arg,
[1] = !!((level - 1) & 2U) != !really_long_arg,
[0] = !!((level - 1) & 4U) != !really_long_arg },
};
return val;
}
```
I understand that trailing commas are sort-of used to change line breaking/block indent behaviour for initializer lists. However, I'd expect that having both trailing commas here would combine the behaviour, like so:
```c
typedef struct {
int one;
int extras[3];
} input_extras_foobar_t;
static input_extras_foobar_t input_extras_from_level(
unsigned int level, int really_long_arg)
{
input_extras_foobar_t val = {
._one_ = 1001,
.extras = {
[2] = !!((level - 1) & 1U) != !really_long_arg,
[1] = !!((level - 1) & 2U) != !really_long_arg,
[0] = !!((level - 1) & 4U) != !really_long_arg,
},
};
return val;
}
```
Completely refusing to format the _expression_ itself is quite surprising.
This appears in `clang-format` 18 and 20.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs