Debian Codegraph @

clang/test/SemaObjCXX/noescape.mm

raw
1 // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -std=c++11 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -fblocks -std=c++1z %s
3
4 typedef void (^BlockTy)();
5
6 struct S {
7 int i;
8 void m();
9 };
10
11 void escapingFunc0(BlockTy);
12 void noescapeFunc0(id, __attribute__((noescape)) BlockTy);
13 void noescapeFunc1(id, [[clang::noescape]] BlockTy);
14 void noescapeFunc2(__attribute__((noescape)) int *); // expected-note {{previous declaration is here}}
15 void noescapeFunc3(__attribute__((noescape)) id);
16 void noescapeFunc4(__attribute__((noescape)) int &);
17 void noescapeFunc2(int *); // expected-error {{conflicting types for 'noescapeFunc2'}}
18
19 template <class T>
20 void noescapeFunc5(__attribute__((noescape)) T); // expected-warning {{'noescape' attribute only applies to pointer arguments}}
21 template <class T>
22 void noescapeFunc6(__attribute__((noescape)) const T &);
23 template <class T>
24 void noescapeFunc7(__attribute__((noescape)) T &&);
25
26 void invalidFunc0(int __attribute__((noescape))); // expected-warning {{'noescape' attribute only applies to pointer arguments}}
27 void invalidFunc1(int __attribute__((noescape(0)))); // expected-error {{'noescape' attribute takes no arguments}}
28 void invalidFunc2(int0 *__attribute__((noescape))); // expected-error {{use of undeclared identifier 'int0'; did you mean 'int'?}}
29 void invalidFunc3(__attribute__((noescape)) int (S::*Ty)); // expected-warning {{'noescape' attribute only applies to pointer arguments}}
30 void invalidFunc4(__attribute__((noescape)) void (S::*Ty)()); // expected-warning {{'noescape' attribute only applies to pointer arguments}}
31 int __attribute__((noescape)) g; // expected-warning {{'noescape' attribute only applies to parameters}}
32
33 struct S1 {
34 virtual void m0(int *__attribute__((noescape))); // expected-note {{parameter of overridden method is annotated with __attribute__((noescape))}}
35 };
36
37 struct S2 : S1 {
38 void m0(int *__attribute__((noescape))) override;
39 };
40
41 struct S3 : S1 {
42 void m0(int *) override; // expected-warning {{parameter of overriding method should be annotated with __attribute__((noescape))}}
43 };
44
45 __attribute__((objc_root_class))
46 @interface C0
47 -(void) m0:(int*)__attribute__((noescape)) p; // expected-note {{parameter of overridden method is annotated with __attribute__((noescape))}}
48 - (void)noescapeLValRefParam:(const BlockTy &)__attribute__((noescape))p;
49 - (void)noescapeRValRefParam:(BlockTy &&)__attribute__((noescape))p;
50 @end
51
52 @implementation C0
53 -(void) m0:(int*)__attribute__((noescape)) p {}
54 - (void)noescapeLValRefParam:(const BlockTy &)__attribute__((noescape))p {
55 }
56 - (void)noescapeRValRefParam:(BlockTy &&)__attribute__((noescape))p {
57 }
58 @end
59
60 @interface C1 : C0
61 -(void) m0:(int*)__attribute__((noescape)) p;
62 @end
63
64 @implementation C1 : C0
65 -(void) m0:(int*)__attribute__((noescape)) p {}
66 @end
67
68 @interface C2 : C0
69 -(void) m0:(int*) p; // expected-warning {{parameter of overriding method should be annotated with __attribute__((noescape))}}
70 @end
71
72 @implementation C2 : C0
73 -(void) m0:(int*) p {}
74 @end
75
76 void func0(int *);
77 void (*fnptr0)(int *);
78 void (*fnptr1)(__attribute__((noescape)) int *);
79 template<void (*fn)(int*)> struct S4 {};
80 template<void (*fn)(int* __attribute__((noescape)))> struct S5 {};
81
82 #if __cplusplus < 201406
83 // expected-note@-4 {{template parameter is declared here}}
84 // expected-note@-4 {{template parameter is declared here}}
85 #endif
86
87 void test0() {
88 fnptr0 = &func0;
89 fnptr0 = &noescapeFunc2;
90 fnptr1 = &func0; // expected-error {{incompatible function pointer types assigning to 'void (*)(__attribute__((noescape)) int *)' from 'void (*)(int *)'}}
91 fnptr1 = &noescapeFunc2;
92 S4<&func0> e0;
93 S4<&noescapeFunc2> e1;
94 S5<&func0> ne0;
95
96 #if __cplusplus < 201406
97 // expected-error@-4 {{non-type template argument of type 'void (*)(__attribute__((noescape)) int *)' cannot be converted to a value of type 'void (*)(int *)'}}
98 // expected-error@-4 {{non-type template argument of type 'void (*)(int *)' cannot be converted to a value of type 'void (*)(__attribute__((noescape)) int *)'}}
99 #else
100 // expected-error@-6 {{value of type 'void (*)(int *)' is not implicitly convertible to 'void (*)(__attribute__((noescape)) int *)'}}
101 #endif
102
103 S5<&noescapeFunc2> ne1;
104 }
105
106 @protocol NoescapeProt
107 -(void) m0:(int*)__attribute__((noescape)) p; // expected-note 2 {{parameter of overridden method is annotated with __attribute__((noescape))}}
108 +(void) m1:(int*)__attribute__((noescape)) p;
109 -(void) m1:(int*) p;
110 @end
111
112 __attribute__((objc_root_class))
113 @interface C3
114 -(void) m0:(int*) p;
115 +(void) m1:(int*)__attribute__((noescape)) p;
116 -(void) m1:(int*) p;
117 @end
118
119 @interface C3 () <NoescapeProt> // expected-note {{class extension conforms to protocol 'NoescapeProt' which defines method 'm0:'}}
120 @end
121
122 @implementation C3
123 -(void) m0:(int*) p { // expected-warning {{parameter of overriding method should be annotated with __attribute__((noescape))}}
124 }
125 +(void) m1:(int*)__attribute__((noescape)) p {
126 }
127 -(void) m1:(int*) p {
128 }
129 @end
130
131 __attribute__((objc_root_class))
132 @interface C4 <NoescapeProt>
133 -(void) m0:(int*) p; // expected-warning {{parameter of overriding method should be annotated with __attribute__((noescape))}}
134 @end
135
136 @implementation C4
137 -(void) m0:(int*) p {
138 }
139 +(void) m1:(int*)__attribute__((noescape)) p {
140 }
141 -(void) m1:(int*) p {
142 }
143 @end
144
145 struct S6 {
146 S6();
147 S6(const S6 &) = delete; // expected-note 12 {{'S6' has been explicitly marked deleted here}}
148 int f;
149 };
150
151 void test1(C0 *c0) {
152 id a;
153 // __block variables that are not captured by escaping blocks don't
154 // necessitate having accessible copy constructors.
155 __block S6 b0;
156 __block S6 b1; // expected-error {{call to deleted constructor of 'S6'}}
157 __block S6 b2; // expected-error {{call to deleted constructor of 'S6'}}
158 __block S6 b3; // expected-error {{call to deleted constructor of 'S6'}}
159 __block S6 b4; // expected-error {{call to deleted constructor of 'S6'}}
160 __block S6 b5; // expected-error {{call to deleted constructor of 'S6'}}
161 __block S6 b6; // expected-error {{call to deleted constructor of 'S6'}}
162 __block S6 b7; // expected-error {{call to deleted constructor of 'S6'}}
163 __block S6 b8; // expected-error {{call to deleted constructor of 'S6'}}
164 __block S6 b9; // expected-error {{call to deleted constructor of 'S6'}}
165 __block S6 b10; // expected-error {{call to deleted constructor of 'S6'}}
166 __block S6 b11; // expected-error {{call to deleted constructor of 'S6'}}
167 __block S6 b12;
168 __block S6 b13;
169 __block S6 b14; // expected-error {{call to deleted constructor of 'S6'}}
170
171 noescapeFunc0(a, ^{ (void)b0; });
172 escapingFunc0(^{ (void)b1; });
173 {
174 noescapeFunc0(a, ^{ (void)b0; (void)b1; });
175 }
176 noescapeFunc0(a, ^{ escapingFunc0(^{ (void)b2; }); });
177 escapingFunc0(^{ noescapeFunc0(a, ^{ (void)b3; }); });
178
179 [c0 noescapeLValRefParam:^{
180 (void)b4;
181 }];
182
183 [c0 noescapeRValRefParam:^{
184 (void)b5;
185 }];
186
187 void noescape_id(__attribute__((noescape)) id);
188 noescape_id(^{
189 (void)b6;
190 });
191
192 void noescapeLValRefParam(__attribute__((noescape)) const BlockTy &);
193 noescapeLValRefParam(^{
194 (void)b7;
195 });
196
197 void noescapeRValRefParam(__attribute__((noescape)) BlockTy &&);
198 noescapeRValRefParam(^{
199 (void)b8;
200 });
201
202 // FIXME: clang shouldn't reject this.
203 noescapeFunc5(^{
204 (void)b9;
205 });
206
207 noescapeFunc6(^{
208 (void)b10;
209 });
210
211 noescapeFunc7(^{
212 (void)b11;
213 });
214
215 struct NoescapeCtor {
216 NoescapeCtor(__attribute__((noescape)) void (^)());
217 };
218 struct EscapeCtor {
219 EscapeCtor(void (^)());
220 };
221
222 void helper1(NoescapeCtor a);
223 helper1(^{
224 (void)b12;
225 });
226
227 void helper2(NoescapeCtor && a);
228 helper2(^{
229 (void)b13;
230 });
231
232 void helper3(__attribute__((noescape)) EscapeCtor && a);
233 helper3(^{
234 (void)b14;
235 });
236 }