-Warray-bounds: Fix false positive in some "switch" stmts (PR tree-optimization/83510)
[official-gcc.git] / gcc / testsuite / gcc.c-torture / compile / pr83510.c
blob907dd80ccd79e4a9792c147e342d50792bb77387
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). */
5 /* { dg-options "-Warray-bounds" } */
7 extern int get_flag (void);
9 unsigned int arr[10];
11 struct xyz {
12 unsigned int a0;
15 extern void wfm(struct xyz *, int, unsigned int);
17 static unsigned int f(struct xyz * ctx, unsigned int number)
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];
27 return 0;
30 int g(struct xyz * ctx) {
31 int i;
33 for (i = 0; i < 10; i++) {
34 wfm(ctx, i, f(ctx, i));
37 return 0;
40 static unsigned int f_signed(struct xyz * ctx, int number)
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];
50 return 0;
53 int g_signed(struct xyz * ctx) {
54 int i;
56 for (i = 0; i < 10; i++) {
57 wfm(ctx, i, f(ctx, i));
60 return 0;
63 void test_2 (struct xyz * ctx)
65 int i;
67 for (i = 0; i < 10; i++) {
68 if (get_flag ())
69 wfm(ctx, i, f(ctx, i));
73 void test_2_signed (struct xyz * ctx)
75 int i;
77 for (i = 0; i < 10; i++) {
78 if (get_flag ())
79 wfm(ctx, i, f_signed(ctx, i));
83 void test_3 (struct xyz * ctx)
85 unsigned int i;
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;
102 void test_3_signed (struct xyz * ctx)
104 int i;
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;
121 void test_4 (struct xyz * ctx)
123 unsigned int i, j;
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;
139 void test_4_signed (struct xyz * ctx)
141 int i, j;
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;
158 void test_5 (struct xyz * ctx)
160 unsigned int i;
161 for (i = 10; i < 20; i++) {
162 wfm(ctx, i, arr[i - 10]);
166 void test_5_signed (struct xyz * ctx)
168 int i;
169 for (i = 10; i < 20; i++) {
170 wfm(ctx, i, arr[i - 10]);