arc: diagnose dereferencing a __weak pointer which may be

null at any time. // rdar://9612030

llvm-svn: 133168
This commit is contained in:
Fariborz Jahanian 2011-06-16 17:29:56 +00:00
parent 2798ded9de
commit 62c72d06ff
3 changed files with 28 additions and 0 deletions

View File

@ -2815,6 +2815,9 @@ def err_typecheck_member_reference_struct_union : Error<
"member reference base type %0 is not a structure or union">;
def err_typecheck_member_reference_ivar : Error<
"%0 does not have a member named %1">;
def error_arc_weak_ivar_access : Error<
"dereferencing a __weak pointer is not allowed due to possible "
"null value caused by race condition, assign it to strong variable first">;
def err_typecheck_member_reference_arrow : Error<
"member reference type %0 is not a pointer">;
def err_typecheck_member_reference_suggestion : Error<

View File

@ -4333,6 +4333,16 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
Diag(MemberLoc, diag::error_protected_ivar_access)
<< IV->getDeclName();
}
if (getLangOptions().ObjCAutoRefCount) {
Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts();
if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp))
if (UO->getOpcode() == UO_Deref)
BaseExp = UO->getSubExpr()->IgnoreParenCasts();
if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp))
if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
}
return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
MemberLoc, BaseExpr.take(),

View File

@ -548,3 +548,18 @@ int Test31() {
int k = (pcls->isa ? i : j); // expected-error {{member reference base type 'Class<PTest31>' is not a structure or union}}
return cls->isa ? i : j; // expected-error {{member reference base type 'Class' is not a structure or union}}
}
// rdar://9612030
@interface ITest32 {
@public
id ivar;
}
@end
id Test32(__weak ITest32 *x) {
__weak ITest32 *y;
x->ivar = 0; // expected-error {{dereferencing a __weak pointer is not allowed}}
return y ? y->ivar // expected-error {{dereferencing a __weak pointer is not allowed}}
: (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}}
}