[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
This commit is contained in:
Nathan Chancellor 2023-06-05 10:49:41 -07:00
parent 9959cdb66a
commit 877210faa4
No known key found for this signature in database
GPG Key ID: 1D6B269171C01A96
2 changed files with 7 additions and 1 deletions

View File

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

View File

@ -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;
}