r340624 - [clang-format] fix PR38557 - comments between "default" and ':' causes the case label to be treated as an identifier

Jonas Toth via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 24 10:25:07 PDT 2018


Author: jonastoth
Date: Fri Aug 24 10:25:06 2018
New Revision: 340624

URL: http://llvm.org/viewvc/llvm-project?rev=340624&view=rev
Log:
[clang-format] fix PR38557 - comments between "default" and ':' causes the case label to be treated as an identifier

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

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=340624&r1=340623&r2=340624&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Fri Aug 24 10:25:06 2018
@@ -350,7 +350,10 @@ void UnwrappedLineParser::parseLevel(boo
       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

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=340624&r1=340623&r2=340624&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Aug 24 10:25:06 2018
@@ -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) {




More information about the cfe-commits mailing list