Skip to content

Commit fc1c160

Browse files
committed
[clang-format] Handle attributes for for/while loops
Fixes #55853. Differential Revision: https://reviews.llvm.org/D127054
1 parent 77e300f commit fc1c160

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2889,6 +2889,7 @@ void UnwrappedLineParser::parseForOrWhileLoop() {
28892889
if (FormatTok->is(tok::l_paren))
28902890
parseParens();
28912891

2892+
handleAttributes();
28922893
parseLoopBody(KeepBraces, /*WrapRightBrace=*/true);
28932894
}
28942895

clang/unittests/Format/FormatTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23846,6 +23846,22 @@ TEST_F(FormatTest, LikelyUnlikely) {
2384623846
" return 29;\n",
2384723847
Style);
2384823848

23849+
verifyFormat("while (limit > 0) [[unlikely]] {\n"
23850+
" --limit;\n"
23851+
"}",
23852+
Style);
23853+
verifyFormat("for (auto &limit : limits) [[likely]] {\n"
23854+
" --limit;\n"
23855+
"}",
23856+
Style);
23857+
23858+
verifyFormat("for (auto &limit : limits) [[unlikely]]\n"
23859+
" --limit;",
23860+
Style);
23861+
verifyFormat("while (limit > 0) [[likely]]\n"
23862+
" --limit;",
23863+
Style);
23864+
2384923865
Style.AttributeMacros.push_back("UNLIKELY");
2385023866
Style.AttributeMacros.push_back("LIKELY");
2385123867
verifyFormat("if (argc > 5) UNLIKELY\n"
@@ -23874,6 +23890,22 @@ TEST_F(FormatTest, LikelyUnlikely) {
2387423890
" return 42;\n"
2387523891
"}\n",
2387623892
Style);
23893+
23894+
verifyFormat("for (auto &limit : limits) UNLIKELY {\n"
23895+
" --limit;\n"
23896+
"}",
23897+
Style);
23898+
verifyFormat("while (limit > 0) LIKELY {\n"
23899+
" --limit;\n"
23900+
"}",
23901+
Style);
23902+
23903+
verifyFormat("while (limit > 0) UNLIKELY\n"
23904+
" --limit;",
23905+
Style);
23906+
verifyFormat("for (auto &limit : limits) LIKELY\n"
23907+
" --limit;",
23908+
Style);
2387723909
}
2387823910

2387923911
TEST_F(FormatTest, PenaltyIndentedWhitespace) {

0 commit comments

Comments
 (0)