xref: /llvm-project/clang/test/SemaObjC/deprecated-objc-introspection.m (revision 760b5e360d6528af5454df27c71896737a0acaa5)
1// 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;
8
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
27  [(*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()}}
29
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  //
35  // FIXME: see if we can avoid the warning that follows the error.
36  [(*y).isa self]; // expected-error {{instance variable 'isa' is protected}} \
37                      expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}}
38  [y->isa self]; // expected-error {{instance variable 'isa' is protected}} \
39                    expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}}
40}
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
48    Class isa; // expected-note 4 {{instance variable is declared here}}
49}
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;
79    (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()}}
83    (void)z->isa;
84    (void)u->isa;
85
86    w->isa = 0; // expected-warning {{assignment to Objective-C's isa is deprecated in favor of object_setClass()}}
87}
88@end
89
90