]> granicus.if.org Git - clang/commitdiff
clang-format: Merge labels and subsequent semicolons.
authorDaniel Jasper <djasper@google.com>
Wed, 6 May 2015 15:19:47 +0000 (15:19 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 6 May 2015 15:19:47 +0000 (15:19 +0000)
E.g.:

  default:;

This can be used to get around restrictions as to what can follow a
label. It fixes llvm.org/PR19648.

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

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

index 793746137a515b9c59bfcdb0a70581e8aeccf709..9f034eca3798a7b4e2c33bb72d4a27d0a16fa847 100644 (file)
@@ -1896,7 +1896,7 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
     return false;
   if (Right.is(tok::colon)) {
     if (Line.First->isOneOf(tok::kw_case, tok::kw_default) ||
-        !Right.getNextNonComment())
+        !Right.getNextNonComment() || Right.getNextNonComment()->is(tok::semi))
       return false;
     if (Right.is(TT_ObjCMethodExpr))
       return false;
index 9461911648175e3b54ea406bfeac4d66d4c09635..117e83802c877ff8eb88721547a1671498a52c2f 100644 (file)
@@ -1381,6 +1381,8 @@ void UnwrappedLineParser::parseLabel() {
     }
     addUnwrappedLine();
   } else {
+    if (FormatTok->is(tok::semi))
+      nextToken();
     addUnwrappedLine();
   }
   Line->Level = OldLineLevel;
index b4c8c26664ac5eb8a1a24a3ee535a72a32f59130..000f87ce40cdc60839a9a19632a64fba31be0c6f 100644 (file)
@@ -692,6 +692,11 @@ TEST_F(FormatTest, FormatsSwitchStatement) {
                "  case OP_name:                        \\\n"
                "    return operations::Operation##name\n",
                getLLVMStyleWithColumns(40));
+  verifyFormat("switch (x) {\n"
+               "case 1:;\n"
+               "default:;\n"
+               "  int i;\n"
+               "}");
 
   verifyGoogleFormat("switch (x) {\n"
                      "  case 1:\n"
@@ -827,6 +832,11 @@ TEST_F(FormatTest, FormatsLabels) {
                "test_label:\n"
                "  some_other_code();\n"
                "}");
+  verifyFormat("{\n"
+               "  some_code();\n"
+               "test_label:;\n"
+               "  int i = 0;\n"
+               "}");
 }
 
 //===----------------------------------------------------------------------===//