Home | History | Annotate | Line # | Download | only in lint1
platform_ilp32_long.c revision 1.6
      1 /*	$NetBSD: platform_ilp32_long.c,v 1.6 2024/03/09 17:34:01 rillig Exp $	*/
      2 # 3 "platform_ilp32_long.c"
      3 
      4 /*
      5  * Test features that only apply to platforms that have 32-bit int, long and
      6  * pointer types and where size_t is unsigned long, not unsigned int.
      7  *
      8  * On these platforms, in portable mode (-p), the type 'int' is in some cases
      9  * assumed to be only 24 bits wide, in order to detect conversions from
     10  * 'long' (or more probably 'size_t') to 'int', which can lose accuracy.
     11  */
     12 
     13 /* lint1-only-if: ilp32 long */
     14 /* lint1-extra-flags: -c -h -a -p -b -r -z -X 351 */
     15 
     16 int s32;
     17 unsigned int u32;
     18 long sl32;
     19 unsigned long ul32;
     20 
     21 void
     22 convert_between_int_and_long(void)
     23 {
     24 	/*
     25 	 * The '-p' option enables checks that apply independently of the
     26 	 * current platform, assuming that 'long' is always wider than 'int'.
     27 	 * This assumption, when applied on its own, leads to wrong warnings
     28 	 * that a 32-bit 'long' may lose accuracy when converted to a 32-bit
     29 	 * 'int'.
     30 	 *
     31 	 * To avoid these, take a look at the actually possible values of the
     32 	 * right-hand side, and if they fit in the left-hand side, don't warn.
     33 	 */
     34 	s32 = sl32;
     35 	sl32 = s32;
     36 	u32 = ul32;
     37 	ul32 = u32;
     38 }
     39 
     40 unsigned char u8;
     41 unsigned long long u64;
     42 unsigned char u8_buf[20];
     43 unsigned long long u64_buf[20];
     44 
     45 void
     46 array_index(void)
     47 {
     48 
     49 	/* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */
     50 	u8 += u8_buf[0x00ffffff];
     51 	/* expect+1: warning: array subscript cannot be > 19: 2147483647 [168] */
     52 	u8 += u8_buf[0x7fffffff];
     53 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
     54 	/* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */
     55 	u8 += u8_buf[2147483648];
     56 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     57 	/* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */
     58 	u8 += u8_buf[0x80000000];
     59 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     60 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     61 	u8 += u8_buf[0xffffffff];
     62 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     63 	/* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */
     64 	u8 += u8_buf[0x80000000];
     65 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     66 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     67 	u8 += u8_buf[0xffffffff];
     68 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
     69 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     70 	u8 += u8_buf[0x00ffffffffffffff];
     71 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     72 	u8 += u8_buf[0xffffffffffffffff];
     73 
     74 	/* expect+1: warning: array subscript cannot be > 19: 16777215 [168] */
     75 	u64 += u64_buf[0x00ffffff];
     76 	/* expect+2: warning: operator '*' produces integer overflow [141] */
     77 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     78 	u64 += u64_buf[0x7fffffff];
     79 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
     80 	/* expect+1: warning: operator '*' produces integer overflow [141] */
     81 	u64 += u64_buf[2147483648];
     82 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     83 	/* expect+1: warning: operator '*' produces integer overflow [141] */
     84 	u64 += u64_buf[0x80000000];
     85 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     86 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     87 	u64 += u64_buf[0xffffffff];
     88 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     89 	/* expect+1: warning: operator '*' produces integer overflow [141] */
     90 	u64 += u64_buf[0x80000000];
     91 	/* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
     92 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     93 	u64 += u64_buf[0xffffffff];
     94 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
     95 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     96 	u64 += u64_buf[0x00ffffffffffffff];
     97 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
     98 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
     99 	u64 += u64_buf[0x0fffffffffffffff];
    100 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
    101 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
    102 	u64 += u64_buf[0x1fffffffffffffff];
    103 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
    104 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
    105 	u64 += u64_buf[0x3fffffffffffffff];
    106 	/* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
    107 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
    108 	u64 += u64_buf[0x7fffffffffffffff];
    109 	/* expect+1: warning: array subscript cannot be negative: -1 [167] */
    110 	u64 += u64_buf[0xffffffffffffffff];
    111 }
    112