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