This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Generalize TransformerClangTidyCheck to take a rule generator.
ClosedPublic

Authored by ymandel on Jun 13 2019, 10:38 AM.

Details

Summary

Tidy check behavior often depends on language and/or clang-tidy options. This revision allows a user of TranformerClangTidyCheck to pass rule _generator_ in place of a rule, where the generator takes both the language and clang-tidy options. Additionally, the generator returns an Optional to allow for the case where the check is deemed irrelevant/disable based on those options.

Diff Detail

Repository
rL LLVM

Event Timeline

ymandel created this revision.Jun 13 2019, 10:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 13 2019, 10:38 AM
Herald added a subscriber: xazax.hun. · View Herald Transcript
gribozavr accepted this revision.Jun 13 2019, 10:53 AM
gribozavr added inline comments.
clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
33 ↗(On Diff #204581)

Can we dispatch to the other constructor?

40 ↗(On Diff #204581)

Keep the definition order of constructors consistent with declaration order?

This revision is now accepted and ready to land.Jun 13 2019, 10:53 AM
ymandel marked an inline comment as done.Jun 13 2019, 11:03 AM

Thanks for the review!

clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
33 ↗(On Diff #204581)

I think not, but please correct me if I'm wrong (and clearly I should add a comment explaining this): in order to get meaningful results from getLangOpts and Options, we need the ClangTidyCheck() constructor to have been called. If we were to dispatch, it would look like:

: TransformerClangTidyCheck(MakeRule(getLangOpts(), Options), Name, Context) {}

which, if I understand correctly, means that MakeRule will access that data _before_ the check object is properly initialized.

That said, I can factor out the assertion into a method to avoid the redundancy. WDYT?

gribozavr added inline comments.Jun 13 2019, 1:36 PM
clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
33 ↗(On Diff #204581)

You're right. I don't think it is necessary to factor out the assertion -- up to you.

ymandel updated this revision to Diff 206253.Jun 24 2019, 10:14 AM
ymandel marked an inline comment as done.

Added tests.

ymandel updated this revision to Diff 206258.Jun 24 2019, 10:31 AM
ymandel marked 4 inline comments as done.

Adjust comments in test.

clang-tools-extra/clang-tidy/utils/TransformerClangTidyCheck.cpp
33 ↗(On Diff #204581)

Decided not to factor.

gribozavr accepted this revision.Jun 25 2019, 6:34 AM
gribozavr added inline comments.
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
92 ↗(On Diff #206258)

Would adding -std=c99 to ExtraArgs, and setting Filename to input.c work?

ymandel marked 2 inline comments as done.Jun 25 2019, 11:17 AM
ymandel added inline comments.
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
92 ↗(On Diff #206258)

No, nothing seems to work. I can't find a way to ever set bits in LangOptions. I'm rather confused why that's the case...

ymandel marked 2 inline comments as done.Jun 25 2019, 11:29 AM
ymandel added inline comments.
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
92 ↗(On Diff #206258)

I found the answer: getLangOpts() is not guaranteed to work at check-creation time. This makes sense, since a single check may be run against many different files. I'd say its a flaw in the API though since it seems like a feature of the check, when its really a feature of the file. We should update the API to warn about this and possibly improve it to avoid the confusion.

I'll update the code to save the std::function and invoke at registerMatchers() time instead.

ymandel marked an inline comment as done.Jun 25 2019, 12:05 PM
ymandel added inline comments.
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
92 ↗(On Diff #206258)

Never mind my previous point -- this only holds for the particular way that runCheckOnCode is implemented (which is arguably a bug). For the real clang tidy, LangOpts is a feature of the tool invocation and is therefore constant across the lifetime of the check.

I will send a change to runCheckOnCode that fixes this -- that is, ensures LangOpts is set *before* the Check is constructed, and then this will work correctly.

ymandel updated this revision to Diff 206510.Jun 25 2019, 12:50 PM

Rebase onto fix to runCheckOnCode.

gribozavr accepted this revision.Jun 26 2019, 2:55 AM
gribozavr added inline comments.
clang-tools-extra/unittests/clang-tidy/TransformerClangTidyCheckTest.cpp
69 ↗(On Diff #206510)

"Objective-C" (with a dash)

ymandel updated this revision to Diff 206668.Jun 26 2019, 7:42 AM

resp. to comments

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptJun 26 2019, 9:05 AM