This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix AlignArrayOfStructures + Cpp11BracedListStyle=false
ClosedPublic

Authored by galenelias on Aug 24 2023, 4:30 PM.

Details

Summary

Fixes: https://github.com/llvm/llvm-project/issues/57611

Currently AlignArrayOfStructures=Left is hard coding setting Spaces to
0 for the token following the initial opening brace, but not touching
Spaces for the subsequent lines, which leads to the array being
misaligned. Additionally, it's not adding a space before the trailing
} which is generally done when Cpp11BracedListStyle=false.

I'm not exactly sure why this function needs to override the Spaces as
it seems to generally already be set to either 0 or 1 according to
the other formatting settings, but I'm going with an explicit fix where
I just force the padding to 1 when Cpp11BracedListStyle=false.

AlignArrayOfStructures=Right doesn't have any alignment problems, but
isn't adding the expected padding around the braces either, so I'm
giving that the same treatment.

Diff Detail

Event Timeline

galenelias created this revision.Aug 24 2023, 4:30 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 24 2023, 4:30 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
galenelias requested review of this revision.Aug 24 2023, 4:30 PM
This revision is now accepted and ready to land.Aug 25 2023, 11:40 AM

I'm not exactly sure why this function needs to override the Spaces as it seems to generally already be set to either 0 or 1 according to the other formatting settings

If so, can we address the issue without the "explicit fix"?

clang/lib/Format/WhitespaceManager.cpp
1233
1251

Can we assert that Spaces == 0? If not, we should add a test case.

1307

Ditto.

clang/unittests/Format/FormatTest.cpp
20659
20899

Ditto.

owenpan added inline comments.Aug 25 2023, 4:14 PM
clang/lib/Format/WhitespaceManager.cpp
1251

We can't assert that, but setting Spaces here seems superfluous as it's set correctly below anyways?

galenelias marked an inline comment as done.Aug 26 2023, 10:10 PM
galenelias added inline comments.
clang/lib/Format/WhitespaceManager.cpp
1251

I admit I'm not super confident on my understanding of this code, but this setting of Spaces is not redundant with any below settings. If we set it to '3' for instance, that won't get overwritten later (because the other sets are all conditional, and don't hit for the } token).

So, I think this is still required (minus the issue of the existing 'Spaces' calculation from previous formatting pass seemingly already setting Spaces to the correct value).

clang/unittests/Format/FormatTest.cpp
20659

This is consistent with basically every single adjacent test in this function. While I agree that this is unnecessary, in general I error on the side of consistency with the surrounding tests. I'll defer to the maintainers, just wanted to make sure this is actually the preferred change given the numerous adjacent tests with this form.

owenpan added inline comments.Aug 26 2023, 10:39 PM
clang/unittests/Format/FormatTest.cpp
20659

If you rebase your patch, you'll see that the trailing newlines in the surrounding tests have been removed. (Even if they had not been removed, we still wouldn't want new tests to have superfluous trailing newlines.)

galenelias marked 4 inline comments as done.
galenelias added inline comments.
clang/unittests/Format/FormatTest.cpp
20659

Thanks for the information, looks like I missed your cleanup change by a day. In general some code-bases favor consistency over some extra gunk, so I tend to default to consistency, but good to know going forward. I've rebased now.

owenpan accepted this revision.Aug 27 2023, 8:17 PM
owenpan added inline comments.
clang/unittests/Format/FormatTest.cpp
20659

Np!

galenelias marked 4 inline comments as done.Aug 28 2023, 2:50 PM
owenpan retitled this revision from Fix AlignArrayOfStructures + Cpp11BracedListStyle=false to [clang-format] Fix AlignArrayOfStructures + Cpp11BracedListStyle=false.Aug 31 2023, 2:22 PM