xref: /netbsd-src/tests/usr.bin/xlint/lint1/decl_direct_abstract.c (revision d2c16d5796af7d64c26094d6e83f5c79714a35d6)
1*d2c16d57Srillig /*	$NetBSD: decl_direct_abstract.c,v 1.12 2024/01/28 08:17:27 rillig Exp $	*/
2697a010bSrillig # 3 "decl_direct_abstract.c"
3697a010bSrillig 
4697a010bSrillig /*
5697a010bSrillig  * Test parsing of direct-abstract-declarator (C99 6.7.6), which are a tricky
6697a010bSrillig  * part of the C standard since they require lookahead and are so complicated
7697a010bSrillig  * that GCC's parser dedicates 34 lines of comments to this topic.
8697a010bSrillig  *
9697a010bSrillig  * See msg_155.c.
10697a010bSrillig  */
11697a010bSrillig 
12b2baa501Srillig /* lint1-extra-flags: -X 351 */
13b2baa501Srillig 
14697a010bSrillig struct incompatible {
15697a010bSrillig 	int member;
16697a010bSrillig } x;
17697a010bSrillig 
18600fe2a7Srillig void
c99_6_7_6_examples(void)19600fe2a7Srillig c99_6_7_6_examples(void)
20600fe2a7Srillig {
21600fe2a7Srillig 	/* expect+1: ... 'int' ... */
22600fe2a7Srillig 	x = (int)x;
23600fe2a7Srillig 	/* expect+1: ... 'pointer to int' ... */
24600fe2a7Srillig 	x = (int *)x;
25600fe2a7Srillig 	/* expect+1: ... 'array[3] of pointer to int' ... */
26600fe2a7Srillig 	x = (int *[3])x;
27600fe2a7Srillig 	/* expect+1: ... 'pointer to array[3] of int' ... */
28600fe2a7Srillig 	x = (int (*)[3])x;
29600fe2a7Srillig 	/* expect+1: ... 'pointer to array[unknown_size] of int' ... */
30600fe2a7Srillig 	x = (int (*)[*])x;
31600fe2a7Srillig 	/* expect+1: ... 'function() returning pointer to int' ... */
32600fe2a7Srillig 	x = (int *())x;
33600fe2a7Srillig 	/* expect+1: ... 'pointer to function(void) returning int' ... */
34600fe2a7Srillig 	x = (int (*)(void))x;
35600fe2a7Srillig 	/* expect+1: ... 'array[unknown_size] of const pointer to function(unsigned int, ...) returning int' ... */
36600fe2a7Srillig 	x = (int (*const[])(unsigned int, ...))x;
37600fe2a7Srillig }
38600fe2a7Srillig 
39600fe2a7Srillig void
function_returning_char(void)40600fe2a7Srillig function_returning_char(void)
41600fe2a7Srillig {
42600fe2a7Srillig 	// GCC adds a pointer, then says 'char (*)(short int (*)(long int))'.
43600fe2a7Srillig 	// Clang says 'char (short (*)(long))'.
44600fe2a7Srillig 	/* cdecl says 'function (pointer to function (long) returning short) returning char' */
455e813441Srillig 	/* expect+1: ... 'function(pointer to function(long) returning short) returning char' ... */
46600fe2a7Srillig 	x = (char(short (*)(long)))x;
47600fe2a7Srillig 
48600fe2a7Srillig 	/* expect+1: warning: nested 'extern' declaration of 'f1' [352] */
49600fe2a7Srillig 	char f1(short (*)(long));
50600fe2a7Srillig 
51600fe2a7Srillig 	/* expect+1: ... 'pointer to function(pointer to function(long) returning short) returning char' ... */
52600fe2a7Srillig 	x = f1;
53600fe2a7Srillig }
54600fe2a7Srillig 
55600fe2a7Srillig void
function_returning_pointer(void)56600fe2a7Srillig function_returning_pointer(void)
57600fe2a7Srillig {
58600fe2a7Srillig 	// GCC says 'error: cast specifies function type'.
59600fe2a7Srillig 	// Clang says 'char (short *(*)(long))'.
60*d2c16d57Srillig 	/* expect+1: ... 'function(pointer to function(long) returning pointer to short) returning char' ... */
61600fe2a7Srillig 	x = (char(short *(long)))x;
62600fe2a7Srillig 
63600fe2a7Srillig 	/* expect+1: warning: nested 'extern' declaration of 'f2' [352] */
64600fe2a7Srillig 	char f2(short *(long));
65600fe2a7Srillig 
66600fe2a7Srillig 	// GCC adds two pointers, saying 'char (*)(short int * (*)(long int))'.
67600fe2a7Srillig 	// Clang says 'char (short *(*)(long))' */
68600fe2a7Srillig 	/* cdecl says 'syntax error' */
695e813441Srillig 	/* expect+1: ... 'pointer to function(pointer to function(long) returning pointer to short) returning char' ... */
70600fe2a7Srillig 	x = f2;
71600fe2a7Srillig }
72600fe2a7Srillig 
737e781e93Srillig 
747e781e93Srillig void int_array(int[]);
757e781e93Srillig void int_array_3(int[3]);
76491a8fb9Srillig /* supported since cgram.y 1.363 from 2021-09-14 */
777e781e93Srillig void int_array_ast(int[*]);
787e781e93Srillig /* expect+1: error: null dimension [17] */
797e781e93Srillig void int_array_7_array(int[7][]);
807e781e93Srillig void int_array_7_array_3(int[7][3]);
817e781e93Srillig /* expect+1: error: null dimension [17] */
827e781e93Srillig void int_array_7_array_ast(int[7][*]);
837e781e93Srillig 
847e781e93Srillig void int_array_array(int[][7]);
857e781e93Srillig void int_array_3_array(int[3][7]);
86491a8fb9Srillig /* supported since cgram.y 1.363 from 2021-09-14 */
877e781e93Srillig void int_array_ast_array(int[*][7]);
88c9025790Srillig 
897c133271Srillig /* expect+1: error: cannot take size/alignment of function type 'function() returning int' [144] */
90c9025790Srillig unsigned long size_unspecified_args = sizeof(int());
915e813441Srillig /* expect+1: error: cannot take size/alignment of function type 'function(void) returning int' [144] */
92c9025790Srillig unsigned long size_prototype_void = sizeof(int(void));
935e813441Srillig /* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
94c9025790Srillig unsigned long size_prototype_unnamed = sizeof(int(double));
955e813441Srillig /* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
96c9025790Srillig unsigned long size_prototype_named = sizeof(int(double dbl));
977c133271Srillig 
987c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function() returning int' [144] */
997c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1007c133271Srillig int size_unspecified_args_return_int[-1000 - (int)sizeof(int())];
1017c133271Srillig 
1027c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function() returning char' [144] */
1037c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1047c133271Srillig int size_unspecified_args_return_char[-1000 - (int)sizeof(char())];
1057c133271Srillig 
1065e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning int' [144] */
1077c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1087c133271Srillig int size_prototype_void_return_int[-1000 - (int)sizeof(int(void))];
1097c133271Srillig 
1105e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning double' [144] */
1117c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1127c133271Srillig int size_prototype_void_return_double[-1000 - (int)sizeof(double(void))];
1137c133271Srillig 
1145e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
1155e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1167c133271Srillig int size_prototype_unnamed_return_int[-1000 - (int)sizeof(int(double))];
1177c133271Srillig 
1185e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning pointer to char' [144] */
1195e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1207c133271Srillig int size_prototype_unnamed_return_pchar[-1000 - (int)sizeof(char *(double))];
1217c133271Srillig 
1225e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
1235e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1247c133271Srillig int size_prototype_named_return_int[-1000 - (int)sizeof(int(double dbl))];
1257c133271Srillig 
1265e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(double) returning pointer to pointer to pointer to char' [144] */
1275e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1287c133271Srillig int size_prototype_named_return_pppchar[-1000 - (int)sizeof(char ***(double dbl))];
1297c133271Srillig 
1307c133271Srillig 
1317c133271Srillig typedef struct {
1327c133271Srillig 	char a[1];
1337c133271Srillig } a01;
1347c133271Srillig 
1357c133271Srillig typedef struct {
1367c133271Srillig 	char a[4];
1377c133271Srillig } a04;
1387c133271Srillig 
1397c133271Srillig typedef struct {
1407c133271Srillig 	char a[8];
1417c133271Srillig } a08;
1427c133271Srillig 
1437c133271Srillig typedef struct {
1447c133271Srillig 	char a[32];
1457c133271Srillig } a32;
1467c133271Srillig 
1477c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a01' [144] */
1487c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1497c133271Srillig int unspecified_args_return_01[-1000 - (int)sizeof(a01())];
1507c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a04' [144] */
1517c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1527c133271Srillig int unspecified_args_return_04[-1000 - (int)sizeof(a04())];
1537c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a08' [144] */
1547c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1557c133271Srillig int unspecified_args_return_08[-1000 - (int)sizeof(a08())];
1567c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function() returning struct typedef a32' [144] */
1577c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1587c133271Srillig int unspecified_args_return_32[-1000 - (int)sizeof(a32())];
1597c133271Srillig 
1605e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a01' [144] */
1617c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1627c133271Srillig int prototype_void_return_01[-1000 - (int)sizeof(a01(void))];
1635e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a04' [144] */
1647c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1657c133271Srillig int prototype_void_return_04[-1000 - (int)sizeof(a04(void))];
1665e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a08' [144] */
1677c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1687c133271Srillig int prototype_void_return_08[-1000 - (int)sizeof(a08(void))];
1695e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(void) returning struct typedef a32' [144] */
1707c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1717c133271Srillig int prototype_void_return_32[-1000 - (int)sizeof(a32(void))];
1727c133271Srillig 
1735e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a01) returning struct typedef a32' [144] */
1745e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1757c133271Srillig int prototype_unnamed_01_return_32[-1000 - (int)sizeof(a32(a01))];
1765e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a04) returning struct typedef a32' [144] */
1775e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1787c133271Srillig int prototype_unnamed_04_return_32[-1000 - (int)sizeof(a32(a04))];
1795e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a08) returning struct typedef a32' [144] */
1805e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1817c133271Srillig int prototype_unnamed_08_return_32[-1000 - (int)sizeof(a32(a08))];
1827c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
1837c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1847c133271Srillig int prototype_unnamed_32_return_32[-1000 - (int)sizeof(a32(a32))];
1857c133271Srillig 
1865e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a01' [144] */
1875e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1887c133271Srillig int prototype_unnamed_32_return_01[-1000 - (int)sizeof(a01(a32))];
1895e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a04' [144] */
1905e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1917c133271Srillig int prototype_unnamed_32_return_04[-1000 - (int)sizeof(a04(a32))];
1925e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a08' [144] */
1935e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1947c133271Srillig int prototype_unnamed_32_return_08[-1000 - (int)sizeof(a08(a32))];
1957c133271Srillig 
1965e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a01) returning struct typedef a32' [144] */
1975e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
1987c133271Srillig int prototype_named_01_return_32[-1000 - (int)sizeof(a32(a01 arg))];
1995e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a04) returning struct typedef a32' [144] */
2005e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
2017c133271Srillig int prototype_named_04_return_32[-1000 - (int)sizeof(a32(a04 arg))];
2025e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a08) returning struct typedef a32' [144] */
2035e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
2047c133271Srillig int prototype_named_08_return_32[-1000 - (int)sizeof(a32(a08 arg))];
2057c133271Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a32' [144] */
2067c133271Srillig /* expect+1: error: negative array dimension (-1000) [20] */
2077c133271Srillig int prototype_named_32_return_32[-1000 - (int)sizeof(a32(a32 arg))];
2087c133271Srillig 
2095e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a01' [144] */
2105e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
2117c133271Srillig int prototype_named_32_return_01[-1000 - (int)sizeof(a01(a32 arg))];
2125e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a04' [144] */
2135e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
2147c133271Srillig int prototype_named_32_return_04[-1000 - (int)sizeof(a04(a32 arg))];
2155e813441Srillig /* expect+2: error: cannot take size/alignment of function type 'function(struct typedef a32) returning struct typedef a08' [144] */
2165e813441Srillig /* expect+1: error: negative array dimension (-1000) [20] */
2177c133271Srillig int prototype_named_32_return_08[-1000 - (int)sizeof(a08(a32 arg))];
21849e867d6Srillig 
21949e867d6Srillig void
abstract_decl_param_list_with_attributes(void)22049e867d6Srillig abstract_decl_param_list_with_attributes(void)
22149e867d6Srillig {
22249e867d6Srillig 	typedef int unspecified_parameters(void (*)() __attribute__(()));
22349e867d6Srillig 	typedef int no_parameters(void (*)(void) __attribute__(()));
22449e867d6Srillig 	typedef int single_parameter(void (*)(int) __attribute__(()));
22549e867d6Srillig 	typedef int several_parameters(void (*)(int, int) __attribute__(()));
22649e867d6Srillig }
227