From 077a1a709807f8427ba74139eed82d0204bb9b72 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 6 Apr 2016 15:02:46 +0000 Subject: [PATCH] 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(); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@265545 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/UnwrappedLineParser.cpp | 3 +++ unittests/Format/FormatTest.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/Format/UnwrappedLineParser.cpp b/lib/Format/UnwrappedLineParser.cpp index 769a23b437..29635bf706 100644 --- a/lib/Format/UnwrappedLineParser.cpp +++ b/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/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 723e05f224..55fd5fba4a 100644 --- a/unittests/Format/FormatTest.cpp +++ b/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); -- 2.40.0