Skip to content

Commit 81035c3

Browse files
authored
[clang-format] Allow short function body on a single line (#151428)
Fix #145161
1 parent c878baf commit 81035c3

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,13 @@ class LineJoiner {
251251
: Limit - TheLine->Last->TotalLength;
252252

253253
if (TheLine->Last->is(TT_FunctionLBrace) &&
254-
TheLine->First == TheLine->Last &&
255-
!Style.BraceWrapping.SplitEmptyFunction &&
256-
NextLine.First->is(tok::r_brace)) {
257-
return tryMergeSimpleBlock(I, E, Limit);
254+
TheLine->First == TheLine->Last) {
255+
const bool EmptyFunctionBody = NextLine.First->is(tok::r_brace);
256+
if ((EmptyFunctionBody && !Style.BraceWrapping.SplitEmptyFunction) ||
257+
(!EmptyFunctionBody &&
258+
Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Always)) {
259+
return tryMergeSimpleBlock(I, E, Limit);
260+
}
258261
}
259262

260263
const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] : nullptr;

clang/unittests/Format/FormatTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14996,6 +14996,18 @@ TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
1499614996
Style);
1499714997
}
1499814998

14999+
TEST_F(FormatTest, MergeShortFunctionBody) {
15000+
auto Style = getLLVMStyle();
15001+
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
15002+
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
15003+
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
15004+
Style.BraceWrapping.AfterFunction = true;
15005+
15006+
verifyFormat("int foo()\n"
15007+
"{ return 1; }",
15008+
Style);
15009+
}
15010+
1499915011
TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
1500015012
FormatStyle Style = getLLVMStyle();
1500115013
Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;

0 commit comments

Comments
 (0)