@@ -266,18 +266,17 @@ class LineJoiner {
266
266
}
267
267
}
268
268
269
- // Try merging record blocks that have had their left brace wrapped.
269
+ // Try merging record blocks that have had their left brace wrapped into
270
+ // a single line.
270
271
if (NextLine.First ->isOneOf (TT_ClassLBrace, TT_StructLBrace,
271
- TT_UnionLBrace) &&
272
- NextLine.First == NextLine.Last && I + 2 != E &&
273
- I[2 ]->First ->isNot (tok::r_brace) &&
274
- Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Always) {
275
- if (unsigned MergedLines = tryMergeSimpleBlock (I, E, Limit))
272
+ TT_UnionLBrace)) {
273
+ if (unsigned MergedLines = tryMergeRecord (I, E, Limit))
276
274
return MergedLines;
277
275
}
278
276
279
277
const auto *PreviousLine = I != AnnotatedLines.begin () ? I[-1 ] : nullptr ;
280
- // Handle empty record blocks where the brace has already been wrapped.
278
+
279
+ // Handle blocks where the brace has already been wrapped.
281
280
if (PreviousLine && TheLine->Last ->is (tok::l_brace) &&
282
281
TheLine->First == TheLine->Last ) {
283
282
const bool EmptyBlock = NextLine.First ->is (tok::r_brace);
@@ -293,11 +292,11 @@ class LineJoiner {
293
292
if (Tok && Tok->is (tok::kw_typedef))
294
293
Tok = Tok->getNextNonComment ();
295
294
296
- if (Tok && Tok->isOneOf (tok::kw_class, tok::kw_struct, tok::kw_union,
297
- tok::kw_extern, Keywords. kw_interface )) {
298
- return (EmptyBlock && !Style. BraceWrapping . SplitEmptyRecord ) ||
299
- (!EmptyBlock && Style. AllowShortBlocksOnASingleLine ==
300
- FormatStyle::SBS_Always)
295
+ if (Tok && Tok->isOneOf (tok::kw_class, tok::kw_struct, tok::kw_union))
296
+ return tryMergeRecord (I, E, Limit);
297
+
298
+ if (Tok && Tok-> isOneOf (tok::kw_extern, Keywords. kw_interface )) {
299
+ return !Style. BraceWrapping . SplitEmptyRecord && EmptyBlock
301
300
? tryMergeSimpleBlock (I, E, Limit)
302
301
: 0 ;
303
302
}
@@ -502,18 +501,6 @@ class LineJoiner {
502
501
: 0 ;
503
502
}
504
503
505
- auto TryMergeShortRecord = [&] {
506
- switch (Style.AllowShortRecordOnASingleLine ) {
507
- case FormatStyle::SRS_Never:
508
- return false ;
509
- case FormatStyle::SRS_EmptyIfAttached:
510
- case FormatStyle::SRS_Empty:
511
- return NextLine.First ->is (tok::r_brace);
512
- case FormatStyle::SRS_Always:
513
- return true ;
514
- }
515
- };
516
-
517
504
if (TheLine->Last ->is (tok::l_brace)) {
518
505
bool ShouldMerge = false ;
519
506
// Try to merge records.
@@ -523,14 +510,7 @@ class LineJoiner {
523
510
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine ;
524
511
} else if (TheLine->Last ->isOneOf (TT_ClassLBrace, TT_StructLBrace,
525
512
TT_UnionLBrace)) {
526
- if (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never) {
527
- // NOTE: We use AfterClass (whereas AfterStruct exists) for both
528
- // classes and structs, but it seems that wrapping is still handled
529
- // correctly elsewhere.
530
- ShouldMerge =
531
- !Style.BraceWrapping .AfterClass ||
532
- (!Style.BraceWrapping .SplitEmptyRecord && TryMergeShortRecord ());
533
- }
513
+ return tryMergeRecord (I, E, Limit);
534
514
} else if (TheLine->InPPDirective ||
535
515
!TheLine->First ->isOneOf (tok::kw_class, tok::kw_enum,
536
516
tok::kw_struct)) {
@@ -600,6 +580,73 @@ class LineJoiner {
600
580
return 0 ;
601
581
}
602
582
583
+ unsigned tryMergeRecord (ArrayRef<AnnotatedLine *>::const_iterator I,
584
+ ArrayRef<AnnotatedLine *>::const_iterator E,
585
+ unsigned Limit) {
586
+ const auto *Line = I[0 ];
587
+ const auto *NextLine = I[1 ];
588
+
589
+ auto GetRelevantAfterOption = [&](const FormatToken *tok) {
590
+ switch (tok->getType ()) {
591
+ case TT_StructLBrace:
592
+ return Style.BraceWrapping .AfterStruct ;
593
+ case TT_ClassLBrace:
594
+ return Style.BraceWrapping .AfterClass ;
595
+ case TT_UnionLBrace:
596
+ return Style.BraceWrapping .AfterUnion ;
597
+ };
598
+ };
599
+
600
+ // Current line begins both record and block, brace was not wrapped.
601
+ if (Line->Last ->isOneOf (TT_StructLBrace, TT_ClassLBrace, TT_UnionLBrace)) {
602
+ auto TryMergeShortRecord = [&] {
603
+ switch (Style.AllowShortRecordOnASingleLine ) {
604
+ case FormatStyle::SRS_EmptyIfAttached:
605
+ case FormatStyle::SRS_Empty:
606
+ return NextLine->First ->is (tok::r_brace);
607
+ case FormatStyle::SRS_Always:
608
+ return true ;
609
+ }
610
+ };
611
+
612
+ if (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never &&
613
+ (!GetRelevantAfterOption (Line->Last ) ||
614
+ (!Style.BraceWrapping .SplitEmptyRecord && TryMergeShortRecord ()))) {
615
+ return tryMergeSimpleBlock (I, E, Limit);
616
+ }
617
+ }
618
+
619
+ // Cases where the l_brace was wrapped.
620
+ // Current line begins record, next line block.
621
+ if (NextLine->First ->isOneOf (TT_StructLBrace, TT_ClassLBrace,
622
+ TT_UnionLBrace)) {
623
+ if (NextLine->First == NextLine->Last && I + 2 != E &&
624
+ I[2 ]->First ->isNot (tok::r_brace) &&
625
+ Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Always) {
626
+ return tryMergeSimpleBlock (I, E, Limit);
627
+ }
628
+ }
629
+
630
+ if (I == AnnotatedLines.begin ())
631
+ return 0 ;
632
+
633
+ const auto *PreviousLine = I[-1 ];
634
+
635
+ // Previous line begins record, current line block.
636
+ if (PreviousLine->First ->isOneOf (tok::kw_struct, tok::kw_class,
637
+ tok::kw_union)) {
638
+ const bool IsEmptyBlock =
639
+ Line->Last ->is (tok::l_brace) && NextLine->First ->is (tok::r_brace);
640
+
641
+ if (IsEmptyBlock && !Style.BraceWrapping .SplitEmptyRecord ||
642
+ Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Always) {
643
+ return tryMergeSimpleBlock (I, E, Limit);
644
+ }
645
+ }
646
+
647
+ return 0 ;
648
+ }
649
+
603
650
unsigned
604
651
tryMergeSimplePPDirective (ArrayRef<AnnotatedLine *>::const_iterator I,
605
652
ArrayRef<AnnotatedLine *>::const_iterator E,
0 commit comments