[llvm-branch-commits] [clang] release/21.x: [clang-format] Annotate ::operator and Foo::operator correctly (#164048) (PR #164112)
Cullen Rhodes via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Mon Oct 20 03:04:41 PDT 2025
https://github.com/c-rhodes updated https://github.com/llvm/llvm-project/pull/164112
>From 54cdd973782e85b111df5888ac137a0fd9ea114d Mon Sep 17 00:00:00 2001
From: owenca <owenpiano at gmail.com>
Date: Sat, 18 Oct 2025 12:13:25 -0700
Subject: [PATCH 1/2] [clang-format] Annotate ::operator and Foo::operator
correctly (#164048)
This effectively reverts commit b5f6689dc93216f9272e790e787548cf29250566
and fixes #111011 more narrowly.
Fixes #160513
(cherry picked from commit 3bb9d4a24e40eea1988f6bdc6a79e7a128a2fad9)
---
clang/lib/Format/TokenAnnotator.cpp | 10 +++-------
clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
2 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index ed8aa514aaf31..96b11d023295a 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3775,18 +3775,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();
@@ -3835,6 +3829,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.isOneOf(tok::kw_return, tok::kw_co_return)) {
return true;
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 5ef7f5ba25a12..04dc69180960c 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) {
>From 3333dd88a4931e9efc766a045c2fe02043a10fe7 Mon Sep 17 00:00:00 2001
From: owenca <owenpiano at gmail.com>
Date: Sat, 18 Oct 2025 13:18:42 -0700
Subject: [PATCH 2/2] Update clang/lib/Format/TokenAnnotator.cpp
---
clang/lib/Format/TokenAnnotator.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 96b11d023295a..57b2872566a47 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3775,7 +3775,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
if (Current.is(TT_FunctionDeclarationName))
return true;
- if (Current.isNoneOf(tok::identifier, tok::kw_operator))
+ if (!Current.isOneOf(tok::identifier, tok::kw_operator))
return false;
const auto *Prev = Current.getPreviousNonComment();
More information about the llvm-branch-commits
mailing list