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