1// RUN: %clang_cc1 -x objective-c -fsyntax-only -fobjc-default-synthesize-properties -verify -Wno-objc-root-class %s 2// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -fobjc-default-synthesize-properties -verify -Wno-objc-root-class %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 { 19 _name = _rank = _serialNumber = 0; 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{ 44 id uid; 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” 53 uid = 0; // Use of the wrong instance variable 54 _uid = 0; // Use of the property instance variable 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 { 68 return self->_var; // compiles because 'var' is synthesized by default 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; 83 return foo->_var; // OK 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 103int* _object; 104 105@interface Test7 106@property (readwrite, assign) id object; 107@end 108 109// With default synthesis, '_object' is be the synthesized ivar not the global 110// 'int*' object. So no error. 111@implementation Test7 112- (id) myMethod { 113 return _object; 114} 115@end 116 117