1 // RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s
5 #define WORD_BIT (sizeof(int) * CHAR_BIT)
21 c = 1 << -1; // expected-warning {{shift count is negative}}
22 c = 1 >> -1; // expected-warning {{shift count is negative}}
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);
39 i = 1 << (WORD_BIT - 2);
40 i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
41 i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}
42 i = -1 << (WORD_BIT - 1); // expected-warning {{shifting a negative signed value is undefined}}
43 i = -1 << 0; // expected-warning {{shifting a negative signed value is undefined}}
44 i = 0 << (WORD_BIT - 1);
45 i = (char)1 << (WORD_BIT - 2);
48 u = 1U << (WORD_BIT - 1);
49 u = 5U << (WORD_BIT - 1);
52 lli = INT_MIN << 2; // expected-warning {{shifting a negative signed value is undefined}}
53 lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
58 enum { b = (a << ashift) };
60 // Don't warn for negative shifts in code that is unreachable.
62 (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
65 void test_shift_too_much(char x) {
67 (void) (x >> 80); // no-warning
68 (void) (x >> 80); // expected-warning {{shift count >= width of type}}
71 typedef unsigned vec16 __attribute__((vector_size(16)));
72 typedef unsigned vec8 __attribute__((vector_size(8)));
74 void vect_shift_1(vec16 *x) { *x = *x << 4; }
76 void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; }
78 void vect_shift_3(vec16 *x, vec8 y) {
79 *x = *x << y; // expected-error {{vector operands do not have the same number of elements}}