1 /* Various examples of safe array access for which -Warray-bounds
2 shouldn't issue a warning at any optimization level
3 (PR tree-optimization/83510). */
4
5 /* { dg-options "-Warray-bounds" } */
6
7 extern int get_flag (void);
8
9 unsigned int arr[10];
10
11 struct xyz {
12 unsigned int a0;
13 };
14
15 extern void wfm(struct xyz *, int, unsigned int);
16
f(struct xyz * ctx,unsigned int number)17 static unsigned int f(struct xyz * ctx, unsigned int number)
18 {
19 switch (number) {
20 case 0x9:
21 return ctx->a0;
22 case 0xA: case 0xB:
23 case 0xC: case 0xD: case 0xE: case 0xF:
24 case 0x10: case 0x11: case 0x12: case 0x13:
25 return arr[number - 0xa];
26 }
27 return 0;
28 }
29
g(struct xyz * ctx)30 int g(struct xyz * ctx) {
31 int i;
32
33 for (i = 0; i < 10; i++) {
34 wfm(ctx, i, f(ctx, i));
35 }
36
37 return 0;
38 }
39
f_signed(struct xyz * ctx,int number)40 static unsigned int f_signed(struct xyz * ctx, int number)
41 {
42 switch (number) {
43 case 0x9:
44 return ctx->a0;
45 case 0xA: case 0xB:
46 case 0xC: case 0xD: case 0xE: case 0xF:
47 case 0x10: case 0x11: case 0x12: case 0x13:
48 return arr[number];
49 }
50 return 0;
51 }
52
g_signed(struct xyz * ctx)53 int g_signed(struct xyz * ctx) {
54 int i;
55
56 for (i = 0; i < 10; i++) {
57 wfm(ctx, i, f(ctx, i));
58 }
59
60 return 0;
61 }
62
test_2(struct xyz * ctx)63 void test_2 (struct xyz * ctx)
64 {
65 int i;
66
67 for (i = 0; i < 10; i++) {
68 if (get_flag ())
69 wfm(ctx, i, f(ctx, i));
70 }
71 }
72
test_2_signed(struct xyz * ctx)73 void test_2_signed (struct xyz * ctx)
74 {
75 int i;
76
77 for (i = 0; i < 10; i++) {
78 if (get_flag ())
79 wfm(ctx, i, f_signed(ctx, i));
80 }
81 }
82
test_3(struct xyz * ctx)83 void test_3 (struct xyz * ctx)
84 {
85 unsigned int i;
86
87 for (i = 0; i < 10; i++) {
88 switch (i) {
89 case 0x9:
90 wfm(ctx, i, ctx->a0);
91 break;
92 case 0xA: case 0xB:
93 case 0xC: case 0xD: case 0xE: case 0xF:
94 case 0x10: case 0x11: case 0x12: case 0x13:
95 if (get_flag ())
96 wfm(ctx, i, arr[i - 0xa]);
97 break;
98 }
99 }
100 }
101
test_3_signed(struct xyz * ctx)102 void test_3_signed (struct xyz * ctx)
103 {
104 int i;
105
106 for (i = 0; i < 10; i++) {
107 switch (i) {
108 case 0x9:
109 wfm(ctx, i, ctx->a0);
110 break;
111 case 0xA: case 0xB:
112 case 0xC: case 0xD: case 0xE: case 0xF:
113 case 0x10: case 0x11: case 0x12: case 0x13:
114 if (get_flag ())
115 wfm(ctx, i, arr[i]);
116 break;
117 }
118 }
119 }
120
test_4(struct xyz * ctx)121 void test_4 (struct xyz * ctx)
122 {
123 unsigned int i, j;
124
125 for (i = 0; i < 10; i++) {
126 switch (i) {
127 case 0x9:
128 wfm(ctx, i, ctx->a0);
129 break;
130 case 0xA: case 0xB:
131 case 0xC: case 0xD: case 0xE: case 0xF:
132 case 0x10: case 0x11: case 0x12: case 0x13:
133 for (j = 0; j < 5; j++)
134 wfm(ctx, i, arr[i - 0xa]);
135 break;
136 }
137 }
138 }
test_4_signed(struct xyz * ctx)139 void test_4_signed (struct xyz * ctx)
140 {
141 int i, j;
142
143 for (i = 0; i < 10; i++) {
144 switch (i) {
145 case 0x9:
146 wfm(ctx, i, ctx->a0);
147 break;
148 case 0xA: case 0xB:
149 case 0xC: case 0xD: case 0xE: case 0xF:
150 case 0x10: case 0x11: case 0x12: case 0x13:
151 for (j = 0; j < 5; j++)
152 wfm(ctx, i, arr[i]);
153 break;
154 }
155 }
156 }
157
test_5(struct xyz * ctx)158 void test_5 (struct xyz * ctx)
159 {
160 unsigned int i;
161 for (i = 10; i < 20; i++) {
162 wfm(ctx, i, arr[i - 10]);
163 }
164 }
165
test_5_signed(struct xyz * ctx)166 void test_5_signed (struct xyz * ctx)
167 {
168 int i;
169 for (i = 10; i < 20; i++) {
170 wfm(ctx, i, arr[i - 10]);
171 }
172 }
173