Issue 235788
Summary [clang-format] Penalty overflow picks a worse layout for long braced lists
Labels clang-format
Assignees
Reporter chfast
    ```sh
gen() { python3 -c "
print('int t[] = {')
for i in range($1): print(' 0x%0100x,' % i)
print('};')"; }
gen 80 | clang-format --style=LLVM | head -2
gen 81 | clang-format --style=LLVM | head -3
```

With 80 elements:

```c
int t[] = {
 0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
```

With 81 elements the brace moves to its own line and every element is indented 4 more:

```c
int t[] =
    {
 0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,
```

Every element line is over the column limit, and with `PenaltyExcessCharacter: 1000000` the penalty of the statement reaches 2^32: `-debug-_only_=format-formatter` reports `Penalty for line: 4240003369` for 80 elements and `646036309` for 81. `OptimizingLineFormatter::analyzeSolutionSpace()` sums the penalty in an `unsigned`, so it wraps and the worse layout wins. With that sum and `OrderedPenalty` changed to 64 bits locally, the layout stays `int t[] = {` (checked up to 1000 elements).

Same in clang-format 21, 23 and trunk. Found in intx (multi-precision integers), where a division test table of 100-digit literals was reformatted this way after adding rows.

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to