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