|
10 | 10 | #include "FormatToken.h" |
11 | 11 | #include "NamespaceEndCommentsFixer.h" |
12 | 12 | #include "WhitespaceManager.h" |
| 13 | +#include "clang/Basic/TokenKinds.h" |
13 | 14 | #include "llvm/Support/Debug.h" |
14 | 15 | #include <queue> |
15 | 16 |
|
@@ -513,7 +514,7 @@ class LineJoiner { |
513 | 514 | return tryMergeRecord(I, E, Limit); |
514 | 515 | } else if (TheLine->InPPDirective || |
515 | 516 | !TheLine->First->isOneOf(tok::kw_class, tok::kw_enum, |
516 | | - tok::kw_struct)) { |
| 517 | + tok::kw_struct, tok::kw_union)) { |
517 | 518 | // Try to merge a block with left brace unwrapped that wasn't yet |
518 | 519 | // covered. |
519 | 520 | ShouldMerge = !Style.BraceWrapping.AfterFunction || |
@@ -586,63 +587,54 @@ class LineJoiner { |
586 | 587 | const auto *Line = I[0]; |
587 | 588 | const auto *NextLine = I[1]; |
588 | 589 |
|
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 | | - default: |
598 | | - return false; |
599 | | - }; |
600 | | - }; |
601 | | - |
602 | 590 | // Current line begins both record and block, brace was not wrapped. |
603 | 591 | if (Line->Last->isOneOf(TT_StructLBrace, TT_ClassLBrace, TT_UnionLBrace)) { |
| 592 | + auto ShouldWrapLBrace = [&](TokenType LBraceType) { |
| 593 | + switch (LBraceType) { |
| 594 | + case TT_StructLBrace: |
| 595 | + return Style.BraceWrapping.AfterStruct; |
| 596 | + case TT_ClassLBrace: |
| 597 | + return Style.BraceWrapping.AfterClass; |
| 598 | + case TT_UnionLBrace: |
| 599 | + return Style.BraceWrapping.AfterUnion; |
| 600 | + default: |
| 601 | + return false; |
| 602 | + }; |
| 603 | + }; |
| 604 | + |
604 | 605 | auto TryMergeShortRecord = [&] { |
605 | 606 | switch (Style.AllowShortRecordOnASingleLine) { |
606 | 607 | case FormatStyle::SRS_Never: |
607 | 608 | return false; |
608 | | - case FormatStyle::SRS_EmptyIfAttached: |
609 | | - case FormatStyle::SRS_Empty: |
610 | | - return NextLine->First->is(tok::r_brace); |
611 | 609 | case FormatStyle::SRS_Always: |
612 | 610 | return true; |
| 611 | + default: |
| 612 | + return NextLine->First->is(tok::r_brace); |
613 | 613 | } |
614 | 614 | }; |
615 | 615 |
|
616 | 616 | if (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never && |
617 | | - (!GetRelevantAfterOption(Line->Last) || |
| 617 | + (!ShouldWrapLBrace(Line->Last->getType()) || |
618 | 618 | (!Style.BraceWrapping.SplitEmptyRecord && TryMergeShortRecord()))) { |
619 | 619 | return tryMergeSimpleBlock(I, E, Limit); |
620 | 620 | } |
621 | 621 | } |
622 | 622 |
|
623 | 623 | // Cases where the l_brace was wrapped. |
624 | 624 | // Current line begins record, next line block. |
625 | | - if (NextLine->First->isOneOf(TT_StructLBrace, TT_ClassLBrace, |
| 625 | + if (NextLine->First->isOneOf(TT_ClassLBrace, TT_StructLBrace, |
626 | 626 | TT_UnionLBrace)) { |
627 | | - if (I + 2 == E) |
| 627 | + if (I + 2 == E || I[2]->First->is(tok::r_brace) || |
| 628 | + Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Always) { |
628 | 629 | return 0; |
629 | | - |
630 | | - const bool IsEmptyBlock = I[2]->First->is(tok::r_brace); |
631 | | - |
632 | | - if (!IsEmptyBlock && |
633 | | - Style.AllowShortRecordOnASingleLine == FormatStyle::SRS_Always) { |
634 | | - return tryMergeSimpleBlock(I, E, Limit); |
635 | 630 | } |
636 | | - } |
637 | | - |
638 | | - if (I == AnnotatedLines.begin()) |
639 | | - return 0; |
640 | 631 |
|
641 | | - const auto *PreviousLine = I[-1]; |
| 632 | + return tryMergeSimpleBlock(I, E, Limit); |
| 633 | + } |
642 | 634 |
|
643 | 635 | // Previous line begins record, current line block. |
644 | | - if (PreviousLine->First->isOneOf(tok::kw_struct, tok::kw_class, |
645 | | - tok::kw_union)) { |
| 636 | + if (I != AnnotatedLines.begin() && |
| 637 | + I[-1]->First->isOneOf(tok::kw_struct, tok::kw_class, tok::kw_union)) { |
646 | 638 | const bool IsEmptyBlock = |
647 | 639 | Line->Last->is(tok::l_brace) && NextLine->First->is(tok::r_brace); |
648 | 640 |
|
|
0 commit comments