Skip to content

Conversation

@owenca
Copy link
Contributor

@owenca owenca commented Oct 28, 2025

For declarations like Type* ::Class::member..., Class was not annotated as TT_StartOfNameas it should be. This prevented member from being updated to TT_FunctionDeclarationName if member was a function name.

Fixes #164866

@llvmbot
Copy link
Member

llvmbot commented Oct 28, 2025

@llvm/pr-subscribers-clang-format

Author: owenca (owenca)

Changes

Fixes #164866


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

2 Files Affected:

  • (modified) clang/lib/Format/TokenAnnotator.cpp (+7-4)
  • (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+5)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 1d0dfd0b9c151..f4ea3948c14c4 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2646,9 +2646,9 @@ class AnnotatingParser {
         return false;
     }
 
-    bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
-                       PreviousNotConst->Previous &&
-                       PreviousNotConst->Previous->is(tok::hash);
+    const auto *PrevPrev = PreviousNotConst->Previous;
+    const bool IsPPKeyword = PreviousNotConst->is(tok::identifier) &&
+                             PrevPrev && PrevPrev->is(tok::hash);
 
     if (PreviousNotConst->is(TT_TemplateCloser)) {
       return PreviousNotConst && PreviousNotConst->MatchingParen &&
@@ -2674,8 +2674,11 @@ class AnnotatingParser {
     }
 
     // *a or &a or &&a.
-    if (PreviousNotConst->is(TT_PointerOrReference))
+    if (PreviousNotConst->is(TT_PointerOrReference) ||
+        (PreviousNotConst->is(tok::coloncolon) && PrevPrev &&
+         PrevPrev->is(TT_PointerOrReference))) {
       return true;
+    }
 
     // MyClass a;
     if (PreviousNotConst->isTypeName(LangOpts))
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index ca99940890984..86e21775ad9ed 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -2344,6 +2344,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
   EXPECT_TOKEN(Tokens[2], tok::identifier, TT_FunctionDeclarationName);
   EXPECT_TOKEN(Tokens[3], tok::l_paren, TT_FunctionDeclarationLParen);
 
+  Tokens = annotate("::foo::bar& ::foo::bar::operator=(::foo::bar& other);");
+  ASSERT_EQ(Tokens.size(), 22u) << Tokens;
+  EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);
+  EXPECT_TOKEN(Tokens[17], tok::amp, TT_PointerOrReference);
+
   Tokens = annotate("int iso_time(time_t);");
   ASSERT_EQ(Tokens.size(), 7u) << Tokens;
   EXPECT_TOKEN(Tokens[1], tok::identifier, TT_FunctionDeclarationName);

@owenca owenca changed the title [clang-format] Fix a bug in annotating operator functions [clang-format] Fix a bug in annotating operator function declarations Oct 28, 2025
@owenca owenca changed the title [clang-format] Fix a bug in annotating operator function declarations [clang-format] Fix a bug in annotating class member names Oct 29, 2025
@owenca owenca merged commit 3fc24a2 into llvm:main Oct 29, 2025
10 checks passed
@owenca owenca deleted the 164866 branch October 29, 2025 03:16
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
For declarations like `Type* ::Class::member...`, `Class` was not
annotated as `TT_StartOfName`as it should be. This prevented `member`
from being updated to `TT_FunctionDeclarationName` if `member` was a
function name.

Fixes llvm#164866
@llvm llvm deleted a comment from llvm-ci Nov 2, 2025
DEBADRIBASAK pushed a commit to DEBADRIBASAK/llvm-project that referenced this pull request Nov 3, 2025
For declarations like `Type* ::Class::member...`, `Class` was not
annotated as `TT_StartOfName`as it should be. This prevented `member`
from being updated to `TT_FunctionDeclarationName` if `member` was a
function name.

Fixes llvm#164866
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

clang-format adds a space between the parameter type and & under google style guide

3 participants