queries.c revision 1.25 1 /* $NetBSD: queries.c,v 1.25 2024/03/30 17:12:26 rillig Exp $ */
2 # 3 "queries.c"
3
4 /*
5 * Demonstrate the case-by-case queries. Unlike warnings, queries do not
6 * point to questionable code but rather to code that may be interesting to
7 * inspect manually on a case-by-case basis.
8 *
9 * Possible use cases are:
10 *
11 * Understanding how C works internally, by making the usual arithmetic
12 * conversions visible.
13 *
14 * Finding code that intentionally suppresses a regular lint warning,
15 * such as casts between arithmetic types.
16 */
17
18 /* lint1-extra-flags: -q 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 -X 351 */
19
20 typedef unsigned char u8_t;
21 typedef unsigned short u16_t;
22 typedef unsigned int u32_t;
23 typedef unsigned long long u64_t;
24 typedef signed char s8_t;
25 typedef signed short s16_t;
26 typedef signed int s32_t;
27 typedef signed long long s64_t;
28
29 typedef float f32_t;
30 typedef double f64_t;
31 typedef float _Complex c32_t;
32 typedef double _Complex c64_t;
33
34 typedef char *str_t;
35 typedef const char *cstr_t;
36 typedef volatile char *vstr_t;
37 typedef typeof(sizeof 0) size_t;
38
39 _Bool cond;
40
41 u8_t u8;
42 u16_t u16;
43 u32_t u32;
44 u64_t u64;
45
46 s8_t s8;
47 s16_t s16;
48 s32_t s32;
49 s64_t s64;
50
51 struct {
52 unsigned u8:8;
53 unsigned u9:9;
54 unsigned u10:10;
55 unsigned u32:32;
56 int s8:8;
57 int s9:9;
58 int s10:10;
59 int s32:32;
60 } bits;
61
62 f32_t f32;
63 f64_t f64;
64
65 c32_t c32;
66 c64_t c64;
67
68 char *str;
69 const char *cstr;
70 volatile char *vstr;
71
72 int
73 Q1(double dbl)
74 {
75 /* expect+1: implicit conversion from floating point 'double' to integer 'int' [Q1] */
76 return dbl;
77 }
78
79 int
80 Q2(double dbl)
81 {
82 /* expect+1: cast from floating point 'double' to integer 'int' [Q2] */
83 return (int)dbl;
84 }
85
86 // The Q3 query triggers so often that it also occurs outside this function.
87 void
88 Q3(int i, unsigned u)
89 {
90 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
91 u = i;
92
93 /* expect+1: implicit conversion changes sign from 'unsigned int' to 'int' [Q3] */
94 i = u;
95
96 /* expect+2: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
97 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned short' [Q3] */
98 u16 += u8;
99 /* expect+2: implicit conversion changes sign from 'unsigned short' to 'int' [Q3] */
100 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
101 u32 += u16;
102 }
103
104 unsigned long long
105 Q4(signed char *ptr, int i, unsigned long long ull, size_t sz)
106 {
107
108 /*
109 * For constants, the usual arithmetic conversions are usually not
110 * interesting, so omit them.
111 */
112 u32 = u32 & 0xff;
113 u32 &= 0xff;
114
115 /* expect+2: usual arithmetic conversion for '&' from 'int' to 'unsigned int' [Q4] */
116 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
117 u32 = u32 & s32;
118 /*
119 * XXX: C99 5.6.16.2 says that the usual arithmetic conversions
120 * happen for compound assignments as well.
121 */
122 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
123 u32 &= s32;
124
125 /* expect+3: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
126 /* expect+2: usual arithmetic conversion for '&' from 'int' to 'unsigned int' [Q4] */
127 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
128 u32 = u32 & u8;
129
130 s8 = ptr[sz];
131
132 /*
133 * The conversion from 'signed char' to 'int' is done by the integer
134 * promotions (C11 6.3.1.1p2), not by the usual arithmetic
135 * conversions (C11 6.3.1.8p1).
136 */
137 /* expect+2: usual arithmetic conversion for '+' from 'int' to 'unsigned long long' [Q4] */
138 /* expect+1: implicit conversion changes sign from 'int' to 'unsigned long long' [Q3] */
139 return ptr[0] + ptr[1] + i + ull;
140 }
141
142 void
143 Q5(signed char *ptr, int i)
144 {
145 if (ptr + i > ptr)
146 return;
147
148 /* expect+1: pointer addition has integer on the left-hand side [Q5] */
149 if (i + ptr > ptr)
150 return;
151
152 if (ptr[i] != '\0')
153 return;
154
155 /* expect+1: pointer addition has integer on the left-hand side [Q5] */
156 if (i[ptr] != '\0')
157 return;
158 }
159
160 void
161 Q6(int i)
162 {
163 /* expect+1: no-op cast from 'int' to 'int' [Q6] */
164 i = (int)4;
165
166 /* expect+1: no-op cast from 'int' to 'int' [Q6] */
167 i = (int)i + 1;
168 }
169
170 void *allocate(void);
171
172 void
173 Q7(void)
174 {
175
176 /* expect+2: no-op cast from '_Bool' to '_Bool' [Q6] */
177 /* expect+1: redundant cast from '_Bool' to '_Bool' before assignment [Q7] */
178 cond = (_Bool)cond;
179 cond = (_Bool)u8;
180 u8 = (u8_t)cond;
181
182 /* expect+2: no-op cast from 'unsigned char' to 'unsigned char' [Q6] */
183 /* expect+1: redundant cast from 'unsigned char' to 'unsigned char' before assignment [Q7] */
184 u8 = (u8_t)u8;
185 u8 = (u8_t)u16;
186 u8 = (u16_t)u8;
187 /* expect+1: no-op cast from 'unsigned short' to 'unsigned short' [Q6] */
188 u8 = (u16_t)u16;
189 /* expect+1: no-op cast from 'unsigned char' to 'unsigned char' [Q6] */
190 u16 = (u8_t)u8;
191 u16 = (u8_t)u16;
192 /* expect+1: redundant cast from 'unsigned char' to 'unsigned short' before assignment [Q7] */
193 u16 = (u16_t)u8;
194 /* expect+2: no-op cast from 'unsigned short' to 'unsigned short' [Q6] */
195 /* expect+1: redundant cast from 'unsigned short' to 'unsigned short' before assignment [Q7] */
196 u16 = (u16_t)u16;
197
198 /* Mixing signed and unsigned types. */
199 u8 = (u8_t)s8;
200 s8 = (s8_t)u8;
201 /* expect+1: redundant cast from 'unsigned char' to 'short' before assignment [Q7] */
202 s16 = (s16_t)u8;
203 /* expect+1: redundant cast from 'signed char' to 'short' before assignment [Q7] */
204 s16 = (s16_t)s8;
205
206
207 /*
208 * Neither GCC nor Clang accept typeof(bit-field), as that would add
209 * unnecessary complexity. Lint accepts it but silently discards the
210 * bit-field portion from the type; see dcs_add_type.
211 */
212 /* expect+1: redundant cast from 'unsigned char' to 'unsigned int' before assignment [Q7] */
213 bits.u9 = (typeof(bits.u9))u8;
214
215
216 /* expect+2: no-op cast from 'float' to 'float' [Q6] */
217 /* expect+1: redundant cast from 'float' to 'float' before assignment [Q7] */
218 f32 = (f32_t)f32;
219 f32 = (f32_t)f64;
220 f32 = (f64_t)f32;
221 /* expect+1: no-op cast from 'double' to 'double' [Q6] */
222 f32 = (f64_t)f64;
223 /* expect+1: no-op cast from 'float' to 'float' [Q6] */
224 f64 = (f32_t)f32;
225 f64 = (f32_t)f64;
226 /* expect+1: redundant cast from 'float' to 'double' before assignment [Q7] */
227 f64 = (f64_t)f32;
228 /* expect+2: no-op cast from 'double' to 'double' [Q6] */
229 /* expect+1: redundant cast from 'double' to 'double' before assignment [Q7] */
230 f64 = (f64_t)f64;
231
232
233 /* expect+2: no-op cast from 'float _Complex' to 'float _Complex' [Q6] */
234 /* expect+1: redundant cast from 'float _Complex' to 'float _Complex' before assignment [Q7] */
235 c32 = (c32_t)c32;
236 c32 = (c32_t)c64;
237 c32 = (c64_t)c32;
238 /* expect+1: no-op cast from 'double _Complex' to 'double _Complex' [Q6] */
239 c32 = (c64_t)c64;
240 /* expect+1: no-op cast from 'float _Complex' to 'float _Complex' [Q6] */
241 c64 = (c32_t)c32;
242 c64 = (c32_t)c64;
243 /* expect+1: redundant cast from 'float _Complex' to 'double _Complex' before assignment [Q7] */
244 c64 = (c64_t)c32;
245 /* expect+2: no-op cast from 'double _Complex' to 'double _Complex' [Q6] */
246 /* expect+1: redundant cast from 'double _Complex' to 'double _Complex' before assignment [Q7] */
247 c64 = (c64_t)c64;
248
249
250 /* Mixing real and complex floating point types. */
251 /* expect+1: no-op cast from 'float' to 'float' [Q6] */
252 c32 = (f32_t)f32;
253 c32 = (c32_t)f32;
254 /* expect+1: no-op cast from 'float' to 'float' [Q6] */
255 c64 = (f32_t)f32;
256 c64 = (f64_t)f32;
257 c64 = (c32_t)f32;
258 c64 = (c64_t)f32;
259
260
261 /*
262 * Converting a void pointer type to an object pointer type requires
263 * an explicit cast in C++, as it is a narrowing conversion. In C,
264 * that conversion is done implicitly.
265 */
266
267 /* expect+1: redundant cast from 'pointer to void' to 'pointer to char' before assignment [Q7] */
268 str = (char *)allocate();
269 /* expect+1: redundant cast from 'pointer to void' to 'pointer to const char' before assignment [Q7] */
270 cstr = (const char *)allocate();
271 cstr = (char *)allocate();
272
273 /* expect+2: no-op cast from 'pointer to char' to 'pointer to char' [Q6] */
274 /* expect+1: redundant cast from 'pointer to char' to 'pointer to char' before assignment [Q7] */
275 str = (str_t)str;
276 str = (str_t)cstr;
277 /* expect+1: warning: operands of '=' have incompatible pointer types to 'char' and 'const char' [128] */
278 str = (cstr_t)str;
279 /* expect+2: no-op cast from 'pointer to const char' to 'pointer to const char' [Q6] */
280 /* expect+1: warning: operands of '=' have incompatible pointer types to 'char' and 'const char' [128] */
281 str = (cstr_t)cstr;
282 /* expect+1: no-op cast from 'pointer to char' to 'pointer to char' [Q6] */
283 cstr = (str_t)str;
284 cstr = (str_t)cstr;
285 cstr = (cstr_t)str;
286 /* expect+2: no-op cast from 'pointer to const char' to 'pointer to const char' [Q6] */
287 /* expect+1: redundant cast from 'pointer to const char' to 'pointer to const char' before assignment [Q7] */
288 cstr = (cstr_t)cstr;
289
290 /* expect+2: no-op cast from 'pointer to char' to 'pointer to char' [Q6] */
291 /* expect+1: redundant cast from 'pointer to char' to 'pointer to char' before assignment [Q7] */
292 str = (str_t)str;
293 str = (str_t)vstr;
294 /* expect+1: warning: operands of '=' have incompatible pointer types to 'char' and 'volatile char' [128] */
295 str = (vstr_t)str;
296 /* expect+2: no-op cast from 'pointer to volatile char' to 'pointer to volatile char' [Q6] */
297 /* expect+1: warning: operands of '=' have incompatible pointer types to 'char' and 'volatile char' [128] */
298 str = (vstr_t)vstr;
299 /* expect+1: no-op cast from 'pointer to char' to 'pointer to char' [Q6] */
300 vstr = (str_t)str;
301 vstr = (str_t)vstr;
302 vstr = (vstr_t)str;
303 /* expect+2: no-op cast from 'pointer to volatile char' to 'pointer to volatile char' [Q6] */
304 /* expect+1: redundant cast from 'pointer to volatile char' to 'pointer to volatile char' before assignment [Q7] */
305 vstr = (vstr_t)vstr;
306 }
307
308 /*
309 * Octal numbers were common in the 1970s, especially on 36-bit machines.
310 * 50 years later, they are still used in numeric file permissions.
311 */
312 void
313 Q8(void)
314 {
315
316 u16 = 0;
317 u16 = 000000;
318 /* expect+1: octal number '0644' [Q8] */
319 u16 = 0644;
320 /* expect+1: octal number '0000644' [Q8] */
321 u16 = 0000644;
322 }
323
324 int
325 Q9(int x)
326 {
327 switch (x) {
328 case 0:
329 return 0;
330 case 1:
331 /* expect+1: parenthesized return value [Q9] */
332 return (0);
333 case 2:
334 return +(0);
335 case 3:
336 return -(13);
337 case 4:
338 /* expect+2: comma operator with types 'int' and 'int' [Q12] */
339 /* expect+1: parenthesized return value [Q9] */
340 return (0), (1);
341 case 5:
342 /* expect+2: comma operator with types 'int' and 'int' [Q12] */
343 /* expect+1: parenthesized return value [Q9] */
344 return (0, 1);
345 case 6:
346 /* expect+1: comma operator with types 'int' and 'int' [Q12] */
347 return 0, 1;
348 case 7:
349 /* expect+1: implicit conversion from floating point 'double' to integer 'int' [Q1] */
350 return 0.0;
351 case 8:
352 /* expect+2: parenthesized return value [Q9] */
353 /* expect+1: implicit conversion from floating point 'double' to integer 'int' [Q1] */
354 return (0.0);
355 case 9:
356 return
357 # 358 "queries.c" 3 4
358 ((void *)0)
359 # 360 "queries.c"
360 /* expect+1: warning: illegal combination of integer 'int' and pointer 'pointer to void' [183] */
361 ;
362 case 10:
363 /* expect+1: warning: illegal combination of integer 'int' and pointer 'pointer to void' [183] */
364 return (void *)(0);
365 default:
366 return 0;
367 }
368 }
369
370 void
371 Q10(void)
372 {
373 int a, b, c;
374
375 /* expect+2: chained assignment with '=' and '=' [Q10] */
376 /* expect+1: chained assignment with '=' and '=' [Q10] */
377 a = b = c = 0;
378
379 /* expect+2: chained assignment with '*=' and '-=' [Q10] */
380 /* expect+1: chained assignment with '+=' and '*=' [Q10] */
381 a += b *= c -= 0;
382 }
383
384 void
385 Q11(void)
386 {
387 /* expect+1: static variable 'static_var_no_init' in function [Q11] */
388 static int static_var_no_init;
389 /* expect+1: static variable 'static_var_init' in function [Q11] */
390 static int static_var_init = 1;
391
392 static_var_no_init++;
393 static_var_init++;
394 }
395
396 void
397 Q12(void)
398 {
399 /* expect+1: comma operator with types 'void' and '_Bool' [Q12] */
400 if (Q11(), cond)
401 return;
402
403 /* expect+5: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
404 /* expect+4: implicit conversion changes sign from 'int' to 'unsigned short' [Q3] */
405 /* expect+3: implicit conversion changes sign from 'unsigned short' to 'int' [Q3] */
406 /* expect+2: implicit conversion changes sign from 'int' to 'unsigned int' [Q3] */
407 /* expect+1: comma operator with types 'unsigned short' and 'unsigned int' [Q12] */
408 u16 += u8, u32 += u16;
409 }
410
411 /* expect+1: redundant 'extern' in function declaration of 'extern_Q13' [Q13] */
412 extern void extern_Q13(void);
413 void extern_Q13(void);
414 /* expect+1: redundant 'extern' in function declaration of 'extern_Q13' [Q13] */
415 extern void extern_Q13(void), *extern_ptr;
416
417 int
418 Q14(signed char sc, unsigned char uc, int wc)
419 {
420 // Plain 'char' is platform-dependent, see queries-{schar,uchar}.c.
421
422 if (sc == 'c' || sc == L'w' || sc == 92 || sc == 0)
423 return 2;
424 /* expect+4: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
425 /* expect+3: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
426 /* expect+2: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
427 /* expect+1: implicit conversion changes sign from 'unsigned char' to 'int' [Q3] */
428 if (uc == 'c' || uc == L'w' || uc == 92 || uc == 0)
429 return 3;
430 if (wc == 'c' || wc == L'w' || wc == 92 || wc == 0)
431 return 4;
432 return 5;
433 }
434
435 void *
436 Q15(void)
437 {
438 /* expect+1: implicit conversion from integer 0 to pointer 'pointer to void' [Q15] */
439 void *ptr_from_int = 0;
440 /* expect+1: implicit conversion from integer 0 to pointer 'pointer to void' [Q15] */
441 void *ptr_from_uint = 0U;
442 /* expect+1: implicit conversion from integer 0 to pointer 'pointer to void' [Q15] */
443 void *ptr_from_long = 0L;
444
445 ptr_from_int = &ptr_from_int;
446 ptr_from_uint = &ptr_from_uint;
447 ptr_from_long = &ptr_from_long;
448
449 /* expect+1: implicit conversion from integer 0 to pointer 'pointer to void' [Q15] */
450 return 0;
451 }
452
453 /*
454 * Even though C99 6.2.2p4 allows a 'static' declaration followed by a
455 * non-'static' declaration, it may look confusing.
456 */
457 static void Q16(void);
458 /* expect+3: 'Q16' was declared 'static', now non-'static' [Q16] */
459 /* expect+2: warning: static function 'Q16' unused [236] */
460 void
461 Q16(void)
462 {
463 }
464
465 /* expect+1: invisible character U+0009 in character constant [Q17] */
466 char Q17_char[] = { ' ', '\0', ' ' };
467 /* expect+1: invisible character U+0009 in string literal [Q17] */
468 char Q17_char_string[] = " \0 ";
469 /* expect+1: invisible character U+0009 in character constant [Q17] */
470 int Q17_wide[] = { L' ', L'\0', L' ' };
471 /* expect+1: invisible character U+0009 in string literal [Q17] */
472 int Q17_wide_string[] = L" \0 ";
473
474 /* For Q18, see queries_schar.c and queries_uchar.c. */
475
476 /*
477 * Since queries do not affect the exit status, force a warning to make this
478 * test conform to the general expectation that a test that produces output
479 * exits non-successfully.
480 */
481 /* expect+1: warning: static variable 'unused' unused [226] */
482 static int unused;
483