[clang] b89f09f - [Clang] Enhance handling of [[deprecated]] attribute diagnostics for local variables (#113575)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 25 00:40:48 PDT 2024


Author: Oleksandr T.
Date: 2024-10-25T09:40:44+02:00
New Revision: b89f09f5cc072b927917844a5781be3600acb72a

URL: https://github.com/llvm/llvm-project/commit/b89f09f5cc072b927917844a5781be3600acb72a
DIFF: https://github.com/llvm/llvm-project/commit/b89f09f5cc072b927917844a5781be3600acb72a.diff

LOG: [Clang] Enhance handling of [[deprecated]] attribute diagnostics for local variables (#113575)

Fixes #90073

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaAvailability.cpp
    clang/test/SemaCXX/deprecated.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d7b47cd2e2ee26..ed0c0e369fca74 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..076d3489fa9438 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->isLocalVarDeclOrParm() && 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..667f4d7d3edb03 100644
--- a/clang/test/SemaCXX/deprecated.cpp
+++ b/clang/test/SemaCXX/deprecated.cpp
@@ -260,5 +260,25 @@ namespace ArrayComp {
   bool b7 = arr1 == +f();
 }
 
+namespace GH90073 {
+[[deprecated]] int f1() { // expected-note {{'f1' 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}}
+}
+
+[[deprecated]] void f2([[deprecated]] int x) { // expected-note {{'f2' has been explicitly marked deprecated here}} \
+                                               // expected-note {{'x' has been explicitly marked deprecated here}}
+  x = 4; // expected-warning {{'x' is deprecated}}
+}
+
+int main() {
+  f1();  // expected-warning {{'f1' is deprecated}}
+  f2(1); // expected-warning {{'f2' is deprecated}}
+  return 0;
+}
+}
+
 # 1 "/usr/include/system-header.h" 1 3
 void system_header_function(void) throw();


        


More information about the cfe-commits mailing list