https://github.com/earnol updated https://github.com/llvm/llvm-project/pull/208324
>From 1e61257d4d352c99884e39b37c60008e01ac1072 Mon Sep 17 00:00:00 2001 From: Vladislav Aranov <[email protected]> Date: Fri, 10 Jul 2026 21:54:29 +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 | 14 +++++- clang/unittests/Format/FormatTestComments.cpp | 46 +++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp index 5596532556538..9411a67a3c847 100644 --- a/clang/lib/Format/WhitespaceManager.cpp +++ b/clang/lib/Format/WhitespaceManager.cpp @@ -1033,8 +1033,20 @@ void WhitespaceManager::alignTrailingComments() { Newlines = 0; } } - if (!C.IsTrailingComment) + if (!C.IsTrailingComment) { + // A non-trailing comment on its own line or a closing brace should break + // the trailing comment alignment sequence, preventing cross-block + // alignment via OverEmptyLines. + if (C.NewlinesBefore > 0 && + (C.Tok->is(tok::comment) || C.Tok->is(tok::r_brace))) { + alignTrailingComments(StartOfSequence, I, MinColumn); + MinColumn = 0; + MaxColumn = INT_MAX; + StartOfSequence = I + 1; + Newlines = 0; + } continue; + } if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) { const int OriginalSpaces = diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp index d9a9f4c18efec..398188afc1ee3 100644 --- a/clang/unittests/Format/FormatTestComments.cpp +++ b/clang/unittests/Format/FormatTestComments.cpp @@ -3007,6 +3007,52 @@ TEST_F(FormatTestComments, AlignTrailingCommentsAcrossEmptyLines) { Style); } +TEST_F(FormatTestComments, NonTrailingCommentBreaksAlignment) { + // Regression test for https://llvm.org/PR208266 + FormatStyle Style = getGoogleStyle(FormatStyle::LK_Proto); + Style.AlignConsecutiveAssignments.Enabled = true; + Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always; + Style.AlignTrailingComments.OverEmptyLines = 4; + Style.ColumnLimit = 0; + + verifyNoChange("enum E {\n" + " A = 0; /* e */\n" + "}\n" + "/* c */\n" + "message M {\n" + " int32 a = 1; /* f */\n" + " int32 abcd = 2; /* g */\n" + "}", + Style); + + Style = getLLVMStyle(); + Style.AlignConsecutiveAssignments.Enabled = true; + Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always; + Style.AlignTrailingComments.OverEmptyLines = 4; + Style.ColumnLimit = 0; + + verifyNoChange("void f() {\n" + " int a = 0; /* e */\n" + "}\n" + "/* c */\n" + "void g() {\n" + " int b = 1; /* f */\n" + " int abcd = 2; /* g */\n" + "}", + Style); + + // OverEmptyLines should not align trailing comments across block boundaries + // even without an intervening comment separator. + verifyNoChange("void f() {\n" + " int a = 0; /* e */\n" + "}\n" + "void g() {\n" + " int b = 1; /* f */\n" + " int abcd = 2; /* 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
