Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2023
Merged

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Sep 23, 2023

Fixes #67116.

@llvmbot
Copy link
Member

llvmbot commented Sep 23, 2023

@llvm/pr-subscribers-clang-format

Changes

Fixes #67116.


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

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+1-1)
  • (modified) clang/unittests/Format/FormatTestComments.cpp (+6)
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"

@owenca owenca reopened this Sep 25, 2023
@owenca owenca merged commit d3f8c88 into llvm:main Sep 25, 2023
@owenca owenca deleted the 67116 branch September 25, 2023 03:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

clang-format-17 AlignTrailingComments: Kind: Leave moves comments #3
3 participants