default-synthesize-2.m
2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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
// 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 {
_name = _rank = _serialNumber = 0;
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
{
id uid; // expected-note {{instance variable is declared here}}
}
@property (readwrite, assign) id uid; // expected-note {{property declared here}}
@end
// rdar://11671080
@implementation Test3 // expected-warning {{autosynthesized property 'uid' will use synthesized instance variable '_uid', not existing instance variable 'uid'}}
// Oops, forgot to write @synthesize! will be default synthesized
- (void) myMethod {
self.uid = 0; // Use of the “setter”
uid = 0; // Use of the wrong instance variable
_uid = 0; // Use of the property instance variable
}
@end
@interface Test4 {
id _var;
}
@property (readwrite, assign) id var;
@end
// default synthesize property named 'var'
@implementation Test4
- (id) myMethod {
return self->_var; // compiles because 'var' is synthesized by default
}
@end
@interface Test5
{
id _var;
}
@property (readwrite, assign) id var;
@end
// default synthesis of property 'var'
@implementation Test5
- (id) myMethod {
Test5 *foo = 0;
return foo->_var; // OK
}
@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
int* _object;
@interface Test7
@property (readwrite, assign) id object;
@end
// With default synthesis, '_object' is be the synthesized ivar not the global
// 'int*' object. So no error.
@implementation Test7
- (id) myMethod {
return _object;
}
@end
// rdar://11671080
@interface Test8
{
id _y;
id y; // expected-note {{instance variable is declared here}}
}
@property(copy) id y; // expected-note {{property declared here}}
@end
@implementation Test8 @end // expected-warning {{autosynthesized property 'y' will use instance variable '_y', not existing instance variable 'y'}}