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