[clang] 0a0cc3c - [clang-format] Don't unwrap lines preceded by line comments

Owen Pan via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 14 19:16:41 PDT 2022


Author: Owen Pan
Date: 2022-03-14T19:16:29-07:00
New Revision: 0a0cc3c58a74f9574d9f4df244e3bd0a8e805945

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

LOG: [clang-format] Don't unwrap lines preceded by line comments

Fixes #53495

Differential Revision: https://reviews.llvm.org/D121576

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 6a46d850c3d5e..de6cdf4afd729 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -1079,13 +1079,15 @@ void WhitespaceManager::alignArrayInitializersRightJustified(
       // So in here we want to see if there is a brace that falls
       // on a line that was split. If so on that line we make sure that
       // the spaces in front of the brace are enough.
-      Changes[CellIter->Index].NewlinesBefore = 0;
-      Changes[CellIter->Index].Spaces = 0;
-      for (const auto *Next = CellIter->NextColumnElement; Next != nullptr;
-           Next = Next->NextColumnElement) {
-        Changes[Next->Index].Spaces = 0;
-        Changes[Next->Index].NewlinesBefore = 0;
-      }
+      const auto *Next = CellIter;
+      do {
+        const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
+        if (Previous && Previous->isNot(TT_LineComment)) {
+          Changes[Next->Index].Spaces = 0;
+          Changes[Next->Index].NewlinesBefore = 0;
+        }
+        Next = Next->NextColumnElement;
+      } while (Next);
       // Unless the array is empty, we need the position of all the
       // immediately adjacent cells
       if (CellIter != Cells.begin()) {

diff  --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 05427c6249749..354b60e27a5c7 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -19176,6 +19176,14 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) {
                "     {init1, init2, init3, init4}}\n"
                "};",
                Style);
+  // TODO: Fix the indentations below when this option is fully functional.
+  verifyFormat("int a[][] = {\n"
+               "    {\n"
+               "     {0, 2}, //\n"
+               " {1, 2}  //\n"
+               "    }\n"
+               "};",
+               Style);
   Style.ColumnLimit = 100;
   EXPECT_EQ(
       "test demo[] = {\n"


        


More information about the cfe-commits mailing list