Author: Nathan Chancellor
Date: 2023-06-05T10:54:47-07:00
New Revision: 877210faa447f4cc7db87812f8ed80e398fedd61

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

LOG: [Sema] Do not emit -Wunused-variable for variables declared with cleanup 
attribute

A variable declared with __attribute__((cleanup)) cannot be unused, as
its address is passed to the clean up function. Do not emit
-Wunused-variable for variables declared with the cleanup attribute,
which matches GCC's behavior: https://godbolt.org/z/dz5YfTsan

Reviewed By: erichkeane, nickdesaulniers

Differential Revision: https://reviews.llvm.org/D152180

Added: 
    

Modified: 
    clang/lib/Sema/SemaDecl.cpp
    clang/test/Sema/warn-unused-variables.c

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index b8aba816283d6..12fd378fb170c 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -1992,7 +1992,8 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) {
     return false;
   }
 
-  if (D->hasAttr<UnusedAttr>() || D->hasAttr<ObjCPreciseLifetimeAttr>())
+  if (D->hasAttr<UnusedAttr>() || D->hasAttr<ObjCPreciseLifetimeAttr>() ||
+      D->hasAttr<CleanupAttr>())
     return false;
 
   if (isa<LabelDecl>(D))

diff  --git a/clang/test/Sema/warn-unused-variables.c 
b/clang/test/Sema/warn-unused-variables.c
index d482c353f0ad1..26b18720849f9 100644
--- a/clang/test/Sema/warn-unused-variables.c
+++ b/clang/test/Sema/warn-unused-variables.c
@@ -30,3 +30,8 @@ int f3(void) {
   (void)(^() { int X = 4; }); // expected-warning{{unused}}
   (void)(^() { int X = 4; return Y + X; }); // expected-error {{use of 
undeclared identifier 'Y'}}
 }
+
+void c1(int *);
+void f4(void) {
+  int __attribute__((cleanup(c1))) X1 = 4;
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to