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