objective-c arc: Issue warning under -Wreceiver-is-weak

if receiver is a 'weak' property, by type or by attribute.
// rdar://10225276


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@155159 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Fariborz Jahanian 2012-04-19 21:44:57 +00:00
Родитель 16c111ba80
Коммит 289677da2d
5 изменённых файлов: 71 добавлений и 7 удалений

Просмотреть файл

@ -660,7 +660,8 @@ def warn_arc_perform_selector_leaks : Warning<
def err_gc_weak_property_strong_type : Error< def err_gc_weak_property_strong_type : Error<
"weak attribute declared on a __strong type property in GC mode">; "weak attribute declared on a __strong type property in GC mode">;
def warn_receiver_is_weak : Warning < def warn_receiver_is_weak : Warning <
"weak receiver may be unpredictably null in ARC mode">, "weak %select{receiver|property|implicit property}0 may be "
"unpredictably null in ARC mode">,
InGroup<DiagGroup<"receiver-is-weak">>, DefaultIgnore; InGroup<DiagGroup<"receiver-is-weak">>, DefaultIgnore;
def error_synthesized_ivar_yet_not_supported : Error< def error_synthesized_ivar_yet_not_supported : Error<

Просмотреть файл

@ -5901,6 +5901,9 @@ public:
bool IsInstance); bool IsInstance);
bool inferObjCARCLifetime(ValueDecl *decl); bool inferObjCARCLifetime(ValueDecl *decl);
void DiagnoseARCUseOfWeakReceiver(NamedDecl *PDecl,
QualType T, SourceLocation Loc);
ExprResult ExprResult
HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT, HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,

Просмотреть файл

@ -1323,6 +1323,32 @@ ObjCMethodDecl *Sema::LookupMethodInQualifiedType(Selector Sel,
return 0; return 0;
} }
void
Sema::DiagnoseARCUseOfWeakReceiver(NamedDecl *PDecl,
QualType T, SourceLocation Loc) {
if (!getLangOpts().ObjCAutoRefCount)
return;
if (T.getObjCLifetime() == Qualifiers::OCL_Weak) {
Diag(Loc, diag::warn_receiver_is_weak)
<< (!PDecl ? 0 : (isa<ObjCPropertyDecl>(PDecl) ? 1 : 2));
if (PDecl) {
if (isa<ObjCPropertyDecl>(PDecl))
Diag(PDecl->getLocation(), diag::note_property_declare);
else
Diag(PDecl->getLocation(), diag::note_method_declared_at) << PDecl;
}
return;
}
if (PDecl)
if (ObjCPropertyDecl *Prop = dyn_cast<ObjCPropertyDecl>(PDecl))
if (Prop->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_weak) {
Diag(Loc, diag::warn_receiver_is_weak) << 1;
Diag(Prop->getLocation(), diag::note_property_declare);
}
}
/// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an /// HandleExprPropertyRefExpr - Handle foo.bar where foo is a pointer to an
/// objective C interface. This is a property reference expression. /// objective C interface. This is a property reference expression.
ExprResult Sema:: ExprResult Sema::
@ -1354,7 +1380,6 @@ HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
// Check whether we can reference this property. // Check whether we can reference this property.
if (DiagnoseUseOfDecl(PD, MemberLoc)) if (DiagnoseUseOfDecl(PD, MemberLoc))
return ExprError(); return ExprError();
if (Super) if (Super)
return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy, return Owned(new (Context) ObjCPropertyRefExpr(PD, Context.PseudoObjectTy,
VK_LValue, OK_ObjCProperty, VK_LValue, OK_ObjCProperty,
@ -1372,7 +1397,7 @@ HandleExprPropertyRefExpr(const ObjCObjectPointerType *OPT,
// Check whether we can reference this property. // Check whether we can reference this property.
if (DiagnoseUseOfDecl(PD, MemberLoc)) if (DiagnoseUseOfDecl(PD, MemberLoc))
return ExprError(); return ExprError();
if (Super) if (Super)
return Owned(new (Context) ObjCPropertyRefExpr(PD, return Owned(new (Context) ObjCPropertyRefExpr(PD,
Context.PseudoObjectTy, Context.PseudoObjectTy,
@ -2379,10 +2404,10 @@ ExprResult Sema::BuildInstanceMessage(Expr *Receiver,
} }
if (getLangOpts().ObjCAutoRefCount) { if (getLangOpts().ObjCAutoRefCount) {
if (Receiver && if (Receiver)
(Receiver->IgnoreParenImpCasts()->getType().getObjCLifetime() DiagnoseARCUseOfWeakReceiver(0 /* PDecl */,
== Qualifiers::OCL_Weak)) Receiver->IgnoreParenImpCasts()->getType(),
Diag(Receiver->getLocStart(), diag::warn_receiver_is_weak); Receiver->getLocStart());
// In ARC, annotate delegate init calls. // In ARC, annotate delegate init calls.
if (Result->getMethodFamily() == OMF_init && if (Result->getMethodFamily() == OMF_init &&

Просмотреть файл

@ -478,6 +478,8 @@ bool ObjCPropertyOpBuilder::findGetter() {
if (RefExpr->isImplicitProperty()) { if (RefExpr->isImplicitProperty()) {
if ((Getter = RefExpr->getImplicitPropertyGetter())) { if ((Getter = RefExpr->getImplicitPropertyGetter())) {
GetterSelector = Getter->getSelector(); GetterSelector = Getter->getSelector();
S.DiagnoseARCUseOfWeakReceiver(Getter, Getter->getResultType(),
RefExpr->getLocation());
return true; return true;
} }
else { else {
@ -497,6 +499,8 @@ bool ObjCPropertyOpBuilder::findGetter() {
} }
ObjCPropertyDecl *prop = RefExpr->getExplicitProperty(); ObjCPropertyDecl *prop = RefExpr->getExplicitProperty();
S.DiagnoseARCUseOfWeakReceiver(prop, prop->getType(),
RefExpr->getLocation());
Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr); Getter = LookupMethodInReceiverType(S, prop->getGetterName(), RefExpr);
return (Getter != 0); return (Getter != 0);
} }

Просмотреть файл

@ -17,3 +17,34 @@ void test0(Test0 *x) {
[weakx setBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably null in ARC mode}} [weakx setBlock: ^{ [x actNow]; }]; // expected-warning {{weak receiver may be unpredictably null in ARC mode}}
weakx.block = ^{ [x actNow]; }; weakx.block = ^{ [x actNow]; };
} }
@interface Test
{
__weak Test* weak_prop;
}
- (void) Meth;
@property __weak Test* weak_prop; // expected-note 2 {{property declared here}}
@property (weak, atomic) id weak_atomic_prop; // expected-note 2 {{property declared here}}
- (__weak id) P; // expected-note 2 {{method 'P' declared here}}
@end
@implementation Test
- (void) Meth {
if (self.weak_prop) { // expected-warning {{weak property may be unpredictably null in ARC mode}}
self.weak_prop = 0;
}
if (self.weak_atomic_prop) { // expected-warning {{weak property may be unpredictably null in ARC mode}}
self.weak_atomic_prop = 0;
}
[self.weak_prop Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
id pi = self.P; // expected-warning {{weak implicit property may be unpredictably null in ARC mode}}
[self.weak_atomic_prop Meth]; // expected-warning {{weak property may be unpredictably null in ARC mode}}
[self.P Meth]; // expected-warning {{weak implicit property may be unpredictably null in ARC mode}}
}
- (__weak id) P { return 0; }
@dynamic weak_prop, weak_atomic_prop;
@end