This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] fix indent in alignChainedConditionals
ClosedPublic

Authored by krasimir on Apr 22 2021, 2:06 AM.

Details

Summary

Clang-format was indenting the lines following the ? in the added test
case by +5 instead of +4. This only happens in a very specific
situation, where the ? is followed by a multiline block comment, as in
the example. This fix addresses this without regressing any of the
existing tests.

From my understanding on the original patch https://github.com/llvm/llvm-project/commit/4db94094b469b4715d08ef37f1799bf3ea7ca8ea together with examining affected test cases,
I believe the intention of this section is to handle, e.g., the alignment of eeeeeeeeeeeeeeeeee and bbbbbbbbbbbbbbbb and 3333333333333333 in cases like this, where the previous toke is the ::

return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :
                           ccccccccccccc     ? dddddddddddddddddd :
                                               eeeeeeeeeeeeeeeeee) :
       bbbbbbbbbbbbbbbb ? 2222222222222222 :
                          3333333333333333;

The check for tok::question that I remove in this context seems superfluous in the first place: I believe currently we don't support a style where chained ternaries are formatted with newlines after the ?-s and alignment across the operands on a new line after the line ending with ?.

Diff Detail

Event Timeline

krasimir requested review of this revision.Apr 22 2021, 2:06 AM
krasimir created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptApr 22 2021, 2:06 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
krasimir added a reviewer: sammccall.
sammccall accepted this revision.Apr 23 2021, 5:37 AM

Can't say I really understand this (either the before/after state).

It seems like we're trying to vertically-align a bunch of operands to a ?: chain, and this patch stops aligning the ones that follow a ? in certain cases. But I don't understand why this is correct, and what the point is of aligning (only) the ones following :.

Is there an intuitive explanation?

This revision is now accepted and ready to land.Apr 23 2021, 5:37 AM
krasimir added a comment.EditedApr 23 2021, 7:06 AM

I also have a limited understanding based on the original patch https://github.com/llvm/llvm-project/commit/4db94094b469b4715d08ef37f1799bf3ea7ca8ea together with examining affected test cases.
I believe the intention of this section is to handle, e.g., the alignment of eeeeeeeeeeeeeeeeee and bbbbbbbbbbbbbbbb and 3333333333333333 in cases like this, where the previous toke is the ::

return aaaaaaaaaaaaaaaa ? (aaaaaaaaaaaaaaaaa ? bbbbbbbbbbbbbbbbbb :
                           ccccccccccccc     ? dddddddddddddddddd :
                                               eeeeeeeeeeeeeeeeee) :
       bbbbbbbbbbbbbbbb ? 2222222222222222 :
                          3333333333333333;

The check for tok::question that I remove in this context seems superfluous in the first place: I believe currently we don't support a style where chained ternaries are formatted with newlines after the ?-s and alignment across the operands on a new line after the line ending with ?.

krasimir edited the summary of this revision. (Show Details)Apr 26 2021, 2:00 AM
This revision was automatically updated to reflect the committed changes.