]> granicus.if.org Git - clang/commitdiff
objective-c: Implement gcc's -Wdirect-ivar-access option.
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 6 Aug 2012 23:50:51 +0000 (23:50 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 6 Aug 2012 23:50:51 +0000 (23:50 +0000)
// rdar://6505197

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161362 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExpr.cpp
lib/Sema/SemaExprMember.cpp
test/SemaObjC/warn-direct-ivar-access.m [new file with mode: 0644]

index 4df26cfa55fec3a1b4100e9b0fff31540d0cb195..455f3b6ec49d8af1bc157a8aefa674283be002c7 100644 (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<
index ea121b8b3cece8aa38eb80ec25b8fd8bf0109014..e8ba54d24fb837c69c76aae86418d80483e603d4 100644 (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));
index 79973b352fe238182e11d4124346124dfba867e5..5a116b4496479ad17b483d97285a7b99c83b1579 100644 (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));
diff --git a/test/SemaObjC/warn-direct-ivar-access.m b/test/SemaObjC/warn-direct-ivar-access.m
new file mode 100644 (file)
index 0000000..6850db6
--- /dev/null
@@ -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}}
+}
+