xref: /netbsd-src/tests/usr.bin/xlint/lint1/parse_type_name.c (revision dcb32fd76a6d98c4a9af231446a80d630943641c)
1 /*	$NetBSD: parse_type_name.c,v 1.12 2023/08/26 10:43:53 rillig Exp $	*/
2 # 3 "parse_type_name.c"
3 
4 /*
5  * Test parsing of the grammar rule 'type_name', which among others appears
6  * in the expression 'sizeof(type_name)'.
7  */
8 
9 /* lint1-extra-flags: -X 351 */
10 
11 void sink(unsigned long);
12 
13 void
cover_type_name(void)14 cover_type_name(void)
15 {
16 	/* cover 'abstract_declaration' */
17 	sink(sizeof(int));
18 }
19 
20 void
cover_abstract_declaration(void)21 cover_abstract_declaration(void)
22 {
23 	/* cover 'qualifier_list' */
24 	/* missing type-specifier, even in traditional C */
25 	/* lint doesn't care since this is caught by the compiler */
26 	sink(sizeof(const));
27 
28 	/* cover 'specifier_qualifier_list' */
29 	sink(sizeof(double));
30 
31 	/* cover 'qualifier_list abstract_declarator' */
32 	/* XXX: This is nonsense, lint should not accept it. */
33 	sink(sizeof(const[3]));
34 
35 	/* cover 'specifier_qualifier_list abstract_declarator' */
36 	sink(sizeof(const int[3]));
37 	sink(sizeof(int const[3]));
38 }
39 
40 void
cover_abstract_declarator(void)41 cover_abstract_declarator(void)
42 {
43 	/* cover 'pointer' */
44 	sink(sizeof(int ***));
45 
46 	/* cover 'direct_abstract_declarator' */
47 	sink(sizeof(int[3]));
48 
49 	/* cover 'pointer direct_abstract_declarator' */
50 	sink(sizeof(int **[3]));
51 
52 	/* cover 'T_TYPEOF cast_expression' */
53 	/* expect+1: error: cannot take size/alignment of function type 'function(int) returning int' [144] */
54 	sink(sizeof(int(typeof(12345))));
55 }
56 
57 void
cover_direct_abstract_declarator(void)58 cover_direct_abstract_declarator(void)
59 {
60 	/* cover 'T_LPAREN abstract_declarator T_RPAREN' */
61 	sink(sizeof(int (*)));
62 
63 	/* cover 'T_LBRACK T_RBRACK' */
64 	sink(sizeof(int[]));
65 
66 	/* cover 'T_LBRACK array_size T_RBRACK' */
67 	sink(sizeof(int[3]));
68 
69 	/* cover 'type_attribute direct_abstract_declarator' */
70 	sink(sizeof(int *__attribute__(())[3]));
71 
72 	/* cover 'direct_abstract_declarator T_LBRACK T_RBRACK' */
73 	/* expect+1: error: null dimension [17] */
74 	sink(sizeof(int[3][]));
75 
76 	/* cover 'direct_abstract_declarator T_LBRACK T_ASTERISK T_RBRACK' */
77 	/* expect+1: error: null dimension [17] */
78 	sink(sizeof(int[3][ *]));
79 
80 	/* cover 'direct_abstract_declarator T_LBRACK array_size T_RBRACK' */
81 	sink(sizeof(int[3][5][8]));
82 
83 	/* cover 'abstract_decl_param_list asm_or_symbolrename_opt' */
84 	/* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
85 	sink(sizeof(int(double)));
86 	/* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
87 	sink(sizeof(int(double) __asm("anything")));
88 	/* expect+1: error: cannot take size/alignment of function type 'function(double) returning int' [144] */
89 	sink(sizeof(int(double) __symbolrename(alias)));
90 
91 	/* cover 'direct_abstract_declarator abstract_decl_param_list asm_or_symbolrename_opt' */
92 	sink(sizeof(int (*)(double)));
93 	sink(sizeof(int (*)(double) __asm("anything")));
94 	sink(sizeof(int (*)(double)__symbolrename(alias)));
95 
96 	/* cover 'direct_abstract_declarator type_attribute_list' */
97 	sink(sizeof(int (*) __attribute__(())));
98 	sink(sizeof(int (*) __attribute__(()) __attribute__(())));
99 }
100 
101 void
cover_abstract_decl_param_list(void)102 cover_abstract_decl_param_list(void)
103 {
104 	/* cover 'abstract_decl_lparen T_RPAREN type_attribute_opt' */
105 	sink(sizeof(void (*)()));
106 	sink(sizeof(void (*)() __attribute__(())));
107 	/*
108 	 * XXX: The grammar allows only a single type_attribute_opt.
109 	 * All following __attribute__ come from direct_abstract_declarator.
110 	 */
111 	sink(sizeof(void (*)() __attribute__(()) __attribute__(())));
112 
113 	/* cover 'abstract_decl_lparen vararg_parameter_type_list T_RPAREN type_attribute_opt' */
114 	sink(sizeof(void (*)(void) __attribute__(())));
115 	/*
116 	 * XXX: The grammar allows only a single type_attribute_opt.
117 	 * All following __attribute__ come from direct_abstract_declarator.
118 	 */
119 	sink(sizeof(void (*)(void) __attribute__(()) __attribute__(())));
120 
121 	/* cover 'abstract_decl_lparen error T_RPAREN type_attribute_opt' */
122 	/* expect+1: error: syntax error 'goto' [249] */
123 	sink(sizeof(void (*)(goto)));
124 	/* expect+1: error: syntax error 'goto' [249] */
125 	sink(sizeof(void (*)(goto) __attribute__(())));
126 	/*
127 	 * XXX: The grammar allows only a single type_attribute_opt.
128 	 * All following __attribute__ come from direct_abstract_declarator.
129 	 */
130 	/* expect+1: error: syntax error 'goto' [249] */
131 	sink(sizeof(void (*)(goto) __attribute__(()) __attribute__(())));
132 }
133 
134 void
cover_vararg_parameter_type_list(void)135 cover_vararg_parameter_type_list(void)
136 {
137 	/* cover 'parameter_type_list' */
138 	sink(sizeof(void (*)(double)));
139 
140 	/* cover 'parameter_type_list T_COMMA T_ELLIPSIS' */
141 	sink(sizeof(void (*)(double, ...)));
142 
143 	/* cover 'T_ELLIPSIS' */
144 	/* expect+1: warning: C90 to C17 require formal parameter before '...' [84] */
145 	sink(sizeof(void (*)(...)));
146 }
147 
148 void
cover_parameter_type_list(void)149 cover_parameter_type_list(void)
150 {
151 	/* cover 'parameter_declaration' */
152 	sink(sizeof(void (*)(double)));
153 
154 	/* cover 'parameter_type_list T_COMMA parameter_declaration' */
155 	sink(sizeof(void (*)(double, double, double, char *)));
156 }
157 
158 void
cover_parameter_declaration(void)159 cover_parameter_declaration(void)
160 {
161 	/* cover 'declmods' */
162 	/* GCC 11 warns: type defaults to 'int' in type name */
163 	sink(sizeof(void (*)(int, const)));
164 
165 	/* cover 'declaration_specifiers' */
166 	sink(sizeof(void (*)(int, double)));
167 
168 	/* cover 'declmods notype_param_declarator' */
169 	/* GCC 11 warns: type defaults to 'int' in declaration of 'x' */
170 	sink(sizeof(void (*)(int, const x)));
171 
172 	/* cover 'begin_type_declaration_specifiers end_type type_param_declarator' */
173 	sink(sizeof(void (*)(int, double x)));
174 
175 	/* cover 'begin_type_declmods end_type abstract_declarator' */
176 	/* GCC 11 warns: type defaults to 'int' in type name */
177 	sink(sizeof(void (*)(int, const *)));
178 
179 	/* cover 'begin_type_declaration_specifiers end_type abstract_declarator' */
180 	sink(sizeof(void (*)(int, double *)));
181 }
182