objective-c: Exclude -Wdirect-ivar-access for arc.
Allow direct ivar access in init and dealloc methods
in mrr. // rdar://650197


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161426 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index e8ba54d..c62df00 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1962,8 +1962,12 @@
 
       MarkAnyDeclReferenced(Loc, IV);
       if (IV->getType()->isObjCObjectPointerType() &&
-          getLangOpts().getGC() == LangOptions::NonGC)
-        Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName();
+          getLangOpts().getGC() == LangOptions::NonGC &&
+          !getLangOpts().ObjCAutoRefCount) {
+        ObjCMethodFamily MF = CurMethod->getMethodFamily();
+        if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize)
+          Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName();
+      }
       return Owned(new (Context)
                    ObjCIvarRefExpr(IV, IV->getType(), Loc,
                                    SelfExpr.take(), true, true));
diff --git a/lib/Sema/SemaExprMember.cpp b/lib/Sema/SemaExprMember.cpp
index 5a116b4..a23155d 100644
--- a/lib/Sema/SemaExprMember.cpp
+++ b/lib/Sema/SemaExprMember.cpp
@@ -1261,8 +1261,17 @@
           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();
+        getLangOpts().getGC() == LangOptions::NonGC &&
+        !getLangOpts().ObjCAutoRefCount) {
+      bool warn = true;
+      if (ObjCMethodDecl *MD = getCurMethodDecl()) {
+        ObjCMethodFamily MF = MD->getMethodFamily();
+        warn = (MF != OMF_init && MF != OMF_dealloc && 
+                MF != OMF_finalize);
+      }
+      if (warn)
+        Diag(MemberLoc, diag::warn_direct_ivar_access) << IV->getDeclName();
+    }
     return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(),
                                                MemberLoc, BaseExpr.take(),
                                                IsArrow));
diff --git a/test/SemaObjC/warn-direct-ivar-access.m b/test/SemaObjC/warn-direct-ivar-access.m
index 6850db6..dfddd82 100644
--- a/test/SemaObjC/warn-direct-ivar-access.m
+++ b/test/SemaObjC/warn-direct-ivar-access.m
@@ -20,6 +20,11 @@
     // expected-warning {{instance variable '_isTickledPink' is being directly accessed}}
 }
 
+- (id) init {
+    _myMaster=0;
+    return _myMaster;
+}
+- (void) dealloc { _myMaster = 0; }
 @end
 
 MyObject * foo ()