Skip to content

Commit e2b219b

Browse files
committed
[clang-format] Handle "// clang-format off" for RemoveBracesLLVM
Differential Revision: https://reviews.llvm.org/D121352
1 parent 19c6098 commit e2b219b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/Format/Format.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1808,7 +1808,8 @@ class BracesRemover : public TokenAnalyzer {
18081808
removeBraces(Line->Children, Result);
18091809
if (!Line->Affected)
18101810
continue;
1811-
for (FormatToken *Token = Line->First; Token; Token = Token->Next) {
1811+
for (FormatToken *Token = Line->First; Token && !Token->Finalized;
1812+
Token = Token->Next) {
18121813
if (!Token->Optional)
18131814
continue;
18141815
assert(Token->isOneOf(tok::l_brace, tok::r_brace));

clang/unittests/Format/FormatTest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24805,6 +24805,19 @@ TEST_F(FormatTest, RemoveBraces) {
2480524805
"}",
2480624806
Style);
2480724807

24808+
verifyFormat("// clang-format off\n"
24809+
"// comment\n"
24810+
"while (i > 0) { --i; }\n"
24811+
"// clang-format on\n"
24812+
"while (j < 0)\n"
24813+
" ++j;",
24814+
"// clang-format off\n"
24815+
"// comment\n"
24816+
"while (i > 0) { --i; }\n"
24817+
"// clang-format on\n"
24818+
"while (j < 0) { ++j; }",
24819+
Style);
24820+
2480824821
verifyFormat("if (a)\n"
2480924822
" b; // comment\n"
2481024823
"else if (c)\n"

0 commit comments

Comments
 (0)