xref: /netbsd-src/tests/usr.bin/xlint/lint1/decl_direct_abstract.c (revision d2c16d5796af7d64c26094d6e83f5c79714a35d6)
1 /*	$NetBSD: decl_direct_abstract.c,v 1.12 2024/01/28 08:17:27 rillig Exp $	*/
2 # 3 "decl_direct_abstract.c"
3 
4 /*
5  * Test parsing of direct-abstract-declarator (C99 6.7.6), which are a tricky
6  * part of the C standard since they require lookahead and are so complicated
7  * that GCC's parser dedicates 34 lines of comments to this topic.
8  *
9  * See msg_155.c.
10  */
11 
12 /* lint1-extra-flags: -X 351 */
13 
14 struct incompatible {
15 	int member;
16 } x;
17 
18 void
c99_6_7_6_examples(void)19 c99_6_7_6_examples(void)
20 {
21 	/* expect+1: ... 'int' ... */
22 	x = (int)x;
23 	/* expect+1: ... 'pointer to int' ... */
24 	x = (int *)x;
25 	/* expect+1: ... 'array[3] of pointer to int' ... */
26 	x = (int *[3])x;
27 	/* expect+1: ... 'pointer to array[3] of int' ... */
28 	x = (int (*)[3])x;
29 	/* expect+1: ... 'pointer to array[unknown_size] of int' ... */
30 	x = (int (*)[*])x;
31 	/* expect+1: ... 'function() returning pointer to int' ... */
32 	x = (int *())x;
33 	/* expect+1: ... 'pointer to function(void) returning int' ... */
34 	x = (int (*)(void))x;
35 	/* expect+1: ... 'array[unknown_size] of const pointer to function(unsigned int, ...) returning int' ... */
36 	x = (int (*const[])(unsigned int, ...))x;
37 }
38 
39 void
function_returning_char(void)40 function_returning_char(void)
41 {
42 	// GCC adds a pointer, then says 'char (*)(short int (*)(long int))'.
43 	// Clang says 'char (short (*)(long))'.
44 	/* cdecl says 'function (pointer to function (long) returning short) returning char' */
45 	/* expect+1: ... 'function(pointer to function(long) returning short) returning char' ... */
46 	x = (char(short (*)(long)))x;
47 
48 	/* expect+1: warning: nested 'extern' declaration of 'f1' [352] */
49 	char f1(short (*)(long));
50 
51 	/* expect+1: ... 'pointer to function(pointer to function(long) returning short) returning char' ... */
52 	x = f1;
53 }
54 
55 void
function_returning_pointer(void)56 function_returning_pointer(void)
57 {
58 	// GCC says 'error: cast specifies function type'.
59 	// Clang says 'char (short *(*)(long))'.
60 	/* expect+1: ... 'function(pointer to function(long) returning pointer to short) returning char' ... */
61 	x = (char(short *(long)))x;
62 
63 	/* expect+1: warning: nested 'extern' declaration of 'f2' [352] */
64 	char f2(short *(long));
65 
66 	// GCC adds two pointers, saying 'char (*)(short int * (*)(long int))'.
67 	// Clang says 'char (short *(*)(long))' */
68 	/* cdecl says 'syntax error' */
69 	/* expect+1: ... 'pointer to function(pointer to function(long) returning pointer to short) returning char' ... */
70 	x = f2;
71 }
72 
73 
74 void int_array(int[]);
75 void int_array_3(int[3]);
76 /* supported since cgram.y 1.363 from 2021-09-14 */
77 void int_array_ast(int[*]);
78 /* expect+1: error: null dimension [17] */
79 void int_array_7_array(int[7][]);
80 void int_array_7_array_3(int[7][3]);
81 /* expect+1: error: null dimension [17] */
82 void int_array_7_array_ast(int[7][*]);
83 
84 void int_array_array(int[][7]);
85 void int_array_3_array(int[3][7]);
86 /* supported since cgram.y 1.363 from 2021-09-14 */
87 void int_array_ast_array(int[*][7]);
88 
89 /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */
90 unsigned long size_unspecified_args = sizeof(int());
91 /* expect+1: error: cannot take size/alignment of function type 'function(void) returning int' [144] */
92 unsigned long size_prototype_void = sizeof(int(void));
93 /* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
94 unsigned long size_prototype_unnamed = sizeof(int(double));
95 /* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
96 unsigned long size_prototype_named = sizeof(int(double dbl));
97 
98 /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */
99 /* expect+1: error: negative array dimension (-1000) [20] */
100 int size_unspecified_args_return_int[-1000 - (int)sizeof(int())];
101 
102 /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */
103 /* expect+1: error: negative array dimension (-1000) [20] */
104 int size_unspecified_args_return_char[-1000 - (int)sizeof(char())];
105 
106 /* expect+2: error: cannot take size/alignment of function type 'function(void) returning int' [144] */
107 /* expect+1: error: negative array dimension (-1000) [20] */
108 int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))];
109 
110 /* expect+2: error: cannot take size/alignment of function type 'function(void) returning double' [144] */
111 /* expect+1: error: negative array dimension (-1000) [20] */
112 int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))];
113 
114 /* expect+2: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
115 /* expect+1: error: negative array dimension (-1000) [20] */
116 int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))];
117 
118 /* expect+2: error: cannot take size/alignment of function type 'function(double) returning pointer to char' [144] */
119 /* expect+1: error: negative array dimension (-1000) [20] */
120 int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))];
121 
122 /* expect+2: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
123 /* expect+1: error: negative array dimension (-1000) [20] */
124 int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))];
125 
126 /* expect+2: error: cannot take size/alignment of function type 'function(double) returning pointer to pointer to pointer to char' [144] */
127 /* expect+1: error: negative array dimension (-1000) [20] */
128 int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))];
129 
130 
131 typedef struct {
132 	char a[1];
133 } a01;
134 
135 typedef struct {
136 	char a[4];
137 } a04;
138 
139 typedef struct {
140 	char a[8];
141 } a08;
142 
143 typedef struct {
144 	char a[32];
145 } a32;
146 
147 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */
148 /* expect+1: error: negative array dimension (-1000) [20] */
149 int unspecified_args_return_01[-1000 - (int)sizeof(a01())];
150 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */
151 /* expect+1: error: negative array dimension (-1000) [20] */
152 int unspecified_args_return_04[-1000 - (int)sizeof(a04())];
153 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */
154 /* expect+1: error: negative array dimension (-1000) [20] */
155 int unspecified_args_return_08[-1000 - (int)sizeof(a08())];
156 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */
157 /* expect+1: error: negative array dimension (-1000) [20] */
158 int unspecified_args_return_32[-1000 - (int)sizeof(a32())];
159 
160 /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a01' [144] */
161 /* expect+1: error: negative array dimension (-1000) [20] */
162 int prototype_void_return_01[-1000 - (int)sizeof(a01(void))];
163 /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a04' [144] */
164 /* expect+1: error: negative array dimension (-1000) [20] */
165 int prototype_void_return_04[-1000 - (int)sizeof(a04(void))];
166 /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a08' [144] */
167 /* expect+1: error: negative array dimension (-1000) [20] */
168 int prototype_void_return_08[-1000 - (int)sizeof(a08(void))];
169 /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a32' [144] */
170 /* expect+1: error: negative array dimension (-1000) [20] */
171 int prototype_void_return_32[-1000 - (int)sizeof(a32(void))];
172 
173 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a01) returning struct typedef a32' [144] */
174 /* expect+1: error: negative array dimension (-1000) [20] */
175 int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))];
176 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a04) returning struct typedef a32' [144] */
177 /* expect+1: error: negative array dimension (-1000) [20] */
178 int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))];
179 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a08) returning struct typedef a32' [144] */
180 /* expect+1: error: negative array dimension (-1000) [20] */
181 int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))];
182 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
183 /* expect+1: error: negative array dimension (-1000) [20] */
184 int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))];
185 
186 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a01' [144] */
187 /* expect+1: error: negative array dimension (-1000) [20] */
188 int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))];
189 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a04' [144] */
190 /* expect+1: error: negative array dimension (-1000) [20] */
191 int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))];
192 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a08' [144] */
193 /* expect+1: error: negative array dimension (-1000) [20] */
194 int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))];
195 
196 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a01) returning struct typedef a32' [144] */
197 /* expect+1: error: negative array dimension (-1000) [20] */
198 int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))];
199 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a04) returning struct typedef a32' [144] */
200 /* expect+1: error: negative array dimension (-1000) [20] */
201 int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))];
202 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a08) returning struct typedef a32' [144] */
203 /* expect+1: error: negative array dimension (-1000) [20] */
204 int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))];
205 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
206 /* expect+1: error: negative array dimension (-1000) [20] */
207 int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))];
208 
209 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a01' [144] */
210 /* expect+1: error: negative array dimension (-1000) [20] */
211 int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))];
212 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a04' [144] */
213 /* expect+1: error: negative array dimension (-1000) [20] */
214 int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))];
215 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a08' [144] */
216 /* expect+1: error: negative array dimension (-1000) [20] */
217 int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))];
218 
219 void
abstract_decl_param_list_with_attributes(void)220 abstract_decl_param_list_with_attributes(void)
221 {
222 	typedef int unspecified_parameters(void (*)() __attribute__(()));
223 	typedef int no_parameters(void (*)(void) __attribute__(()));
224 	typedef int single_parameter(void (*)(int) __attribute__(()));
225 	typedef int several_parameters(void (*)(int, int) __attribute__(()));
226 }
227