]> granicus.if.org Git - clang/commitdiff
[clang-format] fix PR38557 - comments between "default" and ':' causes the case label...
authorJonas Toth <jonas.toth@gmail.com>
Fri, 24 Aug 2018 17:25:06 +0000 (17:25 +0000)
committerJonas Toth <jonas.toth@gmail.com>
Fri, 24 Aug 2018 17:25:06 +0000 (17:25 +0000)
Summary:
The Bug was reported and fixed by Owen Pan. See the original bug report here: https://bugs.llvm.org/show_bug.cgi?id=38557

Patch by Owen Pan!

Reviewers: krasimir, djasper, klimek

Reviewed By: klimek

Subscribers: JonasToth, cfe-commits

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

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

lib/Format/UnwrappedLineParser.cpp
unittests/Format/FormatTest.cpp

index e5afa1264abb5f5870ed7da4a77e224e224ce0b2..07109daf6f9526eabc29851349c5d564832415fe 100644 (file)
@@ -350,7 +350,10 @@ void UnwrappedLineParser::parseLevel(bool HasOpeningBrace) {
       break;
     case tok::kw_default: {
       unsigned StoredPosition = Tokens->getPosition();
-      FormatToken *Next = Tokens->getNextToken();
+      FormatToken *Next;
+      do {
+        Next = Tokens->getNextToken();
+      } while (Next && Next->is(tok::comment));
       FormatTok = Tokens->setPosition(StoredPosition);
       if (Next && Next->isNot(tok::colon)) {
         // default not followed by ':' is not a case label; treat it like
index d73b1d82671854ac9ae14e62525056c70b9acc49..a603012ccf42493b545bf39c6504fcc329603c44 100644 (file)
@@ -1145,6 +1145,22 @@ TEST_F(FormatTest, ShortCaseLabels) {
                "  break;\n"
                "}",
                Style);
+  Style.ColumnLimit = 80;
+  Style.AllowShortCaseLabelsOnASingleLine = false;
+  Style.IndentCaseLabels = true;
+  EXPECT_EQ("switch (n) {\n"
+            "  default /*comments*/:\n"
+            "    return true;\n"
+            "  case 0:\n"
+            "    return false;\n"
+            "}",
+            format("switch (n) {\n"
+                   "default/*comments*/:\n"
+                   "  return true;\n"
+                   "case 0:\n"
+                   "  return false;\n"
+                   "}",
+                   Style));
 }
 
 TEST_F(FormatTest, FormatsLabels) {