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
This commit is contained in:
Daniel Jasper 2016-04-06 15:02:46 +00:00
parent 0dbd37543c
commit 4060947289
2 changed files with 9 additions and 0 deletions

View File

@ -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() {

View File

@ -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);