r265545 - clang-format: Support labels in brace-less ifs.

Daniel Jasper via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 6 08:02:48 PDT 2016


Author: djasper
Date: Wed Apr  6 10:02:46 2016
New Revision: 265545

URL: http://llvm.org/viewvc/llvm-project?rev=265545&view=rev
Log:
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();

Modified:
    cfe/trunk/lib/Format/UnwrappedLineParser.cpp
    cfe/trunk/unittests/Format/FormatTest.cpp

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=265545&r1=265544&r2=265545&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Wed Apr  6 10:02:46 2016
@@ -1010,6 +1010,7 @@ void UnwrappedLineParser::parseStructura
           // 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() {

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=265545&r1=265544&r2=265545&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Wed Apr  6 10:02:46 2016
@@ -301,6 +301,12 @@ TEST_F(FormatTest, FormatIfWithoutCompou
                "  // comment\n"
                "  f();",
                AllowsMergedIf);
+  verifyFormat("{\n"
+               "  if (a)\n"
+               "  label:\n"
+               "    f();\n"
+               "}",
+               AllowsMergedIf);
   verifyFormat("if (a)\n"
                "  ;",
                AllowsMergedIf);




More information about the cfe-commits mailing list