From: Fariborz Jahanian Date: Tue, 7 Aug 2012 23:48:10 +0000 (+0000) Subject: objc-arc: Make -Wdirect-ivar-access accessible to all X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b25466e8b33285a13d0303461db37e903ec505c1;p=clang objc-arc: Make -Wdirect-ivar-access accessible to all memory models, except when arc is accessing a weak ivar (which is an error). // rdar://6505197 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161458 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index c62df00a87..5bc82c7394 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -1961,9 +1961,7 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, return ExprError(); MarkAnyDeclReferenced(Loc, IV); - if (IV->getType()->isObjCObjectPointerType() && - getLangOpts().getGC() == LangOptions::NonGC && - !getLangOpts().ObjCAutoRefCount) { + if (IV->getType()->isObjCObjectPointerType()) { ObjCMethodFamily MF = CurMethod->getMethodFamily(); if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize) Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); diff --git a/lib/Sema/SemaExprMember.cpp b/lib/Sema/SemaExprMember.cpp index a23155d85e..0258b2a158 100644 --- a/lib/Sema/SemaExprMember.cpp +++ b/lib/Sema/SemaExprMember.cpp @@ -1250,6 +1250,7 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, << IV->getDeclName(); } } + bool warn = true; if (getLangOpts().ObjCAutoRefCount) { Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts(); if (UnaryOperator *UO = dyn_cast(BaseExp)) @@ -1257,13 +1258,12 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, BaseExp = UO->getSubExpr()->IgnoreParenCasts(); if (DeclRefExpr *DE = dyn_cast(BaseExp)) - if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) + if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) { Diag(DE->getLocation(), diag::error_arc_weak_ivar_access); + warn = false; + } } - if (IV->getType()->isObjCObjectPointerType() && - getLangOpts().getGC() == LangOptions::NonGC && - !getLangOpts().ObjCAutoRefCount) { - bool warn = true; + if (warn && IV->getType()->isObjCObjectPointerType()) { if (ObjCMethodDecl *MD = getCurMethodDecl()) { ObjCMethodFamily MF = MD->getMethodFamily(); warn = (MF != OMF_init && MF != OMF_dealloc && diff --git a/test/SemaObjC/warn-direct-ivar-access.m b/test/SemaObjC/warn-direct-ivar-access.m index dfddd823e3..d380ebf5ae 100644 --- a/test/SemaObjC/warn-direct-ivar-access.m +++ b/test/SemaObjC/warn-direct-ivar-access.m @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -fsyntax-only -Wdirect-ivar-access -verify -Wno-objc-root-class %s +// RUN: %clang_cc1 -fsyntax-only -fobjc-arc -fobjc-runtime-has-weak -Wdirect-ivar-access -verify -Wno-objc-root-class %s // rdar://6505197 __attribute__((objc_root_class)) @interface MyObject { @@ -7,13 +7,13 @@ __attribute__((objc_root_class)) @interface MyObject { id _isTickledPink; } @property(retain) id myMaster; -@property(assign) id isTickledPink; +@property(assign) id isTickledPink; // expected-note {{property declared here}} @end @implementation MyObject @synthesize myMaster = _myMaster; -@synthesize isTickledPink = _isTickledPink; +@synthesize isTickledPink = _isTickledPink; // expected-error {{existing ivar '_isTickledPink' for property 'isTickledPink'}} - (void) doSomething { _myMaster = _isTickledPink; // expected-warning {{instance variable '_myMaster' is being directly accessed}} \ @@ -36,3 +36,16 @@ MyObject * foo () return p->_isTickledPink; // expected-warning {{instance variable '_isTickledPink' is being directly accessed}} } +@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}} +} +