[clang] [Clang] suppress deprecated field warnings in implicit special-member functions (PR #147400)
Shashi Shankar via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 9 06:59:03 PDT 2025
https://github.com/shashi1687 updated https://github.com/llvm/llvm-project/pull/147400
>From 29c3c9c6d25cc7c47d01a9d819ea37e92361aea2 Mon Sep 17 00:00:00 2001
From: Shashi Shankar <shashishankar1687 at gmail.com>
Date: Mon, 7 Jul 2025 22:54:22 +0200
Subject: [PATCH 1/3] Sema: suppress deprecated field warnings in implicit
special-member functions
Only explicit uses of deprecated fields should warn.
Defaulted constructors and copy/move assignment operators (when
implicitly generated) no longer trigger -Wdeprecated-declarations.
* In SemaExpr.cpp, early-return in DiagnoseUseOfDecl for implicit SM.
* Add lit regression test in clang/test/SemaCXX/deprecated-no-implicit-warning.cpp.
Fixes: https://github.com/llvm/llvm-project/issues/147293
Signed-off-by: Shashi Shankar <shashishankar1687 at gmail.com>
---
clang/lib/Sema/SemaAvailability.cpp | 22 +++++++++++++++++
.../implicit-special-member-deprecated.cpp | 24 +++++++++++++++++++
2 files changed, 46 insertions(+)
create mode 100644 clang/test/Sema/implicit-special-member-deprecated.cpp
diff --git a/clang/lib/Sema/SemaAvailability.cpp b/clang/lib/Sema/SemaAvailability.cpp
index 8c6a17301fba6..a3ff53abd3188 100644
--- a/clang/lib/Sema/SemaAvailability.cpp
+++ b/clang/lib/Sema/SemaAvailability.cpp
@@ -452,6 +452,28 @@ static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
}
}
}
+ // Suppress -Wdeprecated-declarations in purely implicit special-member functions.
+ if (K == AR_Deprecated) {
+ if (const FunctionDecl *FD = S.getCurFunctionDecl()) {
+ // Implicit, defaulted constructor (default / copy / move)
+ if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
+ if (CD->isImplicit() && CD->isDefaulted())
+ return;
+ }
+ //Implicit, defaulted destructor
+ if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) {
+ if (DD->isImplicit() && DD->isDefaulted())
+ return;
+ }
+ //Implicit copy- or move-assignment operator
+ if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
+ if (MD->isImplicit() &&
+ (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()))
+ return;
+ }
+ }
+ }
+
switch (K) {
case AR_NotYetIntroduced: {
diff --git a/clang/test/Sema/implicit-special-member-deprecated.cpp b/clang/test/Sema/implicit-special-member-deprecated.cpp
new file mode 100644
index 0000000000000..f65fc5171eaab
--- /dev/null
+++ b/clang/test/Sema/implicit-special-member-deprecated.cpp
@@ -0,0 +1,24 @@
+// clang/test/Sema/implicit-deprecated-special-member.cpp
+// RUN: %clang_cc1 -std=c++20 -Wdeprecated-declarations -verify %s
+
+struct A {
+ [[deprecated("use something else")]] int x = 42; // expected-note {{has been explicitly marked deprecated here}}
+};
+
+A makeDefaultA() { // implicit default ctor: no diagnostics
+ return {};
+}
+
+A copyA(const A &a) { // implicit copy ctor: no diagnostics
+ return a;
+}
+
+void assignA() {
+ A a, b;
+ a = b; // implicit copy-assign: no diagnostics
+}
+
+void useA() {
+ A a;
+ (void)a.x; // expected-warning {{is deprecated}}
+}
\ No newline at end of file
>From 159e35de70a0d4ada07f8f6bc340a2a3d165b463 Mon Sep 17 00:00:00 2001
From: Shashi Shankar <shashishankar1687 at gmail.com>
Date: Tue, 8 Jul 2025 01:33:39 +0200
Subject: [PATCH 2/3] Docs: add release note for implicit-special-member
deprecation fix
---
clang/docs/ReleaseNotes.rst | 1 +
clang/test/Sema/implicit-special-member-deprecated.cpp | 1 -
2 files changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 581338924943b..711e45f44ac6e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -907,6 +907,7 @@ Bug Fixes to C++ Support
- Fix a bug where private access specifier of overloaded function not respected. (#GH107629)
- Correctly handle allocations in the condition of a ``if constexpr``.(#GH120197) (#GH134820)
- Fixed a crash when handling invalid member using-declaration in C++20+ mode. (#GH63254)
+- Suppress -Wdeprecated-declarations in implicitly generated special-member functions for deprecated data-members; only explicit uses still warn. (PR #147400, Issue #147293)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/test/Sema/implicit-special-member-deprecated.cpp b/clang/test/Sema/implicit-special-member-deprecated.cpp
index f65fc5171eaab..6b0042f5d2bb5 100644
--- a/clang/test/Sema/implicit-special-member-deprecated.cpp
+++ b/clang/test/Sema/implicit-special-member-deprecated.cpp
@@ -1,4 +1,3 @@
-// clang/test/Sema/implicit-deprecated-special-member.cpp
// RUN: %clang_cc1 -std=c++20 -Wdeprecated-declarations -verify %s
struct A {
>From 1d206a0a8e147230ce82b20ed70f6feb5f2eda65 Mon Sep 17 00:00:00 2001
From: Shashi Shankar <shashishankar1687 at gmail.com>
Date: Tue, 8 Jul 2025 10:37:41 +0200
Subject: [PATCH 3/3] Sema: move deprecation-suppression into switch, dedupe
implicit/defaulted checks
- Suppression guard is now inside
- Use to coalesce +
- Drop PR number from release note
---
clang/docs/ReleaseNotes.rst | 2 +-
clang/lib/Sema/SemaAvailability.cpp | 30 +++++--------------
.../implicit-special-member-deprecated.cpp | 2 +-
3 files changed, 10 insertions(+), 24 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 711e45f44ac6e..12a9db280454b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -907,7 +907,7 @@ Bug Fixes to C++ Support
- Fix a bug where private access specifier of overloaded function not respected. (#GH107629)
- Correctly handle allocations in the condition of a ``if constexpr``.(#GH120197) (#GH134820)
- Fixed a crash when handling invalid member using-declaration in C++20+ mode. (#GH63254)
-- Suppress -Wdeprecated-declarations in implicitly generated special-member functions for deprecated data-members; only explicit uses still warn. (PR #147400, Issue #147293)
+- Suppress -Wdeprecated-declarations in implicitly generated special-member functions for deprecated data-members; only explicit uses still warn. (Issue #147293)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaAvailability.cpp b/clang/lib/Sema/SemaAvailability.cpp
index a3ff53abd3188..365be360b6ce2 100644
--- a/clang/lib/Sema/SemaAvailability.cpp
+++ b/clang/lib/Sema/SemaAvailability.cpp
@@ -452,28 +452,6 @@ static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
}
}
}
- // Suppress -Wdeprecated-declarations in purely implicit special-member functions.
- if (K == AR_Deprecated) {
- if (const FunctionDecl *FD = S.getCurFunctionDecl()) {
- // Implicit, defaulted constructor (default / copy / move)
- if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
- if (CD->isImplicit() && CD->isDefaulted())
- return;
- }
- //Implicit, defaulted destructor
- if (auto *DD = dyn_cast<CXXDestructorDecl>(FD)) {
- if (DD->isImplicit() && DD->isDefaulted())
- return;
- }
- //Implicit copy- or move-assignment operator
- if (auto *MD = dyn_cast<CXXMethodDecl>(FD)) {
- if (MD->isImplicit() &&
- (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()))
- return;
- }
- }
- }
-
switch (K) {
case AR_NotYetIntroduced: {
@@ -569,6 +547,14 @@ static void DoEmitAvailabilityWarning(Sema &S, AvailabilityResult K,
return;
}
case AR_Deprecated:
+ // Suppress -Wdeprecated-declarations in purely implicit special-member
+ // functions.
+ if (auto *MD = dyn_cast_if_present<CXXMethodDecl>(S.getCurFunctionDecl());
+ MD && MD->isImplicit() && MD->isDefaulted() &&
+ (isa<CXXConstructorDecl, CXXDestructorDecl>(MD) ||
+ MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator())) {
+ return;
+ }
if (ObjCPropertyAccess)
diag = diag::warn_property_method_deprecated;
else if (S.currentEvaluationContext().IsCaseExpr)
diff --git a/clang/test/Sema/implicit-special-member-deprecated.cpp b/clang/test/Sema/implicit-special-member-deprecated.cpp
index 6b0042f5d2bb5..13ab029444364 100644
--- a/clang/test/Sema/implicit-special-member-deprecated.cpp
+++ b/clang/test/Sema/implicit-special-member-deprecated.cpp
@@ -20,4 +20,4 @@ void assignA() {
void useA() {
A a;
(void)a.x; // expected-warning {{is deprecated}}
-}
\ No newline at end of file
+}
More information about the cfe-commits
mailing list