objective-c: Implement gcc's -Wdirect-ivar-access option.
// rdar://6505197
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@161362 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index 4df26cf..455f3b6 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -5759,6 +5759,8 @@
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<
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index ea121b8..e8ba54d 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1961,6 +1961,9 @@
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));
diff --git a/lib/Sema/SemaExprMember.cpp b/lib/Sema/SemaExprMember.cpp
index 79973b3..5a116b4 100644
--- a/lib/Sema/SemaExprMember.cpp
+++ b/lib/Sema/SemaExprMember.cpp
@@ -1260,7 +1260,9 @@
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
index 0000000..6850db6
--- /dev/null
+++ b/test/SemaObjC/warn-direct-ivar-access.m
@@ -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}}
+}
+