Skip to content

Commit c95fbc5

Browse files
committed
[clang-format] Fix brace wrapping for Java records
The brace wrapping for Java records should now behave similar to classes.
1 parent 81a9d75 commit c95fbc5

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class LineJoiner {
285285
if (Tok && Tok->is(tok::kw_typedef))
286286
Tok = Tok->getNextNonComment();
287287
if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
288-
tok::kw_extern, Keywords.kw_interface)) {
288+
tok::kw_extern, Keywords.kw_interface, Keywords.kw_record)) {
289289
return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
290290
? tryMergeSimpleBlock(I, E, Limit)
291291
: 0;
@@ -498,7 +498,7 @@ class LineJoiner {
498498
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
499499
} else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) {
500500
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
501-
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
501+
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace, TT_RecordLBrace)) {
502502
// NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
503503
// and structs, but it seems that wrapping is still handled correctly
504504
// elsewhere.
@@ -507,7 +507,7 @@ class LineJoiner {
507507
!Style.BraceWrapping.SplitEmptyRecord);
508508
} else if (TheLine->InPPDirective ||
509509
TheLine->First->isNoneOf(tok::kw_class, tok::kw_enum,
510-
tok::kw_struct)) {
510+
tok::kw_struct, Keywords.kw_record)) {
511511
// Try to merge a block with left brace unwrapped that wasn't yet
512512
// covered.
513513
ShouldMerge = !Style.BraceWrapping.AfterFunction ||

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,11 @@ static bool isIIFE(const UnwrappedLine &Line,
948948
}
949949

950950
static bool ShouldBreakBeforeBrace(const FormatStyle &Style,
951-
const FormatToken &InitialToken) {
951+
const FormatToken &InitialToken,
952+
const bool IsJavaRecord) {
953+
if (IsJavaRecord)
954+
return Style.BraceWrapping.AfterClass;
955+
952956
tok::TokenKind Kind = InitialToken.Tok.getKind();
953957
if (InitialToken.is(TT_NamespaceMacro))
954958
Kind = tok::kw_namespace;
@@ -3200,7 +3204,7 @@ void UnwrappedLineParser::parseNamespace() {
32003204
if (FormatTok->is(tok::l_brace)) {
32013205
FormatTok->setFinalizedType(TT_NamespaceLBrace);
32023206

3203-
if (ShouldBreakBeforeBrace(Style, InitialToken))
3207+
if (ShouldBreakBeforeBrace(Style, InitialToken, false))
32043208
addUnwrappedLine();
32053209

32063210
unsigned AddLevels =
@@ -3865,7 +3869,7 @@ bool UnwrappedLineParser::parseEnum() {
38653869
}
38663870

38673871
if (!Style.AllowShortEnumsOnASingleLine &&
3868-
ShouldBreakBeforeBrace(Style, InitialToken)) {
3872+
ShouldBreakBeforeBrace(Style, InitialToken, false)) {
38693873
addUnwrappedLine();
38703874
}
38713875
// Parse enum body.
@@ -4160,7 +4164,7 @@ void UnwrappedLineParser::parseRecord(bool ParseAsExpr, bool IsJavaRecord) {
41604164
if (ParseAsExpr) {
41614165
parseChildBlock();
41624166
} else {
4163-
if (ShouldBreakBeforeBrace(Style, InitialToken))
4167+
if (ShouldBreakBeforeBrace(Style, InitialToken, IsJavaRecord))
41644168
addUnwrappedLine();
41654169

41664170
unsigned AddLevels = Style.IndentAccessModifiers ? 2u : 1u;

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28918,6 +28918,18 @@ TEST_F(FormatTest, KeywordedFunctionLikeMacros) {
2891828918
Style);
2891928919
}
2892028920

28921+
TEST_F(FormatTest, BreakAfterJavaRecord) {
28922+
auto Style = getLLVMStyle();
28923+
Style.Language = FormatStyle::LK_Java;
28924+
Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never;
28925+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
28926+
Style.BraceWrapping.AfterClass = true;
28927+
Style.BraceWrapping.SplitEmptyRecord = true;
28928+
28929+
verifyFormat("public record Foo(int i)\n{\n}", "public record Foo(int i) {}",
28930+
Style);
28931+
}
28932+
2892128933
} // namespace
2892228934
} // namespace test
2892328935
} // namespace format

0 commit comments

Comments
 (0)