yodaldevoid created this revision.
yodaldevoid added reviewers: HazardyKnusperkeks, MyDeveloperDay, curdeius, 
owenpan.
yodaldevoid requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116188

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2511,6 +2511,10 @@
                "  C\n"
                "} ShortEnum1, ShortEnum2;",
                Style);
+  verifyFormat("enum {\n"
+               "  A,\n"
+               "} ShortEnum1, ShortEnum2;",
+               Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,16 @@
 
     // 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 && Tok->is(tok::kw_typedef))
+        Tok = Tok->getNextNonComment();
+      if (Tok && 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 && Tok->is(tok::kw_enum)) {
+        ShouldMerge = Style.AllowShortEnumsOnASingleLine;
       } else {
         ShouldMerge = !Style.BraceWrapping.AfterFunction ||
                       (I[1]->First->is(tok::r_brace) &&


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -2511,6 +2511,10 @@
                "  C\n"
                "} ShortEnum1, ShortEnum2;",
                Style);
+  verifyFormat("enum {\n"
+               "  A,\n"
+               "} ShortEnum1, ShortEnum2;",
+               Style);
   Style.BreakBeforeBraces = FormatStyle::BS_Custom;
   Style.BraceWrapping.AfterEnum = true;
   verifyFormat("enum\n"
Index: clang/lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineFormatter.cpp
+++ clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -393,11 +393,16 @@
 
     // 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 && Tok->is(tok::kw_typedef))
+        Tok = Tok->getNextNonComment();
+      if (Tok && 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 && Tok->is(tok::kw_enum)) {
+        ShouldMerge = Style.AllowShortEnumsOnASingleLine;
       } else {
         ShouldMerge = !Style.BraceWrapping.AfterFunction ||
                       (I[1]->First->is(tok::r_brace) &&
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to