Home | History | Annotate | Line # | Download | only in lint1
msg_132.c revision 1.7
      1 /*	$NetBSD: msg_132.c,v 1.7 2022/04/19 22:40:13 rillig Exp $	*/
      2 # 3 "msg_132.c"
      3 
      4 // Test for message: conversion from '%s' to '%s' may lose accuracy [132]
      5 
      6 /*
      7  * NetBSD's default lint flags only include a single -a, which only flags
      8  * narrowing conversions from long.  To get warnings for all narrowing
      9  * conversions, -aa needs to be given more than once.
     10  *
     11  * https://gnats.netbsd.org/14531
     12  */
     13 
     14 /* lint1-extra-flags: -aa */
     15 
     16 typedef unsigned char u8;
     17 typedef unsigned short u16;
     18 typedef unsigned int u32;
     19 typedef unsigned long long u64;
     20 
     21 typedef signed char i8;
     22 typedef signed short i16;
     23 typedef signed int i32;
     24 typedef signed long long i64;
     25 
     26 void
     27 convert_unsigned(u8 v8, u16 v16, u32 v32, u64 v64)
     28 {
     29 	v8 = v16;		/* expect: 132 */
     30 	v8 = v32;		/* expect: 132 */
     31 	v8 = v64;		/* expect: 132 */
     32 
     33 	v16 = v8;
     34 	v16 = v32;		/* expect: 132 */
     35 	v16 = v64;		/* expect: 132 */
     36 
     37 	v32 = v8;
     38 	v32 = v16;
     39 	v32 = v64;		/* expect: 132 */
     40 
     41 	v64 = v8;
     42 	v64 = v16;
     43 	v64 = v32;
     44 }
     45 
     46 void
     47 convert_signed(i8 v8, i16 v16, i32 v32, i64 v64)
     48 {
     49 	v8 = v16;		/* expect: 132 */
     50 	v8 = v32;		/* expect: 132 */
     51 	v8 = v64;		/* expect: 132 */
     52 
     53 	v16 = v8;
     54 	v16 = v32;		/* expect: 132 */
     55 	v16 = v64;		/* expect: 132 */
     56 
     57 	v32 = v8;
     58 	v32 = v16;
     59 	v32 = v64;		/* expect: 132 */
     60 
     61 	v64 = v8;
     62 	v64 = v16;
     63 	v64 = v32;
     64 }
     65 
     66 /*
     67  * Before tree.c 1.268 from 2021-04-06, lint wrongly warned that conversion to
     68  * _Bool might lose accuracy.  C99 6.3.1.2 defines a special conversion rule
     69  * from scalar to _Bool though.
     70  */
     71 _Bool
     72 to_bool(long a, long b)
     73 {
     74 	/* seen in fp_lib.h, function wideRightShiftWithSticky */
     75 	return a | b;
     76 }
     77 
     78 /* ARGSUSED */
     79 const char *
     80 cover_build_plus_minus(const char *arr, double idx)
     81 {
     82 	/* expect+3: error: operands of '+' have incompatible types (pointer != double) [107] */
     83 	/* expect+2: warning: function 'cover_build_plus_minus' expects to return value [214] */
     84 	if (idx > 0.0)
     85 		return arr + idx;
     86 	return arr + (unsigned int)idx;
     87 }
     88 
     89 int
     90 non_constant_expression(void)
     91 {
     92 	/*
     93 	 * Even though this variable definition looks like a constant, it
     94 	 * does not fall within C's definition of an integer constant
     95 	 * expression.  Due to that, lint does not perform constant folding
     96 	 * on the expression built from this variable and thus doesn't know
     97 	 * that the conversion will always succeed.
     98 	 */
     99 	const int not_a_constant = 8;
    100 	/* expect+1: warning: conversion from 'unsigned long' to 'int' may lose accuracy [132] */
    101 	return sizeof(double) + not_a_constant * sizeof(char *);
    102 }
    103