]> granicus.if.org Git - clang/commitdiff
objc/c++: Issue diagnostic when free-standing ivar is accessed
authorFariborz Jahanian <fjahanian@apple.com>
Tue, 20 Dec 2011 22:21:08 +0000 (22:21 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Tue, 20 Dec 2011 22:21:08 +0000 (22:21 +0000)
in class method instead of crash. // rdar://10593227

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

lib/Sema/SemaExpr.cpp
test/SemaObjC/err-ivar-access-in-class-method.m [new file with mode: 0644]

index 62ee9f554d57f522c849bd1451e378990c63ffc6..65439032f2c29174f1294ad634213ce0fd7a5acf 100644 (file)
@@ -2010,6 +2010,12 @@ Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S,
           Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName();
       }
     }
+  } else if (Lookup.isSingleResult() &&
+             Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) {
+    // If accessing a stand-alone ivar in a class method, this is an error.
+    if (const ObjCIvarDecl *IV = dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl()))
+      return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method)
+                       << IV->getDeclName());
   }
 
   if (Lookup.empty() && II && AllowBuiltinCreation) {
diff --git a/test/SemaObjC/err-ivar-access-in-class-method.m b/test/SemaObjC/err-ivar-access-in-class-method.m
new file mode 100644 (file)
index 0000000..5efd622
--- /dev/null
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -x objective-c -fsyntax-only -verify %s
+// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -verify %s
+// rdar://10593227
+
+@class UIWindow;
+
+@interface CNAppDelegate
+
+@property (strong, nonatomic) UIWindow *window;
+
+@end
+
+
+@interface CNAppDelegate ()
+@property (nonatomic,retain) id foo;
+@end
+
+@implementation CNAppDelegate
+@synthesize foo;
+@synthesize window = _window;
+
++(void)myClassMethod;
+{
+        foo = 0; // expected-error {{instance variable 'foo' accessed in class method}}
+}
+@end