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