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)


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)
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to