Author: Michael Benfield Date: 2021-09-22T17:48:09Z New Revision: af99236747872af7e77092cbf6ddd18fa8623a2f
URL: https://github.com/llvm/llvm-project/commit/af99236747872af7e77092cbf6ddd18fa8623a2f DIFF: https://github.com/llvm/llvm-project/commit/af99236747872af7e77092cbf6ddd18fa8623a2f.diff LOG: Don't diagnose unused but set when the Cleanup attribute is used. This applies to -Wunused-but-set-variable and -Wunused-but-set-parameter. This addresses bug 51865. Differential Revision: https://reviews.llvm.org/D109862 Added: Modified: clang/lib/Sema/SemaDecl.cpp clang/test/Sema/warn-unused-but-set-variables.c Removed: ################################################################################ diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index cc3417d4ccba..13389ebdd72f 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -1921,8 +1921,10 @@ void Sema::DiagnoseUnusedDecl(const NamedDecl *D) { } void Sema::DiagnoseUnusedButSetDecl(const VarDecl *VD) { - // If it's not referenced, it can't be set. - if (!VD->isReferenced() || !VD->getDeclName() || VD->hasAttr<UnusedAttr>()) + // If it's not referenced, it can't be set. If it has the Cleanup attribute, + // it's not really unused. + if (!VD->isReferenced() || !VD->getDeclName() || VD->hasAttr<UnusedAttr>() || + VD->hasAttr<CleanupAttr>()) return; const auto *Ty = VD->getType().getTypePtr()->getBaseElementTypeUnsafe(); diff --git a/clang/test/Sema/warn-unused-but-set-variables.c b/clang/test/Sema/warn-unused-but-set-variables.c index a8d05243321f..38042ba014c4 100644 --- a/clang/test/Sema/warn-unused-but-set-variables.c +++ b/clang/test/Sema/warn-unused-but-set-variables.c @@ -49,3 +49,13 @@ void f2 (void) { x = 0; (void) sizeof(x); } + +void for_cleanup(int *x) { + *x = 0; +} + +void f3(void) { + // Don't warn if the __cleanup__ attribute is used. + __attribute__((__cleanup__(for_cleanup))) int x; + x = 5; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits