Skip to content

Commit 2c2fb8e

Browse files
authored
[clang-format] Treat empty for/while loops as short loops (#70768)
A for/while loop with only a semicolon as its body should be treated as an empty loop. Fixes #61708.
1 parent 3747cde commit 2c2fb8e

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3117,9 +3117,16 @@ void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
31173117
FormatTok->setFinalizedType(TT_ConditionLParen);
31183118
parseParens();
31193119
}
3120-
// Event control.
3121-
if (Style.isVerilog())
3120+
3121+
if (Style.isVerilog()) {
3122+
// Event control.
31223123
parseVerilogSensitivityList();
3124+
} else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
3125+
Tokens->getPreviousToken()->is(tok::r_paren)) {
3126+
nextToken();
3127+
addUnwrappedLine();
3128+
return;
3129+
}
31233130

31243131
handleAttributes();
31253132
parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);

clang/unittests/Format/FormatTest.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,18 +1355,20 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
13551355
}
13561356

13571357
TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
1358+
verifyFormat("while (true)\n"
1359+
" ;");
1360+
verifyFormat("for (;;)\n"
1361+
" ;");
1362+
13581363
FormatStyle AllowsMergedLoops = getLLVMStyle();
13591364
AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
1365+
13601366
verifyFormat("while (true) continue;", AllowsMergedLoops);
13611367
verifyFormat("for (;;) continue;", AllowsMergedLoops);
13621368
verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
13631369
verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
1364-
verifyFormat("while (true)\n"
1365-
" ;",
1366-
AllowsMergedLoops);
1367-
verifyFormat("for (;;)\n"
1368-
" ;",
1369-
AllowsMergedLoops);
1370+
verifyFormat("while (true);", AllowsMergedLoops);
1371+
verifyFormat("for (;;);", AllowsMergedLoops);
13701372
verifyFormat("for (;;)\n"
13711373
" for (;;) continue;",
13721374
AllowsMergedLoops);
@@ -1404,6 +1406,7 @@ TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
14041406
" a++;\n"
14051407
"while (true);",
14061408
AllowsMergedLoops);
1409+
14071410
// Without braces labels are interpreted differently.
14081411
verifyFormat("{\n"
14091412
" do\n"
@@ -1412,6 +1415,17 @@ TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
14121415
" while (true);\n"
14131416
"}",
14141417
AllowsMergedLoops);
1418+
1419+
// Don't merge if there are comments before the null statement.
1420+
verifyFormat("while (1) //\n"
1421+
" ;",
1422+
AllowsMergedLoops);
1423+
verifyFormat("for (;;) /**/\n"
1424+
" ;",
1425+
AllowsMergedLoops);
1426+
verifyFormat("while (true) /**/\n"
1427+
" ;",
1428+
"while (true) /**/;", AllowsMergedLoops);
14151429
}
14161430

14171431
TEST_F(FormatTest, FormatShortBracedStatements) {

0 commit comments

Comments
 (0)