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

    <tr>
        <th>Summary</th>
        <td>
            [clang-format] Wrong formatting of constructor initializer list followed by #ifdef and unnamed scope
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          akien-mga
      </td>
    </tr>
</table>

<pre>
    ```
$ clang-format --version
clang-format version 15.0.0 (Mageia 15.0.0-1.mga9)
```
But this was reproducible on earlier versions too.

The following code is wrongly formatted by clang-format:
```cpp
ManagedCallable::ManagedCallable(GCHandleIntPtr p_delegate_handle) :
                delegate_handle(p_delegate_handle) {
#ifdef GD_MONO_HOT_RELOAD
        {
                MutexLock lock(instances_mutex);
                instances.add(&self_instance);
        }
#endif
}
```

It's pretty specific but the combination of the initializer list (`: delegate_handle(p_delegate_handle)`), the `#ifdef` and the unnamed scope seem to produce this formatting:

```cpp
ManagedCallable::ManagedCallable(GCHandleIntPtr p_delegate_handle) :
                delegate_handle(p_delegate_handle){
#ifdef GD_MONO_HOT_RELOAD
                        { MutexLock lock(instances_mutex);
instances.add(&self_instance);
}
#endif
}
```

Without the `#ifdef`, it's formatted properly:
```cpp
ManagedCallable::ManagedCallable(GCHandleIntPtr p_delegate_handle) :
                delegate_handle(p_delegate_handle) {
        {
                MutexLock lock(instances_mutex);
                instances.add(&self_instance);
        }
}
```

The same happens with LLVM style:
```cpp
ManagedCallable::ManagedCallable(GCHandleIntPtr p_delegate_handle)
    : delegate_handle(p_delegate_handle){
#ifdef GD_MONO_HOT_RELOAD
          {MutexLock lock(instances_mutex);
instances.add(&self_instance);
}
#endif
}
```

The attached project includes the above code samples and their formatted output with both custom `.clang-format` and `-style llvm`.

[clang-format-init-list-ifdef.zip](https://github.com/llvm/llvm-project/files/9615797/clang-format-init-list-ifdef.zip)
</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