This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Fix formatting of macro definitions with a leading comment.
ClosedPublic

Authored by curdeius on Feb 3 2022, 10:03 AM.

Diff Detail

Event Timeline

curdeius requested review of this revision.Feb 3 2022, 10:03 AM
curdeius created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 3 2022, 10:03 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
curdeius updated this revision to Diff 405702.Feb 3 2022, 10:15 AM

Add a test case. Use bool instead of Previous token.

clang/lib/Format/UnwrappedLineParser.cpp
3574

I really don't like bitwise operations on bool. Hit some nasty bugs at my job.

3589

This does not change within the loop, is that on purpose? Then it should be an if around the loop.

curdeius added inline comments.Feb 3 2022, 12:01 PM
clang/lib/Format/UnwrappedLineParser.cpp
3574

Ok, will do.

3589

Oh, good catch. It was certainly not on purpose. And it means that I need to find a new test case that covers that (if appropriate). Ideas welcome!

curdeius planned changes to this revision.Feb 3 2022, 12:10 PM

I'll test it tomorrow but probably a define with a hash as the last character may do the job and show the faulty behaviour.

Indeed, I get the hash on the 2nd line merged into the first one in this test case:

verifyFormat("/* comment */ #define A (parentheses)\n"
            "#");

But! It's unrelated to whether PreviousWasComment is updated inside the loop or not.
Actually it should not be important because # always starts a preprocessor macro and no other statement (at least in C and C++).
On the other hand, FirstNonCommentOnLine should be updated in the loop but I still don't have a test case for this (again, because there's no statement that starts with #).

Interestingly, the problem I encountered with the test case above doesn't happen when there's a new line after #:

verifyFormat("/* comment */ #define A (parentheses)\n"
             "#\n");

Anyway, this merging happens due to something else, but I don't know yet what exactly.
Investigating...

curdeius updated this revision to Diff 406413.Feb 7 2022, 5:51 AM

Add tests, update in loop.

curdeius updated this revision to Diff 406415.Feb 7 2022, 5:55 AM
curdeius marked 2 inline comments as done.

Don't use |=.

clang/lib/Format/UnwrappedLineParser.cpp
3612

There it is again. ;)

The if is the same as above, isn't it?

curdeius updated this revision to Diff 406437.Feb 7 2022, 6:43 AM
curdeius marked an inline comment as done.

Clean up.

@HazardyKnusperkeks, sorry for that. Should be better now.

@HazardyKnusperkeks, sorry for that. Should be better now.

No problem here.

This revision is now accepted and ready to land.Feb 7 2022, 8:44 AM
This revision was landed with ongoing or failed builds.Feb 9 2022, 1:40 PM
This revision was automatically updated to reflect the committed changes.