[llvm-branch-commits] [clang] release/23.x: [clang-format] Honor new lines before // clang-format on (#218022) (PR #218139)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Aug 22 08:57:59 PDT 2026
https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/218139
Backport 6978738e1efe0c33a9540b70697ce0c83852d9c7
Requested by: @HazardyKnusperkeks
>From 356a3d9fe8d9c73475feaad6bd26c9c480d83a8e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Sat, 22 Aug 2026 13:03:31 +0200
Subject: [PATCH] [clang-format] Honor new lines before // clang-format on
(#218022)
Fixes #217872.
(cherry picked from commit 6978738e1efe0c33a9540b70697ce0c83852d9c7)
---
clang/lib/Format/UnwrappedLineFormatter.cpp | 2 ++
clang/unittests/Format/FormatTest.cpp | 8 ++++++++
2 files changed, 10 insertions(+)
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp
index 7afc7a46dd1c0..b783876b6da8f 100644
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp
@@ -1637,6 +1637,8 @@ static auto computeNewlines(const AnnotatedLine &Line,
const SmallVectorImpl<AnnotatedLine *> &Lines,
const FormatStyle &Style) {
const auto &RootToken = *Line.First;
+ if (isClangFormatOn(RootToken.TokenText))
+ return RootToken.NewlinesBefore;
auto Newlines =
std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
// Remove empty lines before "}" where applicable.
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 72e0182181763..92bc4c918fb21 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22420,6 +22420,14 @@ TEST_F(FormatTest, DisableRegions) {
" #endif\n"
"#endif\n"
"// clang-format on");
+
+ verifyNoChange("// clang-format off\n"
+ "\n"
+ "\n"
+ " int i ;\n"
+ "\n"
+ "\n"
+ "// clang-format on");
}
TEST_F(FormatTest, OneLineFormatOffRegex) {
More information about the llvm-branch-commits
mailing list