Repository navigation
[Clang] suppress deprecated field warnings in implicit special-member functions - #147400
Conversation
|
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 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. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang Author: Shashi Shankar (shashi1687) ChangesSummary What’s Changed In SemaExpr.cpp, Sema::DiagnoseUseOfDecl now early-returns when the current FunctionDecl is an implicit defaulted constructor or copy/move assignment operator. A new lit regression test (clang/test/SemaCXX/deprecated-implicit-special-member.cpp) verifies no warnings for implicit uses and ensures explicit member access still triggers a deprecation warning. By narrowing the warning scope to explicit code, this change reduces noise in client codebases without altering existing explicit-deprecation policies. Continuous integration confirms no regressions, and the added test guards against future reversion. IssueFixes #147293 2 Files Affected:
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 3c3d29c415249..93b3768c12fb8 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -396,6 +396,30 @@ bool Sema::DiagnoseUseOfDecl(NamedDecl *D, ArrayRef<SourceLocation> Locs,
if (VD->getTLSKind() != VarDecl::TLS_None)
targetDiag(*Locs.begin(), diag::err_thread_unsupported);
}
+ // Suppress deprecation warnings for implicit special-member functions.
+ if (auto *DA = D->getAttr<DeprecatedAttr>()) {
+ if (const FunctionDecl *CurFD = getCurFunctionDecl()) {
+ bool IsImplicitSpecialMember = false;
+
+ // Defaulted constructors.
+ if (auto *CD = dyn_cast<CXXConstructorDecl>(CurFD)) {
+ if (CD->isImplicit() && CD->isDefaulted())
+ IsImplicitSpecialMember = true;
+ }
+
+ // Copy- and move-assignment operators.
+ if (auto *MD = dyn_cast<CXXMethodDecl>(CurFD)) {
+ if ((MD->isCopyAssignmentOperator() ||
+ MD->isMoveAssignmentOperator()) &&
+ MD->isImplicit())
+ IsImplicitSpecialMember = true;
+ }
+
+ if (!IsImplicitSpecialMember) {
+ // Only warn on *explicit* uses.
+ Diag(Locs.front(), diag::warn_deprecated_declarations) << D;
+ Diag(DA->getLocation(), diag::note_deprecated_declarations) << D;
+ }
return false;
}
diff --git a/clang/test/SemaCXX/deprecated-no-implicit-warning.cpp b/clang/test/SemaCXX/deprecated-no-implicit-warning.cpp
new file mode 100644
index 0000000000000..b830b980d3736
--- /dev/null
+++ b/clang/test/SemaCXX/deprecated-no-implicit-warning.cpp
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++20 -Wdeprecated-declarations -verify %s
+
+struct A {
+ [[deprecated]] int x = 42;
+};
+
+int main() {
+ A a; // no-warning
+ A b = a; // no-warning
+ a = b; // no-warning
+
+ a.x = b.x; // expected-warning {{'A::x' is deprecated}}
+ return 0;
+}
+
|
d8bd088 to
8526d9d
Compare
…unctions 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: llvm#147293 Signed-off-by: Shashi Shankar <[email protected]>
8526d9d to
29c3c9c
Compare
02b3693 to
159e35d
Compare
81184fe to
df26838
Compare
|
Hello @zwuis/everyone, Thank you again for your thorough review. I’ve incorporated all suggestions:
All Sema tests ( Thanks so much! 😊 |
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
df26838 to
ab1112f
Compare
…lted checks - Suppression guard is now inside - Use to coalesce + - Drop PR number from release note
ab1112f to
1d206a0
Compare
d2ce538 to
a7c8090
Compare
a7c8090 to
e541b0d
Compare
|
Hi everyone, I’ve just pushed the final change so the suppression guard now exactly matches what we discussed. Here’s the snippet I added under case AR_Deprecated:: All local Sema tests (check-clang pass without issues. Could you please take another look when you have a moment? I really appreciate all the feedback so far and want to make sure this meets the project’s standards and let me know if there’s anything else I can improve! Thank you for your guidance, |
cor3ntin
left a comment
There was a problem hiding this comment.
There is still unaddressed feedback
718fc3a to
177d6ad
Compare
Signed-off-by: Shashi Shankar <[email protected]>
177d6ad to
713a2a6
Compare
|
Hi @cor3ntin , Thank you for pointing me toward the simpler Whenever you have a moment, could you please give the revision another look? I really appreciate your guidance. Thanks again! |
Thank you so much for your guidance—I really appreciate the clear direction and the time you’ve taken to review my changes. Your feedback has been invaluable! |
Added the suggested change Co-authored-by: Corentin Jabot <[email protected]>
added the comment Co-authored-by: Corentin Jabot <[email protected]>
|
@shashforge Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…nitions (llvm#147400) Do not warn on deprecated member used in an implicit definition (such as a defaulted special member function). Co-authored-by: Corentin Jabot <[email protected]> Fixes llvm#147293
What I did
In clang/lib/Sema/SemaAvailability.cpp, under case AR_Deprecated:, I added an early-exit guard to suppress deprecation diagnostics in purely implicit, defaulted special-member functions:
Added clang/test/Sema/implicit-deprecated-special-member.cpp to verify:
Updated clang/docs/ReleaseNotes.rst under Bug Fixes to C++ Support:
Suppress -Wdeprecated-declarations in implicitly-generated special-member functions for deprecated data-members; only explicit member accesses still warn. (Issue #147293)Testing
ninja clang.ninja check-clangall tests pass, including the new lit test.Fixes #147293
Please let me know if you spot anything I missed or if there’s any further tweak needed. Thanks for your time!