Ted Kremenek | 244e7e5 | 2013-04-22 22:09:21 +0000 | [diff] [blame] | 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 | |
| 7 | typedef unsigned long NSUInteger; |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 8 | |
| 9 | typedef 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 | |
| 22 | static void func() { |
| 23 | |
| 24 | id x; |
| 25 | |
| 26 | // rdar://8290002 |
Fariborz Jahanian | 7e35274 | 2013-03-27 21:19:25 +0000 | [diff] [blame] | 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()}} |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 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 | // |
John McCall | 2fbe92c | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 35 | // FIXME: see if we can avoid the warning that follows the error. |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 36 | [(*y).isa self]; // expected-error {{instance variable 'isa' is protected}} \ |
John McCall | 2fbe92c | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 37 | expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}} |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 38 | [y->isa self]; // expected-error {{instance variable 'isa' is protected}} \ |
John McCall | 2fbe92c | 2013-03-01 09:20:14 +0000 | [diff] [blame] | 39 | expected-warning{{receiver type 'struct objc_class *' is not 'id' or interface pointer, consider casting it to 'id'}} |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 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 |
Fariborz Jahanian | 99a72d2 | 2013-03-28 23:39:11 +0000 | [diff] [blame] | 48 | Class isa; // expected-note 4 {{instance variable is declared here}} |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 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; |
Fariborz Jahanian | 99a72d2 | 2013-03-28 23:39:11 +0000 | [diff] [blame] | 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()}} |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 83 | (void)z->isa; |
| 84 | (void)u->isa; |
Fariborz Jahanian | 99a72d2 | 2013-03-28 23:39:11 +0000 | [diff] [blame] | 85 | |
| 86 | w->isa = 0; // expected-warning {{assignment to Objective-C's isa is deprecated in favor of object_setClass()}} |
Fariborz Jahanian | 0910059 | 2012-06-21 21:35:15 +0000 | [diff] [blame] | 87 | } |
| 88 | @end |
| 89 | |
Ted Kremenek | 4abc9bd | 2013-04-24 06:52:20 +0000 | [diff] [blame] | 90 | // Test for introspection of Objective-C pointers via bitmasking. |
| 91 | |
| 92 | void 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 | } |