[PATCH] D50403: AlignConsecutiveAssignments

Peter Doak via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 7 12:20:35 PDT 2018


PDoakORNL created this revision.
PDoakORNL added a project: clang.
Herald added a subscriber: cfe-commits.

Expanding AlignConsecutiveAssignments to compound assignments.


Repository:
  rC Clang

https://reviews.llvm.org/D50403

Files:
  include/clang/Format/Format.h
  lib/Format/WhitespaceManager.cpp


Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp
+++ lib/Format/WhitespaceManager.cpp
@@ -432,20 +432,26 @@
 void WhitespaceManager::alignConsecutiveAssignments() {
   if (!Style.AlignConsecutiveAssignments)
     return;
+  std::vector<tok::TokenKind> assignment_tokens =
+    {tok::equal, tok::pipeequal, tok::caretequal, tok::percentequal,
+     tok::ampequal, tok::plusequal, tok::minusequal, tok::starequal,
+     tok::slashequal, tok::lesslessequal, tok::greatergreaterequal};
+  for (auto assignment_token : assignment_tokens)
+  {
+    AlignTokens(Style,
+		[&](const Change &C) {
+		  // Do not align on equal signs that are first on a line.
+		  if (C.NewlinesBefore > 0)
+		    return false;
 
-  AlignTokens(Style,
-              [&](const Change &C) {
-                // Do not align on equal signs that are first on a line.
-                if (C.NewlinesBefore > 0)
-                  return false;
+		  // Do not align on equal signs that are last on a line.
+		  if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
+		    return false;
 
-                // Do not align on equal signs that are last on a line.
-                if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
-                  return false;
-
-                return C.Tok->is(tok::equal);
-              },
-              Changes, /*StartAt=*/0);
+		  return C.Tok->is(assignment_token);
+		},
+		Changes, /*StartAt=*/0);
+  }
 }
 
 void WhitespaceManager::alignConsecutiveDeclarations() {
Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h
+++ include/clang/Format/Format.h
@@ -80,12 +80,17 @@
 
   /// If ``true``, aligns consecutive assignments.
   ///
-  /// This will align the assignment operators of consecutive lines. This
-  /// will result in formattings like
+  /// This will align the declaration names of consecutive lines and
+  /// matching assignment operators. This includes consecutive |=, +=
+  /// -=, /=, *=. This will result in formattings like
   /// \code
   ///   int aaaa = 12;
   ///   int b    = 23;
   ///   int ccc  = 23;
+  ///
+  ///   int ddd += 12;
+  ///   int ee  += 22;
+  ///   int f   += 23;
   /// \endcode
   bool AlignConsecutiveAssignments;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50403.159569.patch
Type: text/x-patch
Size: 2374 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180807/e0c804c8/attachment.bin>


More information about the cfe-commits mailing list