]> granicus.if.org Git - clang/commitdiff
objc-arc: Make -Wdirect-ivar-access accessible to all
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 7 Aug 2012 23:48:10 +0000 (23:48 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 7 Aug 2012 23:48:10 +0000 (23:48 +0000)
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

lib/Sema/SemaExpr.cpp
lib/Sema/SemaExprMember.cpp
test/SemaObjC/warn-direct-ivar-access.m

index c62df00a8750a53cd5f0f3928556a8a69ba892d7..5bc82c7394c132e0387c175f3ea63ee91c501dcd 100644 (file)
@@ -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();
index a23155d85e1d31212ac9bbd656378179d346a4a7..0258b2a158fb4fb0504d518c5a6b6bcd703d9e2a 100644 (file)
@@ -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<UnaryOperator>(BaseExp))
@@ -1257,13 +1258,12 @@ Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr,
           BaseExp = UO->getSubExpr()->IgnoreParenCasts();
       
       if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(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 && 
index dfddd823e38161a84406bf1923ad4b56f630a891..d380ebf5aec68b9b5c0f6cd80b42163512e98a96 100644 (file)
@@ -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}}
+}
+