[PATCH] D117421: [clang-format] Fix incorrect alignment of operator= overloads.

Elliott Maguire via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jan 16 22:30:42 PST 2022


glotchimo updated this revision to Diff 400439.
glotchimo added a comment.

Explicitly declare `FormatToken` type instead of using `auto`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117421/new/

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)) {
+          FormatToken *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.400439.patch
Type: text/x-patch
Size: 1526 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220117/c392fdab/attachment.bin>


More information about the cfe-commits mailing list