| 1 |
// PR c++/69662 - -Wplacement-new on allocated one element array members |
| 2 |
// Exercising the more permissive -Wplacement-new=1. The difference |
| 3 |
// between -Wplacement-new=1 is denoted by "no warning at level 1" in |
| 4 |
// the comments below. |
| 5 |
// { dg-do compile } |
| 6 |
// { dg-options "-Wno-pedantic -Wplacement-new=1" } |
| 7 |
|
| 8 |
typedef __typeof__ (sizeof 0) size_t; |
| 9 |
|
| 10 |
void* operator new (size_t, void *p) { return p; } |
| 11 |
void* operator new[] (size_t, void *p) { return p; } |
| 12 |
|
| 13 |
struct Ax { char n, a []; }; |
| 14 |
struct A0 { char n, a [0]; }; |
| 15 |
struct A1 { char n, a [1]; }; |
| 16 |
struct A2 { char n, a [2]; }; |
| 17 |
|
| 18 |
typedef __INT16_TYPE__ Int16; |
| 19 |
typedef __INT32_TYPE__ Int32; |
| 20 |
|
| 21 |
void fAx (Ax *px, Ax &rx) |
| 22 |
{ |
| 23 |
Ax ax; |
| 24 |
new (ax.a) Int32; // { dg-warning "placement" } |
| 25 |
new (px->a) Int32; |
| 26 |
new (rx.a) Int32; |
| 27 |
} |
| 28 |
|
| 29 |
void fAx2 () |
| 30 |
{ |
| 31 |
static Ax ax2 = { 1, { 2, 3 } }; |
| 32 |
|
| 33 |
new (ax2.a) Int16; |
| 34 |
new (ax2.a) Int32; // { dg-warning "placement" } |
| 35 |
} |
| 36 |
|
| 37 |
void fA0 (A0 *p0, A0 &r0) |
| 38 |
{ |
| 39 |
A0 a0; |
| 40 |
new (a0.a) Int32; // { dg-warning "placement" } |
| 41 |
new (p0->a) Int32; |
| 42 |
new (r0.a) Int32; |
| 43 |
} |
| 44 |
|
| 45 |
void fA1 (A1 *p1, A1 &r1) |
| 46 |
{ |
| 47 |
A1 a1; |
| 48 |
new (a1.a) Int32; // { dg-warning "placement" } |
| 49 |
new (p1->a) Int32; // no warning at level 1 |
| 50 |
new (r1.a) Int32; // no warning at level 1 |
| 51 |
} |
| 52 |
|
| 53 |
void fA2 (A2 *p2, A2 &r2) |
| 54 |
{ |
| 55 |
A2 a2; |
| 56 |
new (a2.a) Int32; // { dg-warning "placement" } |
| 57 |
new (p2->a) Int32; // { dg-warning "placement" } |
| 58 |
new (r2.a) Int32; // { dg-warning "placement" } |
| 59 |
} |
| 60 |
|
| 61 |
struct BAx { int i; Ax ax; }; |
| 62 |
struct BA0 { int i; A0 a0; }; |
| 63 |
struct BA1 { int i; A1 a1; }; |
| 64 |
struct BA2 { int i; A2 a2; }; |
| 65 |
|
| 66 |
void fBx (BAx *pbx, BAx &rbx) |
| 67 |
{ |
| 68 |
BAx bax; |
| 69 |
// The uninitialized flexible array takes up the bytes of padding. |
| 70 |
new (bax.ax.a) char; // { dg-warning "placement" "" { target default_packed } } |
| 71 |
new (bax.ax.a) Int16; // { dg-warning "placement" "" { target default_packed } } |
| 72 |
new (bax.ax.a) Int32; // { dg-warning "placement" } |
| 73 |
|
| 74 |
new (pbx->ax.a) char; |
| 75 |
new (rbx.ax.a) char; |
| 76 |
new (pbx->ax.a) Int16; |
| 77 |
new (rbx.ax.a) Int16; |
| 78 |
new (pbx->ax.a) Int32; |
| 79 |
new (rbx.ax.a) Int32; |
| 80 |
new (pbx->ax.a) int[1234]; |
| 81 |
new (rbx.ax.a) int[5678]; |
| 82 |
} |
| 83 |
|
| 84 |
void fBx1 () |
| 85 |
{ |
| 86 |
static BAx bax1 = { 1, /* Ax = */ { 2, /* a[] = */ {} } }; |
| 87 |
|
| 88 |
// The empty flexible array takes up the bytes of padding. |
| 89 |
new (bax1.ax.a) char; // { dg-warning "placement" "" { target default_packed } } |
| 90 |
new (bax1.ax.a) char[2]; // { dg-warning "placement" "" { target default_packed } } |
| 91 |
new (bax1.ax.a) Int16; // { dg-warning "placement" "" { target default_packed } } |
| 92 |
new (bax1.ax.a) char[3]; // { dg-warning "placement" "" { target default_packed } } |
| 93 |
new (bax1.ax.a) char[4]; // { dg-warning "placement" } |
| 94 |
new (bax1.ax.a) Int32; // { dg-warning "placement" } |
| 95 |
} |
| 96 |
|
| 97 |
void fB0 (BA0 *pb0, BA0 &rb0) |
| 98 |
{ |
| 99 |
BA0 ba0; |
| 100 |
new (ba0.a0.a) Int32; // { dg-warning "placement" } |
| 101 |
new (pb0->a0.a) Int32; |
| 102 |
new (rb0.a0.a) Int32; |
| 103 |
} |
| 104 |
|
| 105 |
void fB1 (BA1 *pb1, BA1 &rb1) |
| 106 |
{ |
| 107 |
BA1 ba1; |
| 108 |
new (ba1.a1.a) Int32; // { dg-warning "placement" } |
| 109 |
new (pb1->a1.a) Int32; // no warning at level 1 |
| 110 |
new (rb1.a1.a) Int32; // no warning at level 1 |
| 111 |
} |
| 112 |
|
| 113 |
void fB2 (BA2 *pb2, BA2 &rb2) |
| 114 |
{ |
| 115 |
BA2 ba2; |
| 116 |
new (ba2.a2.a) Int32; // { dg-warning "placement" } |
| 117 |
new (pb2->a2.a) Int32; // { dg-warning "placement" } |
| 118 |
new (rb2.a2.a) Int32; // { dg-warning "placement" } |
| 119 |
} |