| Issue |
180179
|
| Summary |
[clang-format] Option "SpaceAfterCStyleCast" is applied to compound literal since clang-format-19
|
| Labels |
clang-format
|
| Assignees |
|
| Reporter |
BigPeet
|
Hi,
in C there is a semantic difference between the [cast operator](https://en.cppreference.com/w/c/language/cast.html) and [compound literals](https://en.cppreference.com/w/c/language/compound_literal.html), although their syntax is similar.
I prefer to differentiate between those two by using a space for casting but not for compound literals.
For example:
```c
struct S { int x; };
(struct S*) malloc(sizeof(struct S)); // space for C-style cast
(struct S){.x = 1}; // no space for compound literal
```
Therefore I'm setting "SpaceAfterCStyleCast" to `true`.
This worked fine with clang-format-18, but with clang-format-19, SpaceAfterCStyleCast seems to be applied to compound literals as well.
Example code:
```c
// test.c
struct S {
int x;
};
void Take(struct S a, struct S b, struct S c) {
// C-style casts
(void)a; // no space
(void) b; // one space -> my preference
(void) c; // two spaces
}
int main(void) {
// compound literals
Take(
(struct S){.x = 1}, // no space -> my preference
(struct S) {.x = 1}, // one space
(struct S) {.x = 1} // two spaces
);
return 0;
}
```
When formatting this with clang-format-18 (on Ubuntu 24.04), then all the C-style casts in `Take` will have one space and the compound literals will have no space.
But when formatting this with clang-format-19 (or later versions), then the compound literals will also have a space.
```bash
clang-format-18 test.c --style="{BasedOnStyle: llvm, SpaceAfterCStyleCast: true}"
# vs.
clang-format-19 test.c --style="{BasedOnStyle: llvm, SpaceAfterCStyleCast: true}
```
Optimally, there should be a new option which controls whether to insert a space for compound literals. But at least SpaceAfterCStyleCast should not meddle with compound literals.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs