[clang] [clang-format] Use formatted breaks to align trailing comments (PR #246023)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 26 02:32:37 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Raiyyan <3 (7se7en72025)

<details>
<summary>Changes</summary>

With Allman braces, the first formatting pass can pad a block comment after a closing brace to align it with a comment after `else`. The second pass removes that padding because the brace now starts a line in the input.

Use the formatter's recorded line-break decision when checking the closing brace. Fall back to the original whitespace when the token has no formatting decision.

The new regression failed before the change and passed afterward. All 54 comment formatting tests passed in a Windows build that reapplied this change to LLVM 23.1.6.

Fixes #<!-- -->245606.

Assisted-by: OpenAI Codex. Draft for contributor review.

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


2 Files Affected:

- (modified) clang/lib/Format/WhitespaceManager.cpp (+3-1) 
- (modified) clang/unittests/Format/FormatTestComments.cpp (+15) 


``````````diff
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 1d5aec62008238..64ac8c129fb168 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1135,7 +1135,9 @@ void WhitespaceManager::alignTrailingComments() {
 
       while (Tok->Previous && Tok->Previous->is(tok::r_brace))
         Tok = Tok->Previous;
-      return Tok->NewlinesBefore > 0;
+      // Use the formatted line break when one has been decided.
+      return Tok->getDecision() == FD_Break ||
+             (Tok->getDecision() == FD_Unformatted && Tok->NewlinesBefore > 0);
     };
 
     if (I > 0 && C.NewlinesBefore == 0 &&
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 3b281dc29fc39f..8743890a634333 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -2879,6 +2879,21 @@ TEST_F(FormatTestComments, AlignTrailingComments) {
                "int a; //");
 }
 
+TEST_F(FormatTestComments, DontAlignCommentAfterNewlyWrappedClosingBrace) {
+  auto Style = getLLVMStyle();
+  Style.BreakBeforeBraces = FormatStyle::BS_Allman;
+  verifyFormat("void f()\n"
+               "{\n"
+               "  if (true)\n"
+               "  {\n"
+               "  } /*a*/\n"
+               "  else /*b*/\n"
+               "  {\n"
+               "  }\n"
+               "}",
+               "void f(){if(true){}/*a*/else/*b*/{}}", Style);
+}
+
 TEST_F(FormatTestComments, AlignTrailingCommentsAcrossEmptyLines) {
   FormatStyle Style = getLLVMStyle();
   Style.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;

``````````

</details>


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


More information about the cfe-commits mailing list