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