Skip to content

Commit 86073c9

Browse files
committed
Fix interaction between AllowShortRecord and AllowShortBlocks options
1 parent 0fa29c1 commit 86073c9

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ class LineJoiner {
270270
if (NextLine.First->isOneOf(TT_ClassLBrace, TT_StructLBrace,
271271
TT_UnionLBrace) &&
272272
NextLine.First == NextLine.Last && I + 2 != E &&
273-
!I[2]->First->is(tok::r_brace)) {
273+
!I[2]->First->is(tok::r_brace) &&
274+
Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never) {
274275
if (unsigned MergedLines = tryMergeSimpleBlock(I, E, Limit))
275276
return MergedLines;
276277
}
@@ -279,7 +280,7 @@ class LineJoiner {
279280
// Handle empty record blocks where the brace has already been wrapped.
280281
if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
281282
TheLine->First == TheLine->Last) {
282-
bool EmptyBlock = NextLine.First->is(tok::r_brace);
283+
const bool EmptyBlock = NextLine.First->is(tok::r_brace);
283284

284285
const FormatToken *Tok = PreviousLine->First;
285286
if (Tok && Tok->is(tok::comment))
@@ -295,7 +296,9 @@ class LineJoiner {
295296
Tok = Tok->getNextNonComment();
296297
if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
297298
tok::kw_extern, Keywords.kw_interface)) {
298-
return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
299+
return (EmptyBlock && !Style.BraceWrapping.SplitEmptyRecord) ||
300+
(!EmptyBlock && Style.AllowShortBlocksOnASingleLine ==
301+
FormatStyle::SBS_Always)
299302
? tryMergeSimpleBlock(I, E, Limit)
300303
: 0;
301304
}
@@ -909,6 +912,7 @@ class LineJoiner {
909912

910913
if (Line.Last->isOneOf(TT_ClassLBrace, TT_StructLBrace,
911914
TT_UnionLBrace) &&
915+
Line.Last != Line.First &&
912916
Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Always) {
913917
return 0;
914918
}

clang/unittests/Format/FormatTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15360,6 +15360,9 @@ TEST_F(FormatTest, AllowShortRecordOnASingleLine) {
1536015360
verifyFormat("class foo\n"
1536115361
"{};",
1536215362
Style);
15363+
Style.BraceWrapping.SplitEmptyRecord = true;
15364+
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
15365+
verifyFormat("class foo\n{ int i; };", Style);
1536315366

1536415367
Style = getLLVMStyle();
1536515368
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty;

0 commit comments

Comments
 (0)