[clang] [clang-format] Fix a bug in isStartOfName() on macro definitions (PR #72768)
https://github.com/owenca closed https://github.com/llvm/llvm-project/pull/72768 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in isStartOfName() on macro definitions (PR #72768)
https://github.com/HazardyKnusperkeks approved this pull request. https://github.com/llvm/llvm-project/pull/72768 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in isStartOfName() on macro definitions (PR #72768)
llvmbot wrote: @llvm/pr-subscribers-clang-format Author: Owen Pan (owenca) Changes Fixed #72751. --- Full diff: https://github.com/llvm/llvm-project/pull/72768.diff 2 Files Affected: - (modified) clang/lib/Format/TokenAnnotator.cpp (+4-2) - (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+7-1) ``diff diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index 1d376cd8b5794dd..a23a1b28330fe57 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2206,8 +2206,10 @@ class AnnotatingParser { 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 a986806b7a44069..b6abf34c589b146 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 `` https://github.com/llvm/llvm-project/pull/72768 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang-format] Fix a bug in isStartOfName() on macro definitions (PR #72768)
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/72768 Fixed #72751. >From 188eeabc89cfc4851ad1943d5ef6bf11bc6cb629 Mon Sep 17 00:00:00 2001 From: Owen Pan Date: Sat, 18 Nov 2023 16:37:45 -0800 Subject: [PATCH] [clang-format] Fix a bug in isStartOfName() on macro definitions 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 1d376cd8b5794dd..a23a1b28330fe57 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -2206,8 +2206,10 @@ class AnnotatingParser { 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 a986806b7a44069..b6abf34c589b146 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 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits