[PATCH] D34238: clang-format: Do not binpack initialization lists

Francois Ferrand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 15 07:15:52 PDT 2017


Typz created this revision.
Herald added a subscriber: klimek.

This patch tries to avoid binpacking when initializing lists/arrays,
to allow things like:

  static int types[] = {
      registerType1(),
      registerType2(),
      registerType3(),
  };
  std::map<int, std::string> x = {
      { 0, "foo fjakfjaklf kljj" },
      { 1, "bar fjakfjaklf kljj" },
      { 2, "stuff fjakfjaklf kljj" },
  };

This is similar to how dictionnaries are formatted, and actually
corresponds to the same conditions: when initializing a container (and
not just 'calling' a constructor).

Such formatting involves 2 things:

- Line breaks around the content of the block. This can be forced by

adding a comma or comment after the last element

- Elements should not be binpacked

This patch considers the block is an initializer list if it either
ends with a comma, or follows an assignment, which seems to provide a
sensible approximation.


https://reviews.llvm.org/D34238

Files:
  lib/Format/ContinuationIndenter.cpp


Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -999,9 +999,13 @@
     bool EndsInComma = Current.MatchingParen &&
                        Current.MatchingParen->Previous &&
                        Current.MatchingParen->Previous->is(tok::comma);
+    const FormatToken *PreviousNoComment = Current.getPreviousNonComment();
+    bool IsAfterAssignment = PreviousNoComment &&
+                             PreviousNoComment->getPrecedence() ==
+                                 prec::Assignment;
     AvoidBinPacking =
-        (Current.is(TT_ArrayInitializerLSquare) && EndsInComma) ||
-        Current.is(TT_DictLiteral) ||
+        (/*Current.is(TT_ArrayInitializerLSquare) && */EndsInComma) ||
+        IsAfterAssignment || Current.is(TT_DictLiteral) ||
         Style.Language == FormatStyle::LK_Proto || !Style.BinPackArguments ||
         (NextNoComment && NextNoComment->is(TT_DesignatedInitializerPeriod));
     if (Current.ParameterCount > 1)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34238.102670.patch
Type: text/x-patch
Size: 1098 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170615/99ee3183/attachment-0001.bin>


More information about the cfe-commits mailing list