c11.c revision 1.4
1/*	$NetBSD: c11.c,v 1.4 2023/07/28 22:05:44 rillig Exp $	*/
2# 3 "c11.c"
3
4/*
5 * Test the language level C11, which adds _Generic expressions, _Noreturn
6 * functions, anonymous struct/union members, and several more.
7 */
8
9/* lint1-flags: -Ac11 -w -X 192,231,236,351 */
10
11_Noreturn void exit(int);
12void _Noreturn exit(int);
13
14_Noreturn void
15noreturn_before_type(void)
16{
17	exit(0);
18}
19
20void _Noreturn
21noreturn_after_type(void)
22{
23	exit(0);
24}
25
26static _Noreturn void
27noreturn_after_storage_class(void)
28{
29	exit(0);
30}
31
32_Noreturn static void
33noreturn_before_storage_class(void)
34{
35	exit(0);
36}
37
38/* C11 6.7.4p5: A function specifier may appear more than once. */
39_Noreturn _Noreturn _Noreturn void
40three_times(void)
41{
42	exit(0);
43}
44
45
46_Static_assert(1 > 0, "string");
47/* XXX: requires C23 or later */
48_Static_assert(1 > 0);
49
50
51// C11 6.7.6.1p3
52const int *ptr_to_constant;
53int *const constant_ptr;
54
55// C11 6.7.6.1p4
56typedef int *int_ptr;
57const int_ptr constant_ptr;
58
59// C11 6.7.6.2p7
60float fa[11], *afp[17];
61
62// C11 6.7.6.2p8
63extern int *x;
64extern int y[];
65
66// C11 6.7.6.2p9
67extern int n;
68extern int m;
69void fcompat(void)
70{
71	int a[n][6][m];
72	int (*p)[4][n+1];
73	int c[n][n][6][m];
74	int (*r)[n][n][n+1];
75	/* expect+2: warning: 'p' set but not used in function 'fcompat' [191] */
76	/* expect+1: warning: illegal combination of 'pointer to array[4] of array[1] of int' and 'pointer to array[6] of array[1] of int', op '=' [124] */
77	p = a;
78	/* expect+2: warning: 'r' set but not used in function 'fcompat' [191] */
79	/* expect+1: warning: illegal combination of 'pointer to array[1] of array[1] of array[1] of int' and 'pointer to array[1] of array[6] of array[1] of int', op '=' [124] */
80	r = c;
81}
82
83// C11 6.7.6.2p10
84extern int n;
85int A[n];
86extern int (*p2)[n];
87int B[100];
88void fvla(int m, int C[m][m]);
89void fvla(int m, int C[m][m])
90{
91	typedef int VLA[m][m];
92	struct tag {
93		int (*y)[n];
94		int z[n];
95	};
96	int D[m];
97	static int E[m];
98	/* expect+1: warning: nested 'extern' declaration of 'F' [352] */
99	extern int F[m];
100	int (*s)[m];
101	/* expect+1: warning: nested 'extern' declaration of 'r' [352] */
102	extern int (*r)[m];
103	/* expect+2: warning: illegal combination of 'pointer to array[1] of int' and 'pointer to int', op 'init' [124] */
104	/* expect+1: warning: 'q' set but not used in function 'fvla' [191] */
105	static int (*q)[m] = &B;
106}
107
108// C11 6.7.6.3p15
109int f(void), *fip(), (*pfi)();
110
111// C11 6.7.6.3p17
112int (*apfi[3])(int *x, int *y);
113
114// C11 6.7.6.3p18
115int (*fpfi(int (*)(long), int))(int, ...);
116
117// C11 6.7.6.3p19
118void addscalar(int n, int m, double a[n][n*m+300], double x);
119int main(void)
120{
121	double b[4][308];
122	/* expect+1: warning: converting 'pointer to array[308] of double' to incompatible 'pointer to array[1] of double' for argument 3 [153] */
123	addscalar(4, 2, b, 2.17);
124	return 0;
125}
126void addscalar(int n, int m, double a[n][n*m+300], double x)
127{
128	for (int i = 0; i < n; i++)
129		for (int j = 0, k = n*m+300; j < k; j++)
130			a[i][j] += x;
131}
132
133// C11 6.7.6.3p20
134double maximum(int n, int m, double a[n][m]);
135/* expect+1: error: null dimension [17] */
136double maximum(int n, int m, double a[*][*]);
137/* expect+1: error: null dimension [17] */
138double maximum(int n, int m, double a[ ][*]);
139double maximum(int n, int m, double a[ ][m]);
140
141void f1(double (* restrict a)[5]);
142void f2(double a[restrict][5]);
143/* expect+1: error: syntax error '3' [249] */
144void f3(double a[restrict 3][5]);
145void f4(double a[restrict static 3][5]);
146
147
148// In C11 mode, 'thread_local' is not yet known, but '_Thread_local' is.
149/* expect+2: error: old-style declaration; add 'int' [1] */
150/* expect+1: error: syntax error 'int' [249] */
151thread_local int thread_local_variable_c23;
152_Thread_local int thread_local_variable_c11;
153
154/* The '_Noreturn' must not appear after the declarator. */
155void _Noreturn exit(int) _Noreturn;
156/* expect-1: error: formal parameter #1 lacks name [59] */
157/* expect-2: warning: empty declaration [2] */
158/* expect+2: error: syntax error '' [249] */
159/* expect+1: error: cannot recover from previous errors [224] */
160