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

>From 2db57f76b7d81e6a8809c7cab5c143dd2a2c9c93 Mon Sep 17 00:00:00 2001
From: Vladislav Aranov <[email protected]>
Date: Wed, 22 Jul 2026 00:54:49 +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 | 52 +++++++++++++++++++
 2 files changed, 60 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..e226cb685319c 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3007,6 +3007,58 @@ TEST_F(FormatTestComments, 
AlignTrailingCommentsAcrossEmptyLines) {
                Style);
 }
 
+TEST_F(FormatTestComments, OverEmptyLinesBreaksAlignmentAtBlockBoundaries) {
+  // Regression test for https://llvm.org/PR208266
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto);
+  Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
+  Style.AlignTrailingComments.OverEmptyLines = 3;
+  Style.ColumnLimit = 0;
+
+  verifyNoChange("enum E {\n"
+                 "  A = 0; /* e */\n"
+                 "}\n"
+                 "/* c */\n"
+                 "message M {\n"
+                 "  int32 a;    /* f */\n"
+                 "  int32 abcd; /* g */\n"
+                 "}",
+                 Style);
+
+  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;

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to