This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Exclude template instantiations in modernize-use-override
ClosedPublic

Authored by PiotrZSL on Apr 10 2023, 2:37 AM.

Details

Summary

Added IgnoreTemplateInstantiations option to modernize-use-override
check. Allows to ignore virtual function overrides that are part of
template instantiations. This can be useful in cases where the use
of the "override" keyword is not appropriate or causes issues with
template specialization.

Fixes #38276.

Diff Detail

Event Timeline

PiotrZSL created this revision.Apr 10 2023, 2:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2023, 2:37 AM
Herald added a subscriber: xazax.hun. · View Herald Transcript
PiotrZSL requested review of this revision.Apr 10 2023, 2:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 10 2023, 2:37 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

I'm not sure this is really a great improvement.
If the override candidate originates in a templated base class I think we should still warn

struct Foo {
  virtual void foo();
};

template <typeame T>
struct DirectBase : T {
  virtual void foo();
};

DirectBase<Foo> Y; // This instantiation should not warn about the above declaration.

template<typename T>
struct Bar {
  virtual void bar();
};

template <typename T>
struct TemplateBase : Bar<T> {
  virtual void bar();
};

TemplateBase<int> Y; // This instantiation should warn about the above declaration.
                     // Or potentially configurable to either warn or ignore this case.
PiotrZSL added a comment.EditedApr 10 2023, 5:23 AM

Problem is that you may have specialization of Bar class wihtout bar base, and that specialization can be local to some compilation unit.
Like:

template<>
struct Bar<int> {
    void bar(); // function wihout virtual or no function at all.
};

And now your fix (override) won't compile.

Only configuration option possible to be done, is to check template instantiations or not, but this going to produce false-positives.
And probably we don't want false-positives in this check.

Problem is that you may have specialization of Bar class wihtout bar base, and that specialization can be local to some compilation unit.
Like:

template<>
struct Bar<int> {
    void bar(); // function wihout virtual or no function at all.
};

And now your fix (override) won't compile.

Only configuration option possible to be done, is to check template instantiations or not, but this going to produce false-positives.
And probably we don't want false-positives in this check.

My idea was to not check any template instantiations, instead look for template definitions with a template dependent base class that has potential override candidates.

I'd argue a (likely very rare) false positive that would prevent compilation is better than not diagnosing and then potentially(due to a later refactor) getting a near miss override candidate going undiagnosed and breaking the codebase.
It could also be nicer to just emit a warning with no fix, or a fix added to a note explaining the situation. (notes fixes aren't automatically applied when running clang tidy(and clang) for this reason.

Given the contention that is why i suggested this should just be user configurable.

PiotrZSL updated this revision to Diff 512529.Apr 11 2023, 11:09 AM
PiotrZSL edited the summary of this revision. (Show Details)

Introduce option IgnoreTemplateInstantiations.

Looks good, some small comments.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp
16

I think it's a bit more readable to write a normal comment explaining that these functions should not trigger a warning. Since you have a least one positive CHECK-MESSAGES below then the check should no longer complain that there aren't any.

17

I believe I saw a review comment in another patch that these are not needed?

PiotrZSL updated this revision to Diff 513866.Apr 15 2023, 2:16 AM
PiotrZSL marked 2 inline comments as done.

Remove not needed comments from tests, and added new comments

carlosgalvezp added inline comments.Apr 15 2023, 2:33 AM
clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp
47

Same as above, start/end markers seem to not be needed.

PiotrZSL updated this revision to Diff 513871.Apr 15 2023, 2:38 AM
PiotrZSL marked an inline comment as done.

Remove of fix start/end marker.

clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp
47

I will remove it, but note that If I do that, test will still pass if I comment out removal of virtual keyword in code.

carlosgalvezp accepted this revision.Apr 15 2023, 2:45 AM
carlosgalvezp added inline comments.
clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp
47

That's a good point, I didn't consider that. It's important that the test fails if that part is removed, so you are right and the markers are indeed needed. Sorry for the confusion! I can approve now, feel free to bring the markers back before landing.

This revision is now accepted and ready to land.Apr 15 2023, 2:45 AM