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

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 13:58:42 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Vedaant Srivastava (VedaantS)

<details>
<summary>Changes</summary>

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

---
Full diff: https://github.com/llvm/llvm-project/pull/240043.diff


2 Files Affected:

- (modified) clang/lib/Format/ContinuationIndenter.cpp (+5-1) 
- (modified) clang/unittests/Format/FormatTest.cpp (+36) 


``````````diff
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"

``````````

</details>


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


More information about the cfe-commits mailing list