]> granicus.if.org Git - clang/commitdiff
Fix Bug 38713: clang-format mishandles a short block after "default:" in a switch...
authorJonas Toth <jonas.toth@gmail.com>
Sun, 2 Sep 2018 09:04:51 +0000 (09:04 +0000)
committerJonas Toth <jonas.toth@gmail.com>
Sun, 2 Sep 2018 09:04:51 +0000 (09:04 +0000)
Summary:
See https://bugs.llvm.org/show_bug.cgi?id=38713

Patch by Owen Pan!

Reviewers: djasper, klimek, sammccall

Reviewed By: sammccall

Subscribers: cfe-commits

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

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

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

index 906dae40cbee7516f2edde6d37e20b214be654e5..1b7daf67d90bb0107cd50445802603f9ab736190 100644 (file)
@@ -483,6 +483,12 @@ private:
     if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
         (Line.First->Next && Line.First->Next->is(tok::kw_else)))
       return 0;
+    // default: in switch statement
+    if (Line.First->is(tok::kw_default)) {
+      const FormatToken *Tok = Line.First->getNextNonComment();
+      if (Tok && Tok->is(tok::colon))
+        return 0;
+    }
     if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
                             tok::kw___try, tok::kw_catch, tok::kw___finally,
                             tok::kw_for, tok::r_brace, Keywords.kw___except)) {
index a603012ccf42493b545bf39c6504fcc329603c44..3dd9fc00f68f42b174088ba68829077fefd5c61a 100644 (file)
@@ -999,6 +999,24 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
                    "  }\n"
                    "});",
                    getLLVMStyle()));
+  EXPECT_EQ("switch (n) {\n"
+            "case 0: {\n"
+            "  return false;\n"
+            "}\n"
+            "default: {\n"
+            "  return true;\n"
+            "}\n"
+            "}",
+            format("switch (n)\n"
+                   "{\n"
+                   "case 0: {\n"
+                   "  return false;\n"
+                   "}\n"
+                   "default: {\n"
+                   "  return true;\n"
+                   "}\n"
+                   "}",
+                   getLLVMStyle()));
   verifyFormat("switch (a) {\n"
                "case (b):\n"
                "  return;\n"