|
| 1 | +typedef unsigned char uint8_t; |
| 2 | +typedef signed char int8_t; |
| 3 | +typedef unsigned uint32_t; |
| 4 | +typedef signed long long int64_t; |
| 5 | + |
| 6 | +void test_assign_operator(uint8_t x) { |
| 7 | + x &= 7; // [0 .. 7] |
| 8 | +} |
| 9 | + |
| 10 | +void test_non_negative_const(uint8_t x) { |
| 11 | + uint8_t unsigned_const = 7; |
| 12 | + |
| 13 | + // Non-negative range operand and non-negative constant. The operands are promoted |
| 14 | + // to signed ints. |
| 15 | + x & 0; // [0 .. 0] |
| 16 | + x & 7; // [0 .. 7] |
| 17 | + x & unsigned_const; // [0 .. 7] |
| 18 | + |
| 19 | + // This tests what happens when both arguments are promoted to `unsigned int` instead |
| 20 | + // of `int`, and when the constant is larger than the max bound |
| 21 | + x & 0xFFFFFFFF; // [0 .. 255] |
| 22 | +} |
| 23 | + |
| 24 | +void test_non_const(uint8_t a, uint8_t b, uint32_t c, uint32_t d) { |
| 25 | + if (b <= 100) { |
| 26 | + // `a` and `b` are promoted to signed ints, meaning neither the range analysis library |
| 27 | + // nor this extension handle it |
| 28 | + a & b; // [-2147483648 .. 2147483647] |
| 29 | + } |
| 30 | + if (d <= 100) { |
| 31 | + // Handled by the range analysis library |
| 32 | + c & d; // [0 .. 100] |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void test_negative_operand(uint8_t x, int8_t y) { |
| 37 | + uint8_t unsigned_const = 7; |
| 38 | + int8_t signed_const = -7; |
| 39 | + |
| 40 | + // The right operand can be negative |
| 41 | + x & -7; // [-2147483648 .. 2147483647] |
| 42 | + x & signed_const; // [-2147483648 .. 2147483647] |
| 43 | + x & y; // [-2147483648 .. 2147483647] |
| 44 | + |
| 45 | + // The left operand can be negative |
| 46 | + y & 7; // [-2147483648 .. 2147483647] |
| 47 | + y & unsigned_const; // [-2147483648 .. 2147483647] |
| 48 | + y & 0xFFFFFFFF; // [0 .. 4294967295] |
| 49 | + (int64_t)y & 0xFFFFFFFF; // [-9223372036854776000 .. 9223372036854776000] |
| 50 | + y & x; // [-2147483648 .. 2147483647] |
| 51 | + |
| 52 | + // Both can be negative |
| 53 | + y & -7; // [-2147483648 .. 2147483647] |
| 54 | + y & signed_const; // [-2147483648 .. 2147483647] |
| 55 | + signed_const & -7; // [-2147483648 .. 2147483647] |
| 56 | + signed_const & y; // [-2147483648 .. 2147483647] |
| 57 | + -7 & y; // [-2147483648 .. 2147483647] |
| 58 | + -7 & signed_const; // [-2147483648 .. 2147483647] |
| 59 | +} |
0 commit comments