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

Clang format does not remove spaces inside dependent nested name specifiers

Open asoffer opened this issue 8 months ago • 6 comments

Input:

template <typename T>
auto mem = &T :: member;

Expected output:

template <typename T>
auto mem = &T::member; // <-- No spaces here

Actual output:

template <typename T>
auto mem = &T ::member; // <-- Space before colons here

Live repro: https://godbolt.org/z/6oWK58Pzf

Interestingly, this does work as expected for non-dependent names: https://godbolt.org/z/G17d1ocqs

asoffer avatar Mar 26 '26 14:03 asoffer

@llvm/issue-subscribers-clang-format

Author: Andy Soffer (asoffer)

Input: ``` template <typename T> auto mem = &T :: member; ```

Expected output:

template &lt;typename T&gt;
auto mem = &amp;T::member; // &lt;-- No spaces here

Actual output:

template &lt;typename T&gt;
auto mem = &amp;T ::member; // &lt;-- Space before colons here

Live repro: https://godbolt.org/z/6oWK58Pzf

Interestingly, this does work as expected for non-dependent names: https://godbolt.org/z/G17d1ocqs

llvmbot avatar Mar 26 '26 14:03 llvmbot

I’d like to work on this issue. I’ll investigate and send a patch with tests if no one is already working on it.

Lane0218 avatar Mar 26 '26 16:03 Lane0218

Interestingly, this does work as expected for non-dependent names: https://godbolt.org/z/G17d1ocqs

You had no space in your input there, it is handled equally. We have (nearly) no knowledge what an identifier refers to.

HazardyKnusperkeks avatar Mar 26 '26 19:03 HazardyKnusperkeks

I’d like to work on this issue. I’ll investigate and send a patch with tests if no one is already working on it.

Just assign yourself here in github. (I've done it for you this time.)

HazardyKnusperkeks avatar Mar 26 '26 19:03 HazardyKnusperkeks

I’d like to work on this issue. I’ll investigate and send a patch with tests if no one is already working on it.

Just assign yourself here in github. (I've done it for you this time.)

It looks like I don't have permission to assign myself on GitHub. Thanks for assigning it to me. :-)

Lane0218 avatar Mar 27 '26 02:03 Lane0218

Hi @HazardyKnusperkeks, I sent a patch for this in #189024.

While investigating, I noticed we already have a similar macro-detection heuristic in clang/lib/Format/QualifierAlignmentFixer.cpp (isPossibleMacro), so I followed the same general idea here and narrowed the existing spacing heuristic for this case instead of removing it outright.

The change keeps the ALWAYS_INLINE ::std::string-style macro case conservative, while formatting ordinary identifiers like T :: member as T::member.

Lane0218 avatar Mar 27 '26 15:03 Lane0218