platform_ilp32_long.c revision 1.5 1 1.5 rillig /* $NetBSD: platform_ilp32_long.c,v 1.5 2024/03/09 16:47:09 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.5 rillig char ch;
41 1.5 rillig
42 1.5 rillig void
43 1.5 rillig array_index(void)
44 1.5 rillig {
45 1.5 rillig static char buf[20];
46 1.5 rillig
47 1.5 rillig /* expect+1: warning: array subscript cannot be > 19: 2147483647 [168] */
48 1.5 rillig ch += buf[2147483647];
49 1.5 rillig /* expect+2: warning: conversion of 'long long' to 'long' is out of range [119] */
50 1.5 rillig /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */
51 1.5 rillig ch += buf[2147483648];
52 1.5 rillig /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
53 1.5 rillig /* expect+1: warning: array subscript cannot be negative: -2147483648 [167] */
54 1.5 rillig ch += buf[0x80000000];
55 1.5 rillig /* expect+2: warning: conversion of 'unsigned int' to 'long' is out of range [119] */
56 1.5 rillig /* expect+1: warning: array subscript cannot be negative: -1 [167] */
57 1.5 rillig ch += buf[0xffffffff];
58 1.5 rillig /* expect+1: warning: array subscript cannot be negative: -1 [167] */
59 1.5 rillig ch += buf[0xffffffffffffffff];
60 1.5 rillig }
61