[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:58:40 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: llvmbot

<details>
<summary>Changes</summary>

Backport 6978738e1efe0c33a9540b70697ce0c83852d9c7

Requested by: @<!-- -->HazardyKnusperkeks

---
Full diff: https://github.com/llvm/llvm-project/pull/218139.diff


2 Files Affected:

- (modified) clang/lib/Format/UnwrappedLineFormatter.cpp (+2) 
- (modified) clang/unittests/Format/FormatTest.cpp (+8) 


``````````diff
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) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/218139


More information about the llvm-branch-commits mailing list