clang-format: Support labels in brace-less ifs.
authorDaniel Jasper <djasper@google.com>
Wed, 6 Apr 2016 15:02:46 +0000 (15:02 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 6 Apr 2016 15:02:46 +0000 (15:02 +0000)
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

clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp

index 769a23b..29635bf 100644 (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() {
index 723e05f..55fd5fb 100644 (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);