https://github.com/a-tarasyuk updated 
https://github.com/llvm/llvm-project/pull/120734

>From 55fb96c1673911f30721c2a53bea32e7e4b5dc6e Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.taras...@outlook.com>
Date: Fri, 20 Dec 2024 15:32:55 +0200
Subject: [PATCH 1/2] [Clang] handle [[warn_unused]] attribute for unused
 private fields

---
 clang/docs/ReleaseNotes.rst                   |  1 +
 clang/lib/Sema/SemaDeclCXX.cpp                | 43 +++++++++++--------
 .../SemaCXX/warn-unused-private-field.cpp     | 13 ++++++
 3 files changed, 40 insertions(+), 17 deletions(-)

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}}
+};
+}

>From ff26d33c97f9eaf7c1d2c21cf341cb771d831be5 Mon Sep 17 00:00:00 2001
From: Oleksandr T <oleksandr.taras...@outlook.com>
Date: Sat, 18 Jan 2025 01:46:03 +0200
Subject: [PATCH 2/2] add additional test cases

---
 .../SemaCXX/warn-unused-private-field.cpp     | 26 ++++++++++++++-----
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/clang/test/SemaCXX/warn-unused-private-field.cpp 
b/clang/test/SemaCXX/warn-unused-private-field.cpp
index 93a285b159f740..0bcca6b82227f9 100644
--- a/clang/test/SemaCXX/warn-unused-private-field.cpp
+++ b/clang/test/SemaCXX/warn-unused-private-field.cpp
@@ -1,6 +1,6 @@
-// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field 
-Wused-but-marked-unused -Wno-uninitialized -verify -std=c++11 %s
-// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field 
-Wused-but-marked-unused -Wno-uninitialized -verify -std=c++17 %s
-// RUN: %clang_cc1 -fsyntax-only -Wunused-private-field 
-Wused-but-marked-unused -Wno-uninitialized -verify -std=c++20 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused -Wused-but-marked-unused 
-Wno-uninitialized -verify -std=c++11 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused -Wused-but-marked-unused 
-Wno-uninitialized -verify -std=c++17 %s
+// RUN: %clang_cc1 -fsyntax-only -Wunused -Wused-but-marked-unused 
-Wno-uninitialized -verify -std=c++20 %s
 
 #if __cplusplus >= 202002L
 
@@ -108,7 +108,7 @@ class ClassWithTemplateFriend {
 template <typename T> class TemplateFriend {
 public:
   TemplateFriend(ClassWithTemplateFriend my_friend) {
-    int var = my_friend.used_by_friend_;
+    int var = my_friend.used_by_friend_; // expected-warning {{unused variable 
'var'}}
   }
 };
 
@@ -181,10 +181,10 @@ class EverythingUsed {
  public:
   EverythingUsed() : as_array_index_(0), var_(by_initializer_) {
     var_ = sizeof(sizeof_);
-    int *use = &by_reference_;
+    int *use = &by_reference_; // expected-warning {{unused variable 'use'}}
     int test[2];
     test[as_array_index_] = 42;
-    int EverythingUsed::*ptr = &EverythingUsed::by_pointer_to_member_;
+    int EverythingUsed::*ptr = &EverythingUsed::by_pointer_to_member_; // 
expected-warning {{unused variable 'ptr'}}
   }
 
   template<class T>
@@ -336,9 +336,21 @@ class [[gnu::warn_unused]] S {
   S();
 };
 
+struct [[maybe_unused]] T {};
+
+void f() {
+  int i = 0; // expected-warning {{unused variable 'i'}}
+  S s;       // expected-warning {{unused variable 's'}}
+  T t;       // ok
+}
+
 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}}
+  int j = 0;       // expected-warning {{private field 'j' is not used}}
+  const S s1;      // expected-warning {{private field 's1' is not used}}
+  const T t1;      // ok
+  S s2;            // expected-warning {{private field 's2' is not used}}
+  T t2;            // ok
 };
 }

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to