From: Daniel Jasper Date: Wed, 6 May 2015 15:19:47 +0000 (+0000) Subject: clang-format: Merge labels and subsequent semicolons. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4ec9a3892c8625d1920444b23270a1f0bcb30363;p=clang clang-format: Merge labels and subsequent semicolons. 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 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 793746137a..9f034eca37 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -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; diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 9461911648..117e83802c 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -1381,6 +1381,8 @@ void UnwrappedLineParser::parseLabel() { } addUnwrappedLine(); } else { + if (FormatTok->is(tok::semi)) + nextToken(); addUnwrappedLine(); } Line->Level = OldLineLevel; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index b4c8c26664..000f87ce40 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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" + "}"); } //===----------------------------------------------------------------------===//