Skip to content

Commit 0e5f589

Browse files
committed
Comment fixups
1 parent a75325b commit 0e5f589

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "FormatToken.h"
1111
#include "NamespaceEndCommentsFixer.h"
1212
#include "WhitespaceManager.h"
13+
#include "clang/Basic/TokenKinds.h"
1314
#include "llvm/Support/Debug.h"
1415
#include <queue>
1516

@@ -513,7 +514,7 @@ class LineJoiner {
513514
return tryMergeRecord(I, E, Limit);
514515
} else if (TheLine->InPPDirective ||
515516
!TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
516-
tok::kw_struct)) {
517+
tok::kw_struct, tok::kw_union)) {
517518
// Try to merge a block with left brace unwrapped that wasn't yet
518519
// covered.
519520
ShouldMerge = !Style.BraceWrapping.AfterFunction ||
@@ -586,63 +587,54 @@ class LineJoiner {
586587
const auto *Line = I[0];
587588
const auto *NextLine = I[1];
588589

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-
602590
// Current line begins both record and block, brace was not wrapped.
603591
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+
604605
auto TryMergeShortRecord = [&] {
605606
switch (Style.AllowShortRecordOnASingleLine) {
606607
case FormatStyle::SRS_Never:
607608
return false;
608-
case FormatStyle::SRS_EmptyIfAttached:
609-
case FormatStyle::SRS_Empty:
610-
return NextLine->First->is(tok::r_brace);
611609
case FormatStyle::SRS_Always:
612610
return true;
611+
default:
612+
return NextLine->First->is(tok::r_brace);
613613
}
614614
};
615615

616616
if (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never &&
617-
(!GetRelevantAfterOption(Line->Last) ||
617+
(!ShouldWrapLBrace(Line->Last->getType()) ||
618618
(!Style.BraceWrapping.SplitEmptyRecord && TryMergeShortRecord()))) {
619619
return tryMergeSimpleBlock(I, E, Limit);
620620
}
621621
}
622622

623623
// Cases where the l_brace was wrapped.
624624
// 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,
626626
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) {
628629
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);
635630
}
636-
}
637-
638-
if (I == AnnotatedLines.begin())
639-
return 0;
640631

641-
const auto *PreviousLine = I[-1];
632+
return tryMergeSimpleBlock(I, E, Limit);
633+
}
642634

643635
// 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)) {
646638
const bool IsEmptyBlock =
647639
Line->Last->is(tok::l_brace) && NextLine->First->is(tok::r_brace);
648640

0 commit comments

Comments
 (0)