From cb3a605c5d453f9c6af8c44f84a11815aed7fe85 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sun, 19 Nov 2023 15:08:54 -0800 Subject: [PATCH] [clang-format] Fix a bug in isStartOfName() on macro definitions (#72768) Fixed #72751. --- clang/lib/Format/TokenAnnotator.cpp | 6 ++++-- clang/unittests/Format/TokenAnnotatorTest.cpp | 8 +++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 1cc198ceec70..06775b6df945 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2206,8 +2206,10 @@ private: return false; if (const auto *NextNonComment = Tok.getNextNonComment(); - !NextNonComment || NextNonComment->isPointerOrReference() || - NextNonComment->isOneOf(tok::identifier, tok::string_literal)) { + (!NextNonComment && !Line.InMacroBody) || + (NextNonComment && + (NextNonComment->isPointerOrReference() || + NextNonComment->isOneOf(tok::identifier, tok::string_literal)))) { return false; } diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp index a986806b7a44..b6abf34c589b 100644 --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/clang/unittests/Format/TokenAnnotatorTest.cpp @@ -2390,7 +2390,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsDoWhile) { EXPECT_TOKEN(Tokens[4], tok::kw_while, TT_DoWhile); } -TEST_F(TokenAnnotatorTest, NotStartOfName) { +TEST_F(TokenAnnotatorTest, StartOfName) { auto Tokens = annotate("#pragma clang diagnostic push"); ASSERT_EQ(Tokens.size(), 6u) << Tokens; EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown); @@ -2402,6 +2402,12 @@ TEST_F(TokenAnnotatorTest, NotStartOfName) { 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("#define FOO Foo foo"); + 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_StartOfName); } } // namespace -- 2.11.4.GIT