This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Don't move comments if AlignTrailingComments: Kind: Leave
ClosedPublic

Authored by mairacanal on Nov 30 2022, 9:36 AM.

Details

Summary

For comments that start after a new line, currently, the comments are
being indented. This happens because the OriginalWhitespaceRange
considers newlines on the range. Therefore, when AlignTrailingComments:
Kind: Leave, deduct the number of newlines before the token to calculate
the number of spaces for trailing comments.

Fixes llvm#59203

Diff Detail

Event Timeline

mairacanal created this revision.Nov 30 2022, 9:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 30 2022, 9:36 AM
mairacanal requested review of this revision.Nov 30 2022, 9:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 30 2022, 9:36 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
HazardyKnusperkeks added a project: Restricted Project.Nov 30 2022, 11:41 AM

Can you please add a test with more than one newline?

clang/unittests/Format/FormatTestComments.cpp
3065–3075

Makes it clearer what is happening here.

Can you please add a test with more than one newline?

Hi @HazardyKnusperkeks! Thanks for the feedback. It looks like Changes[i].NewlinesBefore values 1 even if I put multiple lines before the comment. Do you have any input on what could be causing this? From my point of view, it looks like the extra new lines are being removed before alignTrailingComments and then Changes[i].NewlinesBefore = 1, but I'm not really sure how to solve it.

Can you please add a test with more than one newline?

Hi @HazardyKnusperkeks! Thanks for the feedback. It looks like Changes[i].NewlinesBefore values 1 even if I put multiple lines before the comment. Do you have any input on what could be causing this? From my point of view, it looks like the extra new lines are being removed before alignTrailingComments and then Changes[i].NewlinesBefore = 1, but I'm not really sure how to solve it.

Try setting MaxEmptyLinesToKeep to a higher number.

Can you please add a test with more than one newline?

Hi @HazardyKnusperkeks! Thanks for the feedback. It looks like Changes[i].NewlinesBefore values 1 even if I put multiple lines before the comment. Do you have any input on what could be causing this? From my point of view, it looks like the extra new lines are being removed before alignTrailingComments and then Changes[i].NewlinesBefore = 1, but I'm not really sure how to solve it.

Try setting MaxEmptyLinesToKeep to a higher number.

We can also use two additional test cases, with MaxEmptyLinesToKeep and without, always good to see that it works.

This comment was removed by mairacanal.
mairacanal updated this revision to Diff 479055.EditedNov 30 2022, 1:11 PM
  • Instead of using Changes[i].NewlinesBefore use Changes[i].Tok->NewlinesBefore: this allows the correct behavior for test cases with multiple blank lines
  • Create test cases of multiple blank lines
  • Separate \n\n on test cases

Thanks, nice work.

Do you need someone to push it for you? In that case we need a name and an email address for the commit.

This revision is now accepted and ready to land.Nov 30 2022, 1:13 PM

Thanks, nice work.

Do you need someone to push it for you? In that case we need a name and an email address for the commit.

Yes, I would need someone to push for me. My name and address are Maíra Canal <mairacanal@riseup.net>. Thanks for the help!

@mairacanal
Thank you for the catch!

clang/unittests/Format/FormatTestComments.cpp
3101

This should probably be reset after this testcase to avoid side effects for other test cases.

mairacanal updated this revision to Diff 479312.Dec 1 2022, 8:40 AM

Reset Style.MaxEmptyLinesToKeep after the test case to avoid side effects for other test cases.

owenpan accepted this revision.Dec 1 2022, 2:24 PM

Please mark comments as done if you have addressed them.

clang/unittests/Format/FormatTestComments.cpp
3071
3078
3087
3096
3108
3116
mairacanal updated this revision to Diff 479455.Dec 1 2022, 3:37 PM
mairacanal marked 8 inline comments as done.

Remove \n from the end of the test cases

This revision was landed with ongoing or failed builds.Dec 1 2022, 4:07 PM
This revision was automatically updated to reflect the committed changes.