| Issue |
203050
|
| Summary |
[clang-format] `AlignTrailingComments: Never`/`Always` relocates a standalone comment wedged between ternary branches to the operand column
|
| Labels |
|
| Assignees |
|
| Reporter |
yfguo
|
## Summary
A standalone (own-line) comment wedged between the branches of a ternary chain, written by
the developer at the branch column (aligned with the `(a == N)` conditions), is moved to the
operand column when `AlignTrailingComments.Kind` is `Never` or `Always`. With `Kind: Leave`
the same comment is left at the branch column. The relocation under `Never`/`Always` is
unconditional: it happens regardless of the comment's input position and regardless of any
other comment in the function.
## 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
AlignTrailingComments:
Kind: Never # also reproduces with Kind: Always
```
## Reproduction
Input (the wedged comment is on its own line at the branch column):
```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;
}
```
With `Kind: Leave`, the comment stays at the branch column (shown for contrast):
```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;
}
```
With `Kind: Never` or `Kind: Always`, the comment is relocated to the
operand column:
```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 alignment change after the wedged comment is not related and reported separated in Github Issue #203044.
## Scope: ternary only
This relocation is specific to the ternary (`?:`) operator and does **not** reproduce for
logical (`&&`, `||`) or arithmetic (`+`, `*`, ...) operand chains. In a binary-operator
chain there is a single operand-alignment column, so a wedged standalone comment already
sits at that column and stays there under all three `Kind` values (`Leave`, `Never`,
`Always` produce identical output). A ternary branch line is special because it has two
distinct alignment columns -- the condition column (where `(a == N)` begins) and the result
column (after `? `); the defect is the comment being moved from the former to the latter,
which can only occur when both columns exist.
For example, this `&&` chain formats identically under `Leave`, `Never`, and `Always` (the
comment stays at the operand column in every case), verified on `main` at commit
`3443243ded167229ddc37e64b7e754854ae1ba2c`:
```cpp
void g(int aaaaaaaaaaaaaaaaaaaaaa) {
bool x = (aaaaaaaaaaaaaaaaaaaaaa == 0xBF) &&
(aaaaaaaaaaaaaaaaaaaaaa == 0xAF) &&
// CPUID Family
(aaaaaaaaaaaaaaaaaaaaaa == 0x8F) && (aaaaaaaaaaaaaaaaaaaaaa == 26);
}
```
## Expected behavior
`Kind: Never`/`Always` should not relocate a standalone comment that is on its own line. The
comment should stay at the branch column where the developer placed it, as it does under
`Kind: Leave`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs