[clang] [clang-format] Fix OverEmptyLines aligning trailing comments across block boundaries (PR #208324)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 27 05:46:30 PDT 2026


https://github.com/earnol updated https://github.com/llvm/llvm-project/pull/208324

>From 9e5f87879920d9e047da6d98f429e9307b687f1d Mon Sep 17 00:00:00 2001
From: Vladislav Aranov <vladislav.aranov at ericsson.com>
Date: Mon, 27 Jul 2026 14:46:16 +0200
Subject: [PATCH] Break trailing comment alignment at non-trailing comments

A non-trailing block comment on its own line (e.g. between declarations)
should break the trailing comment alignment sequence. Before this fix,
such comments were silently skipped, allowing AlignTrailingComments with
OverEmptyLines to align comments across logically unrelated code blocks.

This is a regression introduced by bae9ddca4231 which correctly stopped
marking standalone block comments as trailing, but did not account for
their role as alignment sequence barriers.

Fixes https://github.com/llvm/llvm-project/issues/208266
---
 clang/lib/Format/WhitespaceManager.cpp        |  8 +++++
 clang/unittests/Format/FormatTestComments.cpp | 36 +++++++++++++++++++
 2 files changed, 44 insertions(+)

diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 5596532556538..cd2feb3a5964b 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1033,6 +1033,14 @@ void WhitespaceManager::alignTrailingComments() {
         Newlines = 0;
       }
     }
+    if (NewLineThreshold > 1 && C.Tok->is(BK_Block)) {
+      alignTrailingComments(StartOfSequence, I, MinColumn);
+      MinColumn = 0;
+      MaxColumn = INT_MAX;
+      StartOfSequence = I + 1;
+      Newlines = 0;
+      continue;
+    }
     if (!C.IsTrailingComment)
       continue;
 
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index d9a9f4c18efec..d5a43318626f4 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3007,6 +3007,42 @@ TEST_F(FormatTestComments, AlignTrailingCommentsAcrossEmptyLines) {
                Style);
 }
 
+TEST_F(FormatTestComments, OverEmptyLinesBreaksAlignmentAtBlockBoundaries) {
+  auto Style = getLLVMStyle();
+  EXPECT_EQ(Style.AlignTrailingComments.Kind, FormatStyle::TCAS_Always);
+  Style.AlignTrailingComments.OverEmptyLines = 3;
+  Style.AllowShortFunctionsOnASingleLine = {};
+
+  verifyNoChange("void f() {\n"
+                 "  int a; /* e */\n"
+                 "}\n"
+                 "/* c */\n"
+                 "void g() {\n"
+                 "  int b;    /* f */\n"
+                 "  int abcd; /* g */\n"
+                 "}",
+                 Style);
+
+  verifyFormat("void f() {\n"
+               "  int a; /* e */\n"
+               "}\n"
+               "void g() {\n"
+               "  int b;    /* f */\n"
+               "  int abcd; /* g */\n"
+               "}",
+               Style);
+
+  verifyFormat("struct A {\n"
+               "  int a; /* e */\n"
+               "};\n"
+               "int i; /* comment */\n"
+               "struct M {\n"
+               "  int b;    /* f */\n"
+               "  int abcd; /* g */\n"
+               "};",
+               Style);
+}
+
 TEST_F(FormatTestComments, AlignTrailingCommentsLeave) {
   FormatStyle Style = getLLVMStyle();
   Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Leave;



More information about the cfe-commits mailing list