PR tree-optimization/84047 - missing -Warray-bounds on an out-of-bounds index into...
[official-gcc.git] / gcc / testsuite / gcc.dg / Warray-bounds-30.c
blobac7e9a6e8fbe6aff977c38f84f1a173e5cd41b36
1 /* PR tree-optimization/84047 - missing -Warray-bounds on an out-of-bounds
2 index into an array
3 { dg-do compile }
4 { dg-options "-O2 -Warray-bounds=2 -ftrack-macro-expansion=0" } */
6 #include "range.h"
8 #define MAX DIFF_MAX
9 #define MIN DIFF_MIN
11 void sink (int, ...);
13 #define T(...) sink (0, __VA_ARGS__)
15 void test_global_char_array (void)
17 extern char gcar1[1];
18 char *p = gcar1;
20 T (p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[1]." } */
21 T (p[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[1]." } */
22 T (p[0]);
23 T (p[1]); /* { dg-warning "subscript 1 is outside array bounds of .char\\\[1]." } */
24 T (p[MAX]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[1]." } */
26 T (&p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[1]." } */
27 T (&p[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[1]." } */
28 T (&p[0]);
29 T (&p[1]);
30 T (&p[2]); /* { dg-warning "subscript 2 is \(above|outside\) array bounds of .char\\\[1]." } */
31 T (&p[MAX]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[1]." } */
33 extern char gcar3[3];
34 char *q = gcar3;
36 T (q[MIN]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[3]." } */
37 T (q[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[3]." } */
38 T (q[0]);
39 T (q[1]);
40 T (q[2]);
41 T (q[3]); /* { dg-warning "subscript 3 is outside array bounds of .char\\\[3]." } */
42 T (q[MAX]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[3]." } */
44 T (&q[MIN]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[3]." } */
45 T (&q[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[3]." } */
46 T (&q[0]);
47 T (&q[1]);
48 T (&q[2]);
49 T (&q[3]);
50 T (&q[4]); /* { dg-warning "subscript 4 is \(above|outside\) array bounds of .char\\\[3]." } */
51 T (&q[MAX]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[3]." } */
55 void test_global_int_array (void)
57 /* Use smaller values to prevent false negatives due to undetected
58 integer overflow/wrapping. */
59 ptrdiff_t min = MIN / sizeof (int);
60 ptrdiff_t max = MAX / sizeof (int);
62 extern int giar1[1];
63 extern int giar3[3];
65 int *p = giar1;
67 T (p[min]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .int\\\[1]." } */
68 T (p[-1]); /* { dg-warning "subscript -1 is outside array bounds of .int\\\[1]." } */
69 T (p[0]);
70 T (p[1]); /* { dg-warning "subscript 1 is outside array bounds of .int\\\[1]." } */
71 T (p[max]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .int\\\[1]." } */
73 T (&p[min]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .int\\\[1]." } */
74 T (&p[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .int\\\[1]." } */
75 T (&p[0]);
76 T (&p[1]);
77 T (&p[2]); /* { dg-warning "subscript 2 is \(above|outside\) array bounds of .int\\\[1]." } */
78 T (&p[max]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .int\\\[1]." } */
80 int *q = giar3;
82 T (q[min]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .int\\\[3]." } */
83 T (q[-1]); /* { dg-warning "subscript -1 is outside array bounds of .int\\\[3]." } */
84 T (q[0]);
85 T (q[1]);
86 T (q[2]);
87 T (q[3]); /* { dg-warning "subscript 3 is outside array bounds of .int\\\[3]." } */
88 T (q[max]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .int\\\[3]." } */
90 T (&q[min]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .int\\\[3]." } */
91 T (&q[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .int\\\[3]." } */
92 T (&q[0]);
93 T (&q[1]);
94 T (&q[2]);
95 T (&q[3]);
96 T (&q[4]); /* { dg-warning "subscript 4 is \(above|outside\) array bounds of .int\\\[3]." } */
97 T (&q[max]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .int\\\[3]." } */
101 void test_global_short_2dim_array (void)
103 extern short giar3_5[3][5];
105 short *p = giar3_5[0];
107 /* The access below isn't diagnosed because the reference is transformed
108 into MEM_REF (short*, &giar3_5, 0), i.e., *giar3_5[0][0]. */
109 T (p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .short int\\\[3]" "bug" { xfail *-*-*} } */
110 T (p[-1]); /* { dg-warning "subscript -1 is outside array bounds of .short int\\\[3]" } */
111 T (p[0]);
112 T (p[1]);
113 T (p[2]);
114 T (p[15]); /* { dg-warning "subscript 15 is outside array bounds of .short int\\\[3]" } */
115 T (p[MAX]); /* { dg-warning "subscript -?\[0-9\]+ is outside array bounds of .short int\\\[3]" } */
117 T (&p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .short int\\\[3]" "bug" { xfail *-*-* } } */
118 T (&p[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .short int\\\[3]" } */
119 T (&p[0]);
120 T (&p[1]);
121 T (&p[2]);
122 T (&p[3]);
123 T (&p[16]); /* { dg-warning "subscript 16 is \(above|outside\) array bounds of .short int\\\[3]" } */
124 T (&p[MAX]); /* { dg-warning "subscript -?\[0-9\]+ is \(above|outside\) array bounds of .short int\\\[3]" } */
128 void test_local_char_array (void)
130 char ar1[1] = { 1 };
131 char *p = ar1;
133 T (p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[1]." } */
134 T (p[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[1]." } */
135 T (p[0]);
136 T (p[1]); /* { dg-warning "subscript 1 is outside array bounds of .char\\\[1]." } */
137 T (p[MAX]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[1]." } */
139 T (&p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[1]." } */
140 T (&p[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[1]." } */
141 T (&p[0]);
142 T (&p[1]);
143 T (&p[2]); /* { dg-warning "subscript 2 is \(above|outside\) array bounds of .char\\\[1]." } */
144 T (&p[MAX]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[1]." } */
146 char ar3[3] = { 1, 2, 3 };
147 p = ar3;
149 T (p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is outside array bounds of .char\\\[3]." } */
150 T (p[-1]); /* { dg-warning "subscript -1 is outside array bounds of .char\\\[3]." } */
151 T (p[0]);
152 T (p[1]);
153 T (p[2]);
154 T (p[3]); /* { dg-warning "subscript 3 is outside array bounds of .char\\\[3]." } */
155 T (p[MAX]); /* { dg-warning "subscript \[0-9\]+ is outside array bounds of .char\\\[3]." } */
157 T (&p[MIN]); /* { dg-warning "subscript -\[0-9\]+ is \(below|outside\) array bounds of .char\\\[3]." } */
158 T (&p[-1]); /* { dg-warning "subscript -1 is \(below|outside\) array bounds of .char\\\[3]." } */
159 T (&p[0]);
160 T (&p[1]);
161 T (&p[2]);
162 T (&p[3]);
163 T (&p[4]); /* { dg-warning "subscript 4 is \(above|outside\) array bounds of .char\\\[3]." } */
164 T (&p[MAX]); /* { dg-warning "subscript \[0-9\]+ is \(above|outside\) array bounds of .char\\\[3]." } */
167 struct S
169 int a[2], b[3];
170 } s [4];
172 void test_struct_array_cst (void)
174 T (s[0].a[0] + s[0].a[1] + s[0].b[0] + s[0].b[1] + s[0].b[2] + s[0].b[2]
175 + s[1].a[0] + s[1].a[1] + s[1].b[0] + s[1].b[1] + s[1].b[2] + s[1].b[2]
176 + s[2].a[0] + s[2].a[1] + s[2].b[0] + s[2].b[1] + s[2].b[2] + s[2].b[2]
177 + s[3].a[0] + s[3].a[1] + s[3].b[0] + s[3].b[1] + s[3].b[2] + s[3].b[2]);
179 T (&s[0].a[2],
180 &s[0].b[3],
181 &s[1].a[2],
182 &s[1].b[3],
183 &s[2].a[2],
184 &s[2].b[3],
185 &s[3].a[2],
186 &s[3].b[3]);
188 T (s[0].a[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
189 T (s[0].b[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
190 T (s[1].a[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
191 T (s[1].b[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
192 T (s[2].a[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
193 T (s[2].b[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
194 T (s[3].a[2]); /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." } */
195 T (s[3].b[3]); /* { dg-warning "subscript 3 is above array bounds of .int\\\[3\\\]." } */
197 T (s[4].a[0]); /* { dg-warning "subscript 4 is above array bounds of .struct S\\\[4\\\]." } */
198 T (s[4].a[2]); /* { dg-warning "subscript 4 is above array bounds of .struct S\\\[4\\\]." } */
199 /* { dg-warning "subscript 2 is above array bounds of .int\\\[2\\\]." "" { target *-*-* } .-1 } */