From 676e516354c38ebb496c41cfa6560336de3f0535 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 7 Apr 2015 14:36:33 +0000 Subject: [PATCH] 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; }; llvm-svn: 234318 --- clang/lib/Format/UnwrappedLineParser.cpp | 5 ++--- clang/unittests/Format/FormatTest.cpp | 12 ++++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 7e8efad..3df7839 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/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/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 183fa44..16062dd 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/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) { -- 2.7.4