Debian Codegraph @

gcc/testsuite/gcc.dg/Warray-bounds-78.c

raw
1 /* PR tree-optimization/99475 - bogus -Warray-bounds accessing an array
2 element of empty structs
3 { dg-do compile }
4 { dg-options "-O2 -Wall -Wno-strict-aliasing" } */
5
6 #define NOIPA __attribute__ ((noipa))
7
8 struct S { };
9
10 extern struct S sa3[3];
11 extern struct S sa2_3[2][3];
12 extern struct S sa3_4_5[3][4][5];
13
14 void sink (void*);
15
16
17 NOIPA void access_sa3 (void)
18 {
19 ((bool*)sa3)[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
20 ((bool*)sa3)[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
21 ((bool*)sa3)[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
22 ((bool*)sa3)[3] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
23 }
24
25 NOIPA void access_sa3_ptr (void)
26 {
27 bool *p = (bool*)&sa3[0];
28
29 p[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
30 p[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
31 p[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
32 p[3] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
33 }
34
35 NOIPA void access_sa2_3_ptr (void)
36 {
37 bool *p = (bool*)&sa2_3[0][0];
38
39 p[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
40 p[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
41 p[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
42 p[6] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
43 }
44
45 NOIPA void access_sa3_4_5_ptr (struct S s, int i)
46 {
47 bool *p = (bool*)&sa3_4_5[0][0][0];
48
49 p[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
50 p[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
51 p[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
52 p[60] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
53 }
54
55
56 NOIPA void access_vla3 (struct S s, unsigned n)
57 {
58 struct S vla3[3 < n ? 3 : n];
59
60 ((bool*)vla3)[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
61 ((bool*)vla3)[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
62 ((bool*)vla3)[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
63 ((bool*)vla3)[3] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
64
65 sink (vla3);
66 }
67
68 NOIPA void access_vla3_ptr (struct S s, unsigned n)
69 {
70 struct S vla3[3 < n ? 3 : n];
71 bool *p = (bool*)&vla3[0];
72
73 p[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
74 p[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
75 p[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
76 p[3] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
77
78 sink (vla3);
79 }
80
81 NOIPA void access_vla2_3_ptr (struct S s, unsigned n)
82 {
83 struct S vla2_3[2 < n ? 2 : n][3 < n ? 3 : n];
84 bool *p = (bool*)&vla2_3[0][0];
85
86 p[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
87 p[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
88 p[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
89 p[6] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
90
91 sink (vla2_3);
92 }
93
94 NOIPA void access_vla3_4_5_ptr (struct S s, unsigned n)
95 {
96 struct S vla3_4_5[3 < n ? 3 : n][4 < n ? 4 : n][5 < n ? 5 : n];
97 bool *p = (bool*)&vla3_4_5[0][0][0];
98
99 p[0] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
100 p[1] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
101 p[2] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
102 p[60] = __LINE__; // { dg-warning "\\\[-Warray-bounds" }
103
104 sink (vla3_4_5);
105 }
106
107 // { dg-prune-output "empty struct has size 0 in C" }