Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wplacement-new-size-8.C

raw
1 /* Verify informational notes following the warning.
2 { dg-do compile }
3 { dg-options "-Wall" } */
4
5 #define DISS_MAX __PTRDIFF_MAX__
6 #define SIZE_MAX __SIZE_MAX__
7
8 typedef __SIZE_TYPE__ size_t;
9
10 template <int N> struct S { char a[N]; };
11
12 void* operator new (size_t, void *p) { return p; }
13 void* operator new[] (size_t, void *p) { return p; }
14
15
16 void test_cst_off ()
17 {
18 {
19 char ca0[0]; // { dg-message "'ca0' declared here" "note" }
20 new (ca0 + 0) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[0]' and size '0'" }
21 }
22 {
23 char ca1[1];
24 new (ca1 + 0) S<1>;
25 }
26 {
27 char ca1[1]; // { dg-message "'ca1' declared here" "note" }
28 new (ca1 + 0) S<2>; // { dg-warning "constructing an object of type 'S<2>' and size '2' in a region of type 'char \\\[1]' and size '1'" }
29 }
30 {
31 char ca1[1]; // { dg-message "at offset 1 from 'ca1' declared here" "note" }
32 new (ca1 + 1) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[1]' and size '0'" }
33 }
34 {
35 char ca1[1]; // { dg-message "at offset 2 from 'ca1' declared here" "note" }
36 new (ca1 + 2) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[1]' and size '0'" }
37 }
38 {
39 char ca1[1]; // { dg-message "at offset -1 from 'ca1' declared here" "note" }
40 new (ca1 - 1) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[1]' and size '0'" }
41 }
42 {
43 /* Offsets are treated as signed so SIZE_MAX is indistinguishable
44 from -1. */
45 char ca1[1]; // { dg-message "at offset \\d+ from 'ca1' declared here" "note" { xfail *-*-* } }
46 // { dg-message "at offset -1 from 'ca1' declared here" "note second variant" { target *-*-* } .-1 }
47 new (ca1 + SIZE_MAX) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[1]' and size '0'" }
48 }
49 }
50
51
52 /* Verify that the range of the offset included in the note corresponds
53 to the range of its type (plus the optional constant). */
54
55 void test_var_off_uchar (unsigned char i)
56 {
57 {
58 // Verify that the nore doesn't mention an offset.
59 char ca0[0]; // { dg-message ": 'ca0' declared here" "note" }
60 new (ca0 + i) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[0]' and size '0'" }
61 }
62 {
63 char ca1[1];
64 new (ca1 + i) S<1>;
65 }
66 {
67 // Verify that the nore doesn't mention an offset.
68 char ca1[1]; // { dg-message ": 'ca1' declared here" "note" }
69 new (ca1 + i) S<2>; // { dg-warning "constructing an object of type 'S<2>' and size '2' in a region of type 'char \\\[1]' and size at most '1'" }
70 }
71 {
72 char ca2[2];
73 new (ca2 + i) S<2>;
74 new (ca2 + 1 - i) S<2>;
75 new (ca2 - i + 1) S<2>;
76 new (ca2 - 2 + i) S<2>;
77 new (ca2 - i + 2) S<2>;
78 new (ca2 - i + i) S<2>;
79 new (ca2 + i + i) S<2>;
80 }
81 {
82 char ca2[2]; // { dg-message "at offset \\\[1, 2] from 'ca2' declared here" "note" }
83 new (ca2 + i + 1) S<2>; // { dg-warning "constructing an object of type 'S<2>' and size '2' in a region of type 'char \\\[2]' and size at most '1'" }
84 }
85
86 {
87 char a[65281];
88 new (a + i + 65280) S<1>;
89 }
90 {
91 char a[65281]; // { dg-message "at offset 65281 from 'a' declared here" "note" }
92 new (a + i + 65281) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[65281]' and size '0'" }
93 }
94 {
95 char a[65281]; // { dg-message "at offset \\\[65154, 65281] from 'a' declared here" "note" }
96 new (a + i + 65154) S<128>; // { dg-warning "constructing an object of type 'S<128>' and size '128' in a region of type 'char \\\[65281]' and size at most '127'" }
97 }
98 }
99
100
101 /* Same as above but also verify that the signedness of the offset is
102 considered in the issuing the warning. */
103
104 void test_var_off_schar (signed char i)
105 {
106 {
107 // Verify that the nore doesn't mention an offset.
108 char ca0[0]; // { dg-message ": 'ca0' declared here" "note" }
109 new (ca0 + i) S<1>; // { dg-warning "constructing an object of type 'S<1>' and size '1' in a region of type 'char \\\[0]' and size '0'" }
110 }
111 {
112 char ca1[1];
113 new (ca1 + i) S<1>;
114 new (ca1 - i) S<1>;
115 new (ca1 + i + 1) S<1>;
116 new (ca1 - i + 1) S<1>;
117 new (ca1 + i + i) S<1>;
118 new (ca1 - i - i) S<1>;
119 }
120 {
121 // Verify that the nore doesn't mention an offset.
122 char ca1[1]; // { dg-message ": 'ca1' declared here" "note" }
123 new (ca1 + i) S<2>; // { dg-warning "constructing an object of type 'S<2>' and size '2' in a region of type 'char \\\[1]' and size at most '1'" }
124 }
125 {
126 char ca2[2];
127 new (ca2 + i) S<2>;
128 new (ca2 + 1 - i) S<2>;
129 new (ca2 - i + 1) S<2>;
130 new (ca2 - 2 + i) S<2>;
131 new (ca2 - i + 2) S<2>;
132 new (ca2 - i + i) S<2>;
133 new (ca2 + i + i) S<2>;
134 }
135 {
136 char ca2[2];
137 new (ca2 + i + 1) S<2>;
138 }
139
140 {
141 char a[65281]; // { dg-message "at offset \\\[65153, 65281] from 'a'" "note" }
142 new (a + i + 65280) S<1>;
143 new (a + i + 65281) S<1>;
144 new (a + i + 65281) S<128>;
145 new (a + i + 65281) S<129>; // { dg-warning "constructing an object of type 'S<129>' and size '129' in a region of type 'char \\\[65281]' and size at most '128'" }
146 }
147 }