1 | // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null |
2 | // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++98 |
3 | // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++11 |
4 | |
5 | #include <stddef.h> |
6 | |
7 | #if __cplusplus >= 201103L |
8 | // expected-note@+2 {{candidate constructor (the implicit move constructor) not viable}} |
9 | #endif |
10 | struct S // expected-note {{candidate}} |
11 | { |
12 | S(int, int, double); // expected-note {{candidate}} |
13 | S(double, int); // expected-note 2 {{candidate}} |
14 | S(float, int); // expected-note 2 {{candidate}} |
15 | }; |
16 | struct T; // expected-note{{forward declaration of 'T'}} |
17 | struct U |
18 | { |
19 | // A special new, to verify that the global version isn't used. |
20 | void* operator new(size_t, S*); // expected-note {{candidate}} |
21 | }; |
22 | struct V : U |
23 | { |
24 | }; |
25 | |
26 | inline void operator delete(void *); // expected-warning {{replacement function 'operator delete' cannot be declared 'inline'}} |
27 | |
28 | __attribute__((used)) |
29 | inline void *operator new(size_t) { // no warning, due to __attribute__((used)) |
30 | return 0; |
31 | } |
32 | |
33 | // PR5823 |
34 | void* operator new(const size_t); // expected-note 2 {{candidate}} |
35 | void* operator new(size_t, int*); // expected-note 3 {{candidate}} |
36 | void* operator new(size_t, float*); // expected-note 3 {{candidate}} |
37 | void* operator new(size_t, S); // expected-note 2 {{candidate}} |
38 | |
39 | struct foo { }; |
40 | |
41 | void good_news() |
42 | { |
43 | int *pi = new int; |
44 | float *pf = new (pi) float(); |
45 | pi = new int(1); |
46 | pi = new int('c'); |
47 | const int *pci = new const int(); |
48 | S *ps = new S(1, 2, 3.4); |
49 | ps = new (pf) (S)(1, 2, 3.4); |
50 | S *(*paps)[2] = new S*[*pi][2]; |
51 | typedef int ia4[4]; |
52 | ia4 *pai = new (int[3][4]); |
53 | pi = ::new int; |
54 | U *pu = new (ps) U; |
55 | V *pv = new (ps) V; |
56 | |
57 | pi = new (S(1.0f, 2)) int; |
58 | |
59 | (void)new int[true]; |
60 | |
61 | // PR7147 |
62 | typedef int a[2]; |
63 | foo* f1 = new foo; |
64 | foo* f2 = new foo[2]; |
65 | typedef foo x[2]; |
66 | typedef foo y[2][2]; |
67 | x* f3 = new y; |
68 | } |
69 | |
70 | struct abstract { |
71 | virtual ~abstract() = 0; |
72 | }; |
73 | |
74 | void bad_news(int *ip) |
75 | { |
76 | int i = 1; // expected-note 2{{here}} |
77 | (void)new; // expected-error {{expected a type}} |
78 | (void)new 4; // expected-error {{expected a type}} |
79 | (void)new () int; // expected-error {{expected expression}} |
80 | (void)new int[1.1]; |
81 | #if __cplusplus <= 199711L |
82 | // expected-error@-2 {{array size expression must have integral or enumeration type, not 'double'}} |
83 | #elif __cplusplus <= 201103L |
84 | // expected-error@-4 {{array size expression must have integral or unscoped enumeration type, not 'double'}} |
85 | #else |
86 | // expected-warning@-6 {{implicit conversion from 'double' to 'unsigned int' changes value from 1.1 to 1}} |
87 | #endif |
88 | |
89 | (void)new int[1][i]; // expected-note {{read of non-const variable 'i' is not allowed in a constant expression}} |
90 | (void)new (int[1][i]); // expected-note {{read of non-const variable 'i' is not allowed in a constant expression}} |
91 | #if __cplusplus <= 201103L |
92 | // expected-error@-3 {{only the first dimension}} |
93 | // expected-error@-3 {{only the first dimension}} |
94 | #else |
95 | // expected-error@-6 {{array size is not a constant expression}} |
96 | // expected-error@-6 {{array size is not a constant expression}} |
97 | #endif |
98 | (void)new (int[i]); // expected-warning {{when type is in parentheses}} |
99 | (void)new int(*(S*)0); // expected-error {{no viable conversion from 'S' to 'int'}} |
100 | (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}} |
101 | (void)new S(1); // expected-error {{no matching constructor}} |
102 | (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}} |
103 | (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}} |
104 | (void)new float*(ip); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}} |
105 | // Undefined, but clang should reject it directly. |
106 | (void)new int[-1]; |
107 | #if __cplusplus <= 201103L |
108 | // expected-error@-2 {{array size is negative}} |
109 | #else |
110 | // expected-error@-4 {{array is too large}} |
111 | #endif |
112 | (void)new int[2000000000]; // expected-error {{array is too large}} |
113 | (void)new int[*(S*)0]; |
114 | #if __cplusplus <= 199711L |
115 | // expected-error@-2 {{array size expression must have integral or enumeration type, not 'S'}} |
116 | #elif __cplusplus <= 201103L |
117 | // expected-error@-4 {{array size expression must have integral or unscoped enumeration type, not 'S'}} |
118 | #else |
119 | // expected-error@-6 {{converting 'S' to incompatible type}} |
120 | #endif |
121 | |
122 | (void)::S::new int; // expected-error {{expected unqualified-id}} |
123 | (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}} |
124 | (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}} |
125 | // This must fail, because the member version shouldn't be found. |
126 | (void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}} |
127 | // This must fail, because any member version hides all global versions. |
128 | (void)new U; // expected-error {{no matching function for call to 'operator new'}} |
129 | (void)new (int[]); // expected-error {{array size must be specified in new expressions}} |
130 | (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}} |
131 | // Some lacking cases due to lack of sema support. |
132 | } |
133 | |
134 | void good_deletes() |
135 | { |
136 | delete (int*)0; |
137 | delete [](int*)0; |
138 | delete (S*)0; |
139 | ::delete (int*)0; |
140 | } |
141 | |
142 | void bad_deletes() |
143 | { |
144 | delete 0; // expected-error {{cannot delete expression of type 'int'}} |
145 | delete [0] (int*)0; |
146 | #if __cplusplus <= 199711L |
147 | // expected-error@-2 {{expected expression}} |
148 | #else |
149 | // expected-error@-4 {{expected variable name or 'this' in lambda capture list}} |
150 | #endif |
151 | delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}} |
152 | delete (T*)0; // expected-warning {{deleting pointer to incomplete type}} |
153 | ::S::delete (int*)0; // expected-error {{expected unqualified-id}} |
154 | } |
155 | |
156 | struct X0 { }; |
157 | |
158 | struct X1 { |
159 | operator int*(); |
160 | operator float(); |
161 | }; |
162 | |
163 | struct X2 { |
164 | operator int*(); // expected-note {{conversion}} |
165 | operator float*(); // expected-note {{conversion}} |
166 | }; |
167 | |
168 | void test_delete_conv(X0 x0, X1 x1, X2 x2) { |
169 | delete x0; // expected-error{{cannot delete}} |
170 | delete x1; |
171 | delete x2; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}} |
172 | } |
173 | |
174 | // PR4782 |
175 | class X3 { |
176 | public: |
177 | static void operator delete(void * mem, size_t size); |
178 | }; |
179 | |
180 | class X4 { |
181 | public: |
182 | static void release(X3 *x); |
183 | static void operator delete(void * mem, size_t size); |
184 | }; |
185 | |
186 | |
187 | void X4::release(X3 *x) { |
188 | delete x; |
189 | } |
190 | |
191 | class X5 { |
192 | public: |
193 | void Destroy() const { delete this; } |
194 | }; |
195 | |
196 | class Base { |
197 | public: |
198 | static void *operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}} |
199 | static int operator new[] (size_t) throw(); // expected-error {{operator new[]' must return type 'void *'}} |
200 | }; |
201 | |
202 | class Tier {}; |
203 | class Comp : public Tier {}; |
204 | |
205 | class Thai : public Base { |
206 | public: |
207 | Thai(const Tier *adoptDictionary); |
208 | }; |
209 | |
210 | void loadEngineFor() { |
211 | const Comp *dict; |
212 | new Thai(dict); |
213 | } |
214 | |
215 | template <class T> struct TBase { |
216 | void* operator new(T size, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}} |
217 | }; |
218 | |
219 | TBase<int> t1; |
220 | |
221 | class X6 { |
222 | public: |
223 | static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}} |
224 | }; |
225 | |
226 | class X7 { |
227 | public: |
228 | static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}} |
229 | }; |
230 | |
231 | class X8 : public X6, public X7 { |
232 | }; |
233 | |
234 | void f(X8 *x8) { |
235 | delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}} |
236 | } |
237 | |
238 | class X9 { |
239 | public: |
240 | static void operator delete(void*, int); // expected-note {{'operator delete' declared here}} |
241 | static void operator delete(void*, float); // expected-note {{'operator delete' declared here}} |
242 | }; |
243 | |
244 | void f(X9 *x9) { |
245 | delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}} |
246 | } |
247 | |
248 | struct X10 { |
249 | virtual ~X10(); |
250 | #if __cplusplus >= 201103L |
251 | // expected-note@-2 {{overridden virtual function is here}} |
252 | #endif |
253 | }; |
254 | |
255 | struct X11 : X10 { |
256 | #if __cplusplus <= 199711L |
257 | // expected-error@-2 {{no suitable member 'operator delete' in 'X11'}} |
258 | #else |
259 | // expected-error@-4 {{deleted function '~X11' cannot override a non-deleted function}} |
260 | // expected-note@-5 2 {{virtual destructor requires an unambiguous, accessible 'operator delete'}} |
261 | #endif |
262 | void operator delete(void*, int); |
263 | #if __cplusplus <= 199711L |
264 | // expected-note@-2 {{'operator delete' declared here}} |
265 | #endif |
266 | }; |
267 | |
268 | void f() { |
269 | X11 x11; |
270 | #if __cplusplus <= 199711L |
271 | // expected-note@-2 {{implicit destructor for 'X11' first required here}} |
272 | #else |
273 | // expected-error@-4 {{attempt to use a deleted function}} |
274 | #endif |
275 | } |
276 | |
277 | struct X12 { |
278 | void* operator new(size_t, void*); |
279 | }; |
280 | |
281 | struct X13 : X12 { |
282 | using X12::operator new; |
283 | }; |
284 | |
285 | static void* f(void* g) |
286 | { |
287 | return new (g) X13(); |
288 | } |
289 | |
290 | class X14 { |
291 | public: |
292 | static void operator delete(void*, const size_t); |
293 | }; |
294 | |
295 | void f(X14 *x14a, X14 *x14b) { |
296 | delete x14a; |
297 | } |
298 | |
299 | class X15 { |
300 | private: |
301 | X15(); // expected-note {{declared private here}} |
302 | ~X15(); // expected-note {{declared private here}} |
303 | }; |
304 | |
305 | void f(X15* x) { |
306 | new X15(); // expected-error {{calling a private constructor}} |
307 | delete x; // expected-error {{calling a private destructor}} |
308 | } |
309 | |
310 | namespace PR5918 { // Look for template operator new overloads. |
311 | struct S { template<typename T> static void* operator new(size_t, T); }; |
312 | void test() { |
313 | (void)new(0) S; |
314 | } |
315 | } |
316 | |
317 | namespace Test1 { |
318 | |
319 | void f() { |
320 | (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}} |
321 | |
322 | typedef int T[10]; |
323 | (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}} |
324 | } |
325 | |
326 | template<typename T> |
327 | void g(unsigned i) { |
328 | (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}} |
329 | } |
330 | |
331 | template<typename T> |
332 | void h(unsigned i) { |
333 | (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}} |
334 | } |
335 | template void h<unsigned>(unsigned); |
336 | template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int [10]>' requested here}} |
337 | |
338 | } |
339 | |
340 | // Don't diagnose access for overload candidates that aren't selected. |
341 | namespace PR7436 { |
342 | struct S1 { |
343 | void* operator new(size_t); |
344 | void operator delete(void* p); |
345 | |
346 | private: |
347 | void* operator new(size_t, void*); // expected-note {{declared private here}} |
348 | void operator delete(void*, void*); |
349 | }; |
350 | class S2 { |
351 | void* operator new(size_t); // expected-note {{declared private here}} |
352 | void operator delete(void* p); // expected-note {{declared private here}} |
353 | }; |
354 | |
355 | void test(S1* s1, S2* s2) { |
356 | delete s1; |
357 | delete s2; // expected-error {{is a private member}} |
358 | (void)new S1(); |
359 | (void)new (0L) S1(); // expected-error {{is a private member}} |
360 | (void)new S2(); // expected-error {{is a private member}} |
361 | } |
362 | } |
363 | |
364 | namespace rdar8018245 { |
365 | struct X0 { |
366 | static const int value = 17; |
367 | }; |
368 | |
369 | const int X0::value; |
370 | |
371 | struct X1 { |
372 | static int value; |
373 | }; |
374 | |
375 | int X1::value; |
376 | |
377 | template<typename T> |
378 | int *f() { |
379 | return new (int[T::value]); // expected-warning{{when type is in parentheses, array cannot have dynamic size}} |
380 | } |
381 | |
382 | template int *f<X0>(); |
383 | template int *f<X1>(); // expected-note{{in instantiation of}} |
384 | |
385 | } |
386 | |
387 | // <rdar://problem/8248780> |
388 | namespace Instantiate { |
389 | template<typename T> struct X { |
390 | operator T*(); |
391 | }; |
392 | |
393 | void f(X<int> &xi) { |
394 | delete xi; |
395 | } |
396 | } |
397 | |
398 | namespace PR7810 { |
399 | struct X { |
400 | // cv is ignored in arguments |
401 | static void operator delete(void *const); |
402 | }; |
403 | struct Y { |
404 | // cv is ignored in arguments |
405 | static void operator delete(void *volatile); |
406 | }; |
407 | } |
408 | |
409 | // Don't crash on template delete operators |
410 | namespace TemplateDestructors { |
411 | struct S { |
412 | virtual ~S() {} |
413 | |
414 | void* operator new(const size_t size); |
415 | template<class T> void* operator new(const size_t, const int, T*); |
416 | void operator delete(void*, const size_t); |
417 | template<class T> void operator delete(void*, const size_t, const int, T*); |
418 | }; |
419 | } |
420 | |
421 | namespace DeleteParam { |
422 | struct X { |
423 | void operator delete(X*); // expected-error{{first parameter of 'operator delete' must have type 'void *'}} |
424 | }; |
425 | |
426 | struct Y { |
427 | void operator delete(void* const); |
428 | }; |
429 | } |
430 | |
431 | // <rdar://problem/8427878> |
432 | // Test that the correct 'operator delete' is selected to pair with |
433 | // the unexpected placement 'operator new'. |
434 | namespace PairedDelete { |
435 | template <class T> struct A { |
436 | A(); |
437 | void *operator new(size_t s, double d = 0); |
438 | void operator delete(void *p, double d); |
439 | void operator delete(void *p) { |
440 | T::dealloc(p); |
441 | } |
442 | }; |
443 | |
444 | A<int> *test() { |
445 | return new A<int>(); |
446 | } |
447 | } |
448 | |
449 | namespace PR7702 { |
450 | void test1() { |
451 | new DoesNotExist; // expected-error {{unknown type name 'DoesNotExist'}} |
452 | } |
453 | } |
454 | |
455 | namespace ArrayNewNeedsDtor { |
456 | struct A { A(); private: ~A(); }; |
457 | #if __cplusplus <= 199711L |
458 | // expected-note@-2 {{declared private here}} |
459 | #endif |
460 | struct B { B(); A a; }; |
461 | #if __cplusplus <= 199711L |
462 | // expected-error@-2 {{field of type 'ArrayNewNeedsDtor::A' has private destructor}} |
463 | #else |
464 | // expected-note@-4 {{destructor of 'B' is implicitly deleted because field 'a' has an inaccessible destructor}} |
465 | #endif |
466 | |
467 | B *test9() { |
468 | return new B[5]; |
469 | #if __cplusplus <= 199711L |
470 | // expected-note@-2 {{implicit destructor for 'ArrayNewNeedsDtor::B' first required here}} |
471 | #else |
472 | // expected-error@-4 {{attempt to use a deleted function}} |
473 | #endif |
474 | } |
475 | } |
476 | |
477 | namespace DeleteIncompleteClass { |
478 | struct A; // expected-note {{forward declaration}} |
479 | extern A x; |
480 | void f() { delete x; } // expected-error {{deleting incomplete class type}} |
481 | } |
482 | |
483 | namespace DeleteIncompleteClassPointerError { |
484 | struct A; // expected-note {{forward declaration}} |
485 | void f(A *x) { 1+delete x; } // expected-warning {{deleting pointer to incomplete type}} \ |
486 | // expected-error {{invalid operands to binary expression}} |
487 | } |
488 | |
489 | namespace PR10504 { |
490 | struct A { |
491 | virtual void foo() = 0; |
492 | }; |
493 | void f(A *x) { delete x; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}} |
494 | } |
495 | |
496 | struct PlacementArg {}; |
497 | inline void *operator new[](size_t, const PlacementArg &) throw () { |
498 | return 0; |
499 | } |
500 | inline void operator delete[](void *, const PlacementArg &) throw () { |
501 | } |
502 | |
503 | namespace r150682 { |
504 | |
505 | template <typename X> |
506 | struct S { |
507 | struct Inner {}; |
508 | S() { new Inner[1]; } |
509 | }; |
510 | |
511 | struct T { |
512 | }; |
513 | |
514 | template<typename X> |
515 | void tfn() { |
516 | new (*(PlacementArg*)0) T[1]; // expected-warning 2 {{binding dereferenced null pointer to reference has undefined behavior}} |
517 | } |
518 | |
519 | void fn() { |
520 | tfn<int>(); // expected-note {{in instantiation of function template specialization 'r150682::tfn<int>' requested here}} |
521 | } |
522 | |
523 | } |
524 | |
525 | namespace P12023 { |
526 | struct CopyCounter |
527 | { |
528 | CopyCounter(); |
529 | CopyCounter(const CopyCounter&); |
530 | }; |
531 | |
532 | int main() |
533 | { |
534 | CopyCounter* f = new CopyCounter[10](CopyCounter()); // expected-error {{cannot have initialization arguments}} |
535 | return 0; |
536 | } |
537 | } |
538 | |
539 | namespace PR12061 { |
540 | template <class C> struct scoped_array { |
541 | scoped_array(C* p = __null); |
542 | }; |
543 | template <class Payload> struct Foo { |
544 | Foo() : a_(new scoped_array<int>[5]) { } |
545 | scoped_array< scoped_array<int> > a_; |
546 | }; |
547 | class Bar {}; |
548 | Foo<Bar> x; |
549 | |
550 | template <class C> struct scoped_array2 { |
551 | scoped_array2(C* p = __null, C* q = __null); |
552 | }; |
553 | template <class Payload> struct Foo2 { |
554 | Foo2() : a_(new scoped_array2<int>[5]) { } |
555 | scoped_array2< scoped_array2<int> > a_; |
556 | }; |
557 | class Bar2 {}; |
558 | Foo2<Bar2> x2; |
559 | |
560 | class MessageLoop { |
561 | public: |
562 | explicit MessageLoop(int type = 0); |
563 | }; |
564 | template <class CookieStoreTestTraits> |
565 | class CookieStoreTest { |
566 | protected: |
567 | CookieStoreTest() { |
568 | new MessageLoop; |
569 | } |
570 | }; |
571 | struct CookieMonsterTestTraits { |
572 | }; |
573 | class DeferredCookieTaskTest : public CookieStoreTest<CookieMonsterTestTraits> |
574 | { |
575 | DeferredCookieTaskTest() {} |
576 | }; |
577 | } |
578 | |
579 | class DeletingPlaceholder { |
580 | int* f() { |
581 | delete f; // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}} |
582 | return 0; |
583 | } |
584 | int* g(int, int) { |
585 | delete g; // expected-error {{reference to non-static member function must be called}} |
586 | return 0; |
587 | } |
588 | }; |
589 | |
590 | namespace PR18544 { |
591 | inline void *operator new(size_t); // expected-error {{'operator new' cannot be declared inside a namespace}} |
592 | } |
593 | |
594 | // PR19968 |
595 | inline void* operator new(); // expected-error {{'operator new' must have at least one parameter}} |
596 | |
597 | namespace { |
598 | template <class C> |
599 | struct A { |
600 | void f() { this->::new; } // expected-error {{expected unqualified-id}} |
601 | void g() { this->::delete; } // expected-error {{expected unqualified-id}} |
602 | }; |
603 | } |
604 | |