Skip to content

Commit 13de15c

Browse files
authored
[clang-format] Fix a bug in annotating StartOfName (#127545)
Also ensure we can break before ClassHeadName like StartOfName. Fixes #127470
1 parent 1ae9dd3 commit 13de15c

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,7 @@ class AnnotatingParser {
25962596
(!NextNonComment && !Line.InMacroBody) ||
25972597
(NextNonComment &&
25982598
(NextNonComment->isPointerOrReference() ||
2599-
NextNonComment->is(tok::string_literal) ||
2599+
NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
26002600
(Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
26012601
return false;
26022602
}
@@ -6198,8 +6198,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
61986198
FormatStyle::PAS_Right &&
61996199
(!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
62006200
}
6201-
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6202-
Right.is(tok::kw_operator)) {
6201+
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6202+
TT_ClassHeadName, tok::kw_operator)) {
62036203
return true;
62046204
}
62056205
if (Left.is(TT_PointerOrReference))

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29028,6 +29028,11 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
2902829028
Style);
2902929029
}
2903029030

29031+
TEST_F(FormatTest, BreakBeforeClassName) {
29032+
verifyFormat("class ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_NULLABILITY_COMPATIBLE\n"
29033+
" ArenaSafeUniquePtr {};");
29034+
}
29035+
2903129036
} // namespace
2903229037
} // namespace test
2903329038
} // namespace format

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,6 +3250,10 @@ TEST_F(TokenAnnotatorTest, StartOfName) {
32503250
EXPECT_TOKEN(Tokens[0], tok::at, TT_ObjCDecl);
32513251
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_StartOfName);
32523252

3253+
Tokens = annotate("class FOO BAR C {};");
3254+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3255+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown); // Not StartOfName
3256+
32533257
auto Style = getLLVMStyle();
32543258
Style.StatementAttributeLikeMacros.push_back("emit");
32553259
Tokens = annotate("emit foo = 0;", Style);

0 commit comments

Comments
 (0)