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
This commit is contained in:
Daniel Jasper 2014-08-05 12:06:20 +00:00
parent 202f3c5346
commit d9670878d4
3 changed files with 14 additions and 1 deletions
clang
include/clang/Format
lib/Format
unittests/Format

View File

@ -269,7 +269,7 @@ struct FormatStyle {
/// Like \c Attach, but break before braces on function, namespace and /// Like \c Attach, but break before braces on function, namespace and
/// class definitions. /// class definitions.
BS_Linux, BS_Linux,
/// Like \c Attach, but break before function definitions. /// Like \c Attach, but break before function definitions, and 'else'.
BS_Stroustrup, BS_Stroustrup,
/// Always break before braces. /// Always break before braces.
BS_Allman, BS_Allman,

View File

@ -1080,6 +1080,8 @@ void UnwrappedLineParser::parseIfThenElse() {
--Line->Level; --Line->Level;
} }
if (FormatTok->Tok.is(tok::kw_else)) { if (FormatTok->Tok.is(tok::kw_else)) {
if (Style.BreakBeforeBraces == FormatStyle::BS_Stroustrup)
addUnwrappedLine();
nextToken(); nextToken();
if (FormatTok->Tok.is(tok::l_brace)) { if (FormatTok->Tok.is(tok::l_brace)) {
CompoundStatementIndenter Indenter(this, Style, Line->Level); CompoundStatementIndenter Indenter(this, Style, Line->Level);

View File

@ -7677,6 +7677,17 @@ TEST_F(FormatTest, StroustrupBraceBreaking) {
"}", "}",
BreakBeforeBrace); BreakBeforeBrace);
verifyFormat("void foo()\n"
"{\n"
" if (a) {\n"
" a();\n"
" }\n"
" else {\n"
" b();\n"
" }\n"
"}\n",
BreakBeforeBrace);
verifyFormat("#ifdef _DEBUG\n" verifyFormat("#ifdef _DEBUG\n"
"int foo(int i = 0)\n" "int foo(int i = 0)\n"
"#else\n" "#else\n"