This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Support parenthesized literals in modernize-macro-to-enum
ClosedPublic

Authored by LegalizeAdulthood on Apr 10 2022, 8:36 PM.

Details

Summary

When scanning a macro expansion to examine it as a candidate enum,
first strip off arbitrary matching parentheses from the outside in,
then examine what remains to see if it is Lit, +Lit, -Lit or ~Lit.
If not, reject it as a possible enum candidate.

Fixes #54843

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2022, 8:36 PM
LegalizeAdulthood requested review of this revision.Apr 10 2022, 8:36 PM

Would it not be better if these parens were stripped in the fixit as they are unnecessary in the enum value decl?

Would it not be better if these parens were stripped in the fixit as they are unnecessary in the enum value decl?

I prefer checks to do one thing only in a straightforward manner.

It's easier to place things before and after the macro expansion rather than modify the contents
of the expansion.

It doesn't feel like the responsibility of this check is to decide which parentheses are necessary or not.

aaron.ballman accepted this revision.Apr 11 2022, 11:26 AM

Would it not be better if these parens were stripped in the fixit as they are unnecessary in the enum value decl?

To me, that would be ideal, but not strictly necessary (and perhaps doesn't scale super well across all fix-its) because the code is still correct with the parens, just a bit ugly. We have other similar situations with fix-its (like formatting).

I prefer checks to do one thing only in a straightforward manner.

It's easier to place things before and after the macro expansion rather than modify the contents
of the expansion.

It doesn't feel like the responsibility of this check is to decide which parentheses are necessary or not.

Hmm, I'm more on the fence. On the one hand, yes. On the other hand, there's no automatic cleanup to remove parentheses in clang-format (and there's no way I would trust clang-format if it added one, frankly). This check is suggesting a fix-it and a fix-it that keeps the parentheses means the user may feel compelled to go and manually change their code anyway, which largely removes the benefit of having a fix-it in the first place. That said, the fix-it produces correct code, and we could add a readability-redundant-parentheses check to clang-tidy if we cared deeply about redundant parens. We've had at least one attempt at that I could remember (and I hazily recall there may have been a second attempt), so there's some interest in that. That would solve the problem in a more general way than expecting each author of a fix-it to consider parens individually.

Regardless, this is incremental progress and it solves a real bug. LGTM aside from reflowing a comment.

clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
351–354

Start at both ends and work towards the middle. This is clever, I like this!

clang-tools-extra/docs/clang-tidy/checks/modernize-macro-to-enum.rst
20–23

Reflowing the comment to 80 cols

This revision is now accepted and ready to land.Apr 11 2022, 11:26 AM
LegalizeAdulthood marked 2 inline comments as done.Apr 11 2022, 11:42 AM

Would it not be better if these parens were stripped in the fixit as they are unnecessary in the enum value decl?

To me, that would be ideal, but not strictly necessary (and perhaps doesn't scale super well across all fix-its) because the code is still correct with the parens, just a bit ugly. We have other similar situations with fix-its (like formatting).

I prefer checks to do one thing only in a straightforward manner.

It's easier to place things before and after the macro expansion rather than modify the contents
of the expansion.

It doesn't feel like the responsibility of this check is to decide which parentheses are necessary or not.

Hmm, I'm more on the fence.

Me too. I am just not a fan of an automated check doing "other things" to my code
that aren't advertised in the description. This is a macro-to-enum check not a redundant-parens
check.

BTW, many IDEs highlight unnecessary parentheses and have "quick fixes" to remove them,
ReSharper for C++ is one such IDE add-on that does this.

I'd prefer there to be a readability-redundant-parentheses check that removes unnecessary
parentheses across the board everywhere, rather than it being a weird side-effect of turning
my macros into enums.

Update from review comments

  • Update documentation to reflect multiple pairs of matching parens

Would it not be better if these parens were stripped in the fixit as they are unnecessary in the enum value decl?

To me, that would be ideal, but not strictly necessary (and perhaps doesn't scale super well across all fix-its) because the code is still correct with the parens, just a bit ugly. We have other similar situations with fix-its (like formatting).

I prefer checks to do one thing only in a straightforward manner.

It's easier to place things before and after the macro expansion rather than modify the contents
of the expansion.

It doesn't feel like the responsibility of this check is to decide which parentheses are necessary or not.

Hmm, I'm more on the fence.

Me too. I am just not a fan of an automated check doing "other things" to my code
that aren't advertised in the description. This is a macro-to-enum check not a redundant-parens
check.

What puts me on the fence here is -- your check adds the fix-it and it knows that the parens are unnecessary because there can be no order of operations or side effect issues. So yes, you're not writing a redundant parens check; you're writing a macro to enum check that proposes fixes that keep redundant parens when it could be argued that it's reasonable to remove them by not copying them into the fix-it in the first place.

BTW, many IDEs highlight unnecessary parentheses and have "quick fixes" to remove them,
ReSharper for C++ is one such IDE add-on that does this.

I'd prefer there to be a readability-redundant-parentheses check that removes unnecessary
parentheses across the board everywhere, rather than it being a weird side-effect of turning
my macros into enums.

I tend to come to this conclusion as well. The fix-it is syntactically valid and semantically correct, so it's not wrong to leave it the way it is.

This revision was landed with ongoing or failed builds.Apr 11 2022, 1:08 PM
This revision was automatically updated to reflect the committed changes.