1 /* PR tree-optimization/84079 - missing -Warray-bounds taking the address
2 of past-the-end element of a multidimensional array
3 { dg-do compile }
4 { dg-options "-O2 -Wall -ftrack-macro-expansion=0" } */
5
6 void sink (int, ...);
7
8 #define T(type, dims, inxs) \
9 do { \
10 type a dims; \
11 sink (__LINE__, &a inxs); \
12 } while (0)
13
14
15 void test_char_1_1 (int i0, int i1, int i2)
16 {
17 #undef DIMS
18 #define DIMS [1][1]
19
20 T (char, DIMS, [0]);
21 T (char, DIMS, [1]);
22 T (char, DIMS, [2]); // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]\\\[1]'" }
23
24 T (char, DIMS, [0][0]);
25 T (char, DIMS, [0][1]);
26 T (char, DIMS, [0][2]); // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]'" }
27
28 T (char, DIMS, [1][0]); // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
29 T (char, DIMS, [1][1]); // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
30 T (char, DIMS, [1][2]); // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]'" }
31
32 // Exercise ranges.
33 if (i0 < 0) i0 = 0;
34 if (i1 < 1) i1 = 1;
35 if (i2 < 2) i2 = 2;
36
37 T (char, DIMS, [i0]);
38 T (char, DIMS, [i1]);
39 T (char, DIMS, [i2]); // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]\\\[1]" }
40
41 T (char, DIMS, [i0][i0]);
42 T (char, DIMS, [i0][i1]);
43 T (char, DIMS, [i1][i0]); // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
44 T (char, DIMS, [i1][i1]); // { dg-warning "subscript 1 is above array bounds of 'char\\\[1]\\\[1]'" }
45 T (char, DIMS, [i1][i2]); // { dg-warning "subscript 2 is above array bounds of 'char\\\[1]'" }
46 }
47
48
49 void test_int_3_5 (int i0, int i1, int i2, int i3, int i4, int i5, int i6)
50 {
51 #undef DIMS
52 #define DIMS [3][5]
53
54 T (int, DIMS, [0]);
55 T (int, DIMS, [3]);
56 T (int, DIMS, [4]); // { dg-warning "subscript 4 is above array bounds of 'int\\\[3]\\\[5]'" }
57
58 T (int, DIMS, [0][0]);
59 T (int, DIMS, [0][5]);
60 T (int, DIMS, [0][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
61
62 T (int, DIMS, [1][0]);
63 T (int, DIMS, [1][5]);
64 T (int, DIMS, [1][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
65
66 T (int, DIMS, [3][0]); // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
67 T (int, DIMS, [3][5]); // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
68 T (int, DIMS, [3][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
69
70 // Exercise ranges.
71 if (i0 < 0) i0 = 0;
72 if (i1 < 1) i1 = 1;
73 if (i2 < 2) i2 = 2;
74 if (i3 < 3) i3 = 3;
75 if (i4 < 4) i4 = 4;
76 if (i5 < 5) i5 = 5;
77 if (i6 < 6) i6 = 6;
78
79 T (int, DIMS, [i0]);
80 T (int, DIMS, [i3]);
81 T (int, DIMS, [i4]); // { dg-warning "subscript 4 is above array bounds of 'int\\\[3]\\\[5]" }
82
83 T (int, DIMS, [i0][i0]);
84 T (int, DIMS, [i0][i5]);
85 T (int, DIMS, [i0][i6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
86
87 T (int, DIMS, [i1][i0]);
88 T (int, DIMS, [i1][i5]);
89 T (int, DIMS, [i1][i6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
90
91 T (int, DIMS, [i3][i0]); // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
92 T (int, DIMS, [i3][i5]); // { dg-warning "subscript 3 is above array bounds of 'int\\\[3]\\\[5]'" }
93 T (int, DIMS, [i3][i6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
94 }
95
96
97 void test_int_2_3_4_5 (void)
98 {
99 #undef DIMS
100 #define DIMS [2][3][4][5]
101
102 T (int, DIMS, [0]);
103 T (int, DIMS, [2]);
104 T (int, DIMS, [3]); // { dg-warning "subscript 3 is above array bounds of 'int\\\[2]\\\[3]\\\[4]\\\[5]'" }
105
106 T (int, DIMS, [0][0]);
107 T (int, DIMS, [0][3]);
108 T (int, DIMS, [0][4]); // { dg-warning "subscript 4 is above array bounds of 'int\\\[3]\\\[4]\\\[5]'" }
109 T (int, DIMS, [0][9]); // { dg-warning "subscript 9 is above array bounds of 'int\\\[3]\\\[4]\\\[5]'" }
110
111 T (int, DIMS, [0][0][0]);
112 T (int, DIMS, [0][0][4]);
113 T (int, DIMS, [0][0][5]); // { dg-warning "subscript 5 is above array bounds of 'int\\\[4]\\\[5]'" }
114
115 T (int, DIMS, [0][0][0][0]);
116 T (int, DIMS, [0][0][0][5]);
117 T (int, DIMS, [0][0][0][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
118
119 T (int, DIMS, [0][0][1][0]);
120 T (int, DIMS, [0][0][1][5]);
121 T (int, DIMS, [0][0][1][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
122
123 T (int, DIMS, [0][0][3][0]);
124 T (int, DIMS, [0][0][3][5]);
125 T (int, DIMS, [0][0][3][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
126
127 T (int, DIMS, [0][0][1][0]);
128 T (int, DIMS, [0][0][1][5]);
129 T (int, DIMS, [0][0][1][6]); // { dg-warning "subscript 6 is above array bounds of 'int\\\[5]'" }
130 }