Author: Vedaant Srivastava
Date: 2026-11-20T20:59:30+01:00
New Revision: 301c651c8e33e000fdb97610844217389d5f842e

URL: 
https://github.com/llvm/llvm-project/commit/301c651c8e33e000fdb97610844217389d5f842e
DIFF: 
https://github.com/llvm/llvm-project/commit/301c651c8e33e000fdb97610844217389d5f842e.diff

LOG: [clang-format] Fix single-line enum wrapping with ColumnLimit: 0 (#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

Tool disclosure: The patch and unit tests were authored manually; Google
Antigravity was used to review the diff, run local tests, and write this
commit message.

Added: 
    

Modified: 
    clang/lib/Format/ContinuationIndenter.cpp
    clang/unittests/Format/FormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/ContinuationIndenter.cpp 
b/clang/lib/Format/ContinuationIndenter.cpp
index d36e663d579a0..2f96ad06385cf 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -2406,7 +2406,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 944fed72c3c5c..be4936e9a8d95 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -3007,6 +3007,18 @@ TEST_F(FormatTest, ShortEnums) {
                Style);
 }
 
+TEST_F(FormatTest, ShortEnumsWithoutColumnLimit) {
+  FormatStyle Style = getLLVMStyle();
+  Style.ColumnLimit = 0;
+  verifyNoChange("enum { A, B, C };", Style);
+  verifyNoChange("enum {\n"
+                 "  A,\n"
+                 "  B,\n"
+                 "  C\n"
+                 "};",
+                 Style);
+}
+
 TEST_F(FormatTest, ShortCompoundRequirement) {
   constexpr StringRef Code("template <typename T>\n"
                            "concept c = requires(T x) {\n"


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to