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

    <tr>
        <th>Summary</th>
        <td>
            clang-format 19 idempotent regression from 18 for #define within a function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang-format
      </td>
    </tr>

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

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

<pre>
    Running clang-format 19.1.4 twice produces different results when we have #define statements within an initializer.  Running with clang-format 18.1.1 shows no difference on repeated invocations.

Given `idempotent_define.cpp`

```c++
namespace Foo {
void foo() {
#define GEN_ID(_x) \
    char const *_x {    \
        #_x              \
 }
    GEN_ID(one);
    GEN_ID(two);
}
}
```

using `_clang-format`:

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

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

```
/path/to/llvm-19.1.4/bin/clang-format -style=file idempotent_define.cpp >out.19.1.4.a.cpp 2>&1
/path/to/llvm-19.1.4/bin/clang-format -style=file out.19.1.4.a.cpp >out.19.1.4.b.cpp 2>&1
/path/to/llvm-19.1.4/bin/clang-format -style=file out.19.1.4.b.cpp >out.19.1.4.c.cpp 2>&1
```

```diff
diff -u out.19.1.4.a.cpp out.19.1.4.b.cpp
--- out.19.1.4.a.cpp    2024-12-02 13:24:50.702497070 -0500
+++ out.19.1.4.b.cpp    2024-12-02 13:24:50.714497177 -0500
@@ -1,6 +1,7 @@
 namespace Foo {
 void foo() {
-#define GEN_ID(_x) char const *_x{#_x}
+#define GEN_ID(_x) \
+    char const *_x { #_x }
 GEN_ID(one);
     GEN_ID(two);
 }
diff -u out.19.1.4.b.cpp out.19.1.4.c.cpp
--- out.19.1.4.b.cpp    2024-12-02 13:24:50.714497177 -0500
+++ out.19.1.4.c.cpp    2024-12-02 13:24:50.733497347 -0500
@@ -1,7 +1,6 @@
 namespace Foo {
 void foo() {
-#define GEN_ID(_x) \
-    char const *_x { #_x }
+#define GEN_ID(_x) char const *_x{#_x}
 GEN_ID(one);
     GEN_ID(two);
 }
diff -u out.19.1.4.a.cpp out.19.1.4.c.cpp
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vl2PqzYQ_TXOywjkDxLCAw9kd7O6VXUrtQ99jAwMwZWxI2yS3f76yoHsbhJ2W7W3K7SB8XiOZ8742NI5tTeIOVluyPJxIQff2j7_ybbmQfBFaevX_NfBGGX2UGlp9lFj-056YFnM4gT8SVUIh97WQ4UOatU02KPx0KMbtHdwatHACaGVRwTCRY2NMgjOS48dmuChfKsMSAPKKK-kVn9iHwNcYMP4DfY6ZjED19qTA2PfUCsEa6DHA0qPNShztJX0yhoXE1oQWjyrIxogK6pq7A7Wo_G7cUFxdTiQFR3dwsv5qQjfhIcWRnboDrJC2FoLJA22o1U1NNYSviY8m4zvKT4_fd99eyR8vXs5Dy8fCC0AAKpW9lBZ4zwQXuxewsxgf_c4f3Gxe4Grv8mBpI-T3xuENUh4RsTmdsCf7NvAOG_6f0lxzHhwodJkRXcfCx2GRXFTk1fZaUKLKIoILTbSYf2L-c2_aiSigGdr9xoJLb6ZGo3_XdW-DfaE0OLB6qEzP6tO-WBiNGDHcXy_mrlGe1Woawe-RWis1vYUFvxOvAvMu6Gq0Dl1ROgH42L4bn1oT99KDxMrjVYHByX6E6KBEkMYa0BCqIBG0MFJmhq6QXt1mCyXBrpaKN8epG8J33pL-FbrYxeNiyV8WypD-PYqkciNVXpslEaY7UAg4skOPh7DxPJs40Q8Eb5i_xnyLvI1Wvm_oZUzaNUd2nUPvH0Gjgktwg9Ew30Stzhjb975EZpxypOI8YhyYIKIgidEFEsap5QnWUpTChFd0pHazfjMRP88DEuSLGVp-h4moSShEDHCH1ZA-Ca8pDCaw06dUxWYlZXoU125VZPgH6Rj2uchjS8VKWT5mSiNGjTpzadiM68207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