[clang] 2c2fb8e - [clang-format] Treat empty for/while loops as short loops (#70768)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 2 14:09:05 PDT 2023
Author: Owen Pan
Date: 2023-11-02T14:09:00-07:00
New Revision: 2c2fb8e10a12774bae86a5ff60fd67c5dc5e220f
URL: https://github.com/llvm/llvm-project/commit/2c2fb8e10a12774bae86a5ff60fd67c5dc5e220f
DIFF: https://github.com/llvm/llvm-project/commit/2c2fb8e10a12774bae86a5ff60fd67c5dc5e220f.diff
LOG: [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.
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 30f9bcbfa29308e..94e238039c2a707 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -3117,9 +3117,16 @@ void UnwrappedLineParser::parseForOrWhileLoop(bool HasParens) {
FormatTok->setFinalizedType(TT_ConditionLParen);
parseParens();
}
- // Event control.
- if (Style.isVerilog())
+
+ if (Style.isVerilog()) {
+ // Event control.
parseVerilogSensitivityList();
+ } else if (Style.AllowShortLoopsOnASingleLine && FormatTok->is(tok::semi) &&
+ Tokens->getPreviousToken()->is(tok::r_paren)) {
+ nextToken();
+ addUnwrappedLine();
+ return;
+ }
handleAttributes();
parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 15c12c2b8b0da3f..80903e7630c8073 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -1355,18 +1355,20 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatementButElseWith) {
}
TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
+ verifyFormat("while (true)\n"
+ " ;");
+ verifyFormat("for (;;)\n"
+ " ;");
+
FormatStyle AllowsMergedLoops = getLLVMStyle();
AllowsMergedLoops.AllowShortLoopsOnASingleLine = true;
+
verifyFormat("while (true) continue;", AllowsMergedLoops);
verifyFormat("for (;;) continue;", AllowsMergedLoops);
verifyFormat("for (int &v : vec) v *= 2;", AllowsMergedLoops);
verifyFormat("BOOST_FOREACH (int &v, vec) v *= 2;", AllowsMergedLoops);
- verifyFormat("while (true)\n"
- " ;",
- AllowsMergedLoops);
- verifyFormat("for (;;)\n"
- " ;",
- AllowsMergedLoops);
+ verifyFormat("while (true);", AllowsMergedLoops);
+ verifyFormat("for (;;);", AllowsMergedLoops);
verifyFormat("for (;;)\n"
" for (;;) continue;",
AllowsMergedLoops);
@@ -1404,6 +1406,7 @@ TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
" a++;\n"
"while (true);",
AllowsMergedLoops);
+
// Without braces labels are interpreted
diff erently.
verifyFormat("{\n"
" do\n"
@@ -1412,6 +1415,17 @@ TEST_F(FormatTest, FormatLoopsWithoutCompoundStatement) {
" while (true);\n"
"}",
AllowsMergedLoops);
+
+ // Don't merge if there are comments before the null statement.
+ verifyFormat("while (1) //\n"
+ " ;",
+ AllowsMergedLoops);
+ verifyFormat("for (;;) /**/\n"
+ " ;",
+ AllowsMergedLoops);
+ verifyFormat("while (true) /**/\n"
+ " ;",
+ "while (true) /**/;", AllowsMergedLoops);
}
TEST_F(FormatTest, FormatShortBracedStatements) {
More information about the cfe-commits
mailing list