[clang] [clang-format] Correctly annotate static overloaded operator calls (PR #161944)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 3 21:39:00 PDT 2025


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/161944

Fixes #160513

>From e56033ca008a800ea7b8ebb340fb23a1ac85dc24 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 25 Sep 2025 09:52:05 -0700
Subject: [PATCH] [clang-format] Correctly annotate static overloaded operator
 calls

Fixes #160513
---
 clang/lib/Format/TokenAnnotator.cpp           | 2 +-
 clang/unittests/Format/TokenAnnotatorTest.cpp | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 0c9c88a426c89..99d537f513901 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -3802,7 +3802,7 @@ static bool isFunctionDeclarationName(const LangOptions &LangOpts,
   const auto *Prev = Current.getPreviousNonComment();
   assert(Prev);
 
-  if (Prev->is(tok::coloncolon))
+  if (Prev->is(tok::coloncolon) && Prev->hasWhitespaceBefore())
     Prev = Prev->Previous;
 
   if (!Prev)
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 4a8f27f656f1d..e004a6f3f02f5 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) {



More information about the cfe-commits mailing list