]> granicus.if.org Git - clang/commitdiff
Fixes a couple of bugs with the Allman brace breaking.
authorManuel Klimek <klimek@google.com>
Wed, 7 Aug 2013 19:20:45 +0000 (19:20 +0000)
committerManuel Klimek <klimek@google.com>
Wed, 7 Aug 2013 19:20:45 +0000 (19:20 +0000)
In particular, left braces after an enum declaration now occur on their
own line.  Further, when short ifs/whiles are allowed these no longer
cause the left brace to be on the same line as the if/while when a
brace is included.

Patch by Thomas Gibson-Robinson.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@187901 91177308-0d34-0410-b5e6-96231b3b80d8

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

index 140bda219382c6000c27288d547000722929d8cf..255f5142f07924af910fd73af9380c8fc6ad1571 100644 (file)
@@ -1710,6 +1710,9 @@ private:
                                       unsigned Limit) {
     if (Limit == 0)
       return;
+    if (Style.BreakBeforeBraces == FormatStyle::BS_Allman &&
+        (I + 1)->First->is(tok::l_brace))
+      return;
     if ((I + 1)->InPPDirective != I->InPPDirective ||
         ((I + 1)->InPPDirective && (I + 1)->First->HasUnescapedNewline))
       return;
index c80c297de22fa044db601705ee59d2741a229ae1..56fe81f3f2be177b8615942ba65b4092e15fc2f5 100644 (file)
@@ -930,6 +930,8 @@ void UnwrappedLineParser::parseEnum() {
       nextToken();
   }
   if (FormatTok->Tok.is(tok::l_brace)) {
+    if (Style.BreakBeforeBraces == FormatStyle::BS_Allman)
+      addUnwrappedLine();
     nextToken();
     addUnwrappedLine();
     ++Line->Level;
index 2b1b94c4c61cf1feb2bcd1693f0b164097267d1f..2969e568ebc618b73ab23f243ab1284939bc9900 100644 (file)
@@ -5530,6 +5530,37 @@ TEST_F(FormatTest, AllmanBraceBreaking) {
                "  }\n"
                "}\n",
                BreakBeforeBrace);
+
+  verifyFormat("enum X\n"
+               "{\n"
+               "  Y = 0,\n"
+               "}\n",
+               BreakBeforeBrace);
+
+  FormatStyle BreakBeforeBraceShortIfs = BreakBeforeBrace;
+  BreakBeforeBraceShortIfs.AllowShortIfStatementsOnASingleLine = true;
+  BreakBeforeBraceShortIfs.AllowShortLoopsOnASingleLine = true;
+  verifyFormat("void f(bool b)\n"
+               "{\n"
+               "  if (b)\n"
+               "  {\n"
+               "    return;\n"
+               "  }\n"
+               "}\n",
+               BreakBeforeBraceShortIfs);
+  verifyFormat("void f(bool b)\n"
+               "{\n"
+               "  if (b) return;\n"
+               "}\n",
+               BreakBeforeBraceShortIfs);
+  verifyFormat("void f(bool b)\n"
+               "{\n"
+               "  while (b)\n"
+               "  {\n"
+               "    return;\n"
+               "  }\n"
+               "}\n",
+               BreakBeforeBraceShortIfs);
 }
 
 TEST_F(FormatTest, UnderstandsPragmas) {