[clang] [clang-format] Honor AllowShortFunctionsOnASingleLine.Other w/o Empty (PR #235369)

Theia Dobyns via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 8 12:33:37 PST 2026


https://github.com/theia-ajax updated https://github.com/llvm/llvm-project/pull/235369

>From dcf267f2c5e4b1767539941145c1963e07f471d2 Mon Sep 17 00:00:00 2001
From: Theia Dobyns <theia.ajax at gmail.com>
Date: Sat, 24 Oct 2026 16:07:52 -0700
Subject: [PATCH 1/3] [clang-format] Honor
 AllowShortFunctionsOnASingleLine.Other w/o Empty

Other only took effect through isAll(), so {Empty: false, Other: true}
merged no functions. Merge non-empty functions whenever Other is set and
leave empty ones to Empty and Inline. Share one empty-body check between
Empty and Other, covering both wrapped and unwrapped braces.

Fixes #206144.
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 25 +++++++-----
 clang/unittests/Format/FormatTest.cpp       | 43 +++++++++++++++++++++
 2 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index d1f68877ef0339..84ad693a63f4f7 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -337,17 +337,27 @@ class LineJoiner {
       }
     }
 
+    // The line after the left brace of the function, which may be wrapped
+    // onto its own line.
+    const bool LBraceWrapped = NextLine.First->is(TT_FunctionLBrace) &&
+                               NextLine.First == NextLine.Last;
+    const AnnotatedLine *BodyLine =
+        LBraceWrapped ? (I + 2 != E ? I[2] : nullptr) : &NextLine;
+    const bool IsEmptyFunction = BodyLine && BodyLine->First->is(tok::r_brace);
+
     auto ShouldMergeShortFunctions = [&] {
       if (Style.AllowShortFunctionsOnASingleLine.isAll())
         return true;
 
-      if (Style.AllowShortFunctionsOnASingleLine.Empty &&
-          NextLine.First->is(tok::r_brace)) {
+      if (Style.AllowShortFunctionsOnASingleLine.Empty && IsEmptyFunction)
+        return true;
+
+      // Other covers only non-empty functions. Empty functions are left to
+      // Empty and Inline.
+      if (Style.AllowShortFunctionsOnASingleLine.Other && !IsEmptyFunction)
         return true;
-      }
 
-      if (Style.AllowShortFunctionsOnASingleLine.Inline &&
-          !Style.AllowShortFunctionsOnASingleLine.Other) {
+      if (Style.AllowShortFunctionsOnASingleLine.Inline) {
         if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
           return true;
 
@@ -574,10 +584,7 @@ class LineJoiner {
       Limit -= 2;
 
       unsigned MergedLines = 0;
-      if (MergeShortFunctions ||
-          (Style.AllowShortFunctionsOnASingleLine.Empty &&
-           NextLine.First == NextLine.Last && I + 2 != E &&
-           I[2]->First->is(tok::r_brace))) {
+      if (MergeShortFunctions) {
         MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
         // If we managed to merge the block, count the function header, which is
         // on a separate line.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index f38287757ffba1..ef367116acd829 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15605,6 +15605,49 @@ TEST_F(FormatTest, CustomShortFunctionOptions) {
                "  return 42;\n"
                "}",
                CustomMixed);
+
+  // Test with Other = true, Empty = false
+  FormatStyle CustomOther = getLLVMStyle();
+  CustomOther.AllowShortFunctionsOnASingleLine = {/*Empty=*/false,
+                                                  /*Inline=*/false,
+                                                  /*Other=*/true};
+
+  // Non-empty functions should be on a single line
+  verifyFormat("int f() { return 42; }", CustomOther);
+  verifyFormat("class C {\n"
+               "  int f() { return 42; }\n"
+               "};",
+               CustomOther);
+
+  // Empty functions should be multi-line
+  verifyFormat("int f() {\n"
+               "}",
+               CustomOther);
+  verifyFormat("class C {\n"
+               "  int f() {\n"
+               "  }\n"
+               "};",
+               CustomOther);
+
+  // Test with AfterFunction = true
+  CustomOther.BreakBeforeBraces = FormatStyle::BS_Custom;
+  CustomOther.BraceWrapping.AfterFunction = true;
+  verifyFormat("int f() { return 42; }", CustomOther);
+  verifyFormat("int f()\n"
+               "{\n"
+               "}",
+               CustomOther);
+
+  // Inline = true still merges empty functions in a class
+  CustomOther.AllowShortFunctionsOnASingleLine.Inline = true;
+  verifyFormat("class C {\n"
+               "  int f() {}\n"
+               "};",
+               CustomOther);
+  verifyFormat("int f()\n"
+               "{\n"
+               "}",
+               CustomOther);
 }
 
 TEST_F(FormatTest, PullInlineOnlyFunctionDefinitionsIntoSingleLine) {

>From 62ba6f1d3d6a31bb5f576d63c452a9214de607e3 Mon Sep 17 00:00:00 2001
From: Theia Dobyns <theia.ajax at gmail.com>
Date: Sat, 24 Oct 2026 16:27:19 -0700
Subject: [PATCH 2/3] Simplify the empty-body check

---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 84ad693a63f4f7..1d46ccd0b82485 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -337,13 +337,13 @@ class LineJoiner {
       }
     }
 
-    // The line after the left brace of the function, which may be wrapped
-    // onto its own line.
+    // Whether the function body is empty, checking the line after the left
+    // brace, which may be wrapped onto its own line.
     const bool LBraceWrapped = NextLine.First->is(TT_FunctionLBrace) &&
                                NextLine.First == NextLine.Last;
-    const AnnotatedLine *BodyLine =
-        LBraceWrapped ? (I + 2 != E ? I[2] : nullptr) : &NextLine;
-    const bool IsEmptyFunction = BodyLine && BodyLine->First->is(tok::r_brace);
+    const bool IsEmptyFunction =
+        LBraceWrapped ? I + 2 != E && I[2]->First->is(tok::r_brace)
+                      : NextLine.First->is(tok::r_brace);
 
     auto ShouldMergeShortFunctions = [&] {
       if (Style.AllowShortFunctionsOnASingleLine.isAll())

>From 181c9d3780703dc92fbe67eeee626cee7bdaa7df Mon Sep 17 00:00:00 2001
From: Theia Dobyns <theia.ajax at gmail.com>
Date: Sun, 8 Nov 2026 12:33:13 -0800
Subject: [PATCH 3/3] Leave functions in a class to Inline, not Other

Move the empty-body check into ShouldMergeShortFunctions.
Add tests for SplitEmptyFunction with Other.
---
 clang/lib/Format/UnwrappedLineFormatter.cpp | 91 ++++++++++-----------
 clang/unittests/Format/FormatTest.cpp       | 33 ++++++--
 2 files changed, 72 insertions(+), 52 deletions(-)

diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 1d46ccd0b82485..ae42349ea54aff 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -337,67 +337,66 @@ class LineJoiner {
       }
     }
 
-    // Whether the function body is empty, checking the line after the left
-    // brace, which may be wrapped onto its own line.
-    const bool LBraceWrapped = NextLine.First->is(TT_FunctionLBrace) &&
-                               NextLine.First == NextLine.Last;
-    const bool IsEmptyFunction =
-        LBraceWrapped ? I + 2 != E && I[2]->First->is(tok::r_brace)
-                      : NextLine.First->is(tok::r_brace);
-
     auto ShouldMergeShortFunctions = [&] {
       if (Style.AllowShortFunctionsOnASingleLine.isAll())
         return true;
 
+      // Whether the function body is empty, checking the line after the left
+      // brace, which may be wrapped onto its own line.
+      const bool LBraceWrapped = NextLine.First->is(TT_FunctionLBrace) &&
+                                 NextLine.First == NextLine.Last;
+      const bool IsEmptyFunction =
+          LBraceWrapped ? I + 2 != E && I[2]->First->is(tok::r_brace)
+                        : NextLine.First->is(tok::r_brace);
+
       if (Style.AllowShortFunctionsOnASingleLine.Empty && IsEmptyFunction)
         return true;
 
-      // Other covers only non-empty functions. Empty functions are left to
-      // Empty and Inline.
-      if (Style.AllowShortFunctionsOnASingleLine.Other && !IsEmptyFunction)
+      if (Style.AllowShortFunctionsOnASingleLine.Inline &&
+          Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace)) {
         return true;
+      }
 
-      if (Style.AllowShortFunctionsOnASingleLine.Inline) {
-        if (Style.isJavaScript() && TheLine->Last->is(TT_FunctionLBrace))
-          return true;
-
+      auto IsInRecord = [&] {
         // Just checking `TheLine->Level > 0` is not enough because it would
         // cause functions inside indented namespaces to be treated as short.
-        if (const auto Level = TheLine->Level; Level > 0) {
-          if (!PreviousLine)
-            return false;
-
-          // TODO: Use IndentTracker to avoid loop?
-          // Find the last line with lower level.
-          const AnnotatedLine *Line = nullptr;
-          for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
-            const auto *L = *J;
-            assert(L);
-            if (TheLine->InMacroBody && !L->InMacroBody)
-              break;
-            if (L->isComment() || (!TheLine->InPPDirective && L->InPPDirective))
-              continue;
-            if (L->Level < Level ||
-                (L->Level == Level && L->First->is(tok::l_brace) &&
-                 Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)) {
-              Line = L;
-              break;
-            }
+        const auto Level = TheLine->Level;
+        if (Level == 0 || !PreviousLine)
+          return false;
+
+        // TODO: Use IndentTracker to avoid loop?
+        // Find the last line with lower level.
+        const AnnotatedLine *Line = nullptr;
+        for (auto J = I - 1; J >= AnnotatedLines.begin(); --J) {
+          const auto *L = *J;
+          assert(L);
+          if (TheLine->InMacroBody && !L->InMacroBody)
+            break;
+          if (L->isComment() || (!TheLine->InPPDirective && L->InPPDirective))
+            continue;
+          if (L->Level < Level ||
+              (L->Level == Level && L->First->is(tok::l_brace) &&
+               Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths)) {
+            Line = L;
+            break;
           }
+        }
 
-          if (!Line)
-            return false;
+        if (!Line)
+          return false;
 
-          // Check if the found line starts a record.
-          const auto *LastNonComment = Line->getLastNonComment();
-          // There must be another token (usually `{`), because we chose a
-          // non-PPDirective and non-comment line that has a smaller level.
-          assert(LastNonComment);
-          return isRecordLBrace(*LastNonComment);
-        }
-      }
+        // Check if the found line starts a record.
+        const auto *LastNonComment = Line->getLastNonComment();
+        // There must be another token (usually `{`), because we chose a
+        // non-PPDirective and non-comment line that has a smaller level.
+        assert(LastNonComment);
+        return isRecordLBrace(*LastNonComment);
+      };
 
-      return false;
+      if (IsInRecord())
+        return Style.AllowShortFunctionsOnASingleLine.Inline;
+
+      return Style.AllowShortFunctionsOnASingleLine.Other && !IsEmptyFunction;
     };
 
     bool MergeShortFunctions = ShouldMergeShortFunctions();
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ef367116acd829..9cd14d3e6ca0e2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -15606,20 +15606,22 @@ TEST_F(FormatTest, CustomShortFunctionOptions) {
                "}",
                CustomMixed);
 
-  // Test with Other = true, Empty = false
+  // Test with Other = true, Empty = false.
   FormatStyle CustomOther = getLLVMStyle();
   CustomOther.AllowShortFunctionsOnASingleLine = {/*Empty=*/false,
                                                   /*Inline=*/false,
                                                   /*Other=*/true};
 
-  // Non-empty functions should be on a single line
+  // Non-empty functions are merged, except those defined in a class.
   verifyFormat("int f() { return 42; }", CustomOther);
   verifyFormat("class C {\n"
-               "  int f() { return 42; }\n"
+               "  int f() {\n"
+               "    return 42;\n"
+               "  }\n"
                "};",
                CustomOther);
 
-  // Empty functions should be multi-line
+  // Empty functions are not merged.
   verifyFormat("int f() {\n"
                "}",
                CustomOther);
@@ -15629,7 +15631,7 @@ TEST_F(FormatTest, CustomShortFunctionOptions) {
                "};",
                CustomOther);
 
-  // Test with AfterFunction = true
+  // Test with AfterFunction = true.
   CustomOther.BreakBeforeBraces = FormatStyle::BS_Custom;
   CustomOther.BraceWrapping.AfterFunction = true;
   verifyFormat("int f() { return 42; }", CustomOther);
@@ -15638,12 +15640,31 @@ TEST_F(FormatTest, CustomShortFunctionOptions) {
                "}",
                CustomOther);
 
-  // Inline = true still merges empty functions in a class
+  // Test with SplitEmptyFunction = false.
+  CustomOther.BraceWrapping.SplitEmptyFunction = false;
+  verifyFormat("int f() { return 42; }", CustomOther);
+  verifyFormat("int f()\n"
+               "{}",
+               CustomOther);
+  verifyFormat("class C {\n"
+               "  int f()\n"
+               "  {}\n"
+               "};",
+               CustomOther);
+  CustomOther.AllowShortFunctionsOnASingleLine.Empty = true;
+  verifyFormat("int f() {}", CustomOther);
+  CustomOther.AllowShortFunctionsOnASingleLine.Empty = false;
+  CustomOther.BraceWrapping.SplitEmptyFunction = true;
+
   CustomOther.AllowShortFunctionsOnASingleLine.Inline = true;
   verifyFormat("class C {\n"
                "  int f() {}\n"
                "};",
                CustomOther);
+  verifyFormat("class C {\n"
+               "  int f() { return 42; }\n"
+               "};",
+               CustomOther);
   verifyFormat("int f()\n"
                "{\n"
                "}",



More information about the cfe-commits mailing list