Debian Codegraph @

clang/test/SemaObjC/default-synthesize-2.m

raw
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
3
4 @interface StopAccessingIvarsDirectlyExample
5 @property(strong) id name, rank, serialNumber;
6 @end
7
8 @implementation StopAccessingIvarsDirectlyExample
9
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 @end
25
26
27 // Test2
28 @interface Test2
29 @property(strong, nonatomic) id object;
30 @end
31
32 // object has user declared setter/getter so it won't be
33 // default synthesized; thus causing user error.
34 @implementation Test2
35 - (id) bar { return object; } // expected-error {{use of undeclared identifier 'object'}}
36 - (void)setObject:(id)newObject {}
37 - (id)object { return 0; }
38 @end
39
40 // Test3
41 @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 @end
47
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
50 - (void) myMethod {
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
54 }
55 @end
56
57 @interface Test4 {
58 id _var;
59 }
60 @property (readwrite, assign) id var;
61 @end
62
63
64 // default synthesize property named 'var'
65 @implementation Test4
66 - (id) myMethod {
67 return self->_var; // compiles because 'var' is synthesized by default
68 }
69 @end
70
71 @interface Test5
72 {
73 id _var;
74 }
75 @property (readwrite, assign) id var;
76 @end
77
78 // default synthesis of property 'var'
79 @implementation Test5
80 - (id) myMethod {
81 Test5 *foo = 0;
82 return foo->_var; // OK
83 }
84 @end
85
86 @interface Test6
87 {
88 id _var; // expected-note {{'_var' declared here}}
89 }
90 @property (readwrite, assign) id var;
91 @end
92
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 @end
101
102 int* _object;
103
104 @interface Test7
105 @property (readwrite, assign) id object;
106 @end
107
108 // With default synthesis, '_object' is be the synthesized ivar not the global
109 // 'int*' object. So no error.
110 @implementation Test7
111 - (id) myMethod {
112 return _object;
113 }
114 @end
115
116 @interface Test8
117 {
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 @end
123
124
125 @implementation Test8 @end // expected-warning {{autosynthesized property 'y' will use instance variable '_y', not existing instance variable 'y'}}
126