<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60098>60098</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
clang-format: Wrong alignment of indented comment after trailing comment on `#endif`, despite `AlignTrailingComments: false`
</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>
Version: clang-format 15.0.6
Config:
```
BasedOnStyle: LLVM
AlignTrailingComments: false
IndentWidth: 4
```
The follow example has a trailing comment after `#endif`, and a standalone code-level comment with two levels of indentation with LLVM style + `IndentWidth: 4`:
```cpp
int main() {
int ret = 0;
if (true) {
#if defined(TEST)
ret = 1;
#else
ret = 2;
#endif // TEST
// Comment about the next line, should stay indented with two tabs.
ret *= 3;
}
return ret;
}
```
Despite configuring `AlignTrailingComment: false`, clang-format wrongly infers that `// TEST` and the next comment should be aligned (likely because their respective first column is just off by one, it doesn't happen in other cases):
```cpp
int main() {
int ret = 0;
if (true) {
#if defined(TEST)
ret = 1;
#else
ret = 2;
#endif // TEST
// Comment about the next line, should stay indented with two tabs.
ret *= 3;
}
return ret;
}
```
This happens even with `UseTab: Always` and `TabWidth: 4` which makes it worse, especially when viewing the code in an IDE that uses a different tab width (e.g. GitHub diffs with 8 chars wide tabs).
Interestingly, in some cases where the indentation of the standalone column is wrong, clang-format corrects it well the first time. Then, when running a second time, it makes a mess of it:
Step 1, base code:
```cpp
int main() {
int ret = 0;
if (true) {
#if defined(TEST)
ret = 1;
#else
ret = 2;
#endif // TEST
// Comment about the next line, should stay indented with two tabs.
ret *= 3;
}
return ret;
}
```
Step 2, running clang-format:
```cpp
int main() {
int ret = 0;
if (true) {
#if defined(TEST)
ret = 1;
#else
ret = 2;
#endif // TEST
// Comment about the next line, should stay indented with two tabs.
ret *= 3;
}
return ret;
}
```
This is as expected.
Step 3, running clang-format again, the "ooh those comments look like they were wrongly aligned" logic kicks in:
```cpp
int main() {
int ret = 0;
if (true) {
#if defined(TEST)
ret = 1;
#else
ret = 2;
#endif // TEST
// Comment about the next line, should stay indented with two tabs.
ret *= 3;
}
return ret;
}
```
The behavior might be considered acceptable if `AlignTrailingComment` is true, but when it's false it should never happen.
Reproduction project with above example and `.clang-format` config:
[clang-format-wrong-comment-alignment-after-trailing-comment.zip](https://github.com/llvm/llvm-project/files/10433214/clang-format-wrong-comment-alignment-after-trailing-comment.zip)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkV89v2zoS_mvoy8CCTMWydfAhiZvdAl0ssPW2Z0ocSWwoUiApu35__cNQUhwn6aF9h_eCBoFtib9mvu-bGY7wXjUGccfWd2y9X4ghtNbtxKNCs-wasSitPO--oPPKGpbdQqWFaZa1dZ0IsFonaZKzdM_S23tratWw7HZ8ZHk6_cfHO-FR_td8DmeNtM2nT1_-M47catWYgxNKK9Pc265DEzxNqYX2OM75aCSa8FXJ0NLIzZtHjJ-HFqG2WtsT4HfR9RqhFR4EhOkIqMYzQNQBHdBynqGRqo4_70EYCQJ8EEYKbQ1CZSUuNR5RP609qdBCOFmIrz3YGlS0UQRlzThMLoInh4HxOzrolRt5esHr2qWq78c3ygTohDKMbxkvgG3uxvcAADTmMADL9pCy7PlIDYxvgxvwahHjmapBYq0MSsa3hw-fD4wXl3X0N--4etqRAHri4uU0fjWNcATGHxh_gLj71Zpp4H5moLRDgNAiGPweQCuDRIBv7aAlMXCeUEV5QTyI0idvmMJvyZrsCga22V8eHIbBGfq6WDyPvymlPfpeBeKflD04Eg_L07cEe9HrqKGrKDk5axpNvtToPIRWhFF2F5TyNMruCYpZZxMUJYKgU1ESrVo9oj5DiZUYPNIi5cCh77EK6ohQK-dpCz10BpSHb4MPYOsayjPYEWIVQFr0hvFNgFb0PRpQBmxo0UElPHqSxfvT5i-I8u_T5E8L8tAqP7HlAY84JRqWp__3eBAlqfBWn8TZz4JieXoQ5VXGg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