From: Daniel Jasper Date: Tue, 7 Apr 2015 14:36:33 +0000 (+0000) Subject: clang-format: Don't allow labels when expecting declarations. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bcea7de374d27e5560671e2f99b390c793878a75;p=clang clang-format: Don't allow labels when expecting declarations. This fixes formatting unnamed bitfields (llvm.org/PR21999). Before: struct MyStruct { uchar data; uchar: 8; uchar: 8; uchar other; }; After: struct MyStruct { uchar data; uchar : 8; uchar : 8; uchar other; }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@234318 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 7e8efad26b..3df7839b79 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/lib/Format/UnwrappedLineParser.cpp @@ -841,9 +841,8 @@ void UnwrappedLineParser::parseStructuralElement() { if (Line->Tokens.size() == 1 && // JS doesn't have macros, and within classes colons indicate fields, // not labels. - (Style.Language != FormatStyle::LK_JavaScript || - !Line->MustBeDeclaration)) { - if (FormatTok->Tok.is(tok::colon)) { + Style.Language != FormatStyle::LK_JavaScript) { + if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { parseLabel(); return; } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 183fa4418b..16062dd40b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -812,9 +812,11 @@ TEST_F(FormatTest, FormatsLabels) { " some_more_code();\n" " }\n" "}"); - verifyFormat("some_code();\n" + verifyFormat("{\n" + " some_code();\n" "test_label:\n" - "some_other_code();"); + " some_other_code();\n" + "}"); } //===----------------------------------------------------------------------===// @@ -2143,6 +2145,12 @@ TEST_F(FormatTest, FormatsBitfields) { " int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa : 1,\n" " bbbbbbbbbbbbbbbbbbbbbbbbb;\n" "};"); + verifyFormat("struct MyStruct {\n" + " uchar data;\n" + " uchar : 8;\n" + " uchar : 8;\n" + " uchar other;\n" + "};"); } TEST_F(FormatTest, FormatsNamespaces) {