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); |
| 40 | i = 2 << (WORD_BIT - 1); // expected-warning {{the promoted type of the shift expression is 'int'}} |
| 41 | i = 1 << (WORD_BIT - 1); // expected-warning {{overrides the sign bit of the promoted type of the shift expression ('int')}} |
| 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; |
| 51 | lli = INT_MIN << 2; // expected-warning {{the promoted type of the shift expression is 'int'}} |
| 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 | |