Skip to content

Commit

Permalink
[clang-format] Fix a bug in annotating StartOfName (#127545)
Browse files Browse the repository at this point in the history
Also ensure we can break before ClassHeadName like StartOfName.

Fixes #127470
  • Loading branch information
owenca authored Feb 18, 2025
1 parent 1ae9dd3 commit 13de15c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions clang/lib/Format/TokenAnnotator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions clang/unittests/Format/TokenAnnotatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 13de15c

Please sign in to comment.