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

Owen Pan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 14 00:22:14 PDT 2022


owenpan created this revision.
owenpan added reviewers: MyDeveloperDay, curdeius, HazardyKnusperkeks.
owenpan added a project: clang-format.
Herald added a project: All.
owenpan requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Fixes https://github.com/llvm/llvm-project/issues/53495.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D121576

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


Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -19058,6 +19058,14 @@
                "     {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"
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -1048,13 +1048,14 @@
       // 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) {
+      const auto *Next = CellIter;
+      do {
+        const FormatToken *Previous = Changes[Next->Index].Tok->Previous;
+        if (Previous && Previous->isNot(TT_LineComment))
+          Changes[Next->Index].NewlinesBefore = 0;
         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()) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121576.415017.patch
Type: text/x-patch
Size: 1847 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220314/5ea93e18/attachment.bin>


More information about the cfe-commits mailing list