| Issue |
203044
|
| Summary |
[clang-format] A standalone comment between ternary branches corrupts operand alignment
|
| Labels |
|
| Assignees |
|
| Reporter |
yfguo
|
## Summary
In an operand-aligned ternary chain (`?`/`:` lined up one branch per line), inserting a
single standalone (own-line) comment between two branches breaks the alignment of every
branch after the comment. The operand on the line immediately after the comment is indented
by an extra `ContinuationIndentWidth`, and the operator columns of the remaining branches
shift, so the `?`/`:` operators that were aligned in the comment-free chain are no longer
aligned.
The corruption is caused by the mere presence of the wedged comment; it is not a
comment-placement issue.
## Environment
Reproduced on the following:
- clang-format version: Ubuntu clang-format version 22.1.3
- clang-format version 23.0.0git, built from `main` at commit
`3443243ded167229ddc37e64b7e754854ae1ba2c`.
## Config (`.clang-format`)
```yaml
BasedOnStyle: LLVM
ColumnLimit: 80
BreakBeforeTernaryOperators: false
```
- Also reproduces on `BasedOnStyle: Google`
## Reproduction
Baseline: without the wedged comment, the chain is aligned (the `?`/`:` line up and the
conditions are padded to a common width):
```cpp
void g(int a) {
int x = (a == 0xBF) ? MODEL_ZEN5 :
(a == 0xAF) ? MODEL_ZEN34 :
(a == 0x8F) ? MODEL_ZEN12 :
(a == 26) ? MODEL_ZEN5 :
(a == 25) ? MODEL_ZEN34 :
MODEL_ZEN12;
}
```
Inserting one standalone comment between two branches:
```cpp
void g(int a) {
int x = (a == 0xBF) ? MODEL_ZEN5 :
(a == 0xAF) ? MODEL_ZEN34 :
(a == 0x8F) ? MODEL_ZEN12 :
// CPUID Family
(a == 26) ? MODEL_ZEN5 :
(a == 25) ? MODEL_ZEN34 :
MODEL_ZEN12;
}
```
clang-format produces (the alignment of operands after the comment are corrupted):
```cpp
void g(int a) {
int x = (a == 0xBF) ? MODEL_ZEN5 :
(a == 0xAF) ? MODEL_ZEN34 :
(a == 0x8F) ? MODEL_ZEN12 :
// CPUID Family
(a == 26) ? MODEL_ZEN5 :
(a == 25) ? MODEL_ZEN34 :
MODEL_ZEN12;
}
```
The branch after the comment (`(a == 26)`) is indented by an extra
`ContinuationIndentWidth`, and the operator column of the trailing branches shifts, so the
`?`/`:` no longer align with the lines above.
## Expected behavior
Inserting the standalone comment should not change the alignment of the surrounding ternary
branches. The `?`/`:` should stay aligned exactly as in the comment-free chain, with the
comment simply occupying its own line.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs