Home | History | Annotate | Download | only in Sema
      1 // RUN: %clang -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -Xclang -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 
     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 << c;
     24   c <<= 0;
     25   c >>= 0;
     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);
     37 
     38   int i;
     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);
     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 {{bits to represent, but 'int' only has}}
     52   lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
     53 }
     54 
     55 #define a 0
     56 #define ashift 8
     57 enum { b = (a << ashift) };
     58 
     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 }
     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 }
     69