This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Deal with keyword tokens in preprocessor conditions
ClosedPublic

Authored by LegalizeAdulthood on Apr 7 2022, 6:03 PM.

Details

Summary

When a "keyword" token like __restrict was present in a macro condition,
modernize-macro-to-enum would assert in non-release builds. However,
even for a "keyword" token, calling getIdentifierInfo()->getName() would
retrieve the text of the token, which is what we want. Our intention is
to scan names that appear in conditional expressions in potential enum
clusters and invalidate those clusters if they contain the name.

Also, guard against "raw identifiers" appearing as potential enums.
This shouldn't happen, but it doesn't hurt to generalize the code.

Fixes #54775

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptApr 7 2022, 6:03 PM
LegalizeAdulthood requested review of this revision.Apr 7 2022, 6:03 PM
njames93 added inline comments.
clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
281

inline is pretty redundant here. Did you mean to make this static?

287

Building a std::string here seems unnecessary, Why can't we just keep the StringRef.

291

Again, building a string is unnecessary, the StringRef equality operators will do the job nicely.

432

ditto.

LegalizeAdulthood marked 4 inline comments as done.Apr 8 2022, 11:44 AM
LegalizeAdulthood added inline comments.
clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
281

ReSharper flagged this as redundant as well, but I'm not sure I understand why inline is redundant here.

Static is definitely redundant because this is all inside an anonymous namespace block.

LegalizeAdulthood marked an inline comment as done.

Update from review comments

aaron.ballman accepted this revision.Apr 8 2022, 1:39 PM

LGTM!

clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
281

ReSharper flagged this as redundant as well, but I'm not sure I understand why inline is redundant here.

inline has no real semantic effect there -- it's an internal function so the optimizer is free to inline it at its whim if it wants.

Static is definitely redundant because this is all inside an anonymous namespace block.

We should correct that in a follow-up NFC commit, that's something we recommend against in the style guide (https://llvm.org/docs/CodingStandards.html#anonymous-namespaces) for exactly this scenario (it's not immediately clear when reading the declaration that it already had internal linkage).

This revision is now accepted and ready to land.Apr 8 2022, 1:39 PM
clang-tools-extra/clang-tidy/modernize/MacroToEnumCheck.cpp
281

I don't mind fixing it now since it's a new function

LegalizeAdulthood marked an inline comment as done.

Update from review comments