xref: /netbsd-src/tests/usr.bin/xlint/lint1/decl_direct_abstract.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: decl_direct_abstract.c,v 1.7 2022/06/17 18:54:53 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 /*
13  * The following tests do not use int, to avoid confusion with the implicit
14  * return type.
15  */
16 
17 char func0001(short (*)(long));
18 
19 /* GCC says 'char (*)(short int (*)(long int))' */
20 /* Clang says 'char (short (*)(long))' */
21 /* cdecl says 'function (pointer to function (long) returning short) returning char' */
22 /* expect+1: error: cannot initialize 'double' from 'pointer to function(pointer to function(long) returning short) returning char' [185] */
23 double type_of_func0001 = func0001;
24 
25 char func0002(short *(long));
26 
27 /* GCC says 'char (*)(short int * (*)(long int))' */
28 /* Clang says 'char (short *(*)(long))' */
29 /* cdecl says 'syntax error' */
30 /* FIXME: lint is wrong, it discards the 'short *' */
31 /* expect+1: error: cannot initialize 'double' from 'pointer to function(long) returning char' [185] */
32 double type_of_func0002 = func0002;
33 
34 void c99_6_7_6_example_a(int);
35 void c99_6_7_6_example_b(int *);
36 void c99_6_7_6_example_c(int *[3]);
37 void c99_6_7_6_example_d(int (*)[3]);
38 void c99_6_7_6_example_e(int (*)[*]);
39 void c99_6_7_6_example_f(int *());
40 void c99_6_7_6_example_g(int (*)(void));
41 void c99_6_7_6_example_h(int (*const[])(unsigned int, ...));
42 
43 struct incompatible {
44 	int member;
45 } x;
46 
47 /* expect+1: ... 'pointer to function(int) returning void' ... */
48 double type_of_c99_6_7_6_example_a = c99_6_7_6_example_a;
49 /* expect+1: ... 'pointer to function(pointer to int) returning void' ... */
50 double type_of_c99_6_7_6_example_b = c99_6_7_6_example_b;
51 /* expect+1: ... 'pointer to function(pointer to pointer to int) returning void' ... */
52 double type_of_c99_6_7_6_example_c = c99_6_7_6_example_c;
53 /* expect+1: ... 'pointer to function(pointer to array[3] of int) returning void' ... */
54 double type_of_c99_6_7_6_example_d = c99_6_7_6_example_d;
55 /* expect+1: ... 'pointer to function(pointer to array[unknown_size] of int) returning void' ... */
56 double type_of_c99_6_7_6_example_e = c99_6_7_6_example_e;
57 /* Wrong type before decl.c 1.256 from 2022-04-01. */
58 /* expect+1: ... 'pointer to function(pointer to function() returning pointer to int) returning void' ... */
59 double type_of_c99_6_7_6_example_f = c99_6_7_6_example_f;
60 /* expect+1: ... 'pointer to function(pointer to function(void) returning int) returning void' ... */
61 double type_of_c99_6_7_6_example_g = c99_6_7_6_example_g;
62 /* expect+1: ... 'pointer to function(pointer to const pointer to function(unsigned int, ...) returning int) returning void' ... */
63 double type_of_c99_6_7_6_example_h = c99_6_7_6_example_h;
64 
65 void int_array(int[]);
66 void int_array_3(int[3]);
67 /* supported since cgram.y 1.363 from 2021-09-14 */
68 void int_array_ast(int[*]);
69 /* expect+1: error: null dimension [17] */
70 void int_array_7_array(int[7][]);
71 void int_array_7_array_3(int[7][3]);
72 /* expect+1: error: null dimension [17] */
73 void int_array_7_array_ast(int[7][*]);
74 
75 void int_array_array(int[][7]);
76 void int_array_3_array(int[3][7]);
77 /* supported since cgram.y 1.363 from 2021-09-14 */
78 void int_array_ast_array(int[*][7]);
79 
80 /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */
81 unsigned long size_unspecified_args = sizeof(int());
82 /* FIXME: Must be 'of function', not 'of void'. */
83 /* expect+1: error: cannot take size/alignment of void [146] */
84 unsigned long size_prototype_void = sizeof(int(void));
85 /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
86 unsigned long size_prototype_unnamed = sizeof(int(double));
87 /* TODO: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
88 unsigned long size_prototype_named = sizeof(int(double dbl));
89 
90 /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */
91 /* expect+1: error: negative array dimension (-1000) [20] */
92 int size_unspecified_args_return_int[-1000 - (int)sizeof(int())];
93 
94 /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */
95 /* expect+1: error: negative array dimension (-1000) [20] */
96 int size_unspecified_args_return_char[-1000 - (int)sizeof(char())];
97 
98 /* FIXME: 'of void' must be 'of function'. */
99 /* expect+2: error: cannot take size/alignment of void [146] */
100 /* expect+1: error: negative array dimension (-1000) [20] */
101 int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))];
102 
103 /* FIXME: 'of void' must be 'of function'. */
104 /* expect+2: error: cannot take size/alignment of void [146] */
105 /* expect+1: error: negative array dimension (-1000) [20] */
106 int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))];
107 
108 /* expect+1: error: negative array dimension (-1008) [20] */
109 int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))];
110 
111 /* expect+1: error: negative array dimension (-1008) [20] */
112 int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))];
113 
114 /* expect+1: error: negative array dimension (-1008) [20] */
115 int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))];
116 
117 /* expect+1: error: negative array dimension (-1008) [20] */
118 int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))];
119 
120 
121 typedef struct {
122 	char a[1];
123 } a01;
124 
125 typedef struct {
126 	char a[4];
127 } a04;
128 
129 typedef struct {
130 	char a[8];
131 } a08;
132 
133 typedef struct {
134 	char a[32];
135 } a32;
136 
137 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */
138 /* expect+1: error: negative array dimension (-1000) [20] */
139 int unspecified_args_return_01[-1000 - (int)sizeof(a01())];
140 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */
141 /* expect+1: error: negative array dimension (-1000) [20] */
142 int unspecified_args_return_04[-1000 - (int)sizeof(a04())];
143 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */
144 /* expect+1: error: negative array dimension (-1000) [20] */
145 int unspecified_args_return_08[-1000 - (int)sizeof(a08())];
146 /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */
147 /* expect+1: error: negative array dimension (-1000) [20] */
148 int unspecified_args_return_32[-1000 - (int)sizeof(a32())];
149 
150 /* expect+2: error: cannot take size/alignment of void [146] */
151 /* expect+1: error: negative array dimension (-1000) [20] */
152 int prototype_void_return_01[-1000 - (int)sizeof(a01(void))];
153 /* expect+2: error: cannot take size/alignment of void [146] */
154 /* expect+1: error: negative array dimension (-1000) [20] */
155 int prototype_void_return_04[-1000 - (int)sizeof(a04(void))];
156 /* expect+2: error: cannot take size/alignment of void [146] */
157 /* expect+1: error: negative array dimension (-1000) [20] */
158 int prototype_void_return_08[-1000 - (int)sizeof(a08(void))];
159 /* expect+2: error: cannot take size/alignment of void [146] */
160 /* expect+1: error: negative array dimension (-1000) [20] */
161 int prototype_void_return_32[-1000 - (int)sizeof(a32(void))];
162 
163 /* expect+1: error: negative array dimension (-1001) [20] */
164 int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))];
165 /* expect+1: error: negative array dimension (-1004) [20] */
166 int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))];
167 /* expect+1: error: negative array dimension (-1008) [20] */
168 int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))];
169 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
170 /* expect+1: error: negative array dimension (-1000) [20] */
171 int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))];
172 
173 /* expect+1: error: negative array dimension (-1032) [20] */
174 int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))];
175 /* expect+1: error: negative array dimension (-1032) [20] */
176 int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))];
177 /* expect+1: error: negative array dimension (-1032) [20] */
178 int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))];
179 
180 /* expect+1: error: negative array dimension (-1001) [20] */
181 int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))];
182 /* expect+1: error: negative array dimension (-1004) [20] */
183 int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))];
184 /* expect+1: error: negative array dimension (-1008) [20] */
185 int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))];
186 /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
187 /* expect+1: error: negative array dimension (-1000) [20] */
188 int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))];
189 
190 /* expect+1: error: negative array dimension (-1032) [20] */
191 int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))];
192 /* expect+1: error: negative array dimension (-1032) [20] */
193 int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))];
194 /* expect+1: error: negative array dimension (-1032) [20] */
195 int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))];
196