This is an archive of the discontinued LLVM Phabricator instance.

clang-format: Fix C99 designated initializers corner cases
ClosedPublic

Authored by Typz on May 24 2017, 5:34 AM.

Details

Summary

This fixes the missing space before the designated initializer when Cpp11BracedListStyle=false :

const struct A a = { .a = 1, .b = 2 };
                    ^

Also, wrapping between opening brace and designated array initializers used to have an excessive penalty (like breaking between an expression and the subscript operator), leading to unexpected wrapping:

const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa =
    {[1] = aaaaaaaaaaaaaaaaaaaaaaaaaaa,
     [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbb};

instead of:

const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {
    [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaa,
    [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbb};

Finally, designated array initializers are not binpacked, just like designated member initializers.

Diff Detail

Repository
rL LLVM

Event Timeline

Typz created this revision.May 24 2017, 5:34 AM
Typz added a comment.May 24 2017, 5:36 AM

No fix yet, just wondering if this is an actual issue or the expected behavior.

unittests/Format/FormatTest.cpp
1526 ↗(On Diff #100072)

don't know why this test does not pass similarly to similar one with designated member access: in this case, clang-format puts each member in column.

Typz retitled this revision from clang-format: Fix C99 designated initializers when Cpp11BracedListStyle=false to clang-format: Fix C99 designated initializers corner cases.Jun 14 2017, 9:05 AM
Typz edited the summary of this revision. (Show Details)
Typz updated this revision to Diff 102559.Jun 14 2017, 9:07 AM
Typz marked an inline comment as done.
Typz edited the summary of this revision. (Show Details)

Implement fix

Typz added a comment.Jun 14 2017, 9:07 AM

Seems there is a special exception to BinPacking in case of designated member initializers.
Should BinPacking be disabled also in case of designated array initializers?

krasimir added subscribers: krasimir, cfe-commits.
Typz updated this revision to Diff 102803.EditedJun 16 2017, 4:46 AM

Fix incorrect labelling as ObjCMethodExpr
Do not binpack designated array initializers, just like designated member initializers

Typz edited the summary of this revision. (Show Details)Jun 16 2017, 4:47 AM
djasper added inline comments.Jun 19 2017, 3:21 AM
lib/Format/TokenAnnotator.cpp
339 ↗(On Diff #102803)

Use Style.isCpp().

unittests/Format/FormatTest.cpp
1526 ↗(On Diff #100072)

I don't understand what you mean here.

Typz updated this revision to Diff 103018.Jun 19 2017, 5:03 AM
Typz marked 2 inline comments as done.
Typz edited the summary of this revision. (Show Details)

Fix review comments

unittests/Format/FormatTest.cpp
1526 ↗(On Diff #100072)

I did not understand why designated member access [line 1516] was wrapped, while this did not happen with designated array access; but since then I have found out, and updated the code to handle designated array accesses similarly.

djasper accepted this revision.Jun 19 2017, 6:05 AM

Looks good.

This revision is now accepted and ready to land.Jun 19 2017, 6:05 AM
This revision was automatically updated to reflect the committed changes.