PR c/64856
[official-gcc.git] / gcc / testsuite / gcc.dg / pointer-array-quals-1.c
blob921a37e9e0d5b32104c1fa8b73d11ad54c6f927f
1 /* { dg-do compile } */
2 /* Origin: Martin Uecker <uecker@eecs.berkeley.edu> */
3 /* { dg-options "-Wdiscarded-array-qualifiers" } */
4 void tvoid(void* x);
5 void transpose0(double* out, const double* in) { }
6 void transpose1(double out[2][2], const double in[2][2]) { }
7 void transpose2(double out[2][2][2], const double in[2][2][2]) { }
8 // return
9 int (*y2(const int x[3][3]))[3] { return x; } /* { dg-warning "return discards 'const' qualifier from pointer target type" } */
10 const int (*y3(int x[3][3]))[3] { return x; }
11 void test(void)
13 double x0[2];
14 double y0[2];
15 const double z0[4];
16 double x1[2][2];
17 double y1[2][2];
18 double o1[2][3];
19 const double z1[2][2];
20 double x2[2][2][2];
21 double y2[2][2][2];
22 double o2[2][2][3];
23 const double z2[2][2][2];
24 // void pointers
25 tvoid(x0);
26 tvoid(x1);
27 tvoid(x2);
28 tvoid(z0); /* { dg-warning "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
29 tvoid(z1); /* { dg-warning "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
30 tvoid(z2); /* { dg-warning "passing argument 1 of 'tvoid' discards 'const' qualifier from pointer target type" } */
31 void* p;
32 const void* pc;
33 p = x0;
34 p = x1;
35 p = x2;
36 p = z0; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
37 p = z1; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
38 p = z2; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
39 pc = x0;
40 pc = x1;
41 pc = x2;
42 pc = z0;
43 pc = z1;
44 pc = z2;
45 transpose0(pc, p); /* { dg-warning "passing argument 1 of 'transpose0' discards 'const' qualifier from pointer target type" } */
46 transpose1(pc, p); /* { dg-warning "passing argument 1 of 'transpose1' discards 'const' qualifier from pointer target type" } */
47 transpose2(pc, p); /* { dg-warning "passing argument 1 of 'transpose2' discards 'const' qualifier from pointer target type" } */
48 transpose0(p, pc);
49 transpose1(p, pc);
50 transpose2(p, pc);
51 // passing as arguments
52 transpose0(y0, x0);
53 transpose1(y1, x1);
54 transpose2(y2, x2);
55 // initialization
56 const double (*u0p) = x0;
57 const double (*u1p)[2] = x1;
58 const double (*u2p)[2][2] = x2;
59 double (*v0p) = z0; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
60 double (*v1p)[2] = z1; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
61 double (*v2p)[2][2] = z2; /* { dg-warning "initialization discards 'const' qualifier from pointer target type" } */
62 // subtraction
63 &(x0[1]) - &(z0[0]);
64 &(x1[1]) - &(z1[0]);
65 &(x2[1]) - &(z2[0]);
66 // comparison
67 x0 == z0;
68 x1 == z1;
69 x2 == z2;
70 x0 < z0;
71 x1 < z1;
72 x2 < z2;
73 x0 > z0;
74 x1 > z1;
75 x2 > z2;
76 // assignment
77 u0p = x0;
78 u1p = x1;
79 u2p = x2;
80 v0p = z0; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
81 v1p = z1; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
82 v2p = z2; /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
83 // conditional expressions
84 (void)(1 ? x0 : z0);
85 (void)(1 ? x1 : z1);
86 (void)(1 ? x2 : z2);
87 (void)(1 ? x0 : x1); /* { dg-warning "pointer type mismatch in conditional expression" } */
88 (void)(1 ? x1 : x2); /* { dg-warning "pointer type mismatch in conditional expression" } */
89 (void)(1 ? x2 : x0); /* { dg-warning "pointer type mismatch in conditional expression" } */
90 v0p = (1 ? z0 : v0p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
91 v1p = (1 ? z1 : v1p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
92 v2p = (1 ? z2 : v2p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
93 v0p = (1 ? x0 : u0p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
94 v1p = (1 ? x1 : u1p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
95 v2p = (1 ? x2 : u2p); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
96 (1 ? x0 : z0)[0] = 1; /* { dg-error "assignment of read-only location" } */
97 (1 ? x1 : z1)[0][0] = 1; /* { dg-error "assignment of read-only location" } */
98 (1 ? x2 : z2)[0][0][0] = 1; /* { dg-error "assignment of read-only location" } */
99 v0p = (1 ? p : z0); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
100 v1p = (1 ? p : z1); /* { dg-warning "pointer to array loses qualifier in conditional expression" } */
101 v2p = (1 ? p : z2); /* { dg-warning "pointer to array loses qualifier in conditional expression" } */
102 v0p = (1 ? pc : x0); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
103 v1p = (1 ? pc : x1); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */
104 v2p = (1 ? pc : x2); /* { dg-warning "assignment discards 'const' qualifier from pointer target type" } */