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

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 13 13:41:06 PST 2023


Author: Owen Pan
Date: 2023-11-13T13:41:01-08:00
New Revision: fff993b7cfd01f733216bd81d2a18838b7a4a97f

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

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

Fixed #71825.

Added: 
    

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

Removed: 
    


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


        


More information about the cfe-commits mailing list