From 877210faa447f4cc7db87812f8ed80e398fedd61 Mon Sep 17 00:00:00 2001 From: Nathan Chancellor Date: Mon, 5 Jun 2023 10:49:41 -0700 Subject: [PATCH] [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 --- clang/lib/Sema/SemaDecl.cpp | 3 ++- clang/test/Sema/warn-unused-variables.c | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index b8aba81..12fd378 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() || D->hasAttr()) + if (D->hasAttr() || D->hasAttr() || + D->hasAttr()) return false; if (isa(D)) diff --git a/clang/test/Sema/warn-unused-variables.c b/clang/test/Sema/warn-unused-variables.c index d482c35..26b1872 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; +} -- 2.7.4