2013-09-28 04:21:48 +08:00
|
|
|
// RUN: %clang_cc1 -x objective-c -fsyntax-only -verify -Wno-objc-root-class %s
|
|
|
|
// RUN: %clang_cc1 -x objective-c++ -fsyntax-only -verify -Wno-objc-root-class %s
|
2011-09-01 07:32:48 +08:00
|
|
|
// rdar://8843851
|
|
|
|
|
|
|
|
@interface StopAccessingIvarsDirectlyExample
|
|
|
|
@property(strong) id name, rank, serialNumber;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation StopAccessingIvarsDirectlyExample
|
|
|
|
|
|
|
|
- (void)identifyYourSelf {
|
|
|
|
if (self.name && self.rank && self.serialNumber)
|
|
|
|
self.name = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// @synthesize name, rank, serialNumber;
|
|
|
|
// default synthesis allows direct access to property ivars.
|
|
|
|
- (id)init {
|
2011-09-28 07:39:40 +08:00
|
|
|
_name = _rank = _serialNumber = 0;
|
2011-09-01 07:32:48 +08:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc {
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
// Test2
|
|
|
|
@interface Test2
|
|
|
|
@property(strong, nonatomic) id object;
|
|
|
|
@end
|
|
|
|
|
|
|
|
// object has user declared setter/getter so it won't be
|
|
|
|
// default synthesized; thus causing user error.
|
|
|
|
@implementation Test2
|
|
|
|
- (id) bar { return object; } // expected-error {{use of undeclared identifier 'object'}}
|
|
|
|
- (void)setObject:(id)newObject {}
|
|
|
|
- (id)object { return 0; }
|
|
|
|
@end
|
|
|
|
|
|
|
|
// Test3
|
|
|
|
@interface Test3
|
|
|
|
{
|
2012-09-25 06:00:36 +08:00
|
|
|
id uid; // expected-note {{instance variable is declared here}}
|
2011-09-01 07:32:48 +08:00
|
|
|
}
|
2012-06-20 06:51:22 +08:00
|
|
|
@property (readwrite, assign) id uid; // expected-note {{property declared here}}
|
2011-09-01 07:32:48 +08:00
|
|
|
@end
|
|
|
|
|
2012-06-20 06:51:22 +08:00
|
|
|
// rdar://11671080
|
2012-06-21 07:33:16 +08:00
|
|
|
@implementation Test3 // expected-warning {{autosynthesized property 'uid' will use synthesized instance variable '_uid', not existing instance variable 'uid'}}
|
2011-09-01 07:32:48 +08:00
|
|
|
// Oops, forgot to write @synthesize! will be default synthesized
|
|
|
|
- (void) myMethod {
|
|
|
|
self.uid = 0; // Use of the “setter”
|
2011-09-28 07:39:40 +08:00
|
|
|
uid = 0; // Use of the wrong instance variable
|
|
|
|
_uid = 0; // Use of the property instance variable
|
2011-09-01 07:32:48 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Test4 {
|
|
|
|
id _var;
|
|
|
|
}
|
|
|
|
@property (readwrite, assign) id var;
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
// default synthesize property named 'var'
|
|
|
|
@implementation Test4
|
|
|
|
- (id) myMethod {
|
2011-09-28 07:39:40 +08:00
|
|
|
return self->_var; // compiles because 'var' is synthesized by default
|
2011-09-01 07:32:48 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Test5
|
|
|
|
{
|
|
|
|
id _var;
|
|
|
|
}
|
|
|
|
@property (readwrite, assign) id var;
|
|
|
|
@end
|
|
|
|
|
|
|
|
// default synthesis of property 'var'
|
|
|
|
@implementation Test5
|
|
|
|
- (id) myMethod {
|
|
|
|
Test5 *foo = 0;
|
2011-09-28 07:39:40 +08:00
|
|
|
return foo->_var; // OK
|
2011-09-01 07:32:48 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface Test6
|
|
|
|
{
|
|
|
|
id _var; // expected-note {{'_var' declared here}}
|
|
|
|
}
|
|
|
|
@property (readwrite, assign) id var;
|
|
|
|
@end
|
|
|
|
|
|
|
|
// no default synthesis. So error is expected.
|
|
|
|
@implementation Test6
|
|
|
|
- (id) myMethod
|
|
|
|
{
|
|
|
|
return var; // expected-error {{use of undeclared identifier 'var'}}
|
|
|
|
}
|
|
|
|
@synthesize var = _var;
|
|
|
|
@end
|
|
|
|
|
2011-09-28 07:39:40 +08:00
|
|
|
int* _object;
|
2011-09-01 07:32:48 +08:00
|
|
|
|
2011-09-28 07:39:40 +08:00
|
|
|
@interface Test7
|
2011-09-01 07:32:48 +08:00
|
|
|
@property (readwrite, assign) id object;
|
|
|
|
@end
|
|
|
|
|
2011-09-28 07:39:40 +08:00
|
|
|
// With default synthesis, '_object' is be the synthesized ivar not the global
|
2011-09-01 07:32:48 +08:00
|
|
|
// 'int*' object. So no error.
|
|
|
|
@implementation Test7
|
|
|
|
- (id) myMethod {
|
2011-09-28 07:39:40 +08:00
|
|
|
return _object;
|
2011-09-01 07:32:48 +08:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2012-06-21 01:18:31 +08:00
|
|
|
// rdar://11671080
|
|
|
|
@interface Test8
|
|
|
|
{
|
|
|
|
id _y;
|
2012-09-25 06:00:36 +08:00
|
|
|
id y; // expected-note {{instance variable is declared here}}
|
2012-06-21 01:18:31 +08:00
|
|
|
}
|
|
|
|
@property(copy) id y; // expected-note {{property declared here}}
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2012-06-21 07:33:16 +08:00
|
|
|
@implementation Test8 @end // expected-warning {{autosynthesized property 'y' will use instance variable '_y', not existing instance variable 'y'}}
|
2012-06-21 01:18:31 +08:00
|
|
|
|