llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[clang-format] Spurious spaces around template brackets when callee is UDL operator (`operator""_x<...>()`)

Open chiphogg opened this issue 4 months ago • 5 comments

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 <...>().

chiphogg avatar Jul 16 '26 18:07 chiphogg

I would like to work on this issue. Could a maintainer please assign it to me?

johnnyb2543 avatar Jul 16 '26 18:07 johnnyb2543

@llvm/issue-subscribers-clang-format

Author: Chip Hogg (chiphogg)

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 &lt;char... Cs&gt; auto f() { return operator""_mag&lt;Cs...&gt;(); }

Actual output

clang-format 17.0.6 and 18.1.8:

template &lt;char... Cs&gt; auto f() { return operator""_mag &lt; Cs... &gt; (); }

clang-format 19.1.7 through 22.1.8 (latest):

template &lt;char... Cs&gt; auto f() { return operator""_mag &lt; Cs...&gt;(); }

(19+ stopped spacing the closing &gt;, but still inserts a spurious space before &lt;.)

Expected output

The pre-17 behavior, unchanged from an ordinary template call:

template &lt;char... Cs&gt; auto f() { return operator""_mag&lt;Cs...&gt;(); }

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 &lt;char... Cs&gt; auto f() { return g&lt;Cs...&gt;(); }   // stays g&lt;Cs...&gt;()

Version bisection

clang-format Output Status
14.0.6 operator""_mag&lt;Cs...&gt;() correct
15.0.7 operator""_mag&lt;Cs...&gt;() correct
16.0.6 operator""_mag&lt;Cs...&gt;() correct
17.0.6 operator""_mag &lt; Cs... &gt; () regressed (first bad)
18.1.8 operator""_mag &lt; Cs... &gt; () broken
19.1.7 operator""_mag &lt; Cs...&gt;() partially fixed, still wrong
20.1.8 operator""_mag &lt; Cs...&gt;() partially fixed, still wrong
21.1.8 operator""_mag &lt; Cs...&gt;() partially fixed, still wrong
22.1.8 operator""_mag &lt; Cs...&gt;() 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&lt;...&gt;{}, 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 &lt;...&gt;().

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 avatar Jul 16 '26 18:07 HazardyKnusperkeks

@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.

chiphogg avatar Jul 17 '26 11:07 chiphogg

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.

johnnyb2543 avatar Jul 17 '26 20:07 johnnyb2543