From: Ted Kremenek Date: Thu, 14 Nov 2013 04:27:00 +0000 (+0000) Subject: Refine -Wunused-variable to only suppress warning for __bridge_transfer, not all... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e452d6127ebd8899bb93fae8b6b55f339d261a0a;p=clang Refine -Wunused-variable to only suppress warning for __bridge_transfer, not all bridge casts. Also refine test case to capture the intention of this suppression. Essentially some developers use __bridge_transfer as if it were a safe CFRelease. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194663 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index c0be341b66..9c6f8f6740 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -1313,26 +1313,16 @@ static bool ShouldDiagnoseUnusedDecl(const NamedDecl *D) { } } - // Under ARC, bridged casts can have side-effects on memory - // management semantics. Some users assign a bridged - // value to a temporary to adjust reference counts. + // Under ARC, some users use __bridge_transfer to automate memory + // reclamation of objects that were referenced via C pointers. const Expr *Init = VD->getInit(); if (Init) { if (const ExprWithCleanups *EC = dyn_cast(Init)) Init = EC->getSubExpr(); Init = Init->IgnoreParens(); - if (const ImplicitCastExpr *IC = dyn_cast(Init)) { - switch (IC->getCastKind()) { - case CK_ARCProduceObject: - case CK_ARCConsumeObject: - case CK_ARCReclaimReturnedObject: - case CK_ARCExtendBlockObject: - case CK_CopyAndAutoreleaseBlockObject: + if (const ImplicitCastExpr *IC = dyn_cast(Init)) + if (IC->getCastKind() == CK_ARCConsumeObject) return false; - default: - break; - } - } } // TODO: __attribute__((unused)) templates? diff --git a/test/SemaObjC/arc-bridged-cast.m b/test/SemaObjC/arc-bridged-cast.m index 6f5f3782d5..3707b9ec8a 100644 --- a/test/SemaObjC/arc-bridged-cast.m +++ b/test/SemaObjC/arc-bridged-cast.m @@ -64,13 +64,12 @@ CFTypeRef fixitsWithSpace(id obj) { } // -// Suppressed -Wunused-variable when the initializer is a bridge cast. +// Suppressed -Wunused-variable when the initializer is a __bridge_transfer cast. #pragma clang diagnostic push #pragma clang diagnostic warning "-Wunused-variable" -void rdar15432770() { - void (^block1)() = ^ { }; - void *ptr = (__bridge_retained void *)(block1); - void (^block2)() = (__bridge_transfer void(^)())ptr; // no-warning - int x = 1; // expected-warning {{unused variable}} +void rdar15432770_ptr_release(const void *ptr) { + void (^block)() = (__bridge_transfer void(^)())ptr; // no-warning + // Test that warning is active. + int x = 1; // expected-warning {{unused}} } #pragma clang diagnostic pop