This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Improve add_new_check.py to recognize more checks
ClosedPublic

Authored by LegalizeAdulthood on May 21 2022, 4:38 PM.

Details

Summary

When looking for whether or not a check provides fixits, the script
examines the implementation of the check. Some checks are not
implemented in source files that correspond one-to-one with the check
name, e.g. cert-dcl21-cpp. So if we can't find the check implementation
directly from the check name, open up the corresponding module file and
look for the class name that is registered with the check. Then consult
the file corresponding to the class name.

Some checks are derived from a base class that implements fixits. So if
we can't find fixits in the implementation file for a check, scrape out
the name of it's base class. If it's not ClangTidyCheck, then consult
the base class implementation to look for fixit support.

Fixes #55630

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptMay 21 2022, 4:38 PM
LegalizeAdulthood requested review of this revision.May 21 2022, 4:38 PM

Thanks for this! It generally LGTM (though my Python skills are not particularly awesome, so take it with a grain of salt). Just a few questions at this point.

clang-tools-extra/clang-tidy/add_new_check.py
335–336

Do we have to check this or can we rely on open failing because it's not a file? (It tripped my psychic TOCTOU sensor.)

352

It's a bit early for me to fully grok regex, but: is this going to handle line continuations/newlines okay? I don't know if those show up in cases that matter right now, but I wanted to make sure it was being thought about.

371
LegalizeAdulthood marked 2 inline comments as done.May 23 2022, 7:40 AM
LegalizeAdulthood added inline comments.
clang-tools-extra/clang-tidy/add_new_check.py
335–336

If we don't check here, then it throws an exception when attempting to open the file.

352

\s matches any single whitespace character and \s* matches zero or more whitespace characters. I could sprinkle some more in between tokens, but this catches the existing code correctly.

LegalizeAdulthood marked 2 inline comments as done.May 23 2022, 7:59 AM

Another observation:
If some new pattern comes up and fixits aren't recognized for a check, it might be better
to switch to a whitelist for checks with fixits rather than going crazier on the file scraping.

clang-tools-extra/clang-tidy/add_new_check.py
352

There is some code like this: CheckFactories.registerCheck<bugprone::UnhandledSelfAssignmentCheck>("cert-oop54-cpp"); and I intentionally omit searching for this ([^:>]*) because these are aliases for other checks.

Which makes me wonder if we want to explicitly register aliases differently from regular checks? If you want to run a list of checks and exclude aliases (so the check doesn't run twice), how do you do that?

LegalizeAdulthood marked an inline comment as done.

Update from review comments

clang-tools-extra/clang-tidy/add_new_check.py
335–336

Also, I was following the existing pattern in this file :)

aaron.ballman accepted this revision.May 23 2022, 8:22 AM

LGTM, thank you!

clang-tools-extra/clang-tidy/add_new_check.py
335–336

Sounds like a good reason to leave it in then, thanks!

352

Which makes me wonder if we want to explicitly register aliases differently from regular checks? If you want to run a list of checks and exclude aliases (so the check doesn't run twice), how do you do that?

You can't, which was the subject of https://reviews.llvm.org/D114317 but it's complicated in practice.

This revision is now accepted and ready to land.May 23 2022, 8:22 AM
This revision was landed with ongoing or failed builds.May 23 2022, 8:48 AM
This revision was automatically updated to reflect the committed changes.