Skip to content

clang-format 18 idempotent regression with #define within an initializer #86550

@JohnC32

Description

@JohnC32

Running clang-format 18 twice produces different results when we have #define statements within an initializer. Running with clang-format 17.0.5 shows no difference on repeated invocations.

Given idempotent_define.cpp

int foo(MyType aFooSizeKind) { static const AType0123456789 table{ #define FOO_SIZE_KIND_2_OP_ROW(row) {GET_SYM_SIZE_KIND(row), GET_OP(row)} COMMA_LIST_OF(FOO_SIZE_KIND_2_OP_ROW, FOOBAR12_BINOP_SIZE_LIST), COMMA_LIST_OF(FOO_SIZE_KIND_2_OP_ROW, FOOBAR12_UNARY_SIZE_LIST)}; return table.at(aFooSizeKind); }

using _clang-format:

--- BasedOnStyle: Google IndentWidth: 4 ColumnLimit: 100 ...

clang-format 18.1.1 yields the following differences on successive runs. Notice that define flips between being on a single line and multiple lines.

/path/to/llvm-18.1.1/bin/clang-format -style=file idempotent_define.cpp >out.18.1.1.a.cpp 2>&1 /path/to/llvm-18.1.1/bin/clang-format -style=file out.18.1.1.a.cpp >out.18.1.1.b.cpp 2>&1 /path/to/llvm-18.1.1/bin/clang-format -style=file out.18.1.1.b.cpp >out.18.1.1.c.cpp 2>&1 
diff -u out.18.1.1.a.cpp out.18.1.1.b.cpp --- out.18.1.1.a.cpp	2024-03-25 14:02:26.681362489 -0400 +++ out.18.1.1.b.cpp	2024-03-25 14:02:26.697362606 -0400 @@ -1,7 +1,6 @@ int foo(MyType aFooSizeKind) { static const AType0123456789 table{ -#define FOO_SIZE_KIND_2_OP_ROW(row) \ - { GET_SYM_SIZE_KIND(row), GET_OP(row) } +#define FOO_SIZE_KIND_2_OP_ROW(row) {GET_SYM_SIZE_KIND(row), GET_OP(row)} COMMA_LIST_OF(FOO_SIZE_KIND_2_OP_ROW, FOOBAR12_BINOP_SIZE_LIST), COMMA_LIST_OF(FOO_SIZE_KIND_2_OP_ROW, FOOBAR12_UNARY_SIZE_LIST)}; diff -u out.18.1.1.b.cpp out.18.1.1.c.cpp --- out.18.1.1.b.cpp	2024-03-25 14:02:26.697362606 -0400 +++ out.18.1.1.c.cpp	2024-03-25 14:02:26.715362737 -0400 @@ -1,6 +1,7 @@ int foo(MyType aFooSizeKind) { static const AType0123456789 table{ -#define FOO_SIZE_KIND_2_OP_ROW(row) {GET_SYM_SIZE_KIND(row), GET_OP(row)} +#define FOO_SIZE_KIND_2_OP_ROW(row) \ + { GET_SYM_SIZE_KIND(row), GET_OP(row) } COMMA_LIST_OF(FOO_SIZE_KIND_2_OP_ROW, FOOBAR12_BINOP_SIZE_LIST), COMMA_LIST_OF(FOO_SIZE_KIND_2_OP_ROW, FOOBAR12_UNARY_SIZE_LIST)}; diff -u out.18.1.1.a.cpp out.18.1.1.c.cpp

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions