Skip to content

[Clang] suppress deprecated field warnings in implicit special-member functions - #147400

Merged
cor3ntin merged 16 commits into
llvm:mainfrom
shashforge:clang-no-implicit-deprecated-warnings
Jul 24, 2025
Merged

cor3ntin merged 16 commits into
llvm:mainfrom
shashforge:clang-no-implicit-deprecated-warnings

Conversation

@shashforge

@shashforge shashforge commented Jul 7, 2025 •

Copy link
Copy Markdown
Contributor

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:

if (const auto *FD = dyn_cast_or_null<FunctionDecl>(S.getCurFunctionDecl());
    FD && FD->isImplicit())
  return;

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

  • Rebuilt Clang with ninja clang.
  • Ran ninja check-clang all 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!

@shashforge
shashforge marked this pull request as draft July 7, 2025 21:14
@github-actions

github-actions Bot commented Jul 7, 2025

Copy link
Copy Markdown

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.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@shashforge shashforge changed the title 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 Sema: suppress deprecated field warnings in implicit special-member functions Fixes: https://github.com/llvm/llvm-project/issues/147293 Jul 7, 2025
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 7, 2025
@llvmbot

llvmbot commented Jul 7, 2025

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang

Author: Shashi Shankar (shashi1687)

Changes

Summary
This patch refines Clang’s deprecation diagnostics by silencing -Wdeprecated-declarations for deprecated data members when they’re only referenced inside implicitly-generated special-member functions (default constructors and copy/move assignment operators). Explicit user code accesses of deprecated members continue to warn as before, but implicit uses no longer clutter builds with redundant messages—bringing Clang’s behavior in line with MSVC and recent GCC.

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.

Issue


Fixes #147293


Full diff: https://github.com/llvm/llvm-project/pull/147400.diff

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (+24)
  • (added) clang/test/SemaCXX/deprecated-no-implicit-warning.cpp (+15)
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;
+}
+

@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from d8bd088 to 8526d9d Compare July 7, 2025 23:26
…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]>
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from 8526d9d to 29c3c9c Compare July 7, 2025 23:28
@shashforge
shashforge marked this pull request as ready for review July 7, 2025 23:46
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from 02b3693 to 159e35d Compare July 7, 2025 23:53
Comment thread clang/lib/Sema/SemaAvailability.cpp Resolved
Comment thread clang/lib/Sema/SemaAvailability.cpp Resolved
Comment thread clang/docs/ReleaseNotes.rst Resolved
Comment thread clang/lib/Sema/SemaAvailability.cpp Resolved
Comment thread clang/lib/Sema/SemaAvailability.cpp Resolved
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from 81184fe to df26838 Compare July 8, 2025 10:13
@shashforge

Copy link
Copy Markdown
Contributor Author

Hello @zwuis/everyone,

Thank you again for your thorough review. I’ve incorporated all suggestions:

  • Moved the suppression guard into the case AR_Deprecated: block
  • Used dyn_cast_if_present with a single isImplicit() && isDefaulted() check
  • Combined constructor & destructor detection via isa<…>
  • Cleaned up the release notes entry

All Sema tests (check-clang) pass locally. Could you please take another look when you have a chance? I really appreciate your time and guidance on this.

Thanks so much! 😊

@shashforge
shashforge requested a review from zwuis July 8, 2025 10:34
@cor3ntin cor3ntin changed the title Sema: suppress deprecated field warnings in implicit special-member functions Fixes: https://github.com/llvm/llvm-project/issues/147293 Sema: suppress deprecated field warnings in implicit special-member functions Jul 8, 2025
@cor3ntin cor3ntin changed the title Sema: suppress deprecated field warnings in implicit special-member functions [Clang] suppress deprecated field warnings in implicit special-member functions Jul 8, 2025
Comment thread clang/lib/Sema/SemaAvailability.cpp Resolved
@shashforge
shashforge requested a review from cor3ntin July 8, 2025 13:56
@github-actions

github-actions Bot commented Jul 8, 2025 •

Copy link
Copy Markdown

✅ With the latest revision this PR passed the C/C++ code formatter.

@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from df26838 to ab1112f Compare July 8, 2025 14:20
…lted checks

- Suppression guard is now inside
- Use  to coalesce  +
- Drop PR number from release note
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from ab1112f to 1d206a0 Compare July 8, 2025 15:21
@shashforge
shashforge requested a review from AaronBallman July 9, 2025 21:24
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch 2 times, most recently from d2ce538 to a7c8090 Compare July 11, 2025 22:10
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from a7c8090 to e541b0d Compare July 11, 2025 22:13
@shashforge

Copy link
Copy Markdown
Contributor Author

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::

if (auto *MD = dyn_cast_if_present<CXXMethodDecl>(S.getCurFunctionDecl());
    MD && MD->isImplicit() && MD->isDefaulted() &&
    (isa<CXXConstructorDecl, CXXDestructorDecl>(MD) ||
     MD->isCopyAssignmentOperator() ||
     MD->isMoveAssignmentOperator())) {
  return;
}

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,
Shashi

@cor3ntin cor3ntin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still unaddressed feedback

@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from 718fc3a to 177d6ad Compare July 20, 2025 23:51
@shashforge
shashforge force-pushed the clang-no-implicit-deprecated-warnings branch from 177d6ad to 713a2a6 Compare July 20, 2025 23:59
@shashforge

Copy link
Copy Markdown
Contributor Author

Hi @cor3ntin ,

Thank you for pointing me toward the simpler isImplicit() check—much cleaner! I’ve updated the patch and added the extra test; everything now builds and check-clang is green.

Whenever you have a moment, could you please give the revision another look? I really appreciate your guidance.

Thanks again!

@shashforge
shashforge requested a review from cor3ntin July 21, 2025 00:03
@shashforge

shashforge commented Jul 21, 2025 •

Copy link
Copy Markdown
Contributor Author

There is still unaddressed feedback

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!

@cor3ntin cor3ntin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comment thread clang/docs/ReleaseNotes.rst Resolved
Comment thread clang/lib/Sema/SemaAvailability.cpp Resolved
@cor3ntin
cor3ntin merged commit 31db0f0 into llvm:main Jul 24, 2025
@github-actions

Copy link
Copy Markdown

@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!

mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Don't warn about deprecated fields in implicitly generated special member functions

5 participants