r214857 - clang-format: Break before 'else' in Stroustrup style.

Daniel Jasper djasper at google.com
Tue Aug 5 05:06:20 PDT 2014


Author: djasper
Date: Tue Aug  5 07:06:20 2014
New Revision: 214857

URL: http://llvm.org/viewvc/llvm-project?rev=214857&view=rev
Log:
clang-format: Break before 'else' in Stroustrup style.

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!

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

Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=214857&r1=214856&r2=214857&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Tue Aug  5 07:06:20 2014
@@ -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,

Modified: cfe/trunk/lib/Format/UnwrappedLineParser.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/UnwrappedLineParser.cpp?rev=214857&r1=214856&r2=214857&view=diff
==============================================================================
--- cfe/trunk/lib/Format/UnwrappedLineParser.cpp (original)
+++ cfe/trunk/lib/Format/UnwrappedLineParser.cpp Tue Aug  5 07:06:20 2014
@@ -1080,6 +1080,8 @@ void UnwrappedLineParser::parseIfThenEls
     --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);

Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=214857&r1=214856&r2=214857&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Aug  5 07:06:20 2014
@@ -7677,6 +7677,17 @@ TEST_F(FormatTest, StroustrupBraceBreaki
                "}",
                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"





More information about the cfe-commits mailing list