[clang] [clang-format] Fix a bug in aligning trailing comments (PR #67221)

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 22 23:53:47 PDT 2023


https://github.com/owenca created https://github.com/llvm/llvm-project/pull/67221

Fixes #67116.

>From 9fa6ed9430896ee9164260227f0791986c750f0f Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Fri, 22 Sep 2023 23:47:23 -0700
Subject: [PATCH] [clang-format] Fix a bug in aligning trailing comments

Fixes #67116.
---
 clang/lib/Format/WhitespaceManager.cpp        | 2 +-
 clang/unittests/Format/FormatTestComments.cpp | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index b7bd8d27dc976b1..ccd60ac3b48c456 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1062,7 +1062,7 @@ void WhitespaceManager::alignTrailingComments() {
       auto OriginalSpaces =
           Changes[i].OriginalWhitespaceRange.getEnd().getRawEncoding() -
           Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding() -
-          Changes[i].Tok->NewlinesBefore;
+          Changes[i].Tok->LastNewlineOffset;
       unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
                                     Changes[i].TokenLength + OriginalSpaces;
       // If leaving comments makes the line exceed the column limit, give up to
diff --git a/clang/unittests/Format/FormatTestComments.cpp b/clang/unittests/Format/FormatTestComments.cpp
index 001c5bf5fcda14e..1198329b7b5a8f0 100644
--- a/clang/unittests/Format/FormatTestComments.cpp
+++ b/clang/unittests/Format/FormatTestComments.cpp
@@ -3053,6 +3053,12 @@ TEST_F(FormatTestComments, AlignTrailingCommentsLeave) {
                "}",
                Style);
 
+  Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
+  verifyNoChange("#define FOO    \\\n"
+                 "  /* foo(); */ \\\n"
+                 "  bar();",
+                 Style);
+
   // Allow to keep 2 empty lines
   Style.MaxEmptyLinesToKeep = 2;
   EXPECT_EQ("// do not touch\n"



More information about the cfe-commits mailing list