Skip to content

Commit

Permalink
[clang-format] Handle merging functions containing only a block comme…
Browse files Browse the repository at this point in the history
…nt (#74651)

Fixed #41854.
  • Loading branch information
owenca committed Dec 7, 2023
1 parent 13b8826 commit e1a4b00
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
15 changes: 12 additions & 3 deletions clang/lib/Format/UnwrappedLineFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,16 @@ class LineJoiner {
}
}

const auto *LastNonComment = TheLine->getLastNonComment();
assert(LastNonComment);
// FIXME: There are probably cases where we should use LastNonComment
// instead of TheLine->Last.

// Try to merge a function block with left brace unwrapped.
if (TheLine->Last->is(TT_FunctionLBrace) && TheLine->First != TheLine->Last)
if (LastNonComment->is(TT_FunctionLBrace) &&
TheLine->First != LastNonComment) {
return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
}
// Try to merge a control statement block with left brace unwrapped.
if (TheLine->Last->is(tok::l_brace) && FirstNonComment != TheLine->Last &&
FirstNonComment->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
Expand Down Expand Up @@ -789,7 +796,8 @@ class LineJoiner {
}
}

if (Line.Last->is(tok::l_brace)) {
if (const auto *LastNonComment = Line.getLastNonComment();
LastNonComment && LastNonComment->is(tok::l_brace)) {
if (IsSplitBlock && Line.First == Line.Last &&
I > AnnotatedLines.begin() &&
(I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
Expand All @@ -805,7 +813,8 @@ class LineJoiner {

if (ShouldMerge()) {
// We merge empty blocks even if the line exceeds the column limit.
Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0;
Tok->SpacesRequiredBefore =
(Style.SpaceInEmptyBlock || Line.Last->is(tok::comment)) ? 1 : 0;
Tok->CanBreakBefore = true;
return 1;
} else if (Limit != 0 && !Line.startsWithNamespace() &&
Expand Down
13 changes: 13 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13950,6 +13950,19 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) {
" void f() { int i; } \\\n"
" int j;",
getLLVMStyleWithColumns(23));

verifyFormat(
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {}");

constexpr StringRef Code{"void foo() { /* Empty */ }"};
verifyFormat(Code);
verifyFormat(Code, "void foo() { /* Empty */\n"
"}");
verifyFormat(Code, "void foo() {\n"
"/* Empty */\n"
"}");
}

TEST_F(FormatTest, PullEmptyFunctionDefinitionsIntoSingleLine) {
Expand Down
15 changes: 8 additions & 7 deletions clang/unittests/Format/FormatTestComments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,16 @@ TEST_F(FormatTestComments, UnderstandsBlockComments) {
" /* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);",
format("f(aaaaaaaaaaaaaaaaaaaaaaaaa , \n"
"/* Leading comment for bb... */ bbbbbbbbbbbbbbbbbbbbbbbbb);"));
EXPECT_EQ(

verifyFormat(
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaa,\n"
" aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
"}",
format("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaa ,\n"
" aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
"}"));
" aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/ }",
"void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n"
" aaaaaaaaaaaaaaaaaa ,\n"
" aaaaaaaaaaaaaaaaaa) { /*aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*/\n"
"}");

verifyFormat("f(/* aaaaaaaaaaaaaaaaaa = */\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);");

Expand Down

0 comments on commit e1a4b00

Please sign in to comment.