expr_fold.c revision 1.12
1/* $NetBSD: expr_fold.c,v 1.12 2024/03/09 23:55:11 rillig Exp $ */ 2# 3 "expr_fold.c" 3 4/* 5 * Test folding of constant expressions. 6 */ 7 8/* lint1-extra-flags: -h -X 351 */ 9 10/* 11 * On ILP32 platforms, the integer constant 2147483648 cannot be represented 12 * as 'int' or 'long', therefore it becomes 'long long'. This would 13 * influence the type names in the diagnostics. 14 */ 15/* lint1-only-if: lp64 */ 16 17void take_bool(_Bool); 18void take_int(int); 19void take_uint(unsigned int); 20 21/* 22 * C99 6.4.4.1p5 defines that decimal integer constants without suffix get 23 * one of the signed integer types. On the other hand, octal and hexadecimal 24 * constants get either a signed or unsigned type, whichever fits first. 25 */ 26 27void 28fold_uplus(void) 29{ 30 take_int(+(0)); 31 take_int(+(2147483647)); 32 /* expect+1: warning: conversion of 'long' to 'int' is out of range, arg #1 [295] */ 33 take_int(+(2147483648)); 34 /* expect+1: warning: conversion of 'long' to 'int' is out of range, arg #1 [295] */ 35 take_int(+(4294967295)); 36 37 take_uint(+(0)); 38 take_uint(+(2147483647)); 39 take_uint(+(2147483648)); 40 take_uint(+(4294967295)); 41 42 /* 43 * Hexadecimal constants and constants with the suffix 'U' get either 44 * a signed or an unsigned integer type, so no warning here. 45 */ 46 take_uint(+(2147483648U)); 47 take_uint(+(0x80000000)); 48 take_uint(+(0x80000000U)); 49} 50 51void 52fold_uminus(void) 53{ 54 take_int(-(0)); 55 take_int(-(2147483647)); 56 57 take_int(-(2147483648)); 58 59 /* The '-' is an operator, it is not part of the integer constant. */ 60 take_int(-2147483648); 61 62 /* expect+2: warning: operator '+' produces integer overflow [141] */ 63 /* expect+1: warning: operator '-' produces integer overflow [141] */ 64 take_int(-(2147483647 + 1)); 65 /* expect+1: warning: operator '-' produces integer overflow [141] */ 66 take_int(-(-2147483647 - 1)); 67 /* expect+1: warning: conversion of 'long' to 'int' is out of range, arg #1 [295] */ 68 take_int(-(4294967295)); 69 70 take_uint(-(0)); 71 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 72 take_uint(-(2147483647)); 73 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 74 take_uint(-(2147483648)); 75 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 76 take_uint(-(4294967295)); 77} 78 79void 80fold_compl(void) 81{ 82 take_int(~(0)); 83 take_int(~(2147483647)); 84 /* expect+1: warning: conversion of 'long' to 'int' is out of range, arg #1 [295] */ 85 take_int(~(2147483648)); 86 /* expect+1: warning: conversion of 'long' to 'int' is out of range, arg #1 [295] */ 87 take_int(~(4294967295)); 88 89 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 90 take_uint(~(0)); 91 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 92 take_uint(~(2147483647)); 93 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 94 take_uint(~(2147483648)); 95 /* expect+1: warning: conversion of negative constant to unsigned type, arg #1 [296] */ 96 take_uint(~(4294967295)); 97} 98 99void 100fold_mult(void) 101{ 102 take_int(32767 * 65536); 103 /* expect+1: warning: operator '*' produces integer overflow [141] */ 104 take_int(32768 * 65536); 105 /* expect+1: warning: operator '*' produces integer overflow [141] */ 106 take_int(65536 * 65536); 107 108 take_uint(32767 * 65536U); 109 take_uint(32768 * 65536U); 110 /* expect+1: warning: operator '*' produces integer overflow [141] */ 111 take_uint(65536 * 65536U); 112} 113 114void 115fold_div(void) 116{ 117 /* expect+1: error: division by 0 [139] */ 118 take_int(0 / 0); 119 120 /* expect+1: warning: conversion of 'long' to 'int' is out of range, arg #1 [295] */ 121 take_int(-2147483648 / -1); 122} 123 124void 125fold_mod(void) 126{ 127 /* expect+1: error: modulus by 0 [140] */ 128 take_int(0 % 0); 129 /* expect+1: error: modulus by 0 [140] */ 130 take_int(0 % 0U); 131 /* expect+1: error: modulus by 0 [140] */ 132 take_int(0U % 0); 133 /* expect+1: error: modulus by 0 [140] */ 134 take_int(0U % 0U); 135 136 take_int(-2147483648 % -1); 137} 138 139void 140fold_plus(void) 141{ 142 /* expect+1: warning: operator '+' produces integer overflow [141] */ 143 take_int(2147483647 + 1); 144 145 /* Assume two's complement, so no overflow. */ 146 take_int(-2147483647 + -1); 147 148 /* expect+1: warning: operator '+' produces integer overflow [141] */ 149 take_int(-2147483647 + -2); 150 151 /* 152 * No overflow since one of the operands is unsigned, therefore the 153 * other operand is converted to unsigned as well. 154 * See C99 6.3.1.8p1, paragraph 8 of 10. 155 */ 156 /* wrong integer overflow warning before tree.c 1.338 from 2021-08-19 */ 157 take_uint(2147483647 + 1U); 158 /* wrong integer overflow warning before tree.c 1.338 from 2021-08-19 */ 159 take_uint(2147483647U + 1); 160} 161 162void 163fold_minus(void) 164{ 165 /* expect+1: warning: operator '-' produces integer overflow [141] */ 166 take_int(2147483647 - -1); 167 /* Assume two's complement. */ 168 take_int(-2147483647 - 1); 169 /* expect+1: warning: operator '-' produces integer overflow [141] */ 170 take_int(-2147483647 - 2); 171 172 take_int(0 - 2147483648); 173 /* expect+1: warning: operator '-' produces integer overflow [141] */ 174 take_uint(0 - 2147483648U); 175} 176 177void 178fold_shl(void) 179{ 180 /* expect+1: warning: operator '<<' produces integer overflow [141] */ 181 take_int(1 << 24 << 24); 182 183 /* expect+1: warning: operator '<<' produces integer overflow [141] */ 184 take_uint(1U << 24 << 24); 185 186 /* expect+1: warning: shift amount 104 is greater than bit-size 32 of 'unsigned int' [122] */ 187 take_uint(1U << 24 << 104); 188} 189 190void 191fold_shr(void) 192{ 193 take_int(16777216 >> 24); 194 195 take_int(16777216 >> 25); 196 197 /* expect+1: warning: shift amount 104 is greater than bit-size 32 of 'int' [122] */ 198 take_int(16777216 >> 104); 199} 200 201void 202fold_lt(void) 203{ 204 take_bool(1 < 3); 205 take_bool(3 < 1); 206} 207 208void 209fold_le(void) 210{ 211 take_bool(1 <= 3); 212 take_bool(3 <= 1); 213} 214 215void 216fold_ge(void) 217{ 218 take_bool(1 >= 3); 219 take_bool(3 >= 1); 220} 221 222void 223fold_gt(void) 224{ 225 take_bool(1 > 3); 226 take_bool(3 > 1); 227} 228 229void 230fold_eq(void) 231{ 232 take_bool(1 == 3); 233 take_bool(3 == 1); 234} 235 236void 237fold_ne(void) 238{ 239 take_bool(1 != 3); 240 take_bool(3 != 1); 241} 242 243void 244fold_bitand(void) 245{ 246 take_bool(1 & 3); 247 take_bool(3 & 1); 248} 249 250void 251fold_bitxor(void) 252{ 253 take_bool(1 ^ 3); 254 take_bool(3 ^ 1); 255} 256 257void 258fold_bitor(void) 259{ 260 take_bool(1 | 3); 261 take_bool(3 | 1); 262} 263 264/* 265 * The following expression originated in vndcompress.c 1.29 from 2017-07-29, 266 * where line 310 contained a seemingly harmless compile-time assertion that 267 * expanded to a real monster expression. 268 * 269 * __CTASSERT(MUL_OK(uint64_t, MAX_N_BLOCKS, MAX_BLOCKSIZE)); 270 * 271 * Before tree.c 1.345 from 2021-08-22, lint wrongly assumed that the result 272 * of all binary operators were the common arithmetic type, but that was 273 * wrong for the comparison operators. The expression '1ULL < 2ULL' does not 274 * have type 'unsigned long long' but 'int' in default mode, or '_Bool' in 275 * strict bool mode. 276 */ 277struct ctassert5_struct { 278 unsigned int member: 279 /*CONSTCOND*/ 280 0xfffffffeU 281 <= 282 ((1ULL << 63) + 1 < 1 ? ~(1ULL << 63) : ~0ULL) / 0xfffffe00U 283 ? 1 284 : -1; 285}; 286 287/* 288 * Since Makefile.inc 1.21 from 2022-04-08 (which added -ftrapv) and before 289 * tree.c 1.436 from 2022-04-20, lint crashed with an integer overflow when 290 * calculating '-(uint64_t)INT64_MIN' in val_t.u.integer. 291 */ 292void 293unary_minus_overflow(unsigned long long val) 294{ 295 /* expect+1: warning: operator '-' produces integer overflow [141] */ 296 if (val > -(unsigned long long)(-0x7fffffffffffffffL - 1)) 297 return; 298} 299