From e2b219bded113160500d1084f87ee11ec926877b Mon Sep 17 00:00:00 2001 From: owenca Date: Wed, 9 Mar 2022 20:38:43 -0800 Subject: [PATCH] [clang-format] Handle "// clang-format off" for RemoveBracesLLVM Differential Revision: https://reviews.llvm.org/D121352 --- clang/lib/Format/Format.cpp | 3 ++- clang/unittests/Format/FormatTest.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 6c2765c..a8b8efb 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 1d9b158..dab8f83 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" -- 2.7.4