This is an archive of the discontinued LLVM Phabricator instance.

Clang: Don't warn about unused private fields of types declared maybe_unused
ClosedPublic

Authored by hans on Aug 29 2023, 4:32 AM.

Details

Summary

The compiler should not warn on code such as:

class [[maybe_unused]] MaybeUnusedClass {};
class C {
  MaybeUnusedClass c;
};

Patch based on comments on the bug by Shafik and Aaron.

Fixes #61334

Diff Detail

Event Timeline

hans created this revision.Aug 29 2023, 4:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 4:32 AM
hans requested review of this revision.Aug 29 2023, 4:32 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 4:32 AM

Thank you for working on this! Mostly looks good, but I did have another test request. Also, please be sure to add a release note for the fix.

clang/test/SemaCXX/warn-unused-private-field.cpp
291–292

Let's try one more thing:

template <typename Ty, typename Uy>
class C2 {
  Ty c; // no-warning
  Uy d; // warning
};

class [[maybe_unused]] Good {};
class Bad {};

C2<Good, Bad> c;
hans updated this revision to Diff 554312.Aug 29 2023, 7:12 AM

Add release note.

clang/test/SemaCXX/warn-unused-private-field.cpp
291–292

The !FD->getParent()->isDependentContext() condition prevents the warning from firing on either C2<>::c or d. (It seems that was there from the beginning: https://github.com/llvm/llvm-project/commit/0baec549a3f49d8c04a0092917f758cc89ef238d#diff-9ced4fa47ee2b9c03b6996ce89a1d131c0f5b71013993bc582209f50d5e934daR1649)

aaron.ballman added inline comments.Aug 29 2023, 7:19 AM
clang/test/SemaCXX/warn-unused-private-field.cpp
291–292

Hmmm. I think that's intended to ensure we don't diagnose before we've instantiated the template, but once we instantiate with C2<Good, Bad> c;, the field decl is no longer in a dependent context and we should still diagnose when instantiating, right?

hans added inline comments.Aug 29 2023, 7:33 AM
clang/test/SemaCXX/warn-unused-private-field.cpp
291–292

I tried, and it's not diagnosing: https://godbolt.org/z/osnWbofY5

I'm guessing the motivation was that we shouldn't warn about things that depend on how the template is instantiated, since that could make it hard to write a warning-free template definition.

aaron.ballman accepted this revision.Aug 29 2023, 7:55 AM

LGTM!

clang/test/SemaCXX/warn-unused-private-field.cpp
291–292

I don't think this is your bug to fix, but I still think this is a bug. [[maybe_unused]] is precisely how you would get warning-free template definitions -- you'd put the attribute on the field itself instead of on the type passed in as a template argument.

This revision is now accepted and ready to land.Aug 29 2023, 7:55 AM
shafik accepted this revision.Aug 29 2023, 8:09 AM

LGTM

This revision was landed with ongoing or failed builds.Aug 30 2023, 12:07 AM
This revision was automatically updated to reflect the committed changes.

Thanks! I filed https://github.com/llvm/llvm-project/issues/65111 to track the template instantiation concerns