1 // RUN: %clang_cc1 -x objective-c -fsyntax-only -verify -Wno-objc-root-class %s
2 // RUN: %clang_cc1 -x objective-c++ -fsyntax-only -verify -Wno-objc-root-class %s
4 @interface StopAccessingIvarsDirectlyExample
5 @property(strong) id name, rank, serialNumber;
8 @implementation StopAccessingIvarsDirectlyExample
10 - (void)identifyYourSelf {
11 if (self.name && self.rank && self.serialNumber)
15 // @synthesize name, rank, serialNumber;
16 // default synthesis allows direct access to property ivars.
18 _name = _rank = _serialNumber = 0;
29 @property(strong, nonatomic) id object;
32 // object has user declared setter/getter so it won't be
33 // default synthesized; thus causing user error.
35 - (id) bar { return object; } // expected-error {{use of undeclared identifier 'object'}}
36 - (void)setObject:(id)newObject {}
37 - (id)object { return 0; }
43 id uid; // expected-note {{instance variable is declared here}}
45 @property (readwrite, assign) id uid; // expected-note {{property declared here}}
48 @implementation Test3 // expected-warning {{autosynthesized property 'uid' will use synthesized instance variable '_uid', not existing instance variable 'uid'}}
49 // Oops, forgot to write @synthesize! will be default synthesized
51 self.uid = 0; // Use of the “setter”
52 uid = 0; // Use of the wrong instance variable
53 _uid = 0; // Use of the property instance variable
60 @property (readwrite, assign) id var;
64 // default synthesize property named 'var'
67 return self->_var; // compiles because 'var' is synthesized by default
75 @property (readwrite, assign) id var;
78 // default synthesis of property 'var'
82 return foo->_var; // OK
88 id _var; // expected-note {{'_var' declared here}}
90 @property (readwrite, assign) id var;
93 // no default synthesis. So error is expected.
97 return var; // expected-error {{use of undeclared identifier 'var'}}
99 @synthesize var = _var;
105 @property (readwrite, assign) id object;
108 // With default synthesis, '_object' is be the synthesized ivar not the global
109 // 'int*' object. So no error.
110 @implementation Test7
119 id y; // expected-note {{instance variable is declared here}}
121 @property(copy) id y; // expected-note {{property declared here}}
125 @implementation Test8 @end // expected-warning {{autosynthesized property 'y' will use instance variable '_y', not existing instance variable 'y'}}