[PATCH]Clang-format AlignConsecutiveAssignment

Doak, Peter W. via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 7 12:12:01 PDT 2018


In testing clang-format with our code base QMCPACK (https://github.com/QMCPACK/qmcpack) we realized we would like consecutive identical compound assignments to be aligned as tok::equal assignments were.  The following patch accomplishes this. Not sure if this is the correct way to submit this, but this is my guess from reading the clang website.

Best,
Peter Doak

Index: include/clang/Format/Format.h
===================================================================
--- include/clang/Format/Format.h	(revision 339163)
+++ include/clang/Format/Format.h	(working copy)
@@ -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;
 
Index: lib/Format/WhitespaceManager.cpp
===================================================================
--- lib/Format/WhitespaceManager.cpp	(revision 339163)
+++ lib/Format/WhitespaceManager.cpp	(working copy)
@@ -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() {



More information about the cfe-commits mailing list