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

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 24 20:05:15 PDT 2023


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

>From 8f9df29e1fdfdf49d4a420cc7ec7a28d6bba807f 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 27ad90c6879abec..1790a9df42b5d14 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1062,7 +1062,7 @@ void WhitespaceManager::alignTrailingComments() {
       const int OriginalSpaces =
           C.OriginalWhitespaceRange.getEnd().getRawEncoding() -
           C.OriginalWhitespaceRange.getBegin().getRawEncoding() -
-          C.Tok->NewlinesBefore;
+          C.Tok->LastNewlineOffset;
       assert(OriginalSpaces >= 0);
       const auto RestoredLineLength =
           C.StartOfTokenColumn + C.TokenLength + OriginalSpaces;
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