nathanchance created this revision.
nathanchance added reviewers: aaron.ballman, nickdesaulniers, erichkeane.
Herald added a project: All.
nathanchance requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152180

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


Index: clang/test/Sema/warn-unused-variables.c
===================================================================
--- clang/test/Sema/warn-unused-variables.c
+++ clang/test/Sema/warn-unused-variables.c
@@ -30,3 +30,8 @@
   (void)(^() { int X = 4; }); // expected-warning{{unused}}
   (void)(^() { int X = 4; return Y + X; }); // expected-error {{use of 
undeclared identifier 'Y'}}
 }
+
+void c1(int *);
+int f4(void) {
+  int __attribute__((cleanup(c1))) X1 = 4;
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -1992,7 +1992,8 @@
     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))


Index: clang/test/Sema/warn-unused-variables.c
===================================================================
--- clang/test/Sema/warn-unused-variables.c
+++ clang/test/Sema/warn-unused-variables.c
@@ -30,3 +30,8 @@
   (void)(^() { int X = 4; }); // expected-warning{{unused}}
   (void)(^() { int X = 4; return Y + X; }); // expected-error {{use of undeclared identifier 'Y'}}
 }
+
+void c1(int *);
+int f4(void) {
+  int __attribute__((cleanup(c1))) X1 = 4;
+}
Index: clang/lib/Sema/SemaDecl.cpp
===================================================================
--- clang/lib/Sema/SemaDecl.cpp
+++ clang/lib/Sema/SemaDecl.cpp
@@ -1992,7 +1992,8 @@
     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))
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to