Skip to content

Commit eaff083

Browse files
authored
[clang-format] Fix more bugs in isStartOfName() (#72336)
Fixed #72264.
1 parent 725e599 commit eaff083

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,10 +2018,6 @@ class AnnotatingParser {
20182018
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
20192019
Contexts.back().FirstStartOfName = &Current;
20202020
Current.setType(TT_StartOfName);
2021-
if (auto *PrevNonComment = Current.getPreviousNonComment();
2022-
PrevNonComment && PrevNonComment->is(TT_StartOfName)) {
2023-
PrevNonComment->setType(TT_Unknown);
2024-
}
20252021
} else if (Current.is(tok::semi)) {
20262022
// Reset FirstStartOfName after finding a semicolon so that a for loop
20272023
// with multiple increment statements is not confused with a for loop
@@ -2210,7 +2206,8 @@ class AnnotatingParser {
22102206
return false;
22112207

22122208
if (const auto *NextNonComment = Tok.getNextNonComment();
2213-
!NextNonComment || NextNonComment->isPointerOrReference()) {
2209+
!NextNonComment || NextNonComment->isPointerOrReference() ||
2210+
NextNonComment->isOneOf(tok::identifier, tok::string_literal)) {
22142211
return false;
22152212
}
22162213

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2390,6 +2390,20 @@ TEST_F(TokenAnnotatorTest, UnderstandsDoWhile) {
23902390
EXPECT_TOKEN(Tokens[4], tok::kw_while, TT_DoWhile);
23912391
}
23922392

2393+
TEST_F(TokenAnnotatorTest, NotStartOfName) {
2394+
auto Tokens = annotate("#pragma clang diagnostic push");
2395+
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
2396+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
2397+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
2398+
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
2399+
2400+
Tokens = annotate("#pragma clang diagnostic ignored \"-Wzero-length-array\"");
2401+
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
2402+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
2403+
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
2404+
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
2405+
}
2406+
23932407
} // namespace
23942408
} // namespace format
23952409
} // namespace clang

0 commit comments

Comments
 (0)