-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Description
With AlignArrayOfStructures: Left enabled, clang-format adds spurious spaces to certain expressions inside nested brace initializers. It's kind of hard to explain precisely, so I made a simplified example:
Running clang-format -style="{AlignArrayOfStructures: Left}" on
#define MACRO(a, ...) (a)
struct TestStruct {
int a1[1];
int a2[1];
} test = {
{
MACRO(1, 3, 235, 12, -1, 99, 0),
},
{
MACRO(8, 9, 5, 1, 62, 5, 101),
},
};results in
#define MACRO(a, ...) (a)
struct TestStruct {
int a1[1];
int a2[1];
} test = {
{
MACRO(1, 3, 235, 12, -1, 99, 0),
},
{
MACRO(8, 9, 5, 1, 62, 5, 101),
},
};I'm not sure what it should look like, but definitely not that.
As far as I can tell, this occurs when:
- each inner macro invocation (or parenthesized list) has the same number of comma-separated arguments, and
- each inner list is followed by a trailing comma.
If the lists have different numbers of elements, it seems AlignArrayOfStructures won't touch it—so they just end up separated by single spaces.
This also occurs when using designated initializers (which is how I originally encountered the issue).
I was able to reproduce this issue using:
- the official LLVM 21.1.4 release for 64-bit Windows
- whatever version is bundled with the VS Code C/C++ extension
- Debian's clang-format 19.1.7 (
Debian clang-format version 19.1.7 (7))
Here's an example file that might make the required conditions clearer: clang-format_spacing_example.txt
(GitHub wouldn't let me upload it with a cpp extension. It contains some bonus strange behavior at the end that I didn't have time to look into.)