[clang] [clang-format] Fix single-line enum wrapping with ColumnLimit: 0 (PR #240043)

Vedaant Srivastava via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 13:57:53 PST 2026


https://github.com/VedaantS created https://github.com/llvm/llvm-project/pull/240043

Fixes #239166

When ColumnLimit is 0 and AllowShortEnumsOnASingleLine is true, ContinuationIndenter::moveStateToNewBlock unconditionally set BreakBeforeParameter to true upon entering the enum block. Because startsNextParameter() returns true after each enumerator comma, mustBreak() forced each enumerator after the first onto a separate line.

Set BreakBeforeParameter to false for TT_EnumLBrace when ColumnLimit is 0 and AllowShortEnumsOnASingleLine is true, preserving single-line enums.

Fixes #239166

>From 94dd5d38dd863d3ec6ff49564824e7e2eedc109a Mon Sep 17 00:00:00 2001
From: Vedaant Srivastava <vedaantsriv at gmail.com>
Date: Sat, 7 Nov 2026 14:53:15 -0500
Subject: [PATCH] [clang-format] Fix single-line enum wrapping with
 ColumnLimit: 0

Fixes #239166

When ColumnLimit is 0 and AllowShortEnumsOnASingleLine is true,
ContinuationIndenter::moveStateToNewBlock unconditionally set
BreakBeforeParameter to true upon entering the enum block.
Because startsNextParameter() returns true after each enumerator comma,
mustBreak() forced each enumerator after the first onto a separate line.

Set BreakBeforeParameter to false for TT_EnumLBrace when ColumnLimit is 0
and AllowShortEnumsOnASingleLine is true, preserving single-line enums.
---
 clang/lib/Format/ContinuationIndenter.cpp |  6 +++-
 clang/unittests/Format/FormatTest.cpp     | 36 +++++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 384bad64923150..abf222dd0d0546 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -2372,7 +2372,11 @@ void ContinuationIndenter::moveStateToNewBlock(LineState &State, bool NewLine) {
                                    State.Stack.back().LastSpace,
                                    /*AvoidBinPacking=*/true, NoLineBreak));
   State.Stack.back().NestedBlockIndent = NestedBlockIndent;
-  State.Stack.back().BreakBeforeParameter = true;
+  // Preserve the input's line breaks in short enums with no column limit
+  // instead of forcing each enumerator onto its own line.
+  State.Stack.back().BreakBeforeParameter =
+      Style.ColumnLimit != 0 || !Style.AllowShortEnumsOnASingleLine ||
+      State.NextToken->isNot(TT_EnumLBrace);
 }
 
 static unsigned getLastLineEndColumn(StringRef Text, unsigned StartColumn,
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index da1587641311bc..95668f1ab0e5e5 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3007,6 +3007,42 @@ TEST_F(FormatTest, ShortEnums) {
                Style);
 }
 
+TEST_F(FormatTest, ShortEnumsWithoutColumnLimit) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 0;
+  verifyNoChange("enum { A, B, C };", Style);
+  verifyNoChange("enum E { A, B, C };", Style);
+  verifyNoChange("enum class E { A = 1, B = 2, C = 3 };", Style);
+  verifyNoChange("typedef enum { A, B, C } E;", Style);
+  verifyNoChange("enum E {\n"
+                 "  A,\n"
+                 "  B,\n"
+                 "  C\n"
+                 "};",
+                 Style);
+  verifyNoChange("enum E {\n"
+                 "  A, // Keep the comment.\n"
+                 "  B,\n"
+                 "  C\n"
+                 "};",
+                 Style);
+  Style.AllowShortEnumsOnASingleLine = false;
+  verifyFormat("enum E {\n"
+               "  A,\n"
+               "  B,\n"
+               "  C\n"
+               "};",
+               "enum E { A, B, C };", Style);
+  Style.AllowShortEnumsOnASingleLine = true;
+  Style.IndentWidth = 4;
+  Style.BreakBeforeBraces = FormatStyle::BS_Linux;
+  verifyNoChange("int main()\n"
+                 "{\n"
+                 "    enum { WINDOW_WIDTH = 1080, WINDOW_HEIGHT = 720 };\n"
+                 "}",
+                 Style);
+}
+
 TEST_F(FormatTest, ShortCompoundRequirement) {
   constexpr StringRef Code("template <typename T>\n"
                            "concept c = requires(T x) {\n"



More information about the cfe-commits mailing list