From: Daniel Jasper Date: Wed, 6 Apr 2016 15:02:46 +0000 (+0000) Subject: clang-format: Support labels in brace-less ifs. X-Git-Tag: llvmorg-3.9.0-rc1~9864 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=406094728964cfdef630d25f6034c25175841870;p=platform%2Fupstream%2Fllvm.git clang-format: Support labels in brace-less ifs. While I am not personally convinced about the usefulness of this construct, we should break it. Before: if (a) label: f(); After: if (a) label: f(); llvm-svn: 265545 --- diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index 769a23b..29635bf 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -1010,6 +1010,7 @@ void UnwrappedLineParser::parseStructuralElement() { // not labels. Style.Language != FormatStyle::LK_JavaScript) { if (FormatTok->Tok.is(tok::colon) && !Line->MustBeDeclaration) { + Line->Tokens.begin()->Tok->MustBreakBefore = true; parseLabel(); return; } @@ -1572,6 +1573,8 @@ void UnwrappedLineParser::parseLabel() { addUnwrappedLine(); } Line->Level = OldLineLevel; + if (FormatTok->isNot(tok::l_brace)) + parseStructuralElement(); } void UnwrappedLineParser::parseCaseLabel() { diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 723e05f..55fd5fb 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -301,6 +301,12 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { " // comment\n" " f();", AllowsMergedIf); + verifyFormat("{\n" + " if (a)\n" + " label:\n" + " f();\n" + "}", + AllowsMergedIf); verifyFormat("if (a)\n" " ;", AllowsMergedIf);