]> granicus.if.org Git - clang/commitdiff
[clang-format] Do not merge short case labels if followed by a block.
authorOwen Pan <owenpiano@gmail.com>
Fri, 21 Sep 2018 03:46:36 +0000 (03:46 +0000)
committerOwen Pan <owenpiano@gmail.com>
Fri, 21 Sep 2018 03:46:36 +0000 (03:46 +0000)
Do not allow short case labels on a single line if the label is followed by a
left brace.

Fixes PR38926.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@342708 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 43d71504cac30cee586b1c071473ef958754d02c..76b495f453febc1d42b25f42487802f809af8bde 100644 (file)
@@ -428,6 +428,8 @@ private:
     if (Limit == 0 || I + 1 == E ||
         I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
       return 0;
+    if (I[0]->Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
+      return 0;
     unsigned NumStmts = 0;
     unsigned Length = 0;
     bool EndsWithComment = false;
index db2e226a30efa804ee1de6daec203216d727021e..dac8497c1e6aa9bd1758a776f523dd6ac9f064d2 100644 (file)
@@ -1241,6 +1241,30 @@ TEST_F(FormatTest, ShortCaseLabels) {
                    "  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"
+                   "  {\n"
+                   "    return true;\n"
+                   "  }\n"
+                   "}",
+                   Style));
 }
 
 TEST_F(FormatTest, FormatsLabels) {