Skip to content

Commit

Permalink
[clang-format] Fix a bug in annotating FunctionDeclarationName (#85361)
Browse files Browse the repository at this point in the history
A name is not a FunctionDeclarationName if it's preceded by an
Objective-C keyword.

Fixes #84578.
  • Loading branch information
owenca committed Mar 16, 2024
1 parent 8386a38 commit 7c460c6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
22 changes: 14 additions & 8 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3595,6 +3595,13 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
if (!Current.Tok.getIdentifierInfo())
return false;

const auto &Previous = *Current.Previous;

if (const auto *PrevPrev = Previous.Previous;
PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
return false;
}

auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
for (; Next; Next = Next->Next) {
if (Next->is(TT_OverloadedOperatorLParen))
Expand Down Expand Up @@ -3633,18 +3640,17 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
// Find parentheses of parameter list.
const FormatToken *Next = Current.Next;
if (Current.is(tok::kw_operator)) {
const auto *Previous = Current.Previous;
if (Previous->Tok.getIdentifierInfo() &&
!Previous->isOneOf(tok::kw_return, tok::kw_co_return)) {
if (Previous.Tok.getIdentifierInfo() &&
!Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
return true;
}
if (Previous->is(tok::r_paren) && Previous->is(TT_TypeDeclarationParen)) {
assert(Previous->MatchingParen);
assert(Previous->MatchingParen->is(tok::l_paren));
assert(Previous->MatchingParen->is(TT_TypeDeclarationParen));
if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) {
assert(Previous.MatchingParen);
assert(Previous.MatchingParen->is(tok::l_paren));
assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
return true;
}
if (!Previous->isPointerOrReference() && Previous->isNot(TT_TemplateCloser))
if (!Previous.isPointerOrReference() && Previous.isNot(TT_TemplateCloser))
return false;
Next = skipOperatorName(Next);
} else {
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,11 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_StartOfName);

Tokens = annotate("@interface NSCoder (TestCoder)");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[0], tok::at, TT_ObjCDecl);
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_StartOfName);
}

TEST_F(TokenAnnotatorTest, BraceKind) {
Expand Down

0 comments on commit 7c460c6

Please sign in to comment.