]> granicus.if.org Git - clang/commitdiff
clang-format: Break before 'else' in Stroustrup style.
authorDaniel Jasper <djasper@google.com>
Tue, 5 Aug 2014 12:06:20 +0000 (12:06 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 5 Aug 2014 12:06:20 +0000 (12:06 +0000)
Seems to be the desired thing to do according to:
  http://www.stroustrup.com/Programming/PPP-style-rev3.pdf

Patch by Jarkko Hietaniemi, thank you!

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

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

index 45cccaacd569313e52e06bd49fa0fa59389e1ef9..b21efa132dbf7c1b623cb35f97792c3e7e5bf7ec 100644 (file)
@@ -269,7 +269,7 @@ struct FormatStyle {
     /// Like \c Attach, but break before braces on function, namespace and
     /// class definitions.
     BS_Linux,
-    /// Like \c Attach, but break before function definitions.
+    /// Like \c Attach, but break before function definitions, and 'else'.
     BS_Stroustrup,
     /// Always break before braces.
     BS_Allman,
index ed28497ab999b14697960b27870c7fa004661295..1751173d11c1d39c7f26924fbb0998740998e8db 100644 (file)
@@ -1080,6 +1080,8 @@ void UnwrappedLineParser::parseIfThenElse() {
     --Line->Level;
   }
   if (FormatTok->Tok.is(tok::kw_else)) {
+    if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
+      addUnwrappedLine();
     nextToken();
     if (FormatTok->Tok.is(tok::l_brace)) {
       CompoundStatementIndenter Indenter(this, Style, Line->Level);
index 8089b3e36ec0010fe3fc66e67a664b7b8c751b2c..e0841caa4c70b1267c9c75706bf37e4c4f6fbbbb 100644 (file)
@@ -7677,6 +7677,17 @@ TEST_F(FormatTest, StroustrupBraceBreaking) {
                "}",
                BreakBeforeBrace);
 
+  verifyFormat("void foo()\n"
+               "{\n"
+               "  if (a) {\n"
+               "    a();\n"
+               "  }\n"
+               "  else {\n"
+               "    b();\n"
+               "  }\n"
+               "}\n",
+               BreakBeforeBrace);
+
   verifyFormat("#ifdef _DEBUG\n"
                "int foo(int i = 0)\n"
                "#else\n"