[libc-commits] [libc] [libc][cmake] Collapse extra blank lines in cmake_format (PR #240295)

Jeff Bailey via libc-commits libc-commits at lists.llvm.org
Mon Nov 9 00:31:52 PST 2026


https://github.com/kaladron created https://github.com/llvm/llvm-project/pull/240295

Updated cmake_format.py to collapse consecutive blank lines to a single blank line and strip leading blank lines at the start of files.  Applied the updated formatter across libc CMake files.

Assisted-by: Automated tooling, human reviewed.

>From 7f0647f71acfa9f039905f2f4395a79c5ea3516a Mon Sep 17 00:00:00 2001
From: Jeff Bailey <jbailey at raspberryginger.com>
Date: Mon, 9 Nov 2026 08:14:21 +0000
Subject: [PATCH] [libc][cmake] Collapse extra blank lines in cmake_format

Updated cmake_format.py to collapse consecutive blank lines to a single
blank line and strip leading blank lines at the start of files.  Applied
the updated formatter across libc CMake files.

Assisted-by: Automated tooling, human reviewed.
---
 libc/cmake/modules/LLVMLibCFlagRules.cmake   |  1 -
 libc/cmake/modules/LLVMLibCHeaderRules.cmake |  1 -
 libc/cmake/modules/LLVMLibCObjectRules.cmake |  2 -
 libc/cmake/modules/LLVMLibCTestRules.cmake   |  3 --
 libc/src/__support/FPUtil/CMakeLists.txt     |  1 -
 libc/src/__support/HashTable/CMakeLists.txt  |  1 -
 libc/src/__support/math/CMakeLists.txt       |  5 --
 libc/src/__support/mathvec/CMakeLists.txt    |  1 -
 libc/src/complex/generic/CMakeLists.txt      |  1 -
 libc/src/math/generic/CMakeLists.txt         |  3 --
 libc/src/stdbit/CMakeLists.txt               |  1 -
 libc/src/stdio/scanf_core/CMakeLists.txt     |  1 -
 libc/src/sys/socket/CMakeLists.txt           |  1 -
 libc/src/wchar/CMakeLists.txt                |  1 -
 libc/test/src/__support/CMakeLists.txt       |  1 -
 libc/test/src/__support/CPP/CMakeLists.txt   |  1 -
 libc/test/src/math/smoke/CMakeLists.txt      |  3 --
 libc/test/src/stdio/CMakeLists.txt           |  1 -
 libc/test/src/unistd/CMakeLists.txt          |  1 -
 libc/test/src/wchar/CMakeLists.txt           |  1 -
 libc/utils/cmake_format.py                   |  9 ++--
 libc/utils/cmake_format_test.py              | 56 ++++++++++++++++++++
 22 files changed, 61 insertions(+), 35 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCFlagRules.cmake b/libc/cmake/modules/LLVMLibCFlagRules.cmake
index 23fc9215d13c9e..c658c077372eca 100644
--- a/libc/cmake/modules/LLVMLibCFlagRules.cmake
+++ b/libc/cmake/modules/LLVMLibCFlagRules.cmake
@@ -25,7 +25,6 @@
 # To completely disable a flag FLAG_NAME expansion, set the variable
 #   SKIP_FLAG_EXPANSION_FLAG_NAME=TRUE in this file.
 
-
 function(extract_flag_modifier input_flag output_flag modifier)
   if(${input_flag} MATCHES "__NO$")
     string(REGEX REPLACE "__NO$" "" flag "${input_flag}")
diff --git a/libc/cmake/modules/LLVMLibCHeaderRules.cmake b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
index 9e3845da27fa9f..fde0ff3c1a33f4 100644
--- a/libc/cmake/modules/LLVMLibCHeaderRules.cmake
+++ b/libc/cmake/modules/LLVMLibCHeaderRules.cmake
@@ -161,5 +161,4 @@ function(add_gen_header target_name)
       DEPS "${fq_deps_list}"
   )
 
-
 endfunction()
diff --git a/libc/cmake/modules/LLVMLibCObjectRules.cmake b/libc/cmake/modules/LLVMLibCObjectRules.cmake
index ff701921c784b7..28c0441bf401b0 100644
--- a/libc/cmake/modules/LLVMLibCObjectRules.cmake
+++ b/libc/cmake/modules/LLVMLibCObjectRules.cmake
@@ -28,7 +28,6 @@ function(check_entrypoint_deps entrypoint_deps)
   set(${entrypoint_deps} ${PUBLIC_DEPS} PARENT_SCOPE)
 endfunction()
 
-
 # Rule which is essentially a wrapper over add_library to compile a set of
 # sources to object files.
 # Usage:
@@ -152,7 +151,6 @@ function(add_object_library target_name)
     ${ARGN})
 endfunction()
 
-
 # A rule for entrypoint object targets.
 # Usage:
 #     add_entrypoint_object(
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index f86f79af2d3bd2..6281f8196e6b92 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -880,7 +880,6 @@ function(create_libc_test fq_target_name)
   )
 endfunction()
 
-
 # Rule to add a fuzzer test.
 # Usage
 #    add_libc_fuzzer(
@@ -909,7 +908,6 @@ function(add_libc_fuzzer target_name)
     list(APPEND link_libraries mpfr gmp)
   endif()
 
-
   get_fq_target_name(${target_name} fq_target_name)
   get_fq_deps_list(fq_deps_list ${LIBC_FUZZER_DEPENDS} libc.src.errno.errno)
   get_object_files_for_test(
@@ -960,7 +958,6 @@ function(add_libc_fuzzer target_name)
 
 endfunction()
 
-
 # A unified function to add a libc test.
 # FULL_BUILD_ONLY: skip test in overlay mode.
 # OVERLAY_ONLY: skip test in full build mode.
diff --git a/libc/src/__support/FPUtil/CMakeLists.txt b/libc/src/__support/FPUtil/CMakeLists.txt
index fef304b5c5d768..f2d07ab222bab3 100644
--- a/libc/src/__support/FPUtil/CMakeLists.txt
+++ b/libc/src/__support/FPUtil/CMakeLists.txt
@@ -129,7 +129,6 @@ add_header_library(
     libc.src.__support.sign
 )
 
-
 add_header_library(
   sqrt
   HDRS
diff --git a/libc/src/__support/HashTable/CMakeLists.txt b/libc/src/__support/HashTable/CMakeLists.txt
index f6494dc6e877e9..3f00c382070d03 100644
--- a/libc/src/__support/HashTable/CMakeLists.txt
+++ b/libc/src/__support/HashTable/CMakeLists.txt
@@ -20,7 +20,6 @@ if(NOT ${getrandom_index} EQUAL -1)
     libc.hdr.errno_macros)
 endif()
 
-
 add_header_library(
   table
   HDRS
diff --git a/libc/src/__support/math/CMakeLists.txt b/libc/src/__support/math/CMakeLists.txt
index eb039efc77f544..7ec145524f2abf 100644
--- a/libc/src/__support/math/CMakeLists.txt
+++ b/libc/src/__support/math/CMakeLists.txt
@@ -749,7 +749,6 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
-
 add_header_library(
   bf16divl
   HDRS
@@ -1153,7 +1152,6 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
-
 add_header_library(
   fmin
   HDRS
@@ -1220,7 +1218,6 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
-
 add_header_library(
   fminimum
   HDRS
@@ -4171,7 +4168,6 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
-
 add_header_library(
   frexpf16
   HDRS
@@ -5077,7 +5073,6 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
-
 add_header_library(
   nextdown
   HDRS
diff --git a/libc/src/__support/mathvec/CMakeLists.txt b/libc/src/__support/mathvec/CMakeLists.txt
index dba7b6baba3d6b..fd58ce017e4948 100644
--- a/libc/src/__support/mathvec/CMakeLists.txt
+++ b/libc/src/__support/mathvec/CMakeLists.txt
@@ -178,7 +178,6 @@ add_header_library(
     libc.src.__support.common
 )
 
-
 add_header_library(
   expm1f
   HDRS
diff --git a/libc/src/complex/generic/CMakeLists.txt b/libc/src/complex/generic/CMakeLists.txt
index 51cf19065734a0..6a7b2b1a07d08f 100644
--- a/libc/src/complex/generic/CMakeLists.txt
+++ b/libc/src/complex/generic/CMakeLists.txt
@@ -52,7 +52,6 @@ add_entrypoint_object(
     libc.src.__support.math.atan2f
 )
 
-
 add_entrypoint_object(
   cproj
   SRCS
diff --git a/libc/src/math/generic/CMakeLists.txt b/libc/src/math/generic/CMakeLists.txt
index ac2da02e814acd..7c205b5d01211e 100644
--- a/libc/src/math/generic/CMakeLists.txt
+++ b/libc/src/math/generic/CMakeLists.txt
@@ -1,5 +1,3 @@
-
-
 add_entrypoint_object(
   canonicalize
   SRCS
@@ -278,7 +276,6 @@ add_entrypoint_object(
     libc.src.__support.math.dsqrtf128
 )
 
-
 add_entrypoint_object(
   dsubf128
   SRCS
diff --git a/libc/src/stdbit/CMakeLists.txt b/libc/src/stdbit/CMakeLists.txt
index 0c22b1d2617abb..8d365303d0e7ce 100644
--- a/libc/src/stdbit/CMakeLists.txt
+++ b/libc/src/stdbit/CMakeLists.txt
@@ -15,7 +15,6 @@ function(declare_dependencies prefixes dependencies)
   endforeach()
 endfunction()
 
-
 set(prefixes
   leading_zeros
   leading_ones
diff --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index 84d3998722ce3c..1203fe91458fa7 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -8,7 +8,6 @@ if(scanf_config_copts)
   list(PREPEND scanf_config_copts "COMPILE_OPTIONS")
 endif()
 
-
 list(APPEND file_deps libc.hdr.types.FILE)
 if(LIBC_TARGET_OS_IS_GPU)
   list(APPEND file_deps
diff --git a/libc/src/sys/socket/CMakeLists.txt b/libc/src/sys/socket/CMakeLists.txt
index 2806bb0a752ffb..1faf3af1a22a06 100644
--- a/libc/src/sys/socket/CMakeLists.txt
+++ b/libc/src/sys/socket/CMakeLists.txt
@@ -135,7 +135,6 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.recvmmsg
 )
 
-
 add_entrypoint_object(
   shutdown
   ALIAS
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index 938b7ad2e4c737..3bbb9d483b0492 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -246,7 +246,6 @@ add_entrypoint_object(
     libc.src.__support.libc_errno
 )
 
-
 add_entrypoint_object(
   mbrlen
   SRCS
diff --git a/libc/test/src/__support/CMakeLists.txt b/libc/test/src/__support/CMakeLists.txt
index c39aaf11f1725c..e4620f1c04d58f 100644
--- a/libc/test/src/__support/CMakeLists.txt
+++ b/libc/test/src/__support/CMakeLists.txt
@@ -186,7 +186,6 @@ add_libc_test(
     libc.src.__support.uint128
 )
 
-
 add_libc_test(
   str_to_integer_test
   SUITE
diff --git a/libc/test/src/__support/CPP/CMakeLists.txt b/libc/test/src/__support/CPP/CMakeLists.txt
index 5116f559e71c0a..5cc7e644568be4 100644
--- a/libc/test/src/__support/CPP/CMakeLists.txt
+++ b/libc/test/src/__support/CPP/CMakeLists.txt
@@ -132,7 +132,6 @@ add_libc_test(
     libc.src.__support.CPP.utility
 )
 
-
 # This test fails with invalid address space operations on sm_60
 if(NOT LIBC_TARGET_ARCHITECTURE_IS_NVPTX)
   add_libc_test(
diff --git a/libc/test/src/math/smoke/CMakeLists.txt b/libc/test/src/math/smoke/CMakeLists.txt
index 436e864342beef..0ba84d023e54e6 100644
--- a/libc/test/src/math/smoke/CMakeLists.txt
+++ b/libc/test/src/math/smoke/CMakeLists.txt
@@ -1987,7 +1987,6 @@ add_fp_unittest(
     libc.src.__support.FPUtil.bfloat16
 )
 
-
 add_fp_unittest(
   ufromfp_test
   SUITE
@@ -6476,7 +6475,6 @@ add_fp_unittest(
     libc.src.math.fsqrt
 )
 
-
 add_fp_unittest(
   fsqrtl_test
   SUITE
@@ -7120,7 +7118,6 @@ add_fp_unittest(
     libc.src.__support.FPUtil.bfloat16
 )
 
-
 add_fp_unittest(
   bf16sub_test
   SUITE
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index 663b87c5e690a9..584de1969b0030 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -577,7 +577,6 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
       libc.test.UnitTest.ErrnoSetterMatcher
   )
 
-
   add_libc_test(
     fdopen_test
     SUITE
diff --git a/libc/test/src/unistd/CMakeLists.txt b/libc/test/src/unistd/CMakeLists.txt
index 8d08b382b81047..20bdab4d5960e3 100644
--- a/libc/test/src/unistd/CMakeLists.txt
+++ b/libc/test/src/unistd/CMakeLists.txt
@@ -1023,7 +1023,6 @@ add_libc_test(
     libc.test.UnitTest.ErrnoSetterMatcher
 )
 
-
 add_libc_test(
   confstr_test
   SUITE
diff --git a/libc/test/src/wchar/CMakeLists.txt b/libc/test/src/wchar/CMakeLists.txt
index 558b2739930aba..52b40eeafa56d3 100644
--- a/libc/test/src/wchar/CMakeLists.txt
+++ b/libc/test/src/wchar/CMakeLists.txt
@@ -62,7 +62,6 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
 )
 
-
 add_libc_test(
   mbsrtowcs_test
   SUITE
diff --git a/libc/utils/cmake_format.py b/libc/utils/cmake_format.py
index 67c2f5b03afd33..82129982f660a8 100755
--- a/libc/utils/cmake_format.py
+++ b/libc/utils/cmake_format.py
@@ -48,7 +48,7 @@
   6. Multi-line Argument Layout: Keywords and positional args indented +2 spaces relative to call base; multi-value list items indented +4 spaces; closing `)` at +0 spaces.
   7. Control Block Indentation: 2-space indentation inside `if`/`foreach`/`function`/`macro`.
   8. Comment Formatting: Line comments buffer and align with the indentation level of the code element immediately following them, unless separated by a blank line (standalone comments) or immediately preceding a closing parenthesis `)`.
-  9. Cleanliness: Trailing whitespace stripped, single trailing newline for non-empty files; empty files preserved 0-byte.
+  9. Cleanliness: Trailing whitespace and leading blank lines stripped, consecutive blank lines collapsed to a single blank line, single trailing newline for non-empty files; empty files preserved 0-byte.
 
 Usage:
   cmake_format.py [options] <file|directory>...
@@ -1257,7 +1257,7 @@ def format_cmake_content(content: str, ctx: FormatterContext | None = None) -> s
     and lookahead comment buffering alignment.
 
     The function dispatches each source line to one of five handlers:
-      1. Empty line  — flush buffered comments and emit a blank line.
+      1. Empty line  — flush buffered comments and emit at most one blank line.
       2. Comment line — buffer the comment for lookahead alignment.
       3. Call continuation line — delegate to _process_call_continuation_line().
       4. Command invocation line — delegate to _process_command_invocation_line().
@@ -1291,13 +1291,14 @@ def format_cmake_content(content: str, ctx: FormatterContext | None = None) -> s
             state.current_call_state is not None and state.current_call_state.in_call
         )
 
-        # 1. Empty line — flush buffered comments and emit a blank line.
+        # 1. Empty line — flush buffered comments and emit at most one blank line.
         if not non_ws_toks:
             if state.pending_comments:
                 state.flush_comments(
                     INDENT_STR * (state.block_indent_level + (1 if in_call else 0))
                 )
-            state.formatted_lines.append("")
+            if state.formatted_lines and state.formatted_lines[-1] != "":
+                state.formatted_lines.append("")
             if (
                 state.current_call_state
                 and state.current_call_state.in_call
diff --git a/libc/utils/cmake_format_test.py b/libc/utils/cmake_format_test.py
index 7cf6d2e9ffb9d8..93eee15368b789 100644
--- a/libc/utils/cmake_format_test.py
+++ b/libc/utils/cmake_format_test.py
@@ -242,6 +242,62 @@ def test_9_3_empty_file_preservation(self):
         self.assertEqual(cmake_format.format_cmake_content(""), "")
         self.assertEqual(cmake_format.format_cmake_content("   \n\n  \t "), "")
 
+    def test_9_4_collapse_consecutive_blank_lines(self):
+        """Rule 9.4: Multiple consecutive blank lines are collapsed to a single blank line."""
+        code = (
+            "# Header comment\n\n\n"
+            "set(FOO bar)\n\n"
+            "   \n\t\n"
+            "if(FOO)\n\n\n"
+            "  add_entrypoint_object(\n"
+            "    target\n\n\n"
+            "    SRCS\n"
+            "      s1.cpp\n"
+            "  )\n"
+            "endif()\n"
+        )
+        expected = (
+            "# Header comment\n\n"
+            "set(FOO bar)\n\n"
+            "if(FOO)\n\n"
+            "  add_entrypoint_object(\n"
+            "    target\n\n"
+            "    SRCS\n"
+            "      s1.cpp\n"
+            "  )\n"
+            "endif()\n"
+        )
+        self.assertEqual(cmake_format.format_cmake_content(code), expected)
+
+    def test_9_5_strip_leading_blank_lines(self):
+        """Rule 9.5: Leading blank lines at the start of a file are stripped."""
+        self.assertEqual(
+            cmake_format.format_cmake_content("\n\nset(FOO bar)\n"),
+            "set(FOO bar)\n",
+        )
+        self.assertEqual(
+            cmake_format.format_cmake_content("  \n\t\n# Header\n\nset(FOO bar)\n"),
+            "# Header\n\nset(FOO bar)\n",
+        )
+
+    def test_9_6_preserve_blank_lines_in_multiline_literals(self):
+        """Rule 9.6: Consecutive blank lines inside quoted strings, bracket args, and bracket comments are preserved."""
+        code = (
+            "#[=[\n\n\n"
+            "  Comment block\n\n\n"
+            "]=]\n\n\n"
+            'set(DOC "line1\n\n\nline2")\n\n\n'
+            "set(RAW [=[raw1\n\n\nraw2]=])\n"
+        )
+        expected = (
+            "#[=[\n\n\n"
+            "  Comment block\n\n\n"
+            "]=]\n\n"
+            'set(DOC "line1\n\n\nline2")\n\n'
+            "set(RAW [=[raw1\n\n\nraw2]=])\n"
+        )
+        self.assertEqual(cmake_format.format_cmake_content(code), expected)
+
 
 class TestDynamicSchemaLearning(unittest.TestCase):
     """Dynamic Schema Learning Tests."""



More information about the libc-commits mailing list