[PATCH] D43290: clang-format: tweak formatting of variable initialization blocks

Francois Ferrand via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 27 09:30:47 PST 2018


Typz added inline comments.


================
Comment at: lib/Format/TokenAnnotator.cpp:2183
       return 0;
+    if (Left.Previous && Left.Previous->is(tok::equal) &&
+        !Style.Cpp11BracedListStyle)
----------------
djasper wrote:
> Why is this necessary?
Otherwise the `PenaltyBreakBeforeFirstCallParameter` is used, which is not consistent with the concept of !Cpp11BraceListStyle (e.g. consider this is an initializer), and gives the following format, which is certainly not the expected result:

  const std::unordered_map<std::string, int>
      MyHashTable = { { \"aaaaaaaaaaaaaaaaaaaaa\", 0 },
                      { \"bbbbbbbbbbbbbbbbbbbbb\", 1 },
                      { \"ccccccccccccccccccccc\", 2 } };

(again, the issue only happens when `PenaltyBreakBeforeFirstCallParameter` is increased, e.g. 200 in my case. The default value is 19, so formatting is not affected)


================
Comment at: unittests/Format/FormatTest.cpp:6655
+  FormatStyle AvoidBreakingFirstArgument = getLLVMStyle();
+  AvoidBreakingFirstArgument.PenaltyBreakBeforeFirstCallParameter = 200;
+  verifyFormat("const std::unordered_map<std::string, int> MyHashTable =\n"
----------------
djasper wrote:
> How does this penalty influence the test?
Because breaking after the opening brace is affected by the `PenaltyBreakBeforeFirstCallParameter` : this is an init braced-list, but it is considered as any function.

Specifically, my patch needs (see prev. comment) to change the penalty for breaking after the brace, but this applies only when `Cpp11BracedListStyle=false`, as per your earlier comment. So this test just verify that the behavior is indeed not affected.


Repository:
  rC Clang

https://reviews.llvm.org/D43290





More information about the cfe-commits mailing list