Skip to content

Commit 7c460c6

Browse files
authored
[clang-format] Fix a bug in annotating FunctionDeclarationName (#85361)
A name is not a FunctionDeclarationName if it's preceded by an Objective-C keyword. Fixes #84578.
1 parent 8386a38 commit 7c460c6

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,13 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
35953595
if (!Current.Tok.getIdentifierInfo())
35963596
return false;
35973597

3598+
const auto &Previous = *Current.Previous;
3599+
3600+
if (const auto *PrevPrev = Previous.Previous;
3601+
PrevPrev && PrevPrev->is(TT_ObjCDecl)) {
3602+
return false;
3603+
}
3604+
35983605
auto skipOperatorName = [](const FormatToken *Next) -> const FormatToken * {
35993606
for (; Next; Next = Next->Next) {
36003607
if (Next->is(TT_OverloadedOperatorLParen))
@@ -3633,18 +3640,17 @@ static bool isFunctionDeclarationName(const FormatToken &Current,
36333640
// Find parentheses of parameter list.
36343641
const FormatToken *Next = Current.Next;
36353642
if (Current.is(tok::kw_operator)) {
3636-
const auto *Previous = Current.Previous;
3637-
if (Previous->Tok.getIdentifierInfo() &&
3638-
!Previous->isOneOf(tok::kw_return, tok::kw_co_return)) {
3643+
if (Previous.Tok.getIdentifierInfo() &&
3644+
!Previous.isOneOf(tok::kw_return, tok::kw_co_return)) {
36393645
return true;
36403646
}
3641-
if (Previous->is(tok::r_paren) && Previous->is(TT_TypeDeclarationParen)) {
3642-
assert(Previous->MatchingParen);
3643-
assert(Previous->MatchingParen->is(tok::l_paren));
3644-
assert(Previous->MatchingParen->is(TT_TypeDeclarationParen));
3647+
if (Previous.is(tok::r_paren) && Previous.is(TT_TypeDeclarationParen)) {
3648+
assert(Previous.MatchingParen);
3649+
assert(Previous.MatchingParen->is(tok::l_paren));
3650+
assert(Previous.MatchingParen->is(TT_TypeDeclarationParen));
36453651
return true;
36463652
}
3647-
if (!Previous->isPointerOrReference() && Previous->isNot(TT_TemplateCloser))
3653+
if (!Previous.isPointerOrReference() && Previous.isNot(TT_TemplateCloser))
36483654
return false;
36493655
Next = skipOperatorName(Next);
36503656
} else {

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2645,6 +2645,11 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
26452645
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
26462646
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
26472647
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_StartOfName);
2648+
2649+
Tokens = annotate("@interface NSCoder (TestCoder)");
2650+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2651+
EXPECT_TOKEN(Tokens[0], tok::at, TT_ObjCDecl);
2652+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_StartOfName);
26482653
}
26492654

26502655
TEST_F(TokenAnnotatorTest, BraceKind) {

0 commit comments

Comments
 (0)