clang-format: Add ability to align assignment operators.

In Objective-C some style guides use a style where assignment operators are
aligned, in an effort to increase code readability. This patch adds an option
to the format library which allows this functionality. It is disabled by
default for all the included styles, so it must be explicitly enabled.

The option will change code such as:
  - (void)method {
      NSNumber *one = @1;
      NSNumber *twentyFive = @25;
  }

to:
  - (void)method {
      NSNumber *one        = @1;
      NSNumber *twentyFive = @25;
  }

Patch by Matt Oakes. Thank you!

Accidentally reformatted all the tests...

llvm-svn: 236100
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index 4896ad7..4baaab1 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -93,6 +93,7 @@
 
   std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
   calculateLineBreakInformation();
+  alignConsecutiveAssignments();
   alignTrailingComments();
   alignEscapedNewlines();
   generateChanges();
@@ -141,6 +142,96 @@
   }
 }
 
+// Walk through all of the changes and find sequences of "=" to align.  To do
+// so, keep track of the lines and whether or not an "=" was found on align. If
+// a "=" is found on a line, extend the current sequence. If the current line
+// cannot be part of a sequence, e.g. because there is an empty line before it
+// or it contains non-assignments, finalize the previous sequence.
+void WhitespaceManager::alignConsecutiveAssignments() {
+  if (!Style.AlignConsecutiveAssignments)
+    return;
+
+  unsigned MinColumn = 0;
+  unsigned StartOfSequence = 0;
+  unsigned EndOfSequence = 0;
+  bool FoundAssignmentOnLine = false;
+  bool FoundLeftParenOnLine = false;
+  unsigned CurrentLine = 0;
+
+  auto AlignSequence = [&] {
+    alignConsecutiveAssignments(StartOfSequence, EndOfSequence, MinColumn);
+    MinColumn = 0;
+    StartOfSequence = 0;
+    EndOfSequence = 0;
+  };
+
+  for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
+    if (Changes[i].NewlinesBefore != 0) {
+      CurrentLine += Changes[i].NewlinesBefore;
+      if (StartOfSequence > 0 &&
+          (Changes[i].NewlinesBefore > 1 || !FoundAssignmentOnLine)) {
+        EndOfSequence = i;
+        AlignSequence();
+      }
+      FoundAssignmentOnLine = false;
+      FoundLeftParenOnLine = false;
+    }
+
+    if ((Changes[i].Kind == tok::equal &&
+         (FoundAssignmentOnLine || ((Changes[i].NewlinesBefore > 0 ||
+                                     Changes[i + 1].NewlinesBefore > 0)))) ||
+        (!FoundLeftParenOnLine && Changes[i].Kind == tok::r_paren)) {
+      if (StartOfSequence > 0)
+        AlignSequence();
+    } else if (Changes[i].Kind == tok::l_paren) {
+      FoundLeftParenOnLine = true;
+      if (!FoundAssignmentOnLine && StartOfSequence > 0)
+        AlignSequence();
+    } else if (!FoundAssignmentOnLine && !FoundLeftParenOnLine &&
+               Changes[i].Kind == tok::equal) {
+      FoundAssignmentOnLine = true;
+      EndOfSequence = i;
+      if (StartOfSequence == 0)
+        StartOfSequence = i;
+
+      unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
+      MinColumn = std::max(MinColumn, ChangeMinColumn);
+    }
+  }
+
+  if (StartOfSequence > 0) {
+    EndOfSequence = Changes.size();
+    AlignSequence();
+  }
+}
+
+void WhitespaceManager::alignConsecutiveAssignments(unsigned Start,
+                                                    unsigned End,
+                                                    unsigned Column) {
+  bool AlignedAssignment = false;
+  int PreviousShift = 0;
+  for (unsigned i = Start; i != End; ++i) {
+    int Shift = 0;
+    if (Changes[i].NewlinesBefore > 0)
+      AlignedAssignment = false;
+    if (!AlignedAssignment && Changes[i].Kind == tok::equal) {
+      Shift = Column - Changes[i].StartOfTokenColumn;
+      AlignedAssignment = true;
+      PreviousShift = Shift;
+    }
+    assert(Shift >= 0);
+    Changes[i].Spaces += Shift;
+    if (i + 1 != Changes.size())
+      Changes[i + 1].PreviousEndOfTokenColumn += Shift;
+    Changes[i].StartOfTokenColumn += Shift;
+    if (AlignedAssignment) {
+      Changes[i].StartOfTokenColumn += PreviousShift;
+      if (i + 1 != Changes.size())
+        Changes[i + 1].PreviousEndOfTokenColumn += PreviousShift;
+    }
+  }
+}
+
 void WhitespaceManager::alignTrailingComments() {
   unsigned MinColumn = 0;
   unsigned MaxColumn = UINT_MAX;