brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · ec67baf Raw
127 lines · plain
1// RUN: %clang_cc1 -x objective-c -fsyntax-only -verify -Wno-objc-root-class %s2// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -verify -Wno-objc-root-class %s3 4@interface StopAccessingIvarsDirectlyExample5@property(strong) id name, rank, serialNumber;6@end7 8@implementation StopAccessingIvarsDirectlyExample9 10- (void)identifyYourSelf {11    if (self.name && self.rank && self.serialNumber)12      self.name = 0;13}14 15// @synthesize name, rank, serialNumber;16// default synthesis allows direct access to property ivars.17- (id)init {18        _name = _rank = _serialNumber = 0;19	return self;20}21 22- (void)dealloc {	23}24@end25 26 27// Test228@interface Test2 29@property(strong, nonatomic) id object;30@end31 32// object has user declared setter/getter so it won't be33// default synthesized; thus causing user error.34@implementation Test235- (id) bar { return object; } // expected-error {{use of undeclared identifier 'object'}}36- (void)setObject:(id)newObject {}37- (id)object { return 0; }38@end39 40// Test341@interface Test3 42{ 43  id uid;  // expected-note {{instance variable is declared here}}44} 45@property (readwrite, assign) id uid;  // expected-note {{property declared here}}46@end47 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 synthesized50- (void) myMethod { 51   self.uid = 0; // Use of the “setter” 52   uid = 0; // Use of the wrong instance variable53   _uid = 0; // Use of the property instance variable54} 55@end56 57@interface Test4 { 58  id _var;59} 60@property (readwrite, assign) id var; 61@end62 63 64// default synthesize property named 'var'65@implementation Test4 66- (id) myMethod {67  return self->_var;  //  compiles because 'var' is synthesized by default68}69@end70 71@interface Test5 72{ 73  id _var;74} 75@property (readwrite, assign) id var; 76@end77 78// default synthesis of property 'var'79@implementation Test5 80- (id) myMethod {81  Test5 *foo = 0; 82  return foo->_var; // OK83} 84@end85 86@interface Test6 87{ 88  id _var; // expected-note {{'_var' declared here}}89} 90@property (readwrite, assign) id var; 91@end92 93// no default synthesis. So error is expected.94@implementation Test6 95- (id) myMethod 96{97  return var; // expected-error {{use of undeclared identifier 'var'}}98} 99@synthesize var = _var; 100@end101 102int* _object;103 104@interface Test7105@property (readwrite, assign) id object; 106@end107 108// With default synthesis, '_object' is be the synthesized ivar not the global109// 'int*' object. So no error.110@implementation Test7 111- (id) myMethod {112  return _object;113} 114@end115 116@interface Test8117{118  id _y;119  id y; // expected-note {{instance variable is declared here}}120}121@property(copy) id y; // expected-note {{property declared here}}122@end123 124 125@implementation Test8 @end // expected-warning {{autosynthesized property 'y' will use  instance variable '_y', not existing instance variable 'y'}}126 127