[clang] 813d486 - [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 25 11:45:33 PST 2021
Author: Jesses Gott
Date: 2021-11-25T19:45:07Z
New Revision: 813d486cbc99836ea4e75996e2667cb38f7113a8
URL: https://github.com/llvm/llvm-project/commit/813d486cbc99836ea4e75996e2667cb38f7113a8
DIFF: https://github.com/llvm/llvm-project/commit/813d486cbc99836ea4e75996e2667cb38f7113a8.diff
LOG: [clang-format] Extend AllowShortBlocksOnASingleLine for else blocks
Extend AllowShortBlocksOnASingleLine for else blocks. See https://bugs.llvm.org/show_bug.cgi?id=49722
Reviewed By: HazardyKnusperkeks, owenpan, MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D114320
Added:
Modified:
clang/lib/Format/UnwrappedLineFormatter.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 516b1bcac314c..d099cfee9dea2 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -320,9 +320,9 @@ class LineJoiner {
}
// Try to merge a control statement block with left brace wrapped
if (I[1]->First->is(tok::l_brace) &&
- (TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
- tok::kw_switch, tok::kw_try, tok::kw_do,
- TT_ForEachMacro) ||
+ (TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
+ tok::kw_for, tok::kw_switch, tok::kw_try,
+ tok::kw_do, TT_ForEachMacro) ||
(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
Style.BraceWrapping.AfterControlStatement ==
@@ -335,7 +335,7 @@ class LineJoiner {
? 1
: 0;
} else if (I[1]->First->is(tok::l_brace) &&
- TheLine->First->isOneOf(tok::kw_if, tok::kw_while,
+ TheLine->First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
tok::kw_for)) {
return (Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_Always)
@@ -569,7 +569,7 @@ class LineJoiner {
// 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->is(tok::kw_case) ||
(Line.First->Next && Line.First->Next->is(tok::kw_else)))
return 0;
// default: in switch statement
@@ -578,20 +578,21 @@ class LineJoiner {
if (Tok && Tok->is(tok::colon))
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,
- tok::kw_for, tok::r_brace, Keywords.kw___except)) {
+ if (Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while, tok::kw_do,
+ tok::kw_try, tok::kw___try, tok::kw_catch,
+ tok::kw___finally, tok::kw_for, tok::r_brace,
+ Keywords.kw___except)) {
if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
return 0;
// Don't merge when we can't except the case when
// the control statement block is empty
if (!Style.AllowShortIfStatementsOnASingleLine &&
- Line.startsWith(tok::kw_if) &&
+ Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
!Style.BraceWrapping.AfterControlStatement &&
!I[1]->First->is(tok::r_brace))
return 0;
if (!Style.AllowShortIfStatementsOnASingleLine &&
- Line.startsWith(tok::kw_if) &&
+ Line.First->isOneOf(tok::kw_if, tok::kw_else) &&
Style.BraceWrapping.AfterControlStatement ==
FormatStyle::BWACS_Always &&
I + 2 != E && !I[2]->First->is(tok::r_brace))
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 79a74b5141e82..845b1e5e8809a 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -5407,6 +5407,27 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
EXPECT_EQ("void f() { }", format("void f() {}", Style));
Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty;
EXPECT_EQ("while (true) { }", format("while (true) {}", Style));
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Style.BraceWrapping.BeforeElse = false;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+ verifyFormat("if (a)\n"
+ "{\n"
+ "} else if (b)\n"
+ "{\n"
+ "} else\n"
+ "{ }",
+ Style);
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Never;
+ verifyFormat("if (a) {\n"
+ "} else if (b) {\n"
+ "} else {\n"
+ "}",
+ Style);
+ Style.BraceWrapping.BeforeElse = true;
+ verifyFormat("if (a) { }\n"
+ "else if (b) { }\n"
+ "else { }",
+ Style);
}
TEST_F(FormatTest, FormatBeginBlockEndMacros) {
More information about the cfe-commits
mailing list