[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 10 05:59:19 PDT 2024


https://github.com/guillem-bartina-sonarsource updated https://github.com/llvm/llvm-project/pull/84515

>From 473e8bbeaa8bcb4fb313a5cc75cc7a5de5367879 Mon Sep 17 00:00:00 2001
From: guillem-bartina-sonarsource <guillem.bartrina at sonarsource.com>
Date: Fri, 8 Mar 2024 17:16:56 +0100
Subject: [PATCH] [clang][Sema] Refine unused-member-function diagnostic
 message for constructors

---
 .../clang/Basic/DiagnosticSemaKinds.td        |  2 +-
 clang/lib/Sema/Sema.cpp                       | 15 ++++++++----
 clang/test/SemaCXX/warn-unused-filescoped.cpp | 23 +++++++++++++++++++
 3 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 9b5245695153ec..703803ad2c1212 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -400,7 +400,7 @@ def warn_unused_function : Warning<"unused function %0">,
   InGroup<UnusedFunction>, DefaultIgnore;
 def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">,
   InGroup<UnusedTemplate>, DefaultIgnore;
-def warn_unused_member_function : Warning<"unused member function %0">,
+def warn_unused_member_function : Warning<"unused %select{member function|constructor}0 %1">,
   InGroup<UnusedMemberFunction>, DefaultIgnore;
 def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">,
   InGroup<UsedButMarkedUnused>, DefaultIgnore;
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 720d5fd5f0428d..163ab48998d5e0 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1398,11 +1398,16 @@ void Sema::ActOnEndOfTranslationUnit() {
           if (FD->getDescribedFunctionTemplate())
             Diag(DiagD->getLocation(), diag::warn_unused_template)
                 << /*function=*/0 << DiagD << DiagRange;
-          else
-            Diag(DiagD->getLocation(), isa<CXXMethodDecl>(DiagD)
-                                           ? diag::warn_unused_member_function
-                                           : diag::warn_unused_function)
-                << DiagD << DiagRange;
+          else {
+            if (isa<CXXMethodDecl>(DiagD))
+              Diag(DiagD->getLocation(), diag::warn_unused_member_function)
+                  << (!isa<CXXConstructorDecl>(DiagD) ? /*member function=*/0
+                                                      : /*constructor=*/1)
+                  << DiagD << DiagRange;
+            else
+              Diag(DiagD->getLocation(), diag::warn_unused_function)
+                  << DiagD << DiagRange;
+          }
         }
       } else {
         const VarDecl *DiagD = cast<VarDecl>(*I)->getDefinition();
diff --git a/clang/test/SemaCXX/warn-unused-filescoped.cpp b/clang/test/SemaCXX/warn-unused-filescoped.cpp
index be8d350855c078..b3d1bb4661a5f4 100644
--- a/clang/test/SemaCXX/warn-unused-filescoped.cpp
+++ b/clang/test/SemaCXX/warn-unused-filescoped.cpp
@@ -76,10 +76,33 @@ struct S {
   struct SVS : public VS {
     void vm() { }
   };
+
+  struct CS {
+    CS() {}
+    CS(bool a) {}
+    CS(int b) {} // expected-warning{{unused constructor 'CS'}}
+    CS(float c);
+  };
+
+  struct DCS : public CS {
+    DCS() = default; // expected-warning{{unused constructor 'DCS'}}
+    DCS(bool a) : CS(a) {} // expected-warning{{unused constructor 'DCS'}}
+    DCS(const DCS&) {}
+    DCS(DCS&&) {} // expected-warning{{unused constructor 'DCS'}}
+  };
+
+  template<typename T>
+  struct TCS {
+    TCS();
+  };
+  template <typename T> TCS<T>::TCS() {}
+  template <> TCS<int>::TCS() {} // expected-warning{{unused constructor 'TCS'}}
 }
 
 void S::m3() {} // expected-warning{{unused member function 'm3'}}
 
+CS::CS(float c) {} // expected-warning{{unused constructor 'CS'}}
+
 static inline void f4() {} // expected-warning{{unused function 'f4'}}
 const unsigned int cx = 0; // expected-warning{{unused variable 'cx'}}
 const unsigned int cy = 0;



More information about the cfe-commits mailing list