Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -x objective-c -fsyntax-only -fobjc-nonfragile-abi -fobjc-default-synthesize-properties -verify %s |
| 2 | // RUN: %clang_cc1 -x objective-c++ -fsyntax-only -fobjc-nonfragile-abi -fobjc-default-synthesize-properties -verify %s |
| 3 | // rdar://8843851 |
| 4 | |
| 5 | @interface StopAccessingIvarsDirectlyExample |
| 6 | @property(strong) id name, rank, serialNumber; |
| 7 | @end |
| 8 | |
| 9 | @implementation StopAccessingIvarsDirectlyExample |
| 10 | |
| 11 | - (void)identifyYourSelf { |
| 12 | if (self.name && self.rank && self.serialNumber) |
| 13 | self.name = 0; |
| 14 | } |
| 15 | |
| 16 | // @synthesize name, rank, serialNumber; |
| 17 | // default synthesis allows direct access to property ivars. |
| 18 | - (id)init { |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 19 | _name = _rank = _serialNumber = 0; |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 20 | return self; |
| 21 | } |
| 22 | |
| 23 | - (void)dealloc { |
| 24 | } |
| 25 | @end |
| 26 | |
| 27 | |
| 28 | // Test2 |
| 29 | @interface Test2 |
| 30 | @property(strong, nonatomic) id object; |
| 31 | @end |
| 32 | |
| 33 | // object has user declared setter/getter so it won't be |
| 34 | // default synthesized; thus causing user error. |
| 35 | @implementation Test2 |
| 36 | - (id) bar { return object; } // expected-error {{use of undeclared identifier 'object'}} |
| 37 | - (void)setObject:(id)newObject {} |
| 38 | - (id)object { return 0; } |
| 39 | @end |
| 40 | |
| 41 | // Test3 |
| 42 | @interface Test3 |
| 43 | { |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 44 | id uid; |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 45 | } |
| 46 | @property (readwrite, assign) id uid; |
| 47 | @end |
| 48 | |
| 49 | @implementation Test3 |
| 50 | // Oops, forgot to write @synthesize! will be default synthesized |
| 51 | - (void) myMethod { |
| 52 | self.uid = 0; // Use of the “setter” |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 53 | uid = 0; // Use of the wrong instance variable |
| 54 | _uid = 0; // Use of the property instance variable |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 55 | } |
| 56 | @end |
| 57 | |
| 58 | @interface Test4 { |
| 59 | id _var; |
| 60 | } |
| 61 | @property (readwrite, assign) id var; |
| 62 | @end |
| 63 | |
| 64 | |
| 65 | // default synthesize property named 'var' |
| 66 | @implementation Test4 |
| 67 | - (id) myMethod { |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 68 | return self->_var; // compiles because 'var' is synthesized by default |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 69 | } |
| 70 | @end |
| 71 | |
| 72 | @interface Test5 |
| 73 | { |
| 74 | id _var; |
| 75 | } |
| 76 | @property (readwrite, assign) id var; |
| 77 | @end |
| 78 | |
| 79 | // default synthesis of property 'var' |
| 80 | @implementation Test5 |
| 81 | - (id) myMethod { |
| 82 | Test5 *foo = 0; |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 83 | return foo->_var; // OK |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 84 | } |
| 85 | @end |
| 86 | |
| 87 | @interface Test6 |
| 88 | { |
| 89 | id _var; // expected-note {{'_var' declared here}} |
| 90 | } |
| 91 | @property (readwrite, assign) id var; |
| 92 | @end |
| 93 | |
| 94 | // no default synthesis. So error is expected. |
| 95 | @implementation Test6 |
| 96 | - (id) myMethod |
| 97 | { |
| 98 | return var; // expected-error {{use of undeclared identifier 'var'}} |
| 99 | } |
| 100 | @synthesize var = _var; |
| 101 | @end |
| 102 | |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 103 | int* _object; |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 104 | |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 105 | @interface Test7 |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 106 | @property (readwrite, assign) id object; |
| 107 | @end |
| 108 | |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 109 | // With default synthesis, '_object' is be the synthesized ivar not the global |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 110 | // 'int*' object. So no error. |
| 111 | @implementation Test7 |
| 112 | - (id) myMethod { |
Ted Kremenek | d2ee809 | 2011-09-27 23:39:40 +0000 | [diff] [blame^] | 113 | return _object; |
Fariborz Jahanian | 3269197 | 2011-08-31 23:32:48 +0000 | [diff] [blame] | 114 | } |
| 115 | @end |
| 116 | |