| 1 |
/* PR tree-optimization/84095 - false-positive -Wrestrict warnings for |
| 2 |
memcpy within array |
| 3 |
{ dg-do compile } |
| 4 |
{ dg-options "-O2 -Wrestrict -ftrack-macro-expansion=0" } */ |
| 5 |
|
| 6 |
typedef __SIZE_TYPE__ size_t; |
| 7 |
|
| 8 |
extern void* memcpy (void* restrict, const void* restrict, size_t); |
| 9 |
|
| 10 |
void foo (void *); |
| 11 |
|
| 12 |
#define T(d, s, n) do { memcpy (d, s, n); foo (d); } while (0) |
| 13 |
|
| 14 |
struct S1 { char c; } a8_1[8]; |
| 15 |
|
| 16 |
void test_1_dim_var (int i, int j) |
| 17 |
{ |
| 18 |
/* Variable destination index and constant source index. */ |
| 19 |
T (&a8_1[i], &a8_1[0], 1); |
| 20 |
T (&a8_1[i], &a8_1[0], 2); |
| 21 |
T (&a8_1[i], &a8_1[0], 3); |
| 22 |
T (&a8_1[i], &a8_1[0], 4); |
| 23 |
|
| 24 |
T (&a8_1[i], &a8_1[0], 5); /* { dg-warning "accessing 5 bytes at offsets \\\[0, 8] and 0 overlaps between 2 and 5 bytes at offset \\\[0, 3\\\]" } */ |
| 25 |
T (&a8_1[i], &a8_1[0], 6); /* { dg-warning "accessing 6 bytes at offsets \\\[0, 8] and 0 overlaps between 4 and 6 bytes at offset \\\[0, 2\\\]" } */ |
| 26 |
T (&a8_1[i], &a8_1[0], 7); /* { dg-warning "accessing 7 bytes at offsets \\\[0, 8] and 0 overlaps between 6 and 7 bytes at offset \\\[0, 1\\\]" } */ |
| 27 |
T (&a8_1[i], &a8_1[0], 8); /* { dg-warning "accessing 8 bytes at offsets \\\[0, 8] and 0 overlaps 8 bytes at offset 0" } */ |
| 28 |
|
| 29 |
/* The following is diagnosed by -Warray-bounds when it's enabled |
| 30 |
rather than by -Wrestrict. */ |
| 31 |
T (&a8_1[i], &a8_1[0], 9); /* { dg-warning "accessing 9 bytes at offsets \\\[0, 8] and 0 overlaps 9 bytes at offset 0" } */ |
| 32 |
|
| 33 |
/* Same as above but with constant destination index and variable |
| 34 |
source index. */ |
| 35 |
T (&a8_1[0], &a8_1[i], 1); |
| 36 |
T (&a8_1[0], &a8_1[i], 2); |
| 37 |
T (&a8_1[0], &a8_1[i], 3); |
| 38 |
T (&a8_1[0], &a8_1[i], 4); |
| 39 |
|
| 40 |
T (&a8_1[0], &a8_1[i], 5); /* { dg-warning "accessing 5 bytes at offsets 0 and \\\[0, 8] overlaps between 2 and 5 bytes at offset \\\[0, 3\\\]" } */ |
| 41 |
T (&a8_1[0], &a8_1[i], 6); /* { dg-warning "accessing 6 bytes at offsets 0 and \\\[0, 8] overlaps between 4 and 6 bytes at offset \\\[0, 2\\\]" } */ |
| 42 |
T (&a8_1[0], &a8_1[i], 7); /* { dg-warning "accessing 7 bytes at offsets 0 and \\\[0, 8] overlaps between 6 and 7 bytes at offset \\\[0, 1\\\]" } */ |
| 43 |
T (&a8_1[0], &a8_1[i], 8); /* { dg-warning "accessing 8 bytes at offsets 0 and \\\[0, 8] overlaps 8 bytes at offset 0" } */ |
| 44 |
T (&a8_1[0], &a8_1[i], 9); /* { dg-warning "accessing 9 bytes at offsets 0 and \\\[0, 8] overlaps 9 bytes at offset 0" } */ |
| 45 |
|
| 46 |
|
| 47 |
/* Variable destination and source indices. */ |
| 48 |
T (&a8_1[i], &a8_1[j], 1); |
| 49 |
T (&a8_1[i], &a8_1[j], 2); |
| 50 |
T (&a8_1[i], &a8_1[j], 3); |
| 51 |
T (&a8_1[i], &a8_1[j], 4); |
| 52 |
|
| 53 |
T (&a8_1[i], &a8_1[j], 5); /* { dg-warning "accessing 5 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps between 2 and 5 bytes at offset \\\[0, 3\\\]" } */ |
| 54 |
T (&a8_1[i], &a8_1[j], 6); /* { dg-warning "accessing 6 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps between 4 and 6 bytes at offset \\\[0, 2\\\]" } */ |
| 55 |
T (&a8_1[i], &a8_1[j], 7); /* { dg-warning "accessing 7 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps between 6 and 7 bytes at offset \\\[0, 1\\\]" } */ |
| 56 |
T (&a8_1[i], &a8_1[j], 8); /* { dg-warning "accessing 8 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps 8 bytes at offset 0" } */ |
| 57 |
|
| 58 |
/* The following is diagnosed by -Warray-bounds when it's enabled |
| 59 |
rather than by -Wrestrict. */ |
| 60 |
T (&a8_1[i], &a8_1[j], 9); /* { dg-warning "accessing 9 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps 9 bytes at offset 0" } */ |
| 61 |
} |
| 62 |
|
| 63 |
struct S4 { char a4[4]; } a2_4[2]; |
| 64 |
|
| 65 |
void test_2_dim (int i, int j) |
| 66 |
{ |
| 67 |
T (&a2_4[i], &a2_4[0], 1); |
| 68 |
T (&a2_4[i], &a2_4[0], 4); |
| 69 |
|
| 70 |
T (&a2_4[i], &a2_4[0], 5); /* { dg-warning "accessing 5 bytes at offsets \\\[0, 8] and 0 overlaps between 2 and 5 bytes at offset \\\[0, 3]" } */ |
| 71 |
T (&a2_4[i], &a2_4[0], 6); /* { dg-warning "accessing 6 bytes at offsets \\\[0, 8] and 0 overlaps between 4 and 6 bytes at offset \\\[0, 2]" } */ |
| 72 |
T (&a2_4[i], &a2_4[0], 7); /* { dg-warning "accessing 7 bytes at offsets \\\[0, 8] and 0 overlaps between 6 and 7 bytes at offset \\\[0, 1]" } */ |
| 73 |
T (&a2_4[i], &a2_4[0], 8); /* { dg-warning "accessing 8 bytes at offsets \\\[0, 8] and 0 overlaps 8 bytes at offset 0" } */ |
| 74 |
|
| 75 |
T (a2_4[i].a4, a2_4[0].a4, 1); |
| 76 |
T (a2_4[i].a4, a2_4[0].a4, 4); |
| 77 |
|
| 78 |
T (a2_4[i].a4, a2_4[0].a4, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[0, 8] and 0 overlaps between 2 and 5 bytes at offset \\\[0, 3]" } */ |
| 79 |
T (a2_4[i].a4, a2_4[0].a4, 8); /* { dg-warning "accessing 8 bytes at offsets \\\[0, 8] and 0 overlaps 8 bytes at offset 0" } */ |
| 80 |
|
| 81 |
T (a2_4[i].a4, a2_4[j].a4, 1); |
| 82 |
T (a2_4[i].a4, a2_4[j].a4, 4); |
| 83 |
|
| 84 |
/* The destination and source offsets printed below ignore the size |
| 85 |
of the copy and only indicate the values that are valid for each |
| 86 |
of the destination and source arguments on its own, without |
| 87 |
considering the size of the overlapping access. */ |
| 88 |
T (a2_4[i].a4, a2_4[j].a4, 5); /* { dg-warning "accessing 5 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps between 2 and 5 bytes at offset \\\[0, 3]" } */ |
| 89 |
T (a2_4[i].a4, a2_4[j].a4, 8); /* { dg-warning "accessing 8 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps 8 bytes at offset 0" } */ |
| 90 |
|
| 91 |
/* Same as above but referencing the first elements of each array. */ |
| 92 |
T (&a2_4[i].a4[0], &a2_4[j].a4[0], 1); |
| 93 |
T (&a2_4[i].a4[0], &a2_4[j].a4[0], 4); |
| 94 |
|
| 95 |
T (&a2_4[i].a4[0], &a2_4[j].a4[0], 5); /* { dg-warning "accessing 5 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps between 2 and 5 bytes at offset \\\[0, 3]" } */ |
| 96 |
T (&a2_4[i].a4[0], &a2_4[j].a4[0], 8); /* { dg-warning "accessing 8 bytes at offsets \\\[0, 8] and \\\[0, 8] overlaps 8 bytes at offset 0" } */ |
| 97 |
|
| 98 |
T (&a2_4[i].a4[0], &a2_4[j].a4[1], 3); |
| 99 |
T (&a2_4[i].a4[0], &a2_4[j].a4[2], 2); |
| 100 |
T (&a2_4[i].a4[0], &a2_4[j].a4[3], 1); |
| 101 |
} |
| 102 |
|
| 103 |
struct { int i; } a2[2][8]; |
| 104 |
|
| 105 |
void test_single_2_dim_major (int i) |
| 106 |
{ |
| 107 |
memcpy (&a2[i], &a2[0], sizeof *a2); /* { dg-bogus "\\\[-Wrestrict]" } */ |
| 108 |
} |
| 109 |
|
| 110 |
void test_single_2_dim_minor (int i) |
| 111 |
{ |
| 112 |
memcpy (&a2[i][0], &a2[0][0], sizeof a2[0][0]); /* { dg-bogus "\\\[-Wrestrict]" } */ |
| 113 |
} |
| 114 |
|
| 115 |
void test_single_2_dim_major_minor (int i, int j) |
| 116 |
{ |
| 117 |
memcpy (&a2[i][j], &a2[0][0], sizeof a2[0][0]); /* { dg-bogus "\\\[-Wrestrict]" } */ |
| 118 |
} |