1 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++98
2 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++11
3 // RUN: %clang_cc1 -fsyntax-only -verify %s -triple=i686-pc-linux-gnu -Wno-new-returns-null -std=c++14
4 // RUN: %clang_cc1 -fsyntax-only -verify=expected,cxx17 %s -triple=i686-pc-linux-gnu -Wno-new-returns-null %std_cxx17-
6 // FIXME Location is (frontend)
7 // cxx17-note@*:* {{candidate function not viable: requires 2 arguments, but 3 were provided}}
11 #if __cplusplus >= 201103L
12 // expected-note@+2 {{candidate constructor (the implicit move constructor) not viable}}
14 struct S
// expected-note {{candidate}}
16 S(int, int, double); // expected-note {{candidate}}
17 S(double, int); // expected-note 2 {{candidate}}
18 S(float, int); // expected-note 2 {{candidate}}
20 struct T
; // expected-note{{forward declaration of 'T'}}
23 // A special new, to verify that the global version isn't used.
24 void* operator new(size_t, S
*); // expected-note {{candidate}}
30 inline void operator delete(void *); // expected-warning {{replacement function 'operator delete' cannot be declared 'inline'}}
33 inline void *operator new(size_t) { // no warning, due to __attribute__((used))
34 return 0; // expected-warning {{null returned from function that requires a non-null return value}}
38 void* operator new(const size_t); // expected-note {{candidate}}
39 void* operator new(size_t, int*); // expected-note 2{{candidate}}
40 void* operator new(size_t, float*); // expected-note 2{{candidate}}
41 void* operator new(size_t, S
); // expected-note {{candidate}}
48 float *pf
= new (pi
) float();
51 const int *pci
= new const int();
52 S
*ps
= new S(1, 2, 3.4);
53 ps
= new (pf
) (S
)(1, 2, 3.4);
54 S
*(*paps
)[2] = new S
*[*pi
][2];
56 ia4
*pai
= new (int[3][4]);
61 pi
= new (S(1.0f
, 2)) int;
73 #if __cplusplus >= 201103L
75 (void)new int[]{1, 2, 3};
76 (void)new char[]{"hello"};
81 virtual ~abstract() = 0;
84 void bad_news(int *ip
)
86 int i
= 1; // expected-note 2{{here}}
87 (void)new; // expected-error {{expected a type}}
88 (void)new 4; // expected-error {{expected a type}}
89 (void)new () int; // expected-error {{expected expression}}
91 #if __cplusplus <= 199711L
92 // expected-error@-2 {{array size expression must have integral or enumeration type, not 'double'}}
93 #elif __cplusplus <= 201103L
94 // expected-error@-4 {{array size expression must have integral or unscoped enumeration type, not 'double'}}
96 // expected-warning@-6 {{implicit conversion from 'double' to 'unsigned int' changes value from 1.1 to 1}}
99 (void)new int[1][i
]; // expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
100 (void)new (int[1][i
]); // expected-note {{read of non-const variable 'i' is not allowed in a constant expression}}
101 #if __cplusplus <= 201103L
102 // expected-error@-3 {{only the first dimension}}
103 // expected-error@-3 {{only the first dimension}}
105 // expected-error@-6 {{array size is not a constant expression}}
106 // expected-error@-6 {{array size is not a constant expression}}
108 (void)new (int[i
]); // expected-warning {{when type is in parentheses}}
109 (void)new int(*(S
*)0); // expected-error {{no viable conversion from 'S' to 'int'}}
110 (void)new int(1, 2); // expected-error {{excess elements in scalar initializer}}
111 (void)new S(1); // expected-error {{no matching constructor}}
112 (void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
113 (void)new const int; // expected-error {{default initialization of an object of const type 'const int'}}
114 (void)new float*(ip
); // expected-error {{cannot initialize a new value of type 'float *' with an lvalue of type 'int *'}}
115 // Undefined, but clang should reject it directly.
117 #if __cplusplus <= 201103L
118 // expected-error@-2 {{array size is negative}}
120 // expected-error@-4 {{array is too large}}
122 (void)new int[2000000000]; // expected-error {{array is too large}}
123 (void)new int[*(S
*)0];
124 #if __cplusplus <= 199711L
125 // expected-error@-2 {{array size expression must have integral or enumeration type, not 'S'}}
126 #elif __cplusplus <= 201103L
127 // expected-error@-4 {{array size expression must have integral or unscoped enumeration type, not 'S'}}
129 // expected-error@-6 {{converting 'S' to incompatible type}}
132 (void)::S::new int; // expected-error {{expected unqualified-id}}
133 (void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
134 (void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
135 // This must fail, because the member version shouldn't be found.
136 (void)::new ((S
*)0) U
; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
137 // This must fail, because any member version hides all global versions.
138 (void)new U
; // expected-error {{no matching function for call to 'operator new'}}
139 (void)new (int[]); // expected-error {{array size must be specified in new expression with no initializer}}
140 (void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
141 (void)new int[]; // expected-error {{array size must be specified in new expression with no initializer}}
142 (void)new int[](); // expected-error {{cannot determine allocated array size from initializer}}
143 // FIXME: This is a terrible diagnostic.
144 #if __cplusplus < 201103L
145 (void)new int[]{}; // expected-error {{array size must be specified in new expression with no initializer}}
149 void no_matching_placement_new() {
151 __attribute__((aligned(__alignof(X
)))) unsigned char buffer
[sizeof(X
)];
152 (void)new(buffer
) X
; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
153 (void)new(+buffer
) X
; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
154 (void)new(&buffer
) X
; // expected-error {{no matching 'operator new' function for non-allocating placement new expression; include <new>}}
167 delete 0; // expected-error {{cannot delete expression of type 'int'}}
169 #if __cplusplus <= 199711L
170 // expected-error@-2 {{expected expression}}
172 // expected-error@-4 {{expected variable name or 'this' in lambda capture list}}
174 delete (void*)0; // expected-warning {{cannot delete expression with pointer-to-'void' type 'void *'}}
175 delete (T
*)0; // expected-warning {{deleting pointer to incomplete type}}
176 ::S::delete (int*)0; // expected-error {{expected unqualified-id}}
187 operator int*(); // expected-note {{conversion}}
188 operator float*(); // expected-note {{conversion}}
191 void test_delete_conv(X0 x0
, X1 x1
, X2 x2
) {
192 delete x0
; // expected-error{{cannot delete}}
194 delete x2
; // expected-error{{ambiguous conversion of delete expression of type 'X2' to a pointer}}
200 static void operator delete(void * mem
, size_t size
);
205 static void release(X3
*x
);
206 static void operator delete(void * mem
, size_t size
);
210 void X4::release(X3
*x
) {
216 void Destroy() const { delete this; }
221 static void *operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}}
222 static int operator new[] (size_t) throw(); // expected-error {{operator new[]' must return type 'void *'}}
226 class Comp
: public Tier
{};
228 class Thai
: public Base
{
230 Thai(const Tier
*adoptDictionary
);
233 void loadEngineFor() {
238 template <class T
> struct TBase
{
239 void* operator new(T size
, int); // expected-error {{'operator new' cannot take a dependent type as first parameter; use size_t}}
246 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
251 static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
254 class X8
: public X6
, public X7
{
258 delete x8
; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
263 static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
264 static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
268 delete x9
; // expected-error {{no suitable member 'operator delete' in 'X9'}}
273 #if __cplusplus >= 201103L
274 // expected-note@-2 {{overridden virtual function is here}}
279 #if __cplusplus <= 199711L
280 // expected-error@-2 {{no suitable member 'operator delete' in 'X11'}}
282 // expected-error@-4 {{deleted function '~X11' cannot override a non-deleted function}}
283 // expected-note@-5 2 {{virtual destructor requires an unambiguous, accessible 'operator delete'}}
285 void operator delete(void*, int);
286 #if __cplusplus <= 199711L
287 // expected-note@-2 {{'operator delete' declared here}}
293 #if __cplusplus <= 199711L
294 // expected-note@-2 {{implicit destructor for 'X11' first required here}}
296 // expected-error@-4 {{attempt to use a deleted function}}
301 void* operator new(size_t, void*);
305 using X12::operator new;
308 static void* f(void* g
)
310 return new (g
) X13();
315 static void operator delete(void*, const size_t);
318 void f(X14
*x14a
, X14
*x14b
) {
324 X15(); // expected-note {{declared private here}}
325 ~X15(); // expected-note {{declared private here}}
329 new X15(); // expected-error {{calling a private constructor}}
330 delete x
; // expected-error {{calling a private destructor}}
333 namespace PR5918
{ // Look for template operator new overloads.
334 struct S
{ template<typename T
> static void* operator new(size_t, T
); };
343 (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
346 (void)new T(1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
351 (void)new T
[1](i
); // expected-error {{array 'new' cannot have initialization arguments}}
356 (void)new T(i
); // expected-error {{array 'new' cannot have initialization arguments}}
358 template void h
<unsigned>(unsigned);
359 template void h
<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int[10]>' requested here}}
363 // Don't diagnose access for overload candidates that aren't selected.
366 void* operator new(size_t);
367 void operator delete(void* p
);
370 void* operator new(size_t, void*); // expected-note {{declared private here}}
371 void operator delete(void*, void*);
374 void* operator new(size_t); // expected-note {{declared private here}}
375 void operator delete(void* p
); // expected-note {{declared private here}}
378 void test(S1
* s1
, S2
* s2
) {
380 delete s2
; // expected-error {{is a private member}}
382 (void)new (0L) S1(); // expected-error {{is a private member}}
383 (void)new S2(); // expected-error {{is a private member}}
387 namespace rdar8018245
{
389 static const int value
= 17;
402 return new (int[T::value
]); // expected-warning{{when type is in parentheses, array cannot have dynamic size}}
405 template int *f
<X0
>();
406 template int *f
<X1
>(); // expected-note{{in instantiation of}}
410 namespace Instantiate
{
411 template<typename T
> struct X
{
422 // cv is ignored in arguments
423 static void operator delete(void *const);
426 // cv is ignored in arguments
427 #if __cplusplus < 202002L
428 static void operator delete(void *volatile);
430 static void operator delete(void *);
435 // Don't crash on template delete operators
436 namespace TemplateDestructors
{
440 void* operator new(const size_t size
);
441 template<class T
> void* operator new(const size_t, const int, T
*);
442 void operator delete(void*, const size_t);
443 template<class T
> void operator delete(void*, const size_t, const int, T
*);
447 namespace DeleteParam
{
449 void operator delete(X
*); // expected-error{{first parameter of 'operator delete' must have type 'void *'}}
453 void operator delete(void* const);
457 // Test that the correct 'operator delete' is selected to pair with
458 // the unexpected placement 'operator new'.
459 namespace PairedDelete
{
460 template <class T
> struct A
{
462 void *operator new(size_t s
, double d
= 0);
463 void operator delete(void *p
, double d
);
464 void operator delete(void *p
) {
476 new DoesNotExist
; // expected-error {{unknown type name 'DoesNotExist'}}
480 namespace ArrayNewNeedsDtor
{
481 struct A
{ A(); private: ~A(); };
482 #if __cplusplus <= 199711L
483 // expected-note@-2 {{declared private here}}
485 struct B
{ B(); A a
; };
486 #if __cplusplus <= 199711L
487 // expected-error@-2 {{field of type 'A' has private destructor}}
489 // expected-note@-4 {{destructor of 'B' is implicitly deleted because field 'a' has an inaccessible destructor}}
494 #if __cplusplus <= 199711L
495 // expected-note@-2 {{implicit destructor for 'ArrayNewNeedsDtor::B' first required here}}
497 // expected-error@-4 {{attempt to use a deleted function}}
502 namespace DeleteIncompleteClass
{
503 struct A
; // expected-note {{forward declaration}}
505 void f() { delete x
; } // expected-error {{deleting incomplete class type}}
508 namespace DeleteIncompleteClassPointerError
{
509 struct A
; // expected-note {{forward declaration}}
510 void f(A
*x
) { 1+delete x
; } // expected-warning {{deleting pointer to incomplete type}} \
511 // expected-error {{invalid operands to binary expression}}
516 virtual void foo() = 0;
518 void f(A
*x
) { delete x
; } // expected-warning {{delete called on 'PR10504::A' that is abstract but has non-virtual destructor}}
521 struct PlacementArg
{};
522 inline void *operator new[](size_t, const PlacementArg
&) throw () {
525 inline void operator delete[](void *, const PlacementArg
&) throw () {
530 template <typename X
>
533 S() { new Inner
[1]; }
541 new (*(PlacementArg
*)0) T
[1]; // expected-warning 2 {{binding dereferenced null pointer to reference has undefined behavior}}
545 tfn
<int>(); // expected-note {{in instantiation of function template specialization 'r150682::tfn<int>' requested here}}
554 CopyCounter(const CopyCounter
&);
559 CopyCounter
* f
= new CopyCounter
[10](CopyCounter()); // expected-error {{cannot have initialization arguments}}
565 template <class C
> struct scoped_array
{
566 scoped_array(C
* p
= __null
);
568 template <class Payload
> struct Foo
{
569 Foo() : a_(new scoped_array
<int>[5]) { }
570 scoped_array
< scoped_array
<int> > a_
;
575 template <class C
> struct scoped_array2
{
576 scoped_array2(C
* p
= __null
, C
* q
= __null
);
578 template <class Payload
> struct Foo2
{
579 Foo2() : a_(new scoped_array2
<int>[5]) { }
580 scoped_array2
< scoped_array2
<int> > a_
;
587 explicit MessageLoop(int type
= 0);
589 template <class CookieStoreTestTraits
>
590 class CookieStoreTest
{
596 struct CookieMonsterTestTraits
{
598 class DeferredCookieTaskTest
: public CookieStoreTest
<CookieMonsterTestTraits
>
600 DeferredCookieTaskTest() {}
604 class DeletingPlaceholder
{
606 delete f
; // expected-error {{reference to non-static member function must be called; did you mean to call it with no arguments?}}
610 delete g
; // expected-error {{reference to non-static member function must be called}}
616 inline void *operator new(size_t); // expected-error {{'operator new' cannot be declared inside a namespace}}
620 inline void* operator new(); // expected-error {{'operator new' must have at least one parameter}}
625 void f() { this->::new; } // expected-error {{expected unqualified-id}}
626 void g() { this->::delete; } // expected-error {{expected unqualified-id}}
630 #if __cplusplus >= 201103L
631 template<typename
...T
> int *dependent_array_size(T
...v
) {
632 return new int[]{v
...}; // expected-error {{cannot initialize}}
634 int *p0
= dependent_array_size();
635 int *p3
= dependent_array_size(1, 2, 3);
636 int *fail
= dependent_array_size("hello"); // expected-note {{instantiation of}}
639 // FIXME: Our behavior here is incredibly inconsistent. GCC allows
640 // constant-folding in array bounds in new-expressions.
641 int (*const_fold
)[12] = new int[3][&const_fold
+ 12 - &const_fold
];
642 #if __cplusplus >= 201402L
643 // expected-error@-2 {{array size is not a constant expression}}
644 // expected-note@-3 {{cannot refer to element 12 of non-array}}
645 #elif __cplusplus < 201103L
646 // expected-error@-5 {{cannot allocate object of variably modified type}}
647 // expected-warning@-6 {{variable length arrays in C++ are a Clang extension}}