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

via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 5 09:21:38 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 1/3] [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;

>From 0a7349318c816a5ce2d338a55ea0830767fae644 Mon Sep 17 00:00:00 2001
From: guillem-bartina-sonarsource <guillem.bartrina at sonarsource.com>
Date: Fri, 5 Apr 2024 18:19:46 +0200
Subject: [PATCH 2/3] Remove constructor name from diagnostic

---
 clang/include/clang/Basic/DiagnosticSemaKinds.td |  2 +-
 clang/lib/Sema/Sema.cpp                          |  9 +++++----
 clang/test/SemaCXX/warn-unused-filescoped.cpp    | 12 ++++++------
 3 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 703803ad2c1212..98a33f01132d23 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 %select{member function|constructor}0 %1">,
+def warn_unused_member_function : Warning<"unused %select{constructor|member function %1}0">,
   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 163ab48998d5e0..ca2f8daf0fc083 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -1399,11 +1399,12 @@ void Sema::ActOnEndOfTranslationUnit() {
             Diag(DiagD->getLocation(), diag::warn_unused_template)
                 << /*function=*/0 << DiagD << DiagRange;
           else {
-            if (isa<CXXMethodDecl>(DiagD))
+            if (isa<CXXConstructorDecl>(DiagD))
               Diag(DiagD->getLocation(), diag::warn_unused_member_function)
-                  << (!isa<CXXConstructorDecl>(DiagD) ? /*member function=*/0
-                                                      : /*constructor=*/1)
-                  << DiagD << DiagRange;
+                  << /*constructor=*/0 << DiagRange;
+            else if (isa<CXXMethodDecl>(DiagD))
+              Diag(DiagD->getLocation(), diag::warn_unused_member_function)
+                  << /*member function=*/1 << DiagD << DiagRange;
             else
               Diag(DiagD->getLocation(), diag::warn_unused_function)
                   << DiagD << DiagRange;
diff --git a/clang/test/SemaCXX/warn-unused-filescoped.cpp b/clang/test/SemaCXX/warn-unused-filescoped.cpp
index b3d1bb4661a5f4..12020b88dbaae4 100644
--- a/clang/test/SemaCXX/warn-unused-filescoped.cpp
+++ b/clang/test/SemaCXX/warn-unused-filescoped.cpp
@@ -80,15 +80,15 @@ struct S {
   struct CS {
     CS() {}
     CS(bool a) {}
-    CS(int b) {} // expected-warning{{unused constructor 'CS'}}
+    CS(int b) {} // expected-warning{{unused constructor}}
     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() = default; // expected-warning{{unused constructor}}
+    DCS(bool a) : CS(a) {} // expected-warning{{unused constructor}}
     DCS(const DCS&) {}
-    DCS(DCS&&) {} // expected-warning{{unused constructor 'DCS'}}
+    DCS(DCS&&) {} // expected-warning{{unused constructor}}
   };
 
   template<typename T>
@@ -96,12 +96,12 @@ struct S {
     TCS();
   };
   template <typename T> TCS<T>::TCS() {}
-  template <> TCS<int>::TCS() {} // expected-warning{{unused constructor 'TCS'}}
+  template <> TCS<int>::TCS() {} // expected-warning{{unused constructor}}
 }
 
 void S::m3() {} // expected-warning{{unused member function 'm3'}}
 
-CS::CS(float c) {} // expected-warning{{unused constructor 'CS'}}
+CS::CS(float c) {} // expected-warning{{unused constructor}}
 
 static inline void f4() {} // expected-warning{{unused function 'f4'}}
 const unsigned int cx = 0; // expected-warning{{unused variable 'cx'}}

>From 0e5e09879ea11c9073e5ef08c032fbf150c215e5 Mon Sep 17 00:00:00 2001
From: guillem-bartina-sonarsource <guillem.bartrina at sonarsource.com>
Date: Fri, 5 Apr 2024 18:21:03 +0200
Subject: [PATCH 3/3] Add release note

---
 clang/docs/ReleaseNotes.rst | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 3b89d5a8720785..af1edbf84ffcbe 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -228,6 +228,8 @@ Improvements to Clang's diagnostics
 - Clang now diagnoses lambda function expressions being implicitly cast to boolean values, under ``-Wpointer-bool-conversion``.
   Fixes #GH82512.
 
+- The ``-Wunused-member-function`` diagnostic now prints a message specific to the constructors.
+
 Improvements to Clang's time-trace
 ----------------------------------
 



More information about the cfe-commits mailing list