Skip to content

Commit 7ad60f6

Browse files
author
Mitchell Balan
committed
[clang-format] fix BreakBeforeBraces.MultiLine with for each macros
Summary: The MultiLine option in BreakBeforeBraces was only handling standard control statement, leading to invalid indentation with for each macros: Previous behavior: /* invalid: brace should be on the same line */ Q_FOREACH(int a; list) { foo(); } /* valid */ Q_FOREACH(int longVariable; list) { foo(); } To fix this, simply add the TT_ForEachMacro kind in the list of recognized control statements for the multiline option. This is a fix for https://bugs.llvm.org/show_bug.cgi?id=44632 Reviewers: MyDeveloperDay, mitchell-stellar Reviewed by: mitchell-stellar Contributed by: vthib Subscribers: cfe-commits Tags: #clang, #clang-format, #clang-tools-extra Differential Revision: https://reviews.llvm.org/D85304
1 parent 4062618 commit 7ad60f6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ class LineJoiner {
309309
// Try to merge a control statement block with left brace wrapped
310310
if (I[1]->First->is(tok::l_brace) &&
311311
(TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
312-
tok::kw_switch, tok::kw_try, tok::kw_do) ||
312+
tok::kw_switch, tok::kw_try, tok::kw_do,
313+
TT_ForEachMacro) ||
313314
(TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
314315
TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
315316
Style.BraceWrapping.AfterControlStatement ==

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,6 +1663,20 @@ TEST_F(FormatTest, MultiLineControlStatements) {
16631663
" foo();\n"
16641664
"}",
16651665
format("for(int i=0;i<10;++i){foo();}", Style));
1666+
EXPECT_EQ("foreach (int i,\n"
1667+
" list)\n"
1668+
"{\n"
1669+
" foo();\n"
1670+
"}",
1671+
format("foreach(int i, list){foo();}", Style));
1672+
Style.ColumnLimit =
1673+
40; // to concentrate at brace wrapping, not line wrap due to column limit
1674+
EXPECT_EQ("foreach (int i, list) {\n"
1675+
" foo();\n"
1676+
"}",
1677+
format("foreach(int i, list){foo();}", Style));
1678+
Style.ColumnLimit =
1679+
20; // to concentrate at brace wrapping, not line wrap due to column limit
16661680
EXPECT_EQ("while (foo || bar ||\n"
16671681
" baz)\n"
16681682
"{\n"

0 commit comments

Comments
 (0)