Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 1 | // RUN: %clang -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -Xclang -verify %s |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 2 | |
| 3 | #include <limits.h> |
| 4 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 5 | #define WORD_BIT (sizeof(int) * CHAR_BIT) |
| 6 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7 | enum { |
| 8 | X = 1 << 0, |
| 9 | Y = 1 << 1, |
| 10 | Z = 1 << 2 |
| 11 | }; |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 12 | |
| 13 | void test() { |
| 14 | char c; |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 15 | |
| 16 | c = 0 << 0; |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 17 | c = 0 << 1; |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 18 | c = 1 << 0; |
| 19 | c = 1 << -0; |
| 20 | c = 1 >> -0; |
| 21 | c = 1 << -1; // expected-warning {{shift count is negative}} |
| 22 | c = 1 >> -1; // expected-warning {{shift count is negative}} |
| 23 | c = 1 << c; |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 24 | c <<= 0; |
| 25 | c >>= 0; |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 26 | c <<= 1; |
| 27 | c >>= 1; |
| 28 | c <<= -1; // expected-warning {{shift count is negative}} |
| 29 | c >>= -1; // expected-warning {{shift count is negative}} |
| 30 | c <<= 999999; // expected-warning {{shift count >= width of type}} |
| 31 | c >>= 999999; // expected-warning {{shift count >= width of type}} |
| 32 | c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}} |
| 33 | c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}} |
| 34 | c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}} |
| 35 | c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}} |
| 36 | (void)((long)c << CHAR_BIT); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 37 | |
| 38 | int i; |
| 39 | i = 1 << (WORD_BIT - 2); |
Chandler Carruth | 5fa05cb | 2011-02-24 17:13:15 +0000 | [diff] [blame] | 40 | i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}} |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 41 | i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}} |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 42 | i = -1 << (WORD_BIT - 1); |
| 43 | i = 0 << (WORD_BIT - 1); |
| 44 | i = (char)1 << (WORD_BIT - 2); |
| 45 | |
| 46 | unsigned u; |
| 47 | u = 1U << (WORD_BIT - 1); |
| 48 | u = 5U << (WORD_BIT - 1); |
| 49 | |
| 50 | long long int lli; |
Chandler Carruth | 5fa05cb | 2011-02-24 17:13:15 +0000 | [diff] [blame] | 51 | lli = INT_MIN << 2; // expected-warning {{bits to represent, but 'int' only has}} |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 52 | lli = 1LL << (sizeof(long long) * CHAR_BIT - 2); |
Chris Lattner | 1dcf2c8 | 2007-12-13 07:28:16 +0000 | [diff] [blame] | 53 | } |
Ryan Flynn | 8045c73 | 2009-08-08 19:18:23 +0000 | [diff] [blame] | 54 | |
| 55 | #define a 0 |
| 56 | #define ashift 8 |
| 57 | enum { b = (a << ashift) }; |
| 58 | |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 59 | // Don't warn for negative shifts in code that is unreachable. |
| 60 | void test_pr5544() { |
| 61 | (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning |
| 62 | } |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 63 | |
| 64 | void test_shift_too_much(char x) { |
| 65 | if (0) |
| 66 | (void) (x >> 80); // no-warning |
| 67 | (void) (x >> 80); // expected-warning {{shift count >= width of type}} |
| 68 | } |