Skip to content

[clang-format] Correctly annotate return type of function pointer #66893

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

Merged
merged 1 commit into from
Sep 26, 2023
Merged
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
8 changes: 6 additions & 2 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3144,8 +3144,12 @@ static FormatToken *getFunctionName(const AnnotatedLine &Line) {
}

// Make sure the name is followed by a pair of parentheses.
if (Name)
return Tok->is(tok::l_paren) && Tok->MatchingParen ? Name : nullptr;
if (Name) {
return Tok->is(tok::l_paren) && Tok->isNot(TT_FunctionTypeLParen) &&
Tok->MatchingParen
? Name
: nullptr;
}

// Skip keywords that may precede the constructor/destructor name.
if (Tok->isOneOf(tok::kw_friend, tok::kw_inline, tok::kw_virtual,
Expand Down
7 changes: 7 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1655,6 +1655,13 @@ TEST_F(TokenAnnotatorTest, UnderstandsFunctionDeclarationNames) {
"FOO Foo();");
ASSERT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[6], tok::identifier, TT_FunctionDeclarationName);

Tokens = annotate("struct Foo {\n"
" Bar (*func)();\n"
"};");
ASSERT_EQ(Tokens.size(), 14u) << Tokens;
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_FunctionTypeLParen);
}

TEST_F(TokenAnnotatorTest, UnderstandsC11GenericSelection) {
Expand Down