[PATCH] D117421: Fix 31568 (i.e. incorrect alignment of operator= overloads).

Elliott Maguire via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 16 09:19:41 PST 2022


glotchimo created this revision.
glotchimo added a comment.
glotchimo updated this revision to Diff 400390.
glotchimo added reviewers: MyDeveloperDay, djasper.
glotchimo added a project: clang-format.
glotchimo added a subscriber: cfe-commits.
glotchimo published this revision for review.
Herald added a project: clang.

I don't know why, but `clang-format` reformatted most if not all of the long/block comments in `WhitespaceManager.cpp`. Will it be necessary for me to revert the changes to those comments and skip formatting when updating the diff so as to keep it minimal?


glotchimo added a comment.

Same change as before but without the changes to long and block comments.


Added a look-ahead for brackets in the operator= context to avoid incorrect alignment.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117421

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
@@ -16167,6 +16167,14 @@
   verifyFormat("int oneTwoThree = 123; // comment\n"
                "int oneTwo      = 12;  // comment",
                Alignment);
+  verifyFormat("int()           = default;\n"
+               "int &operator() = default;\n"
+               "int &operator=() {",
+               Alignment);
+  verifyFormat("int()           = default; // comment\n"
+               "int &operator() = default; // comment\n"
+               "int &operator=() {",
+               Alignment);
 
   // Bug 25167
   /* Uncomment when fixed
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -731,6 +731,16 @@
         if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
           return false;
 
+        // Do not align operator= overloads.
+        if (C.Tok->Previous && C.Tok->Previous->is(tok::kw_operator)) {
+          auto *Next = C.Tok->Next;
+          while (Next && Next->NewlinesBefore == 0) {
+            if (Next->Tok.is(tok::l_brace))
+              return false;
+            Next = Next->Next;
+          }
+        }
+
         return C.Tok->is(tok::equal);
       },
       Changes, /*StartAt=*/0, Style.AlignConsecutiveAssignments);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117421.400390.patch
Type: text/x-patch
Size: 1519 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220116/28f2b1a5/attachment.bin>


More information about the cfe-commits mailing list