[PATCH] D52021: Fix Bug 38926: don't merge short case labels if followed by a block

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 13 03:35:32 PDT 2018


owenpan created this revision.
owenpan added reviewers: djasper, klimek, sammccall, krasimir.
Herald added a subscriber: cfe-commits.

https://bugs.llvm.org/show_bug.cgi?id=38926


Repository:
  rC Clang

https://reviews.llvm.org/D52021

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


Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -1241,6 +1241,29 @@
                    "  return false;\n"
                    "}",
                    Style));
+  Style.AllowShortCaseLabelsOnASingleLine = true;
+  Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+  Style.BraceWrapping.AfterControlStatement = true;
+  EXPECT_EQ("switch (n)\n"
+            "{\n"
+            "  case 0:\n"
+            "  {\n"
+            "    return false;\n"
+            "  }\n"
+            "  default:\n"
+            "  {\n"
+            "    return true;\n"
+            "  }\n"
+            "}",
+            format("switch (n) {\n"
+                   "  case 0: {\n"
+                   "    return false;\n"
+                   "  }\n"
+                   "  default: {\n"
+                   "    return true;\n"
+                   "  }\n"
+                   "}",
+                   Style));
 }
 
 TEST_F(FormatTest, FormatsLabels) {
Index: lib/Format/UnwrappedLineFormatter.cpp
===================================================================
--- lib/Format/UnwrappedLineFormatter.cpp
+++ lib/Format/UnwrappedLineFormatter.cpp
@@ -428,6 +428,14 @@
     if (Limit == 0 || I + 1 == E ||
         I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
       return 0;
+
+    // Don't merge if a block follows the case label
+    FormatToken *Tok = I[0]->Last;
+    if (Tok->is(tok::comment))
+      Tok = Tok->getPreviousNonComment();
+    if ((Tok && Tok->is(tok::l_brace)) || I[1]->First->is(tok::l_brace))
+      return 0;
+
     unsigned NumStmts = 0;
     unsigned Length = 0;
     bool EndsWithComment = false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52021.165242.patch
Type: text/x-patch
Size: 1754 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180913/053efc28/attachment.bin>


More information about the cfe-commits mailing list