Description
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 -3With 80 elements:
int t[] = {
0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,With 81 elements the brace moves to its own line and every element is indented 4 more:
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.
Activity
llvmorg-github-actions commentedon Oct 26, 2026
@llvm/issue-subscribers-clang-format
Author: Paweł Bylica (chfast)
Details
```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:
With 81 elements the brace moves to its own line and every element is indented 4 more:
Every element line is over the column limit, and with
PenaltyExcessCharacter: 1000000the penalty of the statement reaches 2^32:-debug-only=format-formatterreportsPenalty for line: 4240003369for 80 elements and646036309for 81.OptimizingLineFormatter::analyzeSolutionSpace()sums the penalty in anunsigned, so it wraps and the worse layout wins. With that sum andOrderedPenaltychanged to 64 bits locally, the layout staysint 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.
yashaggarwal85d commentedon Oct 27, 2026
Hi, I started working on this one, have a PR ready, thanks
HazardyKnusperkeks commentedon Oct 30, 2026
While it's likely that the PR will be merged, which will postpone that problem. I think it's also a good idea to either disable clang-format on such code, or provide a bigger column limit. There will always be the chance of an overflow.
[clang-format] Use 64-bit penalties in the optimizing line formatter (l…
[clang-format] Use 64-bit penalties in the optimizing line formatter (l…