objective-c: Implement gcc's -Wdirect-ivar-access option.

// rdar://6505197

llvm-svn: 161362
This commit is contained in:
Fariborz Jahanian 2012-08-06 23:50:51 +00:00
parent 6297fa8a14
commit 14f1aa70a9
4 changed files with 41 additions and 1 deletions

View File

@ -5759,6 +5759,8 @@ def ext_typecheck_base_super : Warning<
def warn_missing_method_return_type : Warning<
"method has no return type specified; defaults to 'id'">,
InGroup<MissingMethodReturnType>, DefaultIgnore;
def warn_direct_ivar_access : Warning<"instance variable %0 is being "
"directly accessed">, InGroup<DiagGroup<"direct-ivar-access">>, DefaultIgnore;
// Spell-checking diagnostics
def err_unknown_type_or_class_name_suggest : Error<

View File

@ -1961,6 +1961,9 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S,
return ExprError();
MarkAnyDeclReferenced(Loc, IV);
if (IV->getType()->isObjCObjectPointerType() &&
getLangOpts().getGC() == LangOptions::NonGC)
Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName();
return Owned(new (Context)
ObjCIvarRefExpr(IV, IV->getType(), Loc,
SelfExpr.take(), true, true));

View File

@ -1260,7 +1260,9 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak)
Diag(DE->getLocation(), diag::error_arc_weak_ivar_access);
}
if (IV->getType()->isObjCObjectPointerType() &&
getLangOpts().getGC() == LangOptions::NonGC)
Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
MemberLoc, BaseExpr.take(),
IsArrow));

View File

@ -0,0 +1,33 @@
// RUN: %clang_cc1 -fsyntax-only -Wdirect-ivar-access -verify -Wno-objc-root-class %s
// rdar://6505197
__attribute__((objc_root_class)) @interface MyObject {
@public
id _myMaster;
id _isTickledPink;
}
@property(retain) id myMaster;
@property(assign) id isTickledPink;
@end
@implementation MyObject
@synthesize myMaster = _myMaster;
@synthesize isTickledPink = _isTickledPink;
- (void) doSomething {
_myMaster = _isTickledPink; // expected-warning {{instance variable '_myMaster' is being directly accessed}} \
// expected-warning {{instance variable '_isTickledPink' is being directly accessed}}
}
@end
MyObject * foo ()
{
MyObject* p=0;
p.isTickledPink = p.myMaster; // ok
p->_isTickledPink = (*p)._myMaster; // expected-warning {{instance variable '_isTickledPink' is being directly accessed}} \
// expected-warning {{instance variable '_myMaster' is being directly accessed}}
return p->_isTickledPink; // expected-warning {{instance variable '_isTickledPink' is being directly accessed}}
}