Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Clang]: enhance handling of [[deprecated]] attribute diagnostics for local variables #113575

Merged
merged 5 commits into from
Oct 25, 2024

Conversation

a-tarasyuk
Copy link
Member

Fixes #90073

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 24, 2024

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #90073


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

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+2)
  • (modified) clang/lib/Sema/SemaAvailability.cpp (+6)
  • (modified) clang/test/SemaCXX/deprecated.cpp (+14)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 28bb83a1c9d60f..89c97d4b6631f2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -424,6 +424,8 @@ Improvements to Clang's diagnostics
   name was a reserved name, which we improperly allowed to suppress the
   diagnostic.
 
+- Clang now diagnoses [[deprecated]] attribute usage on local variables (#GH90073).
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaAvailability.cpp b/clang/lib/Sema/SemaAvailability.cpp
index 798cabaa31a476..d62b3eb1a480e7 100644
--- a/clang/lib/Sema/SemaAvailability.cpp
+++ b/clang/lib/Sema/SemaAvailability.cpp
@@ -182,6 +182,12 @@ static bool ShouldDiagnoseAvailabilityInContext(
       return false;
   }
 
+  if (K == AR_Deprecated) {
+    if (const auto *VD = dyn_cast<VarDecl>(OffendingDecl))
+      if (VD->isLocalVarDecl() && VD->isDeprecated())
+        return true;
+  }
+
   // Checks if we should emit the availability diagnostic in the context of C.
   auto CheckContext = [&](const Decl *C) {
     if (K == AR_NotYetIntroduced) {
diff --git a/clang/test/SemaCXX/deprecated.cpp b/clang/test/SemaCXX/deprecated.cpp
index a93331023f7e21..02fdf0bec5c088 100644
--- a/clang/test/SemaCXX/deprecated.cpp
+++ b/clang/test/SemaCXX/deprecated.cpp
@@ -260,5 +260,19 @@ namespace ArrayComp {
   bool b7 = arr1 == +f();
 }
 
+namespace GH90073 {
+[[deprecated]] int f() { // expected-note {{'f' has been explicitly marked deprecated here}}
+  [[deprecated]] int a;  // expected-note {{'a' has been explicitly marked deprecated here}} \
+                         // expected-note {{'a' has been explicitly marked deprecated here}}
+  a = 0;    // expected-warning {{'a' is deprecated}}
+  return a; // expected-warning {{'a' is deprecated}}
+}
+
+int main() {
+  f(); // expected-warning {{'f' is deprecated}}
+  return 0;
+}
+}
+
 # 1 "/usr/include/system-header.h" 1 3
 void system_header_function(void) throw();

@a-tarasyuk a-tarasyuk requested a review from Sirraide October 25, 2024 07:22
Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

LGTM

On the issue for this, there was some discussion about the fact that the standard might disallow deprecating local variables at some point, but so long as we (still) allow that, it only makes sense to actually diagnose the deprecation as well considering that it seems to be straight-forward enough to implement.

@Sirraide Sirraide merged commit b89f09f into llvm:main Oct 25, 2024
9 checks passed
@frobtech frobtech mentioned this pull request Oct 25, 2024
NoumanAmir657 pushed a commit to NoumanAmir657/llvm-project that referenced this pull request Nov 4, 2024
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.

[[deprecated]] applied to local variables
3 participants