-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[Clang] handle [[warn_unused]] attribute for unused private fields #120734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@llvm/pr-subscribers-clang Author: Oleksandr T. (a-tarasyuk) ChangesFixes #62472 Full diff: https://github.com/llvm/llvm-project/pull/120734.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index a85ef60b7b58ba..c1d3596140721e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -707,6 +707,7 @@ Improvements to Clang's diagnostics
}
- Fix -Wdangling false positives on conditional operators (#120206).
+- Clang now diagnoses unused private fields with the ``[[warn_unused]]`` attribute (#GH62472).
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index c5a72cf812ebc9..760ed06fc1b858 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -3307,6 +3307,29 @@ void Sema::CheckShadowInheritedFields(const SourceLocation &Loc,
}
}
+template <typename AttrType>
+inline static bool HasAttribute(const QualType &T) {
+ if (const TagDecl *TD = T->getAsTagDecl())
+ return TD->hasAttr<AttrType>();
+ if (const TypedefType *TDT = T->getAs<TypedefType>())
+ return TDT->getDecl()->hasAttr<AttrType>();
+ return false;
+}
+
+inline static bool IsUnusedPrivateField(FieldDecl *FD) {
+ if (FD->getAccess() == AS_private && FD->getDeclName()) {
+ QualType FieldType = FD->getType();
+ if (HasAttribute<WarnUnusedAttr>(FieldType))
+ return true;
+
+ return !FD->isImplicit() && !FD->hasAttr<UnusedAttr>() &&
+ !FD->getParent()->isDependentContext() &&
+ !HasAttribute<UnusedAttr>(FieldType) &&
+ !InitializationHasSideEffects(*FD);
+ }
+ return false;
+}
+
NamedDecl *
Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
MultiTemplateParamsArg TemplateParameterLists,
@@ -3589,25 +3612,11 @@ Sema::ActOnCXXMemberDeclarator(Scope *S, AccessSpecifier AS, Declarator &D,
FieldDecl *FD = cast<FieldDecl>(Member);
FieldCollector->Add(FD);
- if (!Diags.isIgnored(diag::warn_unused_private_field, FD->getLocation())) {
+ if (!Diags.isIgnored(diag::warn_unused_private_field, FD->getLocation()) &&
+ IsUnusedPrivateField(FD)) {
// Remember all explicit private FieldDecls that have a name, no side
// effects and are not part of a dependent type declaration.
-
- auto DeclHasUnusedAttr = [](const QualType &T) {
- if (const TagDecl *TD = T->getAsTagDecl())
- return TD->hasAttr<UnusedAttr>();
- if (const TypedefType *TDT = T->getAs<TypedefType>())
- return TDT->getDecl()->hasAttr<UnusedAttr>();
- return false;
- };
-
- if (!FD->isImplicit() && FD->getDeclName() &&
- FD->getAccess() == AS_private &&
- !FD->hasAttr<UnusedAttr>() &&
- !FD->getParent()->isDependentContext() &&
- !DeclHasUnusedAttr(FD->getType()) &&
- !InitializationHasSideEffects(*FD))
- UnusedPrivateFields.insert(FD);
+ UnusedPrivateFields.insert(FD);
}
}
diff --git a/clang/test/SemaCXX/warn-unused-private-field.cpp b/clang/test/SemaCXX/warn-unused-private-field.cpp
index 0cc6f687f1b35f..93a285b159f740 100644
--- a/clang/test/SemaCXX/warn-unused-private-field.cpp
+++ b/clang/test/SemaCXX/warn-unused-private-field.cpp
@@ -329,3 +329,16 @@ class C {
MaybeUnusedTypedef t; // no-warning
};
}
+
+namespace GH62472 {
+class [[gnu::warn_unused]] S {
+public:
+ S();
+};
+
+class C {
+private:
+ const int i = 0; // expected-warning {{private field 'i' is not used}}
+ const S s; // expected-warning {{private field 's' is not used}}
+};
+}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This LGTM and I think it does the behavior we want. Aaron to confirm?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/73/builds/12607 Here is the relevant piece of the build log for the reference
|
Fixes #62472