[clang] [clang-format] Fix a bug in annotating StartOfName (PR #127545)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 17 13:43:38 PST 2025
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/127545
Also ensure we can break before ClassHeadName like StartOfName.
Fixes #127470
>From aa6431824a7fa759393339e4738b777a870602a0 Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Mon, 17 Feb 2025 13:36:47 -0800
Subject: [PATCH] [clang-format] Fix a bug in annotating StartOfName
Also ensure we can break before ClassHeadName like StartOfName.
Fixes #127470
---
clang/lib/Format/TokenAnnotator.cpp | 6 +++---
clang/unittests/Format/FormatTest.cpp | 5 +++++
clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
3 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 069fd40e2834c..e68daa422b7c4 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2596,7 +2596,7 @@ class AnnotatingParser {
(!NextNonComment && !Line.InMacroBody) ||
(NextNonComment &&
(NextNonComment->isPointerOrReference() ||
- NextNonComment->is(tok::string_literal) ||
+ NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
(Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
return false;
}
@@ -6198,8 +6198,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
FormatStyle::PAS_Right &&
(!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
}
- if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
- Right.is(tok::kw_operator)) {
+ if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
+ TT_ClassHeadName, tok::kw_operator)) {
return true;
}
if (Left.is(TT_PointerOrReference))
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 2365a7c40bf76..d6d028436d39c 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -29028,6 +29028,11 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
Style);
}
+TEST_F(FormatTest, BreakBeforeClassName) {
+ verifyFormat("class ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_NULLABILITY_COMPATIBLE\n"
+ " ArenaSafeUniquePtr {};");
+}
+
} // namespace
} // namespace test
} // namespace format
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 1d0870c818acc..8ada6c3daeaf6 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -3250,6 +3250,10 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
EXPECT_TOKEN(Tokens[0], tok::at, TT_ObjCDecl);
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_StartOfName);
+ Tokens = annotate("class FOO BAR C {};");
+ ASSERT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown); // Not StartOfName
+
auto Style = getLLVMStyle();
Style.StatementAttributeLikeMacros.push_back("emit");
Tokens = annotate("emit foo = 0;", Style);
More information about the cfe-commits
mailing list