2009-03-24 10:24:46 +08:00
|
|
|
// RUN: clang-cc -fsyntax-only -verify %s
|
2008-11-25 12:08:05 +08:00
|
|
|
|
2008-12-04 04:26:15 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
|
2008-11-22 03:14:01 +08:00
|
|
|
struct S // expected-note {{candidate}}
|
|
|
|
{
|
|
|
|
S(int, int, double); // expected-note {{candidate}}
|
2009-02-08 03:52:04 +08:00
|
|
|
S(double, int); // expected-note 2 {{candidate}}
|
|
|
|
S(float, int); // expected-note 2 {{candidate}}
|
2008-11-22 03:14:01 +08:00
|
|
|
};
|
2009-01-20 03:26:10 +08:00
|
|
|
struct T; // expected-note{{forward declaration of 'struct T'}}
|
2008-12-05 01:24:46 +08:00
|
|
|
struct U
|
|
|
|
{
|
|
|
|
// A special new, to verify that the global version isn't used.
|
2009-05-15 02:11:41 +08:00
|
|
|
void* operator new(size_t, S*); // expected-note {{candidate}}
|
2008-12-05 01:24:46 +08:00
|
|
|
};
|
2008-12-05 06:20:51 +08:00
|
|
|
struct V : U
|
|
|
|
{
|
|
|
|
};
|
2008-11-22 03:14:01 +08:00
|
|
|
|
2009-02-08 03:52:04 +08:00
|
|
|
void* operator new(size_t); // expected-note 2 {{candidate}}
|
|
|
|
void* operator new(size_t, int*); // expected-note 3 {{candidate}}
|
|
|
|
void* operator new(size_t, float*); // expected-note 3 {{candidate}}
|
2009-06-01 03:49:47 +08:00
|
|
|
void* operator new(size_t, S); // expected-note 2 {{candidate}}
|
2008-12-04 04:26:15 +08:00
|
|
|
|
2008-11-22 03:14:01 +08:00
|
|
|
void good_news()
|
|
|
|
{
|
|
|
|
int *pi = new int;
|
|
|
|
float *pf = new (pi) float();
|
|
|
|
pi = new int(1);
|
|
|
|
pi = new int('c');
|
|
|
|
const int *pci = new const int();
|
|
|
|
S *ps = new S(1, 2, 3.4);
|
2008-12-02 22:43:59 +08:00
|
|
|
ps = new (pf) (S)(1, 2, 3.4);
|
2008-11-22 03:14:01 +08:00
|
|
|
S *(*paps)[2] = new S*[*pi][2];
|
|
|
|
ps = new (S[3])(1, 2, 3.4);
|
|
|
|
typedef int ia4[4];
|
|
|
|
ia4 *pai = new (int[3][4]);
|
2008-12-03 00:35:44 +08:00
|
|
|
pi = ::new int;
|
2008-12-05 01:24:46 +08:00
|
|
|
U *pu = new (ps) U;
|
2009-09-30 08:03:47 +08:00
|
|
|
V *pv = new (ps) V;
|
2009-06-01 03:49:47 +08:00
|
|
|
|
|
|
|
pi = new (S(1.0f, 2)) int;
|
2009-09-23 08:37:25 +08:00
|
|
|
|
|
|
|
(void)new int[true];
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
|
2009-03-25 03:52:54 +08:00
|
|
|
struct abstract {
|
|
|
|
virtual ~abstract() = 0;
|
|
|
|
};
|
|
|
|
|
2008-11-25 12:08:05 +08:00
|
|
|
void bad_news(int *ip)
|
2008-11-22 03:14:01 +08:00
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
(void)new; // expected-error {{missing type specifier}}
|
|
|
|
(void)new 4; // expected-error {{missing type specifier}}
|
|
|
|
(void)new () int; // expected-error {{expected expression}}
|
2008-12-02 22:43:59 +08:00
|
|
|
(void)new int[1.1]; // expected-error {{array size expression must have integral or enumerated type, not 'double'}}
|
2008-11-22 03:14:01 +08:00
|
|
|
(void)new int[1][i]; // expected-error {{only the first dimension}}
|
|
|
|
(void)new (int[1][i]); // expected-error {{only the first dimension}}
|
2009-10-26 05:45:37 +08:00
|
|
|
(void)new (int[i]); // expected-error {{when type is in parentheses}}
|
2008-11-22 03:14:01 +08:00
|
|
|
(void)new int(*(S*)0); // expected-error {{incompatible type initializing}}
|
|
|
|
(void)new int(1, 2); // expected-error {{initializer of a builtin type can only take one argument}}
|
|
|
|
(void)new S(1); // expected-error {{no matching constructor}}
|
2008-11-25 12:08:05 +08:00
|
|
|
(void)new S(1, 1); // expected-error {{call to constructor of 'S' is ambiguous}}
|
2008-11-22 03:14:01 +08:00
|
|
|
(void)new const int; // expected-error {{must provide an initializer}}
|
2008-11-25 12:08:05 +08:00
|
|
|
(void)new float*(ip); // expected-error {{incompatible type initializing 'int *', expected 'float *'}}
|
2008-12-02 22:43:59 +08:00
|
|
|
// Undefined, but clang should reject it directly.
|
|
|
|
(void)new int[-1]; // expected-error {{array size is negative}}
|
|
|
|
(void)new int[*(S*)0]; // expected-error {{array size expression must have integral or enumerated type, not 'struct S'}}
|
2008-12-03 00:35:44 +08:00
|
|
|
(void)::S::new int; // expected-error {{expected unqualified-id}}
|
2008-12-04 04:26:15 +08:00
|
|
|
(void)new (0, 0) int; // expected-error {{no matching function for call to 'operator new'}}
|
2008-12-05 06:20:51 +08:00
|
|
|
(void)new (0L) int; // expected-error {{call to 'operator new' is ambiguous}}
|
2008-12-05 01:24:46 +08:00
|
|
|
// This must fail, because the member version shouldn't be found.
|
|
|
|
(void)::new ((S*)0) U; // expected-error {{no matching function for call to 'operator new'}}
|
2009-05-15 02:11:41 +08:00
|
|
|
// This must fail, because any member version hides all global versions.
|
|
|
|
(void)new U; // expected-error {{no matching function for call to 'operator new'}}
|
2009-02-10 02:24:27 +08:00
|
|
|
(void)new (int[]); // expected-error {{array size must be specified in new expressions}}
|
|
|
|
(void)new int&; // expected-error {{cannot allocate reference type 'int &' with new}}
|
2008-11-22 03:14:01 +08:00
|
|
|
// Some lacking cases due to lack of sema support.
|
|
|
|
}
|
|
|
|
|
|
|
|
void good_deletes()
|
|
|
|
{
|
|
|
|
delete (int*)0;
|
|
|
|
delete [](int*)0;
|
|
|
|
delete (S*)0;
|
2008-12-03 00:35:44 +08:00
|
|
|
::delete (int*)0;
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void bad_deletes()
|
|
|
|
{
|
|
|
|
delete 0; // expected-error {{cannot delete expression of type 'int'}}
|
|
|
|
delete [0] (int*)0; // expected-error {{expected ']'}} \
|
2008-11-24 07:17:07 +08:00
|
|
|
// expected-note {{to match this '['}}
|
2008-11-22 03:14:01 +08:00
|
|
|
delete (void*)0; // expected-error {{cannot delete expression}}
|
|
|
|
delete (T*)0; // expected-warning {{deleting pointer to incomplete type}}
|
2008-12-03 00:35:44 +08:00
|
|
|
::S::delete (int*)0; // expected-error {{expected unqualified-id}}
|
2008-11-22 03:14:01 +08:00
|
|
|
}
|
2009-09-10 07:39:55 +08:00
|
|
|
|
|
|
|
struct X0 { };
|
|
|
|
|
|
|
|
struct X1 {
|
|
|
|
operator int*();
|
|
|
|
operator float();
|
|
|
|
};
|
|
|
|
|
|
|
|
struct X2 {
|
2009-09-16 01:21:47 +08:00
|
|
|
operator int*(); // expected-note {{candidate function}}
|
|
|
|
operator float*(); // expected-note {{candidate function}}
|
2009-09-10 07:39:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void test_delete_conv(X0 x0, X1 x1, X2 x2) {
|
|
|
|
delete x0; // expected-error{{cannot delete}}
|
|
|
|
delete x1;
|
2009-09-16 01:21:47 +08:00
|
|
|
delete x2; // expected-error{{ambiguous conversion of delete expression of type 'struct X2' to a pointer}}
|
|
|
|
}
|
2009-09-30 02:16:17 +08:00
|
|
|
|
|
|
|
// PR4782
|
|
|
|
class X3 {
|
|
|
|
public:
|
2009-11-16 13:14:40 +08:00
|
|
|
static void operator delete(void * mem, size_t size);
|
2009-09-30 02:16:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class X4 {
|
|
|
|
public:
|
|
|
|
static void release(X3 *x);
|
2009-11-16 13:14:40 +08:00
|
|
|
static void operator delete(void * mem, size_t size);
|
2009-09-30 02:16:17 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void X4::release(X3 *x) {
|
|
|
|
delete x;
|
|
|
|
}
|
2009-09-30 05:38:53 +08:00
|
|
|
|
2009-09-30 08:03:47 +08:00
|
|
|
class X5 {
|
2009-09-30 05:38:53 +08:00
|
|
|
public:
|
|
|
|
void Destroy() const { delete this; }
|
|
|
|
};
|
2009-11-11 07:47:18 +08:00
|
|
|
|
|
|
|
class Base {
|
|
|
|
public:
|
2009-11-12 06:45:41 +08:00
|
|
|
static int operator new(signed char) throw(); // expected-error {{'operator new' takes type size_t}} \
|
2009-11-11 07:47:18 +08:00
|
|
|
// expected-error {{operator new' must return type 'void *'}}
|
2009-11-12 06:45:41 +08:00
|
|
|
static int operator new[] (signed char) throw(); // expected-error {{'operator new[]' takes type size_t}} \
|
2009-11-11 07:47:18 +08:00
|
|
|
// expected-error {{operator new[]' must return type 'void *'}}
|
|
|
|
};
|
|
|
|
|
|
|
|
class Tier {};
|
|
|
|
class Comp : public Tier {};
|
|
|
|
|
|
|
|
class Thai : public Base {
|
|
|
|
public:
|
|
|
|
Thai(const Tier *adoptDictionary);
|
|
|
|
};
|
|
|
|
|
|
|
|
void loadEngineFor() {
|
|
|
|
const Comp *dict;
|
|
|
|
new Thai(dict);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T> struct TBase {
|
|
|
|
void* operator new(T size, int); // expected-error {{'operator new' takes type size_t}}
|
|
|
|
};
|
|
|
|
|
|
|
|
TBase<int> t1; // expected-note {{in instantiation of template class 'struct TBase<int>' requested here}}
|
|
|
|
|
2009-11-14 11:17:38 +08:00
|
|
|
class X6 {
|
|
|
|
public:
|
|
|
|
static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
|
|
|
|
};
|
|
|
|
|
|
|
|
class X7 {
|
|
|
|
public:
|
|
|
|
static void operator delete(void*, int); // expected-note {{member found by ambiguous name lookup}}
|
|
|
|
};
|
|
|
|
|
|
|
|
class X8 : public X6, public X7 {
|
|
|
|
};
|
|
|
|
|
2009-11-16 00:43:15 +08:00
|
|
|
void f(X8 *x8) {
|
2009-11-14 11:17:38 +08:00
|
|
|
delete x8; // expected-error {{member 'operator delete' found in multiple base classes of different types}}
|
|
|
|
}
|
|
|
|
|
2009-11-16 00:43:15 +08:00
|
|
|
class X9 {
|
|
|
|
static void operator delete(void*, int); // expected-note {{'operator delete' declared here}}
|
|
|
|
static void operator delete(void*, float); // expected-note {{'operator delete' declared here}}
|
|
|
|
};
|
|
|
|
|
|
|
|
void f(X9 *x9) {
|
|
|
|
delete x9; // expected-error {{no suitable member 'operator delete' in 'X9'}}
|
|
|
|
}
|