blob: faaef254d5962bf41894f335906ffda173eb4714 [file] [log] [blame]
Ted Kremenek244e7e52013-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 Jahanian09100592012-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}
17@end
18@interface Whatever : NSObject
19+self;
20@end
21
22static void func() {
23
24 id x;
25
26 // rdar://8290002
Fariborz Jahanian7e352742013-03-27 21:19:25 +000027 [(*x).isa self]; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
28 [x->isa self]; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
Fariborz Jahanian09100592012-06-21 21:35:15 +000029
30 Whatever *y;
31
32 // GCC allows this, with the following warning:
33 // instance variable 'isa' is @protected; this will be a hard error in the future
34 //
John McCall2fbe92c2013-03-01 09:20:14 +000035 // FIXME: see if we can avoid the warning that follows the error.
Fariborz Jahanian09100592012-06-21 21:35:15 +000036 [(*y).isa self]; // expected-error {{instance variable 'isa' is protected}} \
John McCall2fbe92c2013-03-01 09:20:14 +000037 expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}}
Fariborz Jahanian09100592012-06-21 21:35:15 +000038 [y->isa self]; // expected-error {{instance variable 'isa' is protected}} \
John McCall2fbe92c2013-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 Jahanian09100592012-06-21 21:35:15 +000040}
41
42// rdar://11702488
43// If an ivar is (1) the first ivar in a root class and (2) named `isa`,
44// then it should get the same warnings that id->isa gets.
45
46@interface BaseClass {
47@public
Fariborz Jahanian99a72d22013-03-28 23:39:11 +000048 Class isa; // expected-note 4 {{instance variable is declared here}}
Fariborz Jahanian09100592012-06-21 21:35:15 +000049}
50@end
51
52@interface OtherClass {
53@public
54 id firstIvar;
55 Class isa; // note, not first ivar;
56}
57@end
58
59@interface Subclass : BaseClass @end
60
61@interface SiblingClass : BaseClass @end
62
63@interface Root @end
64
65@interface hasIsa : Root {
66@public
67 Class isa; // note, isa is not in root class
68}
69@end
70
71@implementation Subclass
72-(void)method {
73 hasIsa *u;
74 id v;
75 BaseClass *w;
76 Subclass *x;
77 SiblingClass *y;
78 OtherClass *z;
Fariborz Jahanian99a72d22013-03-28 23:39:11 +000079 (void)v->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
80 (void)w->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
81 (void)x->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
82 (void)y->isa; // expected-warning {{direct access to Objective-C's isa is deprecated in favor of object_getClass()}}
Fariborz Jahanian09100592012-06-21 21:35:15 +000083 (void)z->isa;
84 (void)u->isa;
Fariborz Jahanian99a72d22013-03-28 23:39:11 +000085
86 w->isa = 0; // expected-warning {{assignment to Objective-C's isa is deprecated in favor of object_setClass()}}
Fariborz Jahanian09100592012-06-21 21:35:15 +000087}
88@end
89
Ted Kremenek4abc9bd2013-04-24 06:52:20 +000090// Test for introspection of Objective-C pointers via bitmasking.
91
92void testBitmasking(NSObject *p) {
93 (void) (((NSUInteger) p) & 0x1); // expected-warning {{bitmasking for introspection of Objective-C object pointers is strongly discouraged}}
94 (void) (0x1 & ((NSUInteger) p)); // expected-warning {{bitmasking for introspection of Objective-C object pointers is strongly discouraged}}
95 (void) (((NSUInteger) p) ^ 0x1); // no-warning
96 (void) (0x1 ^ ((NSUInteger) p)); // no-warning
97}