Skip to content

Commit 73cdd98

Browse files
committed
Extract record merging into a separate function
1 parent 5892208 commit 73cdd98

File tree

1 file changed

+79
-32
lines changed

1 file changed

+79
-32
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,17 @@ class LineJoiner {
266266
}
267267
}
268268

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.
270271
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))
276274
return MergedLines;
277275
}
278276

279277
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.
281280
if (PreviousLine && TheLine->Last->is(tok::l_brace) &&
282281
TheLine->First == TheLine->Last) {
283282
const bool EmptyBlock = NextLine.First->is(tok::r_brace);
@@ -293,11 +292,11 @@ class LineJoiner {
293292
if (Tok && Tok->is(tok::kw_typedef))
294293
Tok = Tok->getNextNonComment();
295294

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
301300
? tryMergeSimpleBlock(I, E, Limit)
302301
: 0;
303302
}
@@ -502,18 +501,6 @@ class LineJoiner {
502501
: 0;
503502
}
504503

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-
517504
if (TheLine->Last->is(tok::l_brace)) {
518505
bool ShouldMerge = false;
519506
// Try to merge records.
@@ -523,14 +510,7 @@ class LineJoiner {
523510
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
524511
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace,
525512
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);
534514
} else if (TheLine->InPPDirective ||
535515
!TheLine->First->isOneOf(tok::kw_class, tok::kw_enum,
536516
tok::kw_struct)) {
@@ -600,6 +580,73 @@ class LineJoiner {
600580
return 0;
601581
}
602582

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+
603650
unsigned
604651
tryMergeSimplePPDirective(ArrayRef<AnnotatedLine *>::const_iterator I,
605652
ArrayRef<AnnotatedLine *>::const_iterator E,

0 commit comments

Comments
 (0)