Author: Ben Dunkin Date: 2025-12-09T20:47:38-08:00 New Revision: 0bac8f1729512a0b392908d6a57a00b0387a855f
URL: https://github.com/llvm/llvm-project/commit/0bac8f1729512a0b392908d6a57a00b0387a855f DIFF: https://github.com/llvm/llvm-project/commit/0bac8f1729512a0b392908d6a57a00b0387a855f.diff LOG: [clang-format] Handle templates in qualified typenames (#143194) This fixes the `SpaceBeforeParensOptions.AfterFunctionDeclarationName` and `SpaceBeforeParensOptions.AfterFunctionDefinitionName` options not adding spaces when a template type's constructor or destructor is forward declared or defined outside of the type definition. Attribution Note - I have been authorized to contribute this change on behalf of my company: ArenaNet LLC Added: Modified: clang/lib/Format/TokenAnnotator.cpp clang/unittests/Format/TokenAnnotatorTest.cpp Removed: ################################################################################ diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 79cfa73001e54..f6341ff0305a9 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -3668,6 +3668,39 @@ 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) { + assert(Tok); + + // Qualified names must start with an identifier. + if (Tok->isNot(tok::identifier)) + return nullptr; + + Tok = Tok->getNextNonComment(); + if (!Tok) + 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)) { + Tok = Tok->MatchingParen; + if (!Tok) + return nullptr; + + Tok = Tok->getNextNonComment(); + if (!Tok) + return nullptr; + } + + return Tok->is(tok::coloncolon) ? Tok->getNextNonComment() : nullptr; +} + // Returns the name of a function with no return type, e.g. a constructor or // destructor. static FormatToken *getFunctionName(const AnnotatedLine &Line, @@ -3697,6 +3730,23 @@ 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; + + // If the next token after the template keyword is not an opening bracket, + // it is a template instantiation, and not a function. + if (Tok->isNot(TT_TemplateOpener)) + return nullptr; + + Tok = Tok->MatchingParen; + + continue; + } + // A qualified name may start from the global namespace. if (Tok->is(tok::coloncolon)) { Tok = Tok->Next; @@ -3705,12 +3755,11 @@ 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 (auto *Next = skipNameQualifier(Tok)) + Tok = Next; + + if (!Tok) + return nullptr; // Skip the `~` if a destructor name. if (Tok->is(tok::tilde)) { @@ -3737,10 +3786,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 a3c0a32857a32..69c2c2f17b376 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -2461,6 +2461,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 [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
