Skip to content

Conversation

@owenca
Copy link
Contributor

@owenca owenca commented Oct 18, 2025

This effectively reverts commit b5f6689 and fixes #111011 more narrowly.

Fixes #160513

This effectively reverts commit b5f6689 and
fixes llvm#111011 more narrowly.

Fixes llvm#160513
@llvmbot
Copy link
Member

llvmbot commented Oct 18, 2025

@llvm/pr-subscribers-clang-format

Author: owenca (owenca)

Changes

This effectively reverts commit b5f6689 and fixes #111011 more narrowly.

Fixes #160513


Full diff: https://github.com/llvm/llvm-project/pull/164048.diff

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+3-7)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 5b784eded4601..ffbd3839e8eb7 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3794,18 +3794,12 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
   if (Current.is(TT_FunctionDeclarationName))
     return true;
 
-  if (!Current.Tok.getIdentifierInfo())
+  if (Current.isNoneOf(tok::identifier, tok::kw_operator))
     return false;
 
   const auto *Prev = Current.getPreviousNonComment();
   assert(Prev);
 
-  if (Prev->is(tok::coloncolon))
-    Prev = Prev->Previous;
-
-  if (!Prev)
-    return false;
-
   const auto &Previous = *Prev;
 
   if (const auto *PrevPrev = Previous.getPreviousNonComment();
@@ -3854,6 +3848,8 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
 
   // Find parentheses of parameter list.
   if (Current.is(tok::kw_operator)) {
+    if (Line.startsWith(tok::kw_friend))
+      return true;
     if (Previous.Tok.getIdentifierInfo() &&
         Previous.isNoneOf(tok::kw_return, tok::kw_co_return)) {
       return true;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1152466b0179f..100251514a4bd 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1129,6 +1129,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsOverloadedOperators) {
   ASSERT_EQ(Tokens.size(), 7u) << Tokens;
   // Not TT_FunctionDeclarationName.
   EXPECT_TOKEN(Tokens[3], tok::kw_operator, TT_Unknown);
+
+  Tokens = annotate("SomeAPI::operator()();");
+  ASSERT_EQ(Tokens.size(), 9u) << Tokens;
+  // Not TT_FunctionDeclarationName.
+  EXPECT_TOKEN(Tokens[2], tok::kw_operator, TT_Unknown);
 }
 
 TEST_F(TokenAnnotatorTest, OverloadedOperatorInTemplate) {

@owenca owenca merged commit 3bb9d4a into llvm:main Oct 18, 2025
12 checks passed
@owenca owenca deleted the overload-operator branch October 18, 2025 19:13
c-rhodes pushed a commit to llvmbot/llvm-project that referenced this pull request Oct 20, 2025
…164048)

This effectively reverts commit b5f6689
and fixes llvm#111011 more narrowly.

Fixes llvm#160513

(cherry picked from commit 3bb9d4a)
@PiJoules
Copy link
Contributor

I believe this introduced a regression in clang-format. Prior to this commit, the following code would remain unchanged

::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication& other) noexcept {
  storage_ = other.CloneStorage_();
  return *this;
}

with clang-format --style='{BasedOnStyle: google, ColumnLimit: 0}' /tmp/test.cc. With this commit it becomes

::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication & other) noexcept {
  storage_ = other.CloneStorage_();                                                                                                                                                                                                                                                       
  return *this;                                                                                                                                                                                                                                                                           
}                       

with a space inserted in the function parameter. Could you send out a fix or revert?

@owenca
Copy link
Contributor Author

owenca commented Oct 22, 2025

@PiJoules unfortunately, this has been backported to 21.1.4, but I will come up with a fix in the next few days.

@HazardyKnusperkeks we may have to use the space (or the lack of) before ::operator as a hint from the user after all.

@HazardyKnusperkeks
Copy link
Contributor

@PiJoules unfortunately, this has been backported to 21.1.4, but I will come up with a fix in the next few days.

@HazardyKnusperkeks we may have to use the space (or the lack of) before ::operator as a hint from the user after all.

Yeah ok... but at least add a fixme test.

PiJoules added a commit that referenced this pull request Oct 22, 2025
PiJoules added a commit that referenced this pull request Oct 22, 2025
…y" (#164670)

Reverts #164048

This led to a regression in clang-format where a space gets added in
between the parameter type and `&`. For example, this

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication& other) noexcept {
```

becomes

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication & other) noexcept {
```
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Oct 22, 2025
…or correctly" (#164670)

Reverts llvm/llvm-project#164048

This led to a regression in clang-format where a space gets added in
between the parameter type and `&`. For example, this

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication& other) noexcept {
```

becomes

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication & other) noexcept {
```
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
…y" (llvm#164670)

Reverts llvm#164048

This led to a regression in clang-format where a space gets added in
between the parameter type and `&`. For example, this

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication& other) noexcept {
```

becomes

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication & other) noexcept {
```
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
…y" (llvm#164670)

Reverts llvm#164048

This led to a regression in clang-format where a space gets added in
between the parameter type and `&`. For example, this

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication& other) noexcept {
```

becomes

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication & other) noexcept {
```
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
…y" (llvm#164670)

Reverts llvm#164048

This led to a regression in clang-format where a space gets added in
between the parameter type and `&`. For example, this

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication& other) noexcept {
```

becomes

```
::test_anonymous::FunctionApplication& ::test_anonymous::FunctionApplication::operator=(const ::test_anonymous::FunctionApplication & other) noexcept {
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

4 participants