clang-format: Don't merge short else blocks.

Before (with the appropriate flags settings):
  if (a) {
    f();
  } else { g(); }

Before (with other appropriate flags settings):
  if (a) { f(); } else {
    g();
  }

After:
  if (a) {
    f();
  } else {
    g();
  }

llvm-svn: 236217
This commit is contained in:
Daniel Jasper 2015-04-30 09:24:17 +00:00
parent 90b059d555
commit e9f5357f49
2 changed files with 18 additions and 1 deletions

View File

@ -203,7 +203,8 @@ private:
// Check that the current line allows merging. This depends on whether we
// are in a control flow statements as well as several style flags.
if (Line.First->isOneOf(tok::kw_else, tok::kw_case))
if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
(Line.First->Next && Line.First->Next->is(tok::kw_else)))
return 0;
if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
tok::kw___try, tok::kw_catch, tok::kw___finally,
@ -264,6 +265,10 @@ private:
if (Tok->isNot(tok::r_brace))
return 0;
// Don't merge "if (a) { .. } else {".
if (Tok->Next && Tok->Next->is(tok::kw_else))
return 0;
return 2;
}
return 0;

View File

@ -416,6 +416,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
" f();\n"
"}",
AllowSimpleBracedStatements);
verifyFormat("if (true) {\n"
" f();\n"
"} else {\n"
" f();\n"
"}",
AllowSimpleBracedStatements);
verifyFormat("template <int> struct A2 {\n"
" struct B {};\n"
@ -427,6 +433,12 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
" f();\n"
"}",
AllowSimpleBracedStatements);
verifyFormat("if (true) {\n"
" f();\n"
"} else {\n"
" f();\n"
"}",
AllowSimpleBracedStatements);
AllowSimpleBracedStatements.AllowShortLoopsOnASingleLine = false;
verifyFormat("while (true) {\n"