<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/86550>86550</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            clang-format 18 idempotent regression with #define within an initializer
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          JohnC32
      </td>
    </tr>
</table>

<pre>
    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`

```c++
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`:

```yaml
---
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
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
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUV1tv2zgT_TX0y0ACRV394AdfoiJfmzhI8qHovhiUNLa4oElDpGK4v35By_c4u03aAruCYMjD0Zzh8PBwxI0RC4U4IPGIxJMeb22tm8H_dK3GIesVutoMHlulhFpAKblaeHPdLLmFIAO7FiXCqtFVW6KBSszn2KCy0KBppTWwrlHBGqHmLwiEhRXOhUIwlltconIewtZCAVcglLCCS_EdGx9gD-nGL3BTn_oxmFqvDSh9QC0RtIIGV8gtViDUiy65FVoZn9AJocPu95N4QQUkoaLC5UpbVHbWpeWXqxVJ6Kmz-7u9S8JG7t5ahbIw15qw7G7zvFkh8FzrJ_EdPwtVEdYHku48tzMVJZRaGQtD50wDFkZxkmZ9sLyQeP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