[clang-format] Spurious spaces around template brackets when callee is UDL operator (`operator""_x<...>()`)
Hi! I ran into this issue using clang-format 17, where it was treating variadic user-defined literals (UDLs) strangely, adding extra spaces. I had Claude help me explore the behavior across different versions. Here's what we found.
First, this is a regression: clang-format 14, 15, and 16 all format the construct correctly. It looks like it got partially fixed in clang 19, but is still "half present" even in the latest release.
Minimal reproducer
Input (--style=LLVM, but style-independent):
template <char... Cs> auto f() { return operator""_mag<Cs...>(); }
Actual output
clang-format 17.0.6 and 18.1.8:
template <char... Cs> auto f() { return operator""_mag < Cs... > (); }
clang-format 19.1.7 through 22.1.8 (latest):
template <char... Cs> auto f() { return operator""_mag < Cs...>(); }
(19+ stopped spacing the closing >, but still inserts a spurious space before <.)
Expected output
The pre-17 behavior, unchanged from an ordinary template call:
template <char... Cs> auto f() { return operator""_mag<Cs...>(); }
Control (correct in all versions)
Replacing the UDL operator with a normal function name is formatted correctly everywhere, confirming the operator-literal name is what triggers the bug:
template <char... Cs> auto f() { return g<Cs...>(); } // stays g<Cs...>()
Version bisection
| clang-format | Output | Status |
|---|---|---|
| 14.0.6 | operator""_mag<Cs...>() |
correct |
| 15.0.7 | operator""_mag<Cs...>() |
correct |
| 16.0.6 | operator""_mag<Cs...>() |
correct |
| 17.0.6 | operator""_mag < Cs... > () |
regressed (first bad) |
| 18.1.8 | operator""_mag < Cs... > () |
broken |
| 19.1.7 | operator""_mag < Cs...>() |
partially fixed, still wrong |
| 20.1.8 | operator""_mag < Cs...>() |
partially fixed, still wrong |
| 21.1.8 | operator""_mag < Cs...>() |
partially fixed, still wrong |
| 22.1.8 | operator""_mag < Cs...>() |
partially fixed, still wrong |
(Binaries from the PyPI clang-format wheels.)
Notes
This looks related to the "unnecessary spaces around template brackets" family of regressions (#110968, #112487, #123144), but those cover other triggers (conditional_t<...>{}, binary operators inside the argument list). None of them involve a user-defined-literal operator as the template-id name, and the operator"" case remains unfixed on the latest release. The distinguishing feature here is purely the operator"" name preceding the <...>().
I would like to work on this issue. Could a maintainer please assign it to me?
@llvm/issue-subscribers-clang-format
Author: Chip Hogg (chiphogg)
First, this is a regression: clang-format 14, 15, and 16 all format the construct correctly. It looks like it got partially fixed in clang 19, but is still "half present" even in the latest release.
Minimal reproducer
Input (--style=LLVM, but style-independent):
template <char... Cs> auto f() { return operator""_mag<Cs...>(); }
Actual output
clang-format 17.0.6 and 18.1.8:
template <char... Cs> auto f() { return operator""_mag < Cs... > (); }
clang-format 19.1.7 through 22.1.8 (latest):
template <char... Cs> auto f() { return operator""_mag < Cs...>(); }
(19+ stopped spacing the closing >, but still inserts a spurious space before <.)
Expected output
The pre-17 behavior, unchanged from an ordinary template call:
template <char... Cs> auto f() { return operator""_mag<Cs...>(); }
Control (correct in all versions)
Replacing the UDL operator with a normal function name is formatted correctly everywhere, confirming the operator-literal name is what triggers the bug:
template <char... Cs> auto f() { return g<Cs...>(); } // stays g<Cs...>()
Version bisection
| clang-format | Output | Status |
|---|---|---|
| 14.0.6 | operator""_mag<Cs...>() |
correct |
| 15.0.7 | operator""_mag<Cs...>() |
correct |
| 16.0.6 | operator""_mag<Cs...>() |
correct |
| 17.0.6 | operator""_mag < Cs... > () |
regressed (first bad) |
| 18.1.8 | operator""_mag < Cs... > () |
broken |
| 19.1.7 | operator""_mag < Cs...>() |
partially fixed, still wrong |
| 20.1.8 | operator""_mag < Cs...>() |
partially fixed, still wrong |
| 21.1.8 | operator""_mag < Cs...>() |
partially fixed, still wrong |
| 22.1.8 | operator""_mag < Cs...>() |
partially fixed, still wrong |
(Binaries from the PyPI clang-format wheels.)
Notes
This looks related to the "unnecessary spaces around template brackets" family of regressions (#110968, #112487, #123144), but those cover other triggers (conditional_t<...>{}, binary operators inside the argument list). None of them involve a user-defined-literal operator as the template-id name, and the operator"" case remains unfixed on the latest release. The distinguishing feature here is purely the operator"" name preceding the <...>().
Can you test current main? With my last build it is correct:
$ format "template <char... Cs> auto f() { return operator""_mag<Cs...>(); }"
template <char... Cs> auto f() { return operator_mag<Cs...>(); }
@HazardyKnusperkeks, your output changed the inputs. It dropped the quotes. I think it might be a quoting error? This looks like it would be two concatenated double-quoted strings, not a single double-quoted string with two double quotes in it.
I've been tracing the tokenization flow for operator""_mag. It seems like FormatTokenLexer.cpp isn't correctly unifying the operator keyword and the ""_mag literal into a single token, which is causing the issue described here.