Skip to content

[clang-format] Handle templates in qualified typenames #143194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 59 additions & 7 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!Tok->is(tok::identifier))
if (Tok->isNot(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,
Expand Down Expand Up @@ -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;
Expand All @@ -3667,12 +3712,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 (FormatToken *Next = skipNameQualifier(Tok))
Tok = Next;

if (!Tok)
return nullptr;

// Skip the `~` if a destructor name.
if (Tok->is(tok::tilde)) {
Expand All @@ -3699,10 +3743,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;
}

Expand Down
55 changes: 55 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down