[clang] 92d8738 - [clang-format] Fix a bug in merging blocks with a wrapped l_brace
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 2 23:05:05 PDT 2022
Author: owenca
Date: 2022-09-02T21:38:36-07:00
New Revision: 92d8738c38448a478e0c5b097b24b450a224843f
URL: https://github.com/llvm/llvm-project/commit/92d8738c38448a478e0c5b097b24b450a224843f
DIFF: https://github.com/llvm/llvm-project/commit/92d8738c38448a478e0c5b097b24b450a224843f.diff
LOG: [clang-format] Fix a bug in merging blocks with a wrapped l_brace
When the opening brace of a control statement block is wrapped, we
must check the previous line to determine whether to try to merge
the block.
Fixes #38639.
Fixes #48007.
Fixes #57421.
Differential Revision: https://reviews.llvm.org/D133093
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 9fa6e9e638853..7076a9f5cb3f4 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -711,16 +711,23 @@ class LineJoiner {
if (Tok && Tok->is(tok::colon))
return 0;
}
- 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, TT_ForEachMacro,
- tok::r_brace, Keywords.kw___except)) {
- if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
- return 0;
- if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty &&
- !I[1]->First->is(tok::r_brace)) {
+
+ auto IsCtrlStmt = [](const auto &Line) {
+ return Line.First->isOneOf(tok::kw_if, tok::kw_else, tok::kw_while,
+ tok::kw_do, tok::kw_for, TT_ForEachMacro);
+ };
+
+ const bool IsSplitBlock =
+ Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never ||
+ (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Empty &&
+ I[1]->First->isNot(tok::r_brace));
+
+ if (IsCtrlStmt(Line) ||
+ Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
+ tok::kw___finally, tok::r_brace,
+ Keywords.kw___except)) {
+ if (IsSplitBlock)
return 0;
- }
// Don't merge when we can't except the case when
// the control statement block is empty
if (!Style.AllowShortIfStatementsOnASingleLine &&
@@ -763,6 +770,11 @@ class LineJoiner {
}
if (Line.Last->is(tok::l_brace)) {
+ if (IsSplitBlock && Line.First == Line.Last &&
+ I > AnnotatedLines.begin() &&
+ (I[-1]->endsWith(tok::kw_else) || IsCtrlStmt(*I[-1]))) {
+ return 0;
+ }
FormatToken *Tok = I[1]->First;
auto ShouldMerge = [Tok]() {
if (Tok->isNot(tok::r_brace) || Tok->MustBreakBefore)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 57a6d83578f1d..d330c65470f0d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1860,6 +1860,69 @@ TEST_F(FormatTest, FormatShortBracedStatements) {
" f();\n"
"}",
AllowSimpleBracedStatements);
+
+ FormatStyle Style = getLLVMStyle();
+ Style.BreakBeforeBraces = FormatStyle::BS_Custom;
+ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
+
+ verifyFormat("while (i > 0)\n"
+ "{\n"
+ " --i;\n"
+ "}",
+ Style);
+
+ verifyFormat("if (a)\n"
+ "{\n"
+ " ++b;\n"
+ "}",
+ Style);
+
+ verifyFormat("if (a)\n"
+ "{\n"
+ " b = 1;\n"
+ "} else\n"
+ "{\n"
+ " b = 0;\n"
+ "}",
+ Style);
+
+ verifyFormat("if (a)\n"
+ "{\n"
+ " b = 1;\n"
+ "} else if (c)\n"
+ "{\n"
+ " b = 2;\n"
+ "} else\n"
+ "{\n"
+ " b = 0;\n"
+ "}",
+ Style);
+
+ Style.BraceWrapping.BeforeElse = true;
+
+ verifyFormat("if (a)\n"
+ "{\n"
+ " b = 1;\n"
+ "}\n"
+ "else\n"
+ "{\n"
+ " b = 0;\n"
+ "}",
+ Style);
+
+ verifyFormat("if (a)\n"
+ "{\n"
+ " b = 1;\n"
+ "}\n"
+ "else if (c)\n"
+ "{\n"
+ " b = 2;\n"
+ "}\n"
+ "else\n"
+ "{\n"
+ " b = 0;\n"
+ "}",
+ Style);
}
TEST_F(FormatTest, UnderstandsMacros) {
@@ -25281,8 +25344,6 @@ TEST_F(FormatTest, InsertBraces) {
Style.BreakBeforeBraces = FormatStyle::BS_Custom;
Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always;
- // TODO: Delete the following line after #57421 is fixed.
- Style.BraceWrapping.AfterFunction = true;
verifyFormat("if (a) //\n"
"{\n"
More information about the cfe-commits
mailing list