]> granicus.if.org Git - clang/commitdiff
[clang-format] Wrapped block after case label should not be merged into one line
authorOwen Pan <owenpiano@gmail.com>
Thu, 13 Sep 2018 07:27:15 +0000 (07:27 +0000)
committerOwen Pan <owenpiano@gmail.com>
Thu, 13 Sep 2018 07:27:15 +0000 (07:27 +0000)
PR38854

Differential Revision: http://reviews.llvm.org/D51719

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

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

index f0d93a1f4f3b945c490599a8bdb95bc59fe25c8b..43d71504cac30cee586b1c071473ef958754d02c 100644 (file)
@@ -323,6 +323,10 @@ private:
           kwId == clang::tok::objc_synchronized)
         return 0;
     }
+    // Don't merge block with left brace wrapped after case labels
+    if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
+        I[-1]->First->isOneOf(tok::kw_case, tok::kw_default))
+      return 0;
     // Try to merge a block with left brace wrapped that wasn't yet covered
     if (TheLine->Last->is(tok::l_brace)) {
       return !Style.BraceWrapping.AfterFunction ||
index e7c467547a12a7e1f16bf13e432dd962116613ff..70bc9e0ed73b6908be78bf80883bef0012719fed 100644 (file)
@@ -1064,6 +1064,32 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
                "  return;\n"
                "}",
                getLLVMStyleWithColumns(34));
+
+  FormatStyle Style = getLLVMStyle();
+  Style.IndentCaseLabels = true;
+  Style.AllowShortBlocksOnASingleLine = false;
+  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, CaseRanges) {