From bd2e27e8484c1d378c9c9d089f61ff496627f391 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Fri, 6 Jul 2012 21:09:27 +0000 Subject: [PATCH] objc-arc: warn when assigning retained object to a 'weak' property just as we do the same for 'weak' variables. // rdar://11814185 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@159859 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Basic/DiagnosticSemaKinds.td | 3 ++- lib/Sema/SemaChecking.cpp | 12 +++++++++++- test/SemaObjC/arc.m | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index d691169381..38c346595f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -3481,7 +3481,8 @@ def warn_arc_non_pod_class_with_object_member : Warning< "to make it ABI-compatible">, InGroup, DefaultIgnore; def warn_arc_retained_assign : Warning< - "assigning retained object to %select{weak|unsafe_unretained}0 variable" + "assigning retained object to %select{weak|unsafe_unretained}0 " + "%select{property|variable}1" "; object will be released after assignment">, InGroup; def warn_arc_retained_property_assign : Warning< diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index f3bc273d99..708fd14e49 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -5233,7 +5233,7 @@ bool Sema::checkUnsafeAssigns(SourceLocation Loc, while (ImplicitCastExpr *cast = dyn_cast(RHS)) { if (cast->getCastKind() == CK_ARCConsumeObject) { Diag(Loc, diag::warn_arc_retained_assign) - << (LT == Qualifiers::OCL_ExplicitNone) + << (LT == Qualifiers::OCL_ExplicitNone) << 1 << RHS->getSourceRange(); return true; } @@ -5290,6 +5290,16 @@ void Sema::checkUnsafeExprAssigns(SourceLocation Loc, RHS = cast->getSubExpr(); } } + else if (Attributes & ObjCPropertyDecl::OBJC_PR_weak) { + while (ImplicitCastExpr *cast = dyn_cast(RHS)) { + if (cast->getCastKind() == CK_ARCConsumeObject) { + Diag(Loc, diag::warn_arc_retained_assign) + << 0 << 0<< RHS->getSourceRange(); + return; + } + RHS = cast->getSubExpr(); + } + } } } diff --git a/test/SemaObjC/arc.m b/test/SemaObjC/arc.m index 9c3b298cb5..cdc02d9821 100644 --- a/test/SemaObjC/arc.m +++ b/test/SemaObjC/arc.m @@ -696,3 +696,24 @@ void _NSCalcBeze(NSColor* color, NSColor* bezelColors[]); // expected-error {{mu } @end +// rdar://11814185 +@interface Radar11814185 +@property (nonatomic, weak) Radar11814185* picker1; ++ alloc; +- init; +@end + +@implementation Radar11814185 + +@synthesize picker1; + +- (void)viewDidLoad +{ + picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak variable; object will be released after assignment}} + self.picker1 = [[Radar11814185 alloc] init]; // expected-warning {{assigning retained object to weak property; object will be released after assignment}} +} + ++ alloc { return 0; } +- init { return 0; } +@end + -- 2.40.0