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

    <tr>
        <th>Summary</th>
        <td>
            clang-format 18 idempotent regression with #if within struct
        </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 18 twice produces different results when we have #if's within a struct.
Running with clang-format 17.0.5 shows no difference on repeated invocations.

Given `idempotent_regression.cpp`

```c++
extern const char** foo();
extern int bar();
const char** foo() {
    if (bar()) {
        static const char *a_long_named_identifier[] = {
 /* abc comment */ "abc",
#if FOO
            /* a comment describing what this is and so on */ "xyz",
#endif
            /* last */ "last"
        };
        return a_long_named_identifier;
 } else {
        return nullptr;
    }
}
```

using `_clang-format`:

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

clang-format 18.1.1 yields the following differences on successive runs. Notice that the curly brace
moves up and down:

```
/path/to/llvm-18.1.1/bin/clang-format -style=file idempotent_regression.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 13:18:52.544355938 -0400
+++ out.18.1.1.b.cpp    2024-03-25 13:18:52.565356089 -0400
@@ -7,7 +7,8 @@
 #if FOO
             /* a comment describing what this is and so on */ "xyz",
 #endif
- /* last */ "last"};
+            /* last */ "last"
+ };
         return a_long_named_identifier;
     } else {
 return nullptr;

diff -u out.18.1.1.b.cpp out.18.1.1.c.cpp
--- out.18.1.1.b.cpp    2024-03-25 13:18:52.565356089 -0400
+++ out.18.1.1.c.cpp    2024-03-25 13:18:52.586356241 -0400
@@ -7,8 +7,7 @@
 #if FOO
             /* a comment describing what this is and so on */ "xyz",
 #endif
-            /* last */ "last"
-        };
+ /* last */ "last"};
         return a_long_named_identifier;
     } else {
         return nullptr;

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/eJzMVl2vozYQ_TXOywgENgR4yEPuzWbVqupK7UMfr4wZgitjR7ZJNv31lUO-b-juVrdSIxSZsT1nPHM4Y-6c3GjEBclfSL6a8cF3xi5-Np1-ZXRWm-aw-G3QWuoNCMX1JmqN7bmHtAS_lwJha00zCHTQyLZFi9qDRTco72DfoYY9Qsd3CIQy2RJaONhL30kNHJy3g_AxSVYkWZ5RwuwDVBEncQ6uM3sH2lyABILRYHGL3GMDUu-M4F4a7U4ux__PcocayDyRDfZb41H7N4sbi85Jo2Ox3ZJ5crshvB4fQehLeI5W_OrRahBGOw-i45bQJaFLaI0htCS0Iux-pdQe6rDsbnJ6P5DitAYAQLZAaHnd_zgffs5zL8VNSEDokr8pozdvmvfYvMkGtZetRDvWFwhb3fghdB1C4HVw0vehdseo1kAo5bUglBL6espKqB-sv3y5jyH8zm4uThp0wsr6WM6Oe_CddCAdcN2AM6FqV5ivh78eYFA3sp1EUdzdRhlew_675aRYXRJ-tln0g9UwlZ7LclKsAJXD9-k-edCDUltv7wAC4Bj-ZXAm0S2zBhdSQubJ2y3BwyK2fMrAA-_VaIqiaBy8cIfNF_27PygkbAmfjdkoHOd-0uFAf8jGd2EqG62vRg29_kX20gdrmpxiiuP4H2J9-NrjNE7hIFE1DnyH0BqlzD4c5_o5ulBZNwgRPq0dgh20i-FX44NO-JEICGKw6gC15eIUdW926GDYHunRmL2eysaZIest9x2ha28IXSu166MxPkLXtdSEru9ij9yYqlUrFcKkCABhn8zg49FVzI82StgnQufpRyC_c34PWP-XgPUTQPEM8BkTLsZQ6d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