Skip to content

[clang-format] Fix more bugs in isStartOfName() #72336

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
Nov 15, 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
7 changes: 2 additions & 5 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2018,10 +2018,6 @@ class AnnotatingParser {
(!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) {
Contexts.back().FirstStartOfName = &Current;
Current.setType(TT_StartOfName);
if (auto *PrevNonComment = Current.getPreviousNonComment();
PrevNonComment && PrevNonComment->is(TT_StartOfName)) {
PrevNonComment->setType(TT_Unknown);
}
} else if (Current.is(tok::semi)) {
// Reset FirstStartOfName after finding a semicolon so that a for loop
// with multiple increment statements is not confused with a for loop
Expand Down Expand Up @@ -2210,7 +2206,8 @@ class AnnotatingParser {
return false;

if (const auto *NextNonComment = Tok.getNextNonComment();
!NextNonComment || NextNonComment->isPointerOrReference()) {
!NextNonComment || NextNonComment->isPointerOrReference() ||
NextNonComment->isOneOf(tok::identifier, tok::string_literal)) {
return false;
}

Expand Down
14 changes: 14 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2373,6 +2373,20 @@ TEST_F(TokenAnnotatorTest, UnderstandsDoWhile) {
EXPECT_TOKEN(Tokens[4], tok::kw_while, TT_DoWhile);
}

TEST_F(TokenAnnotatorTest, NotStartOfName) {
auto Tokens = annotate("#pragma clang diagnostic push");
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);

Tokens = annotate("#pragma clang diagnostic ignored \"-Wzero-length-array\"");
ASSERT_EQ(Tokens.size(), 7u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[3], tok::identifier, TT_Unknown);
EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
}

} // namespace
} // namespace format
} // namespace clang