[clang] [clang-format] Fix a bug in isStartOfName() on macro definitions (PR #72768)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 18 16:41:22 PST 2023
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 <owenpiano at gmail.com>
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
More information about the cfe-commits
mailing list