https://github.com/bdunkin updated https://github.com/llvm/llvm-project/pull/143194
>From 839d068df748189470f2c69eb47714425de9524b Mon Sep 17 00:00:00 2001 From: Ben Dunkin <bdun...@arena.net> Date: Fri, 6 Jun 2025 12:29:13 -0700 Subject: [PATCH] Fix identifiers not being marked as constructor/destructor names if they are qualified by a template type, or have template typename declarations in front of them. --- clang/lib/Format/TokenAnnotator.cpp | 65 +++++++++++++++++-- clang/unittests/Format/TokenAnnotatorTest.cpp | 55 ++++++++++++++++ 2 files changed, 114 insertions(+), 6 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index aed1672afac66..0a76b00cc6c5b 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3630,6 +3630,36 @@ static unsigned maxNestingDepth(const AnnotatedLine &Line) { return Result; } +// Returns the token after the first qualifier of the name, or nullptr if there +// is no qualifier. +static FormatToken* skipNameQualifier(const FormatToken *Tok) { + // Qualified names must start with an identifier. + if (!Tok->is(tok::identifier)) + return nullptr; + + Tok = Tok->getNextNonComment(); + if (Tok == nullptr) + return nullptr; + + // Consider: A::B::B() + // Tok --^ + if (Tok->is(tok::coloncolon)) + return Tok->getNextNonComment(); + + // Consider: A<float>::B<int>::B() + // Tok --^ + if (Tok->is(TT_TemplateOpener)) { + if (!Tok->MatchingParen) + return nullptr; + + Tok = Tok->MatchingParen; + if (Tok->startsSequence(TT_TemplateCloser, tok::coloncolon)) + return Tok->getNextNonComment()->getNextNonComment(); + } + + return nullptr; +} + // Returns the name of a function with no return type, e.g. a constructor or // destructor. static FormatToken *getFunctionName(const AnnotatedLine &Line, @@ -3659,6 +3689,21 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line, continue; } + // Skip past template typename declarations that may precede the + // constructor/destructor name. + if (Tok->is(tok::kw_template)) { + Tok = Tok->getNextNonComment(); + if (!Tok) + return nullptr; + + assert(Tok->is(TT_TemplateOpener)); + Tok = Tok->MatchingParen; + if (!Tok) + return nullptr; + + continue; + } + // A qualified name may start from the global namespace. if (Tok->is(tok::coloncolon)) { Tok = Tok->Next; @@ -3667,13 +3712,13 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line, } // Skip to the unqualified part of the name. - while (Tok->startsSequence(tok::identifier, tok::coloncolon)) { - assert(Tok->Next); - Tok = Tok->Next->Next; - if (!Tok) - return nullptr; + while (FormatToken* Next = skipNameQualifier(Tok)) { + Tok = Next; } + if (!Tok) + return nullptr; + // Skip the `~` if a destructor name. if (Tok->is(tok::tilde)) { Tok = Tok->Next; @@ -3699,10 +3744,18 @@ static bool isCtorOrDtorName(const FormatToken *Tok) { if (Prev && Prev->is(tok::tilde)) Prev = Prev->Previous; - if (!Prev || !Prev->endsSequence(tok::coloncolon, tok::identifier)) + // Consider: A::A() and A<int>::A() + if (!Prev || (!Prev->endsSequence(tok::coloncolon, tok::identifier) && + !Prev->endsSequence(tok::coloncolon, TT_TemplateCloser))) { return false; + } assert(Prev->Previous); + if (Prev->Previous->is(TT_TemplateCloser) && Prev->Previous->MatchingParen) { + Prev = Prev->Previous->MatchingParen; + assert(Prev->Previous); + } + return Prev->Previous->TokenText == Tok->TokenText; } diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index 873c6c492d18c..c3538b2a8edfa 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -2351,6 +2351,61 @@ TEST_F(TokenAnnotatorTest, UnderstandsCtorAndDtorDeclNames) { EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionDeclarationLParen); EXPECT_TOKEN(Tokens[6], tok::l_brace, TT_FunctionLBrace); + Tokens = annotate("Foo<int>::Foo() {}"); + ASSERT_EQ(Tokens.size(), 11u) << Tokens; + EXPECT_TOKEN(Tokens[5], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("Foo<int>::~Foo() {}"); + ASSERT_EQ(Tokens.size(), 12u) << Tokens; + EXPECT_TOKEN(Tokens[6], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[7], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[9], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("template <typename V> Foo<V>::Foo() {}"); + ASSERT_EQ(Tokens.size(), 16u) << Tokens; + EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[13], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("template <typename V> Foo<V>::~Foo() {}"); + ASSERT_EQ(Tokens.size(), 17u) << Tokens; + EXPECT_TOKEN(Tokens[11], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[12], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[14], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("template <typename V> [[nodiscard]] Foo<V>::Foo() {}"); + ASSERT_EQ(Tokens.size(), 21u) << Tokens; + EXPECT_TOKEN(Tokens[15], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[16], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[18], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("template <typename V> Foo<V>::Foo() [[nodiscard]] {}"); + ASSERT_EQ(Tokens.size(), 21u) << Tokens; + EXPECT_TOKEN(Tokens[10], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[11], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[18], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("template <typename V, typename U> Foo<V, U>::Foo() {}"); + ASSERT_EQ(Tokens.size(), 21u) << Tokens; + EXPECT_TOKEN(Tokens[15], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[16], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[18], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate("template <typename V, typename U> Foo<V, U>::~Foo() {}"); + ASSERT_EQ(Tokens.size(), 22u) << Tokens; + EXPECT_TOKEN(Tokens[16], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[17], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[19], tok::l_brace, TT_FunctionLBrace); + + Tokens = annotate( + "template <typename V> template<typename W> Foo<V>::Foo(W x) {}"); + ASSERT_EQ(Tokens.size(), 23u) << Tokens; + EXPECT_TOKEN(Tokens[15], tok::identifier, TT_CtorDtorDeclName); + EXPECT_TOKEN(Tokens[16], tok::l_paren, TT_FunctionDeclarationLParen); + EXPECT_TOKEN(Tokens[20], tok::l_brace, TT_FunctionLBrace); + Tokens = annotate("struct Test {\n" " Test()\n" " : l([] {\n" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits