summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorowenca <owenpiano@gmail.com>2022-03-09 20:38:43 -0800
committerowenca <owenpiano@gmail.com>2022-03-10 13:30:53 -0800
commite2b219bded113160500d1084f87ee11ec926877b (patch)
tree6bb439a2fccd59a0c0ccb4a99e884c73fd9ccf40
parent19c60980971bc227d94712d4799ded894f9f0f9a (diff)
[clang-format] Handle "// clang-format off" for RemoveBracesLLVM
Differential Revision: https://reviews.llvm.org/D121352
-rw-r--r--clang/lib/Format/Format.cpp3
-rw-r--r--clang/unittests/Format/FormatTest.cpp13
2 files changed, 15 insertions, 1 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 6c2765c03843..a8b8efb5b22f 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -1808,7 +1808,8 @@ private:
removeBraces(Line->Children, Result);
if (!Line->Affected)
continue;
- for (FormatToken *Token = Line->First; Token; Token = Token->Next) {
+ for (FormatToken *Token = Line->First; Token && !Token->Finalized;
+ Token = Token->Next) {
if (!Token->Optional)
continue;
assert(Token->isOneOf(tok::l_brace, tok::r_brace));
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 1d9b158ee647..dab8f83fffe3 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -24805,6 +24805,19 @@ TEST_F(FormatTest, RemoveBraces) {
"}",
Style);
+ verifyFormat("// clang-format off\n"
+ "// comment\n"
+ "while (i > 0) { --i; }\n"
+ "// clang-format on\n"
+ "while (j < 0)\n"
+ " ++j;",
+ "// clang-format off\n"
+ "// comment\n"
+ "while (i > 0) { --i; }\n"
+ "// clang-format on\n"
+ "while (j < 0) { ++j; }",
+ Style);
+
verifyFormat("if (a)\n"
" b; // comment\n"
"else if (c)\n"