diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -306,7 +306,8 @@ } // Try to merge a control statement block with left brace unwrapped if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last && - TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) { + TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for, + TT_ForEachMacro)) { return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never ? tryMergeSimpleBlock(I, E, Limit) : 0; @@ -421,7 +422,8 @@ ? tryMergeSimpleControlStatement(I, E, Limit) : 0; } - if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do)) { + if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while, tok::kw_do, + TT_ForEachMacro)) { return Style.AllowShortLoopsOnASingleLine ? tryMergeSimpleControlStatement(I, E, Limit) : 0; @@ -474,7 +476,7 @@ if (1 + I[1]->Last->TotalLength > Limit) return 0; if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while, - TT_LineComment)) + TT_ForEachMacro, TT_LineComment)) return 0; // Only inline simple if's (no nested if or else), unless specified if (Style.AllowShortIfStatementsOnASingleLine != FormatStyle::SIS_Always) { @@ -578,12 +580,14 @@ I + 2 != E && !I[2]->First->is(tok::r_brace)) return 0; if (!Style.AllowShortLoopsOnASingleLine && - Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) && + Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for, + TT_ForEachMacro) && !Style.BraceWrapping.AfterControlStatement && !I[1]->First->is(tok::r_brace)) return 0; if (!Style.AllowShortLoopsOnASingleLine && - Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) && + Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for, + TT_ForEachMacro) && 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 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -993,9 +993,12 @@ TEST_F(FormatTest, ForEachLoops) { verifyFormat("void f() {\n" - " foreach (Item *item, itemlist) {}\n" - " Q_FOREACH (Item *item, itemlist) {}\n" - " BOOST_FOREACH (Item *item, itemlist) {}\n" + " foreach (Item *item, itemlist) {\n" + " }\n" + " Q_FOREACH (Item *item, itemlist) {\n" + " }\n" + " BOOST_FOREACH (Item *item, itemlist) {\n" + " }\n" " UNKNOWN_FORACH(Item * item, itemlist) {}\n" "}"); @@ -1003,9 +1006,12 @@ Style.SpaceBeforeParens = FormatStyle::SBPO_ControlStatementsExceptForEachMacros; verifyFormat("void f() {\n" - " foreach(Item *item, itemlist) {}\n" - " Q_FOREACH(Item *item, itemlist) {}\n" - " BOOST_FOREACH(Item *item, itemlist) {}\n" + " foreach(Item *item, itemlist) {\n" + " }\n" + " Q_FOREACH(Item *item, itemlist) {\n" + " }\n" + " BOOST_FOREACH(Item *item, itemlist) {\n" + " }\n" " UNKNOWN_FORACH(Item * item, itemlist) {}\n" "}", Style); @@ -17838,6 +17844,18 @@ "}", format(Source, Style)); } + +TEST_F(FormatTest, TreatForEachMacrosAsLoops) { + FormatStyle Style = getLLVMStyle(); + Style.ForEachMacros.push_back("rng"); + Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; + verifyFormat("rng (i, 0, 10) { int j = 1; }", Style); + Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; + verifyFormat("rng (i, 0, 10) {\n" + " int j = 1;\n" + "}", + Style); +} } // namespace } // namespace format } // namespace clang