https://github.com/brandb97 updated 
https://github.com/llvm/llvm-project/pull/151428

>From f024cf1ea515d3218c87300ca6563392fb74b8a2 Mon Sep 17 00:00:00 2001
From: Lidong Yan <yldhome...@gmail.com>
Date: Tue, 26 Aug 2025 19:40:05 +0800
Subject: [PATCH] [clang-format] allow short function body on a single line

When set AllowShortBlocksOnASingleLine to Always and short function
cannot be put on a single line, clang-format doesn't tries to put
the function body on a seperate new single line. Add
tryMergeSimpleBlocks() to put short function body on a single line if
possible.

Signed-off-by: Lidong Yan <yldhome...@gmail.com>
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 13 ++++++++----
 clang/unittests/Format/FormatTest.cpp       | 23 +++++++++++++++++++++
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp 
b/clang/lib/Format/UnwrappedLineFormatter.cpp
index c938ff3965f9e..53c1d2587fbca 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -251,10 +251,15 @@ class LineJoiner {
                 : Limit - TheLine->Last->TotalLength;
 
     if (TheLine->Last->is(TT_FunctionLBrace) &&
-        TheLine->First == TheLine->Last &&
-        !Style.BraceWrapping.SplitEmptyFunction &&
-        NextLine.First->is(tok::r_brace)) {
-      return tryMergeSimpleBlock(I, E, Limit);
+        TheLine->First == TheLine->Last) {
+      const bool EmptyFunctionBody = NextLine.First->is(tok::r_brace);
+      const bool ShortFunctionBody =
+          !EmptyFunctionBody && I + 2 != E && I[2]->First->is(tok::r_brace);
+      if ((EmptyFunctionBody && !Style.BraceWrapping.SplitEmptyFunction) ||
+          (ShortFunctionBody &&
+           Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Always)) {
+        return tryMergeSimpleBlock(I, E, Limit);
+      }
     }
 
     const auto *PreviousLine = I != AnnotatedLines.begin() ? I[-1] : nullptr;
diff --git a/clang/unittests/Format/FormatTest.cpp 
b/clang/unittests/Format/FormatTest.cpp
index 8bc4ba53c3e1a..0f11f1dd33737 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -14996,6 +14996,29 @@ TEST_F(FormatTest, SplitEmptyFunctionButNotRecord) {
                Style);
 }
 
+TEST_F(FormatTest, SplitShortFunction) {
+  FormatStyle Style = getLLVMStyleWithColumns(40);
+  Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None;
+  Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always;
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterFunction = true;
+
+  verifyFormat("int foo()\n"
+               "{ return 1; }",
+               Style);
+  verifyFormat("int foo()\n"
+               "{\n"
+               "  // comment\n"
+               "  return 1;\n"
+               "}",
+               Style);
+  verifyFormat("int foo()\n"
+               "{\n"
+               "  return 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1;\n"
+               "}",
+               Style);
+}
+
 TEST_F(FormatTest, KeepShortFunctionAfterPPElse) {
   FormatStyle Style = getLLVMStyle();
   Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to