Skip to content

Commit 34f5f90

Browse files
owencatstellar
authored andcommitted
[clang-format] Fix a bug in annotating StartOfName (llvm#127545)
Also ensure we can break before ClassHeadName like StartOfName. Fixes llvm#127470 (cherry picked from commit 13de15c)
1 parent 3dedc99 commit 34f5f90

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
@@ -2595,7 +2595,7 @@ class AnnotatingParser {
25952595
(!NextNonComment && !Line.InMacroBody) ||
25962596
(NextNonComment &&
25972597
(NextNonComment->isPointerOrReference() ||
2598-
NextNonComment->is(tok::string_literal) ||
2598+
NextNonComment->isOneOf(TT_ClassHeadName, tok::string_literal) ||
25992599
(Line.InPragmaDirective && NextNonComment->is(tok::identifier))))) {
26002600
return false;
26012601
}
@@ -6194,8 +6194,8 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line,
61946194
FormatStyle::PAS_Right &&
61956195
(!Right.Next || Right.Next->isNot(TT_FunctionDeclarationName)));
61966196
}
6197-
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName) ||
6198-
Right.is(tok::kw_operator)) {
6197+
if (Right.isOneOf(TT_StartOfName, TT_FunctionDeclarationName,
6198+
TT_ClassHeadName, tok::kw_operator)) {
61996199
return true;
62006200
}
62016201
if (Left.is(TT_PointerOrReference))

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28720,6 +28720,11 @@ TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
2872028720
Style);
2872128721
}
2872228722

28723+
TEST_F(FormatTest, BreakBeforeClassName) {
28724+
verifyFormat("class ABSL_ATTRIBUTE_TRIVIAL_ABI ABSL_NULLABILITY_COMPATIBLE\n"
28725+
" ArenaSafeUniquePtr {};");
28726+
}
28727+
2872328728
} // namespace
2872428729
} // namespace test
2872528730
} // namespace format

clang/unittests/Format/TokenAnnotatorTest.cpp

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

3242+
Tokens = annotate("class FOO BAR C {};");
3243+
ASSERT_EQ(Tokens.size(), 8u) << Tokens;
3244+
EXPECT_TOKEN(Tokens[2], tok::identifier, TT_Unknown); // Not StartOfName
3245+
32423246
auto Style = getLLVMStyle();
32433247
Style.StatementAttributeLikeMacros.push_back("emit");
32443248
Tokens = annotate("emit foo = 0;", Style);

0 commit comments

Comments
 (0)