| 1 |
// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx17 -std=c++17 %s |
| 2 |
// RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx2a -std=c++2a %s |
| 3 |
|
| 4 |
#include <limits.h> |
| 5 |
|
| 6 |
#define WORD_BIT (sizeof(int) * CHAR_BIT) |
| 7 |
|
| 8 |
enum { |
| 9 |
X = 1 << 0, |
| 10 |
Y = 1 << 1, |
| 11 |
Z = 1 << 2 |
| 12 |
}; |
| 13 |
|
| 14 |
void test() { |
| 15 |
char c; |
| 16 |
|
| 17 |
c = 0 << 0; |
| 18 |
c = 0 << 1; |
| 19 |
c = 1 << 0; |
| 20 |
c = 1 << -0; |
| 21 |
c = 1 >> -0; |
| 22 |
c = 1 << -1; // expected-warning {{shift count is negative}} |
| 23 |
c = 1 >> -1; // expected-warning {{shift count is negative}} |
| 24 |
c = 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}} |
| 25 |
// expected-warning@-1 {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}} |
| 26 |
c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}} |
| 27 |
c = 1 << c; |
| 28 |
c <<= 0; |
| 29 |
c >>= 0; |
| 30 |
c <<= 1; |
| 31 |
c >>= 1; |
| 32 |
c <<= -1; // expected-warning {{shift count is negative}} |
| 33 |
c >>= -1; // expected-warning {{shift count is negative}} |
| 34 |
c <<= 999999; // expected-warning {{shift count >= width of type}} |
| 35 |
c >>= 999999; // expected-warning {{shift count >= width of type}} |
| 36 |
c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}} |
| 37 |
c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}} |
| 38 |
c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}} |
| 39 |
c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}} |
| 40 |
(void)((long)c << CHAR_BIT); |
| 41 |
|
| 42 |
int i; |
| 43 |
i = 1 << (WORD_BIT - 2); |
| 44 |
i = 2 << (WORD_BIT - 1); // cxx17-warning {{bits to represent, but 'int' only has}} |
| 45 |
i = 1 << (WORD_BIT - 1); // cxx17-warning {{sets the sign bit of the shift expression}} |
| 46 |
i = -1 << (WORD_BIT - 1); // cxx17-warning {{shifting a negative signed value is undefined}} |
| 47 |
i = -1 << 0; // cxx17-warning {{shifting a negative signed value is undefined}} |
| 48 |
i = 0 << (WORD_BIT - 1); |
| 49 |
i = (char)1 << (WORD_BIT - 2); |
| 50 |
|
| 51 |
unsigned u; |
| 52 |
u = 1U << (WORD_BIT - 1); |
| 53 |
u = 5U << (WORD_BIT - 1); |
| 54 |
|
| 55 |
long long int lli; |
| 56 |
lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}} |
| 57 |
lli = 1LL << (sizeof(long long) * CHAR_BIT - 2); |
| 58 |
} |
| 59 |
|
| 60 |
#define a 0 |
| 61 |
#define ashift 8 |
| 62 |
enum { b = (a << ashift) }; |
| 63 |
|
| 64 |
// Don't warn for negative shifts in code that is unreachable. |
| 65 |
void test_pr5544() { |
| 66 |
(void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning |
| 67 |
} |
| 68 |
|
| 69 |
void test_shift_too_much(char x) { |
| 70 |
if (0) |
| 71 |
(void) (x >> 80); // no-warning |
| 72 |
(void) (x >> 80); // expected-warning {{shift count >= width of type}} |
| 73 |
} |
| 74 |
|
| 75 |
typedef unsigned vec16 __attribute__((vector_size(16))); |
| 76 |
typedef unsigned vec8 __attribute__((vector_size(8))); |
| 77 |
|
| 78 |
void vect_shift_1(vec16 *x) { *x = *x << 4; } |
| 79 |
|
| 80 |
void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; } |
| 81 |
|
| 82 |
void vect_shift_3(vec16 *x, vec8 y) { |
| 83 |
*x = *x << y; // expected-error {{vector operands do not have the same number of elements}} |
| 84 |
} |