Skip to content

Commit 7604f55

Browse files
committed
Fixups
1 parent e1062d7 commit 7604f55

File tree

3 files changed

+40
-113
lines changed

3 files changed

+40
-113
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5960,6 +5960,7 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
59605960
}
59615961

59625962
// Don't attempt to interpret record return types as records.
5963+
// FIXME: Not covered by tests.
59635964
if (Right.isNot(TT_FunctionLBrace)) {
59645965
return ((Line.startsWith(tok::kw_class) &&
59655966
Style.BraceWrapping.AfterClass) ||

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,10 @@ class LineJoiner {
267267
}
268268

269269
// Try merging record blocks that have had their left brace wrapped.
270-
if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union) &&
271-
NextLine.First->is(tok::l_brace) && NextLine.First == NextLine.Last &&
272-
I + 2 != E && !I[2]->First->is(tok::r_brace)) {
270+
if (NextLine.First->isOneOf(TT_ClassLBrace, TT_StructLBrace,
271+
TT_UnionLBrace) &&
272+
NextLine.First == NextLine.Last && I + 2 != E &&
273+
!I[2]->First->is(tok::r_brace)) {
273274
if (unsigned MergedLines = tryMergeSimpleBlock(I, E, Limit))
274275
return MergedLines;
275276
}
@@ -499,7 +500,7 @@ class LineJoiner {
499500
: 0;
500501
}
501502

502-
const bool TryMergeShortRecord = [this, &NextLine]() {
503+
const bool TryMergeShortRecord = [&]() {
503504
switch (Style.AllowShortRecordOnASingleLine) {
504505
case FormatStyle::SRS_Never:
505506
return false;
@@ -520,7 +521,7 @@ class LineJoiner {
520521
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
521522
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace,
522523
TT_UnionLBrace)) {
523-
if (Style.AllowShortRecordOnASingleLine > FormatStyle::SRS_Never) {
524+
if (Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Never) {
524525
// NOTE: We use AfterClass (whereas AfterStruct exists) for both
525526
// classes and structs, but it seems that wrapping is still handled
526527
// correctly elsewhere.
@@ -902,7 +903,7 @@ class LineJoiner {
902903
!startsExternCBlock(Line)) {
903904
// Merge short records only when requested.
904905
if (isRecordLBrace(*Line.Last) &&
905-
Style.AllowShortRecordOnASingleLine < FormatStyle::SRS_Always) {
906+
Style.AllowShortRecordOnASingleLine != FormatStyle::SRS_Always) {
906907
return 0;
907908
}
908909

clang/unittests/Format/FormatTest.cpp

Lines changed: 32 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -8632,19 +8632,6 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) {
86328632
Style);
86338633
}
86348634

8635-
TEST_F(FormatTest, BreakFunctionsReturningRecords) {
8636-
FormatStyle Style = getLLVMStyle();
8637-
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
8638-
Style.BraceWrapping.AfterFunction = true;
8639-
Style.BraceWrapping.AfterClass = false;
8640-
Style.BraceWrapping.AfterStruct = false;
8641-
Style.BraceWrapping.AfterUnion = false;
8642-
8643-
verifyFormat("class Bar foo() {}", Style);
8644-
verifyFormat("struct Bar foo() {}", Style);
8645-
verifyFormat("union Bar foo() {}", Style);
8646-
}
8647-
86488635
TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) {
86498636
// Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516:
86508637
// Prefer keeping `::` followed by `operator` together.
@@ -15347,129 +15334,67 @@ TEST_F(FormatTest, NeverMergeShortRecords) {
1534715334
Style);
1534815335
}
1534915336

15350-
TEST_F(FormatTest, AllowShortRecordOnASingleLineNonSplit) {
15337+
TEST_F(FormatTest, AllowShortRecordOnASingleLine) {
1535115338
auto Style = getLLVMStyle();
15352-
15353-
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15354-
Style.BraceWrapping.SplitEmptyRecord = false;
15339+
EXPECT_EQ(Style.AllowShortRecordOnASingleLine,
15340+
FormatStyle::SRS_EmptyIfAttached);
1535515341

1535615342
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Never;
1535715343
verifyFormat("class foo {\n"
15358-
" void bar();\n"
15359-
"};",
15360-
Style);
15361-
verifyFormat("class foo {\n"
15362-
"};",
15363-
Style);
15364-
15365-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_EmptyIfAttached;
15366-
verifyFormat("class foo {\n"
15367-
" void bar();\n"
15368-
"};",
15369-
Style);
15370-
verifyFormat("class foo {};", Style);
15371-
15372-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty;
15373-
verifyFormat("class foo {\n"
15374-
" void bar();\n"
15344+
"};\n"
15345+
"class bar {\n"
15346+
" int i;\n"
1537515347
"};",
1537615348
Style);
15377-
verifyFormat("class foo {};", Style);
15378-
15379-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Always;
15380-
verifyFormat("class foo { void bar(); };", Style);
15381-
verifyFormat("class foo {};", Style);
15382-
15349+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1538315350
Style.BraceWrapping.AfterClass = true;
15384-
15385-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Never;
1538615351
verifyFormat("class foo\n"
1538715352
"{\n"
15388-
" void bar();\n"
15389-
"};",
15390-
Style);
15391-
verifyFormat("class foo\n{};", Style);
15392-
15393-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_EmptyIfAttached;
15394-
verifyFormat("class foo\n"
15353+
"};\n"
15354+
"class bar\n"
1539515355
"{\n"
15396-
" void bar();\n"
15356+
" int i;\n"
1539715357
"};",
1539815358
Style);
15399-
verifyFormat("class foo\n{};", Style);
15400-
15401-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty;
15359+
Style.BraceWrapping.SplitEmptyRecord = false;
1540215360
verifyFormat("class foo\n"
15403-
"{\n"
15404-
" void bar();\n"
15405-
"};",
15406-
Style);
15407-
verifyFormat("class foo {};", Style);
15408-
15409-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Always;
15410-
verifyFormat("class foo { void bar(); };", Style);
15411-
verifyFormat("class foo {};", Style);
15412-
}
15413-
15414-
TEST_F(FormatTest, AllowShortRecordOnASingleLineSplit) {
15415-
auto Style = getLLVMStyle();
15416-
15417-
EXPECT_EQ(Style.BraceWrapping.SplitEmptyRecord, true);
15418-
15419-
EXPECT_EQ(Style.AllowShortRecordOnASingleLine,
15420-
FormatStyle::SRS_EmptyIfAttached);
15421-
verifyFormat("class foo {\n"
15422-
" void bar();\n"
15423-
"};",
15424-
Style);
15425-
verifyFormat("class foo {};", Style);
15426-
15427-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Never;
15428-
verifyFormat("class foo {\n"
15429-
" void bar();\n"
15430-
"};",
15431-
Style);
15432-
verifyFormat("class foo {\n"
15433-
"};",
15361+
"{};",
1543415362
Style);
1543515363

15364+
Style = getLLVMStyle();
1543615365
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty;
15437-
verifyFormat("class foo {\n"
15438-
" void bar();\n"
15366+
verifyFormat("class foo {};\n"
15367+
"class bar {\n"
15368+
" int i;\n"
1543915369
"};",
1544015370
Style);
15441-
verifyFormat("class foo {};", Style);
15442-
15443-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Always;
15444-
verifyFormat("class foo { void bar(); };", Style);
15445-
verifyFormat("class foo {};", Style);
15446-
1544715371
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
1544815372
Style.BraceWrapping.AfterClass = true;
15449-
15450-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Never;
15451-
verifyFormat("class foo\n"
15452-
"{\n"
15453-
"}",
15454-
Style);
15455-
15456-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_EmptyIfAttached;
1545715373
verifyFormat("class foo\n"
1545815374
"{\n"
15459-
"}",
15460-
Style);
15461-
15462-
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Empty;
15463-
verifyFormat("class foo\n"
15375+
"};\n"
15376+
"class bar\n"
1546415377
"{\n"
15465-
"}",
15378+
" int i;\n"
15379+
"};",
1546615380
Style);
15381+
Style.BraceWrapping.SplitEmptyRecord = false;
15382+
verifyFormat("class foo {};", Style);
1546715383

15384+
Style = getLLVMStyle();
1546815385
Style.AllowShortRecordOnASingleLine = FormatStyle::SRS_Always;
15386+
verifyFormat("class foo {};\n"
15387+
"class bar { int i; };",
15388+
Style);
15389+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15390+
Style.BraceWrapping.AfterClass = true;
1546915391
verifyFormat("class foo\n"
1547015392
"{\n"
15471-
"}",
15393+
"};\n"
15394+
"class bar { int i; };",
1547215395
Style);
15396+
Style.BraceWrapping.SplitEmptyRecord = false;
15397+
verifyFormat("class foo {};", Style);
1547315398
}
1547415399

1547515400
TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {

0 commit comments

Comments
 (0)