This is an archive of the discontinued LLVM Phabricator instance.

Make the -Wunused-template default.
Changes PlannedPublic

Authored by v.g.vassilev on Feb 7 2023, 1:17 PM.

Details

Reviewers
aaron.ballman
Summary

https://reviews.llvm.org/D29877 implements a useful -Wunused-template diagnostic detects unused internal linkage templates. This helps finding potential ODR issues in headers.

Many years ago we made the diagnostic optional because libcxx was not ready. I am hoping @ldionne to help with that if we have not cleaned up libcxx already.

Diff Detail

Repository
rC Clang

Event Timeline

v.g.vassilev created this revision.Feb 7 2023, 1:17 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 1:17 PM
v.g.vassilev requested review of this revision.Feb 7 2023, 1:17 PM
aaron.ballman added a reviewer: Restricted Project.Feb 23 2023, 10:09 AM

Precommit CI shows issues with the libc++ tests that need to be addressed. This should also come with a release note.

clang/test/SemaCXX/warn-func-not-needed.cpp
13

Why is this unused? f<long>() in foo() should cause this to be used, right?

How should a user silence this diagnostic without disabling it entirely?

The emitted warnings from the libc++ CI look like a false-positive to me. While the functions are never called, they are used in an unevaluated context. I would expect -Wunused warnings to only be emitted when I can just remove the code without problems, which doesn't seem to be the case here. It would probably just get turned off again by lots of people if there are too many false-positives, which I don't think is the goal here.

The emitted warnings from the libc++ CI look like a false-positive to me. While the functions are never called, they are used in an unevaluated context. I would expect -Wunused warnings to only be emitted when I can just remove the code without problems, which doesn't seem to be the case here. It would probably just get turned off again by lots of people if there are too many false-positives, which I don't think is the goal here.

From what I see is that most of the templates are marked with static which means internal linkage. Entities with internal linkage in header files are essentially different across translation units which is an ODR violation. I believe the discussion here gives more insights of how this works: https://reviews.llvm.org/D29877

Indeed the warning is noisy but it will potentially fix broken code which were unable to diagnose before. Especially that in some cases where static templates in header files are used as an idiom. In theory this approach can extend to cases described in https://wg21.link/p2691 where our inability to diagnose/fix these cases leads to some design decisions which may not be optimal.

v.g.vassilev marked an inline comment as done.Feb 23 2023, 10:37 AM
v.g.vassilev added inline comments.
clang/test/SemaCXX/warn-func-not-needed.cpp
13

Nobody uses foo.

The emitted warnings from the libc++ CI look like a false-positive to me. While the functions are never called, they are used in an unevaluated context. I would expect -Wunused warnings to only be emitted when I can just remove the code without problems, which doesn't seem to be the case here. It would probably just get turned off again by lots of people if there are too many false-positives, which I don't think is the goal here.

From what I see is that most of the templates are marked with static which means internal linkage. Entities with internal linkage in header files are essentially different across translation units which is an ODR violation. I believe the discussion here gives more insights of how this works: https://reviews.llvm.org/D29877

Indeed the warning is noisy but it will potentially fix broken code which were unable to diagnose before. Especially that in some cases where static templates in header files are used as an idiom. In theory this approach can extend to cases described in https://wg21.link/p2691 where our inability to diagnose/fix these cases leads to some design decisions which may not be optimal.

I missed the static at the beginning. That explains the warning, thanks! I agree this should be fixed. I'll look into making a patch to enable -Wunused-template and fix any problems. Hopefully there aren't too many.

v.g.vassilev marked an inline comment as done.Feb 23 2023, 10:40 AM

The emitted warnings from the libc++ CI look like a false-positive to me. While the functions are never called, they are used in an unevaluated context. I would expect -Wunused warnings to only be emitted when I can just remove the code without problems, which doesn't seem to be the case here. It would probably just get turned off again by lots of people if there are too many false-positives, which I don't think is the goal here.

From what I see is that most of the templates are marked with static which means internal linkage. Entities with internal linkage in header files are essentially different across translation units which is an ODR violation. I believe the discussion here gives more insights of how this works: https://reviews.llvm.org/D29877

Indeed the warning is noisy but it will potentially fix broken code which were unable to diagnose before. Especially that in some cases where static templates in header files are used as an idiom. In theory this approach can extend to cases described in https://wg21.link/p2691 where our inability to diagnose/fix these cases leads to some design decisions which may not be optimal.

I missed the static at the beginning. That explains the warning, thanks! I agree this should be fixed. I'll look into making a patch to enable -Wunused-template and fix any problems. Hopefully there aren't too many.

This would be awesome as I don't have a lot of bandwidth right now and that potentially will have quite positive impact (once people understand what they need to do). I am not sure if we could somehow emit a fix-it suggestion. So far I have failed in figuring out what's a good suggestion when using this static template idiom...

It looks like this warning is incompatible with -Wctad-maybe-unsupported. It warns that the deduction guide is unused, but the deduction guide is required suppress -Wctad-maybe-unsupported. https://godbolt.org/z/G8bMjYsbn

It looks like this warning is incompatible with -Wctad-maybe-unsupported. It warns that the deduction guide is unused, but the deduction guide is required suppress -Wctad-maybe-unsupported. https://godbolt.org/z/G8bMjYsbn

That should not be too hard to fix. It seems that just need to ignore CXXDeductionGuideDecl in Sema::ShouldRemoveFromUnused. Is that blocking you?

It looks like this warning is incompatible with -Wctad-maybe-unsupported. It warns that the deduction guide is unused, but the deduction guide is required suppress -Wctad-maybe-unsupported. https://godbolt.org/z/G8bMjYsbn

That should not be too hard to fix. It seems that just need to ignore CXXDeductionGuideDecl in Sema::ShouldRemoveFromUnused. Is that blocking you?

No, I just added a [[maybe_unused]]. D144667 should fix any libc++ problems.

Indeed the warning is noisy but it will potentially fix broken code which were unable to diagnose before. Especially that in some cases where static templates in header files are used as an idiom. In theory this approach can extend to cases described in https://wg21.link/p2691 where our inability to diagnose/fix these cases leads to some design decisions which may not be optimal.

We need to ensure the diagnostic is not so noisy that people disable it, though. That means a low false positive rate and a straight-forward way to silence the diagnostic on a case-by-case basis.

clang/test/SemaCXX/warn-func-not-needed.cpp
13

Ah, good point on foo not being used, but the question still stands -- how does the user silence this diagnostic? It's not at all uncommon to have a primary template with specializations where the TU only uses either the primary or a specialization, but not both (and certainly not all specializations).

Indeed the warning is noisy but it will potentially fix broken code which were unable to diagnose before. Especially that in some cases where static templates in header files are used as an idiom. In theory this approach can extend to cases described in https://wg21.link/p2691 where our inability to diagnose/fix these cases leads to some design decisions which may not be optimal.

We need to ensure the diagnostic is not so noisy that people disable it, though. That means a low false positive rate and a straight-forward way to silence the diagnostic on a case-by-case basis.

I agree, so far I have not seen false positives. All issues (except for -Wctad-maybe-unsupported as mentioned above) seemed real. Do you have a particular example in mind?

clang/test/SemaCXX/warn-func-not-needed.cpp
13

@philnik used [[maybe_unused]] which seemed reasonable to me for silencing the diagnostic. Maybe take a look at the changes done here: https://reviews.llvm.org/D144667

Indeed the warning is noisy but it will potentially fix broken code which were unable to diagnose before. Especially that in some cases where static templates in header files are used as an idiom. In theory this approach can extend to cases described in https://wg21.link/p2691 where our inability to diagnose/fix these cases leads to some design decisions which may not be optimal.

We need to ensure the diagnostic is not so noisy that people disable it, though. That means a low false positive rate and a straight-forward way to silence the diagnostic on a case-by-case basis.

I agree, so far I have not seen false positives. All issues (except for -Wctad-maybe-unsupported as mentioned above) seemed real. Do you have a particular example in mind?

The concrete example I have is the one under discussion from the test cases, but I was also speaking about the libc++ work that's going on as well. In general, it's best to try the diagnostic on some large-scale C++ projects to see what kind of results you get (compiling projects like LLVM, Qt, Kokkos, Chromium, etc). That usually helps spot other situations like -Wctad-maybe-unused etc.

clang/test/SemaCXX/warn-func-not-needed.cpp
13

That's reasonable if the interface is one the user controls, such as one within a .cpp file. But the situation I'm worried about is where the primary template and specializations live in a header file that's shared between multiple TUs. I don't think it's reasonable to expect users to put [[maybe_unused]] on the primary template and all specializations in that situation.

ldionne added inline comments.Feb 27 2023, 10:39 AM
clang/test/SemaCXX/warn-func-not-needed.cpp
13

Don't y'all find it weird to have to use [[maybe_unused]] on something that is only a declaration like those CTAD guides? And I agree with @aaron.ballman here: we provide headers that are used in various TUs, and we obviously never expect that the entirety of our headers is going to be used by every single TU.

In other words, we totally expect that those deduction guides will be unused in some cases, since it's entirely fine for a user not to use them but for us to still provide them. If I understood this correctly, this seems like a flaw in the warning that we should fix in Clang.

v.g.vassilev planned changes to this revision.Feb 28 2023, 1:07 PM
v.g.vassilev added inline comments.
clang/test/SemaCXX/warn-func-not-needed.cpp
13

Yes, I agree I am pretty sure we can fix the CTAD guides. I just need a few spare cycles...

ldionne removed reviewers: ldionne, Restricted Project.Sep 8 2023, 6:13 AM

[Github PR transition cleanup]

Dropping libc++ since this shouldn't affect libc++ since D144667.