From d9670878d46372e402f67372361cd408f98c54ef Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 5 Aug 2014 12:06:20 +0000 Subject: [PATCH] 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! llvm-svn: 214857 --- clang/include/clang/Format/Format.h | 2 +- clang/lib/Format/UnwrappedLineParser.cpp | 2 ++ clang/unittests/Format/FormatTest.cpp | 11 +++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index 45cccaacd569..b21efa132dbf 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -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, diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index ed28497ab999..1751173d11c1 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -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); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 8089b3e36ec0..e0841caa4c70 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -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"