Skip to content
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

[clang-format] Fix a bug in aligning comments in vector of structs #72099

Merged
merged 1 commit into from
Nov 13, 2023

Conversation

owenca
Copy link
Contributor

@owenca owenca commented Nov 13, 2023

Fixed #71825.

@llvmbot
Copy link
Collaborator

llvmbot commented Nov 13, 2023

@llvm/pr-subscribers-clang-format

Author: Owen Pan (owenca)

Changes

Fixed #71825.


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

2 Files Affected:

  • (modified) clang/lib/Format/WhitespaceManager.cpp (+1-1)
  • (modified) clang/unittests/Format/FormatTest.cpp (+5)
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 764a068912e780a..32d8b97cc8dadb1 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1465,7 +1465,7 @@ WhitespaceManager::CellDescriptions WhitespaceManager::getCells(unsigned Start,
           // Account for the added token lengths
           Changes[j].Spaces = InitialSpaces - InitialTokenLength;
         }
-      } else if (C.Tok->is(tok::comment)) {
+      } else if (C.Tok->is(tok::comment) && C.Tok->NewlinesBefore == 0) {
         // Trailing comments stay at a space past the last token
         C.Spaces = Changes[i - 1].Tok->is(tok::comma) ? 1 : 2;
       } else if (C.Tok->is(tok::l_brace)) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 21df86bf2eace55..f531e6da6a5c31b 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -21076,6 +21076,11 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) {
                "    {7,  5,                  \"!!\"   }\n"
                "};",
                Style);
+  verifyFormat("Foo foo = {\n"
+               "    // comment\n"
+               "    {1, 2}\n"
+               "};",
+               Style);
 
   Style.ColumnLimit = 20;
   // FIXME: unstable test case

@owenca owenca merged commit fff993b into llvm:main Nov 13, 2023
4 checks passed
@owenca owenca deleted the 71825 branch November 13, 2023 21:41
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
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-18 AlignTrailingComments: Kind: Leave moves comments #4
3 participants