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

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 24 20:07:27 PDT 2023


Author: Owen Pan
Date: 2023-09-24T20:07:23-07:00
New Revision: d3f8c88abe2ecc455165f0a32c389fec61730ace

URL: https://github.com/llvm/llvm-project/commit/d3f8c88abe2ecc455165f0a32c389fec61730ace
DIFF: https://github.com/llvm/llvm-project/commit/d3f8c88abe2ecc455165f0a32c389fec61730ace.diff

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

Fixes #67116.

Added: 
    

Modified: 
    clang/lib/Format/WhitespaceManager.cpp
    clang/unittests/Format/FormatTestComments.cpp

Removed: 
    


################################################################################
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