[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
@@ -402,7 +402,7 @@ def warn_unused_function : Warning<"unused function %0">, InGroup, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, DefaultIgnore; -def warn_unused_member_function : Warning<"unused member function %0">, +def warn_unused_member_function : Warning<"unused %select{constructor|member function %1}0">, AaronBallman wrote: I would recommend using `%sub{select_special_member_kind}1` instead of manually spelling out constructor; see `note_member_synthesized_at` for an example of it being used. https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
https://github.com/AaronBallman edited https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
@@ -76,10 +76,33 @@ struct S { struct SVS : public VS { void vm() { } }; + + struct CS { AaronBallman wrote: When switching the diagnostic approach, be sure to add test coverage for unused copy/move constructors, copy/move assignment, and destructors (you can get an unused destructor if you only use `new` to allocate the class rather than stack allocate it). https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
https://github.com/AaronBallman commented: Thank you for the diagnostic improvement! https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
steakhal wrote: Ping. https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
guillem-bartina-sonarsource wrote: Ping https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
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 Date: Fri, 8 Mar 2024 17:16:56 +0100 Subject: [PATCH 1/4] [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 9b5245695153e..703803ad2c121 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, InGroup, DefaultIgnore; diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp index 720d5fd5f0428..163ab48998d5e 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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } } } else { const VarDecl *DiagD = cast(*I)->getDefinition(); diff --git a/clang/test/SemaCXX/warn-unused-filescoped.cpp b/clang/test/SemaCXX/warn-unused-filescoped.cpp index be8d350855c07..b3d1bb4661a5f 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 + struct TCS { +TCS(); + }; + template TCS::TCS() {} + template <> TCS::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 Date: Fri, 5 Apr 2024 18:19:46 +0200 Subject: [PATCH 2/4] 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 703803ad2c121..98a33f01132d2 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked_unused:
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
https://github.com/guillem-bartina-sonarsource edited https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
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 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, InGroup, 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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } } } else { const VarDecl *DiagD = cast(*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 + struct TCS { +TCS(); + }; + template TCS::TCS() {} + template <> TCS::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 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
hazohelet wrote: Please add a release note line to `clang/docs/ReleaseNotes.rst` because this patch changes the diagnostic behavior of clang https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
https://github.com/guillem-bartina-sonarsource edited https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
@@ -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'}} guillem-bartina-sonarsource wrote: Yes, you are right, it will always be the same. I left it for consistency with the previous message but I agree that it doesn't provide useful information. Would you prefer the message to be "unused constructor"? https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
@@ -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'}} tbaederr wrote: Is the name of the constructor actually useful in this diagostic? It will always be the name of the surrounding class, won't it? https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
guillem-bartina-sonarsource wrote: Ping https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
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 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, InGroup, 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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } } } else { const VarDecl *DiagD = cast(*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 + struct TCS { +TCS(); + }; + template TCS::TCS() {} + template <> TCS::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; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
https://github.com/guillem-bartina-sonarsource edited https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
@@ -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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } guillem-bartina-sonarsource wrote: Note that `warn_unused_function` and `warn_unused_member_function` are two different diagnostics, with different messages. The former has only one argument, whereas the latter now has two (one of them selects between `member function` and `constructor`). Unless I'm missing some hidden logic regarding diagnostic constructors and the '<<' operator, we can't merge the two because the two diagnostics have different number of arguments. https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
@@ -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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } cor3ntin wrote: ```suggestion else { Diag(DiagD->getLocation(), isa(DiagD) ? diag::warn_unused_member_function : diag::warn_unused_function) << DiagD << (!isa(DiagD) ? /*member function=*/0 : /*constructor=*/1 << DiagRange; } ``` I think you can simplify like that. And then change ` warn_unused_member_function : Warning<"unused %select{member function|constructor}1 %0"` https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
shafik wrote: Looks like the build failed b/c you did not run `git clang-format` https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
llvmbot wrote: @llvm/pr-subscribers-clang Author: None (guillem-bartina-sonarsource) Changes The current diagnostic message is `unused member function A` for every kind of method, including constructors. The idea is to refine the message to `unused constructor A` when the method is a constructor. --- Full diff: https://github.com/llvm/llvm-project/pull/84515.diff 3 Files Affected: - (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+1-1) - (modified) clang/lib/Sema/Sema.cpp (+10-5) - (modified) clang/test/SemaCXX/warn-unused-filescoped.cpp (+23) ``diff diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 6da49facd27ec2..770bda6b3ba193 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, InGroup, 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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } } } else { const VarDecl *DiagD = cast(*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 + struct TCS { +TCS(); + }; + template TCS::TCS() {} + template <> TCS::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; `` https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
github-actions[bot] wrote: Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using `@` followed by their GitHub username. If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the [LLVM GitHub User Guide](https://llvm.org/docs/GitHub.html). You can also ask questions in a comment on this PR, on the [LLVM Discord](https://discord.com/invite/xS7Z362) or on the [forums](https://discourse.llvm.org/). https://github.com/llvm/llvm-project/pull/84515 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [clang][Sema] Refine unused-member-function diagnostic message for constructors (PR #84515)
https://github.com/guillem-bartina-sonarsource created https://github.com/llvm/llvm-project/pull/84515 The current diagnostic message is `unused member function A` for every kind of method, including constructors. The idea is to refine the message to `unused constructor A` when the method is a constructor. >From f8f93fe4ece515aa892c15aff0d62b32cc4ec896 Mon Sep 17 00:00:00 2001 From: guillem-bartina-sonarsource 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 6da49facd27ec2..770bda6b3ba193 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, DefaultIgnore; def warn_unused_template : Warning<"unused %select{function|variable}0 template %1">, InGroup, 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, DefaultIgnore; def warn_used_but_marked_unused: Warning<"%0 was marked unused but was used">, InGroup, 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(DiagD) - ? diag::warn_unused_member_function - : diag::warn_unused_function) -<< DiagD << DiagRange; + else { +if (isa(DiagD)) + Diag(DiagD->getLocation(), diag::warn_unused_member_function) + << (!isa(DiagD) ? /*member function=*/0 + : /*constructor=*/1) + << DiagD << DiagRange; +else + Diag(DiagD->getLocation(), diag::warn_unused_function) + << DiagD << DiagRange; + } } } else { const VarDecl *DiagD = cast(*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 + struct TCS { +TCS(); + }; + template TCS::TCS() {} + template <> TCS::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; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits