lang_level_c99.c revision 1.4 1 /* $NetBSD: lang_level_c99.c,v 1.4 2024/06/08 09:09:20 rillig Exp $ */
2 # 3 "lang_level_c99.c"
3
4 /*
5 * Tests that are specific to the C99 language level, in particular:
6 *
7 * * syntax elements that were added in C99
8 * * lint diagnostics that differ between the C90 and C99 language levels
9 * * lint diagnostics that differ between the C99 and C11 language levels
10 */
11
12 /* lint1-flags: -S -w -X 351 */
13
14 /*
15 * Features that were added in the C99 standard, as listed in the C99 foreword.
16 *
17 * In the below comments, [-] means unsupported and [x] means supported.
18 */
19
20 // [-] restricted character set support via digraphs and <iso646.h>
21 //
22 // Lint neither parses digraphs nor trigraphs.
23
24 // [x] wide character library support in <wchar.h> and <wctype.h>
25 //
26 // On all supported platforms, 'wchar_t' == 'int'.
27
28 const int wide_string[] = L"wide";
29
30 // [x] more precise aliasing rules via effective type
31 //
32 // Irrelevant, as lint does not check the runtime behavior.
33
34 // [x] restricted pointers
35 //
36 // Can be parsed, are otherwise ignored.
37
38 // [-] variable length arrays
39 //
40 // Variable length arrays are handled as if the number of elements in the array
41 // were always 1.
42
43 /* FIXME: Parameter 'n' _is_ actually used. */
44 /* expect+2: warning: parameter 'n' unused in function 'variable_length_arrays' [231] */
45 unsigned long long
46 variable_length_arrays(int n)
47 {
48 int vla[n];
49 /* FIXME: The array dimension is not constant, but still negative. */
50 /* expect+1: error: negative array dimension (-4) [20] */
51 typedef int sizeof_vla[-(int)sizeof(vla)];
52 return sizeof(vla);
53 }
54
55 // [x] flexible array members
56 //
57 // Flexible array members are parsed but not validated thoroughly.
58
59 void
60 flexible_array_members(void)
61 {
62 struct {
63 int regular;
64 int flexible[];
65 } s = {
66 0,
67 // Flexible array member must not be initialized. Lint does
68 // not detect this, leaving the job to the C99 compiler.
69 { 1, 3, 4, }
70 };
71 /* expect+1: error: negative array dimension (-4) [20] */
72 typedef int sizeof_s[-(int)sizeof(s)];
73 }
74
75 // [x] static and type qualifiers in parameter array declarators
76 //
77 // Can be parsed, are otherwise ignored.
78
79 // [-] complex (and imaginary) support in <complex.h>
80 //
81 // Lint does not keep track of which parts of a complex object are initialized.
82 //
83 // Lint does not support '_Imaginary'.
84
85 // [x] type-generic math macros in <tgmath.h>
86 //
87 // Irrelevant, as lint only sees the preprocessed source code.
88
89 // [x] the long long int type and library functions
90 //
91 // On all platforms supported by lint, 'long long' is 64 bits wide. The other
92 // fixed-width types are 'char', 'short', 'int' and (only on 64-bit platforms)
93 // '__int128_t'.
94 //
95 // The lint standard libraries -lstdc and -lposix do not contain the
96 // functions added in C99.
97
98 /* expect+1: error: negative array dimension (-1) [20] */
99 typedef int sizeof_char[-(int)sizeof(char)];
100 /* expect+1: error: negative array dimension (-2) [20] */
101 typedef int sizeof_short[-(int)sizeof(short)];
102 /* expect+1: error: negative array dimension (-4) [20] */
103 typedef int sizeof_int[-(int)sizeof(int)];
104 /* expect+1: error: negative array dimension (-8) [20] */
105 typedef int sizeof_long_long[-(int)sizeof(long long)];
106
107 // [x] increased minimum translation limits
108 //
109 // Irrelevant, as lint does not have any hard-coded limits.
110
111 // [x] additional floating-point characteristics in <float.h>
112 //
113 // Lint has very limited support for floating point numbers, as it fully relies
114 // on the host platform. This is noticeable when cross-compiling between
115 // platforms with different size or representation of 'long double'.
116
117 // [x] remove implicit int
118 //
119 // Lint parses old-style declarations and marks them as errors.
120
121 // [x] reliable integer division
122 //
123 // The lint source code requires a C99 compiler, so when mapping the integer
124 // operations to those from the host platform, lint uses these.
125
126 // [-] universal character names (\u and \U)
127 //
128 // No, as nothing in the NetBSD source tree uses this feature.
129
130 // [-] extended identifiers
131 //
132 // No, as nothing in the NetBSD source tree uses this feature.
133
134 // [x] hexadecimal floating-point constants and %a and %A printf/scanf
135 // conversion specifiers
136
137 void pf(); /* no prototype parameters */
138
139 void
140 hexadecimal_floating_point_constants(void)
141 {
142 double hex = 0x1.0p34;
143 pf("%s %a\n", "hex", hex);
144 }
145
146 // [x] compound literals
147 //
148 // See d_c99_compound_literal_comma.c.
149
150 // [x] designated initializers
151 //
152 // See d_c99_init.c.
153
154 // [x] // comments
155 //
156 // Also supported in GCC mode.
157
158 // [?] extended integer types and library functions in <inttypes.h> and
159 // <stdint.h>
160 //
161 // TODO
162
163 // [x] remove implicit function declaration
164
165 void
166 call_implicitly_declared_function(void)
167 {
168 /* expect+1: error: function 'implicitly_declared_function' implicitly declared to return int [215] */
169 implicitly_declared_function(0);
170 }
171
172 // [x] preprocessor arithmetic done in intmax_t/uintmax_t
173 //
174 // Irrelevant, as lint only sees the preprocessed source code.
175
176 // [x] mixed declarations and code
177
178 // [x] new block scopes for selection and iteration statements
179 void
180 for_scope(void)
181 {
182 // A for loop may have a declaration in its first part.
183 for (int i = 0; i < 10; i++)
184 continue;
185
186 // Test that the scope of the previous i has ended.
187 for (int i = 0; i < 10; i++)
188 continue;
189 }
190
191
192 // [?] integer constant type rules
193 //
194 // TODO
195
196 // [?] integer promotion rules
197 //
198 // TODO
199
200 // [x] macros with a variable number of arguments
201 //
202 // Irrelevant, as lint only sees the preprocessed source code.
203
204 // [x] the vscanf family of functions in <stdio.h> and <wchar.h>
205 //
206 // Irrelevant, as typical C99 compilers already check these.
207
208 // [x] additional math library functions in <math.h>
209 //
210 // Irrelevant, as lint does not check arithmetic expressions.
211 //
212 // Lint also does not generate its own standard library definition for libm.
213
214 // [x] treatment of error conditions by math library functions
215 // (math_errhandling)
216 //
217 // Irrelevant, as lint does not check for error handling.
218
219 // [x] floating-point environment access in <fenv.h>
220 //
221 // TODO
222
223 // [x] IEC 60559 (also known as IEC 559 or IEEE arithmetic) support
224 //
225 // On platforms that conform to IEC 60559, lint performs the arithmetic
226 // operations accordingly. When cross-compiling on a vax host for other target
227 // platforms, no such support is available.
228
229 // [x] trailing comma allowed in enum declaration
230 //
231 // Yes, see the grammar rule 'enums_with_opt_comma'.
232
233 // [-] %lf conversion specifier allowed in printf
234 //
235 // TODO: see tests/lint2/msg_013.exp.
236
237 // [x] inline functions
238 //
239 // Yes, also allowed in GCC mode.
240
241 // [x] the snprintf family of functions in <stdio.h>
242 //
243 // The snprintf functions are treated like all other functions. The checks for
244 // matching format strings targets traditional C only and thus does not apply
245 // to these functions, as they have a prototype definition.
246
247 // [x] boolean type in <stdbool.h>
248 //
249 // Yes. Conversion to and from boolean follows 6.3.1.2. See also the -T flag,
250 // which enables 'strict bool mode'.
251
252 // [x] idempotent type qualifiers
253 //
254 // Lint warns about duplicate type qualifiers but accepts them otherwise.
255
256 /* expect+1: warning: duplicate 'const' [10] */
257 const const int duplicate_type_qualifier = 2;
258
259 // [x] empty macro arguments
260 //
261 // Irrelevant, as lint only sees the preprocessed source code.
262
263 // [?] new structure type compatibility rules (tag compatibility)
264 //
265 // TODO
266
267 // [x] additional predefined macro names
268 //
269 // Irrelevant, as lint only sees the preprocessed source code.
270
271 // [-] _Pragma preprocessing operator
272 //
273 // No, not yet asked for.
274
275 // [-] standard pragmas
276 //
277 // No, not yet asked for.
278
279 // [x] __func__ predefined identifier
280 //
281 // Yes, see 'fallback_symbol'.
282
283 // [x] va_copy macro
284 //
285 // Irrelevant, as lint only sees the preprocessed source code.
286
287 // [x] additional strftime conversion specifiers
288 //
289 // Irrelevant, as lint does not check strftime in depth.
290
291 // [?] LIA compatibility annex
292 //
293 // TODO
294
295 // [x] deprecate ungetc at the beginning of a binary file
296 //
297 // Irrelevant, as lint's analysis is not that deep into the runtime behavior.
298
299 // [x] remove deprecation of aliased array parameters
300 //
301 // Irrelevant, as lint does not check for aliasing.
302
303 // [?] conversion of array to pointer not limited to lvalues
304 //
305 // TODO
306
307 // [x] relaxed constraints on aggregate and union initialization
308 //
309 // Yes, struct and union members can be initialized with non-constant
310 // expressions. Members that have struct or union type can be initialized with
311 // an expression of the same type.
312
313 // [x] relaxed restrictions on portable header names
314 //
315 // Irrelevant, as lint only sees the preprocessed source code.
316
317 // [x] return without expression not permitted in function that returns a value
318 // (and vice versa)
319
320 void
321 return_no_expr(int x)
322 {
323 x++;
324 /* expect+1: error: void function 'return_no_expr' cannot return value [213] */
325 return x;
326 }
327
328 int
329 return_expr(void)
330 {
331 /* expect+1: error: function 'return_expr' expects to return value [214] */
332 return;
333 }
334