blob: 4f7484145865d7fb15b8f4c51c1303f25903036c [file] [log] [blame]
Ted Kremenek760b5e32013-04-22 22:09:21 +00001// RUN: %clang_cc1 -triple=x86_64-apple-darwin -fsyntax-only -verify %s
2
3//====------------------------------------------------------------====//
4// Test deprecated direct usage of the 'isa' pointer.
5//====------------------------------------------------------------====//
6
7typedef unsigned long NSUInteger;
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +00008
9typedef struct objc_object {
10 struct objc_class *isa;
11} *id;
12
13@interface NSObject {
14 id firstobj;
15 struct objc_class *isa;
16}
Ted Kremenek009d61d2013-06-24 21:35:39 +000017- (id)performSelector:(SEL)aSelector;;
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000018@end
19@interface Whatever : NSObject
20+self;
Ted Kremenek009d61d2013-06-24 21:35:39 +000021-(id)foo;
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000022@end
23
24static void func() {
25
26 id x;
27
28 // rdar://8290002
Fariborz Jahanian84510742013-03-27 21:19:25 +000029 [(*x).isa self]; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
30 [x->isa self]; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000031
32 Whatever *y;
33
34 // GCC allows this, with the following warning:
35 // instance variable 'isa' is @protected; this will be a hard error in the future
36 //
John McCall80c93a02013-03-01 09:20:14 +000037 // FIXME: see if we can avoid the warning that follows the error.
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000038 [(*y).isa self]; // expected-error {{instance variable 'isa' is protected}} \
John McCall80c93a02013-03-01 09:20:14 +000039 expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}}
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000040 [y->isa self]; // expected-error {{instance variable 'isa' is protected}} \
John McCall80c93a02013-03-01 09:20:14 +000041 expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}}
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000042}
43
44// rdar://11702488
45// If an ivar is (1) the first ivar in a root class and (2) named `isa`,
46// then it should get the same warnings that id->isa gets.
47
48@interface BaseClass {
49@public
Fariborz Jahanian3b602ce2013-03-28 23:39:11 +000050 Class isa; // expected-note 4 {{instance variable is declared here}}
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000051}
52@end
53
54@interface OtherClass {
55@public
56 id firstIvar;
57 Class isa; // note, not first ivar;
58}
59@end
60
61@interface Subclass : BaseClass @end
62
63@interface SiblingClass : BaseClass @end
64
65@interface Root @end
66
67@interface hasIsa : Root {
68@public
69 Class isa; // note, isa is not in root class
70}
71@end
72
73@implementation Subclass
74-(void)method {
75 hasIsa *u;
76 id v;
77 BaseClass *w;
78 Subclass *x;
79 SiblingClass *y;
80 OtherClass *z;
Fariborz Jahanian3b602ce2013-03-28 23:39:11 +000081 (void)v->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
82 (void)w->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
83 (void)x->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
84 (void)y->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000085 (void)z->isa;
86 (void)u->isa;
Fariborz Jahanian3b602ce2013-03-28 23:39:11 +000087
88 w->isa = 0; // expected-warning {{assignment to Objective-C's isa is deprecated in favor of object_setClass()}}
Fariborz Jahanian25cb4ac2012-06-21 21:35:15 +000089}
90@end
91
Ted Kremenekbedcd852013-04-24 06:52:20 +000092// Test for introspection of Objective-C pointers via bitmasking.
93
94void testBitmasking(NSObject *p) {
95 (void) (((NSUInteger) p) & 0x1); // expected-warning {{bitmasking for introspection of Objective-C object pointers is strongly discouraged}}
96 (void) (0x1 & ((NSUInteger) p)); // expected-warning {{bitmasking for introspection of Objective-C object pointers is strongly discouraged}}
97 (void) (((NSUInteger) p) ^ 0x1); // no-warning
98 (void) (0x1 ^ ((NSUInteger) p)); // no-warning
Ted Kremenek009d61d2013-06-24 21:35:39 +000099 (void) (0x1 & ((NSUInteger) [p performSelector:@selector(foo)])); // expected-warning {{bitmasking for introspection of Objective-C object pointers is strongly discouraged}}
100#pragma clang diagnostic push
101#pragma clang diagnostic ignored "-Wdeprecated-objc-pointer-introspection-performSelector"
102 (void) (0x1 & ((NSUInteger) [p performSelector:@selector(foo)])); // no-warning
103#pragma clang diagnostic pop
Ted Kremenekbedcd852013-04-24 06:52:20 +0000104}