objective-C: When implementing custom accessor method for

a property, the -Wdirect-ivar-access should not warn when 
accessing the property's synthesized instance variable.
// rdar://13142820

llvm-svn: 175195
This commit is contained in:
Fariborz Jahanian 2013-02-14 19:07:19 +00:00
parent ba4a6d10e0
commit a934a022af
5 changed files with 60 additions and 3 deletions

View File

@ -2418,6 +2418,12 @@ public:
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& PropMap,
llvm::DenseMap<IdentifierInfo *, ObjCPropertyDecl*>& SuperPropMap);
/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
/// an ivar synthesized for 'Method' and 'Method' is a property accessor
/// declared in class 'IFace'.
bool IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
ObjCMethodDecl *Method, ObjCIvarDecl *IV);
/// Called by ActOnProperty to handle \@property declarations in
/// class extensions.
ObjCPropertyDecl *HandlePropertyInClassExtension(Scope *S,

View File

@ -2042,7 +2042,8 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S,
MarkAnyDeclReferenced(Loc, IV, true);
ObjCMethodFamily MF = CurMethod->getMethodFamily();
if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize)
if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize &&
!IvarBacksCurrentMethodAccessor(IFace, CurMethod, IV))
Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName();
ObjCIvarRefExpr *Result = new (Context) ObjCIvarRefExpr(IV, IV->getType(),

View File

@ -1259,7 +1259,8 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
if (ObjCMethodDecl *MD = getCurMethodDecl()) {
ObjCMethodFamily MF = MD->getMethodFamily();
warn = (MF != OMF_init && MF != OMF_dealloc &&
MF != OMF_finalize);
MF != OMF_finalize &&
!IvarBacksCurrentMethodAccessor(IDecl, MD, IV));
}
if (warn)
Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();

View File

@ -1541,6 +1541,33 @@ static void CollectSuperClassPropertyImplementations(ObjCInterfaceDecl *CDecl,
}
}
/// IvarBacksCurrentMethodAccessor - This routine returns 'true' if 'IV' is
/// an ivar synthesized for 'Method' and 'Method' is a property accessor
/// declared in class 'IFace'.
bool
Sema::IvarBacksCurrentMethodAccessor(ObjCInterfaceDecl *IFace,
ObjCMethodDecl *Method, ObjCIvarDecl *IV) {
if (!IV->getSynthesize())
return false;
ObjCMethodDecl *IMD = IFace->lookupMethod(Method->getSelector(),
Method->isInstanceMethod());
if (!IMD || !IMD->isPropertyAccessor())
return false;
// look up a property declaration whose one of its accessors is implemented
// by this method.
for (ObjCContainerDecl::prop_iterator P = IFace->prop_begin(),
E = IFace->prop_end(); P != E; ++P) {
ObjCPropertyDecl *property = *P;
if ((property->getGetterName() == IMD->getSelector() ||
property->getSetterName() == IMD->getSelector()) &&
(property->getPropertyIvarDecl() == IV))
return true;
}
return false;
}
/// \brief Default synthesizes all properties which must be synthesized
/// in class's \@implementation.
void Sema::DefaultSynthesizeProperties(Scope *S, ObjCImplDecl* IMPDecl,

View File

@ -1,4 +1,4 @@
// RUN: %clang_cc1 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -Wdirect-ivar-access -verify -Wno-objc-root-class %s
// RUN: %clang_cc1 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -fobjc-default-synthesize-properties -Wdirect-ivar-access -verify -Wno-objc-root-class %s
// rdar://6505197
__attribute__((objc_root_class)) @interface MyObject {
@ -54,3 +54,25 @@ id Test32(__weak ITest32 *x) {
: (*x).ivar; // expected-error {{dereferencing a __weak pointer is not allowed}}
}
// rdar://13142820
@protocol PROTOCOL
@property (copy, nonatomic) id property_in_protocol;
@end
__attribute__((objc_root_class)) @interface INTF <PROTOCOL>
@property (copy, nonatomic) id foo;
- (id) foo;
@end
@interface INTF()
@property (copy, nonatomic) id foo1;
- (id) foo1;
@end
@implementation INTF
- (id) foo { return _foo; }
- (id) property_in_protocol { return _property_in_protocol; } // expected-warning {{instance variable '_property_in_protocol' is being directly accessed}}
- (id) foo1 { return _foo1; }
@synthesize property_in_protocol = _property_in_protocol;
@end