[clang] 8ea64d5 - [clang-format] Fix short enums getting wrapped even when denied

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 24 11:48:32 PST 2021


Author: Gabriel Smith
Date: 2021-12-24T11:38:55-08:00
New Revision: 8ea64d5585ec3a0a52db20c9e57ac9bed9e80fc2

URL: https://github.com/llvm/llvm-project/commit/8ea64d5585ec3a0a52db20c9e57ac9bed9e80fc2
DIFF: https://github.com/llvm/llvm-project/commit/8ea64d5585ec3a0a52db20c9e57ac9bed9e80fc2.diff

LOG: [clang-format] Fix short enums getting wrapped even when denied

Single-variant enums were still getting placed on a single line
even when AllowShortEnumsOnASingleLine was false. This fixes that
by checking that setting when looking to merge lines.

Differential Revision: https://reviews.llvm.org/D116188

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 3d4c1a4f903b2..f652a4e7088f1 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,18 @@ class LineJoiner {
 
     // Try to merge a block with left brace wrapped that wasn't yet covered
     if (TheLine->Last->is(tok::l_brace)) {
+      const FormatToken *Tok = TheLine->First;
       bool ShouldMerge = false;
-      if (TheLine->First->isOneOf(tok::kw_class, tok::kw_struct)) {
+      if (Tok->is(tok::kw_typedef)) {
+        Tok = Tok->getNextNonComment();
+        assert(Tok);
+      }
+      if (Tok->isOneOf(tok::kw_class, tok::kw_struct)) {
         ShouldMerge = !Style.BraceWrapping.AfterClass ||
                       (I[1]->First->is(tok::r_brace) &&
                        !Style.BraceWrapping.SplitEmptyRecord);
+      } else if (Tok->is(tok::kw_enum)) {
+        ShouldMerge = Style.AllowShortEnumsOnASingleLine;
       } else {
         ShouldMerge = !Style.BraceWrapping.AfterFunction ||
                       (I[1]->First->is(tok::r_brace) &&

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index ee486f4521949..374f3865acc3b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -2504,6 +2504,7 @@ TEST_F(FormatTest, ShortEnums) {
   FormatStyle Style = getLLVMStyle();
   Style.AllowShortEnumsOnASingleLine = true;
   verifyFormat("enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
+  verifyFormat("typedef enum { A, B, C } ShortEnum1, ShortEnum2;", Style);
   Style.AllowShortEnumsOnASingleLine = false;
   verifyFormat("enum {\n"
                "  A,\n"
@@ -2511,6 +2512,20 @@ TEST_F(FormatTest, ShortEnums) {
                "  C\n"
                "} ShortEnum1, ShortEnum2;",
                Style);
+  verifyFormat("typedef enum {\n"
+               "  A,\n"
+               "  B,\n"
+               "  C\n"
+               "} ShortEnum1, ShortEnum2;",
+               Style);
+  verifyFormat("enum {\n"
+               "  A,\n"
+               "} ShortEnum1, ShortEnum2;",
+               Style);
+  verifyFormat("typedef enum {\n"
+               "  A,\n"
+               "} ShortEnum1, ShortEnum2;",
+               Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"


        


More information about the cfe-commits mailing list