xref: /minix3/external/bsd/llvm/dist/clang/test/Sema/shift.c (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -Xclang -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc #include <limits.h>
4*f4a2713aSLionel Sambuc 
5*f4a2713aSLionel Sambuc #define WORD_BIT (sizeof(int) * CHAR_BIT)
6*f4a2713aSLionel Sambuc 
7*f4a2713aSLionel Sambuc enum {
8*f4a2713aSLionel Sambuc   X = 1 << 0,
9*f4a2713aSLionel Sambuc   Y = 1 << 1,
10*f4a2713aSLionel Sambuc   Z = 1 << 2
11*f4a2713aSLionel Sambuc };
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc void test() {
14*f4a2713aSLionel Sambuc   char c;
15*f4a2713aSLionel Sambuc 
16*f4a2713aSLionel Sambuc   c = 0 << 0;
17*f4a2713aSLionel Sambuc   c = 0 << 1;
18*f4a2713aSLionel Sambuc   c = 1 << 0;
19*f4a2713aSLionel Sambuc   c = 1 << -0;
20*f4a2713aSLionel Sambuc   c = 1 >> -0;
21*f4a2713aSLionel Sambuc   c = 1 << -1; // expected-warning {{shift count is negative}}
22*f4a2713aSLionel Sambuc   c = 1 >> -1; // expected-warning {{shift count is negative}}
23*f4a2713aSLionel Sambuc   c = 1 << c;
24*f4a2713aSLionel Sambuc   c <<= 0;
25*f4a2713aSLionel Sambuc   c >>= 0;
26*f4a2713aSLionel Sambuc   c <<= 1;
27*f4a2713aSLionel Sambuc   c >>= 1;
28*f4a2713aSLionel Sambuc   c <<= -1; // expected-warning {{shift count is negative}}
29*f4a2713aSLionel Sambuc   c >>= -1; // expected-warning {{shift count is negative}}
30*f4a2713aSLionel Sambuc   c <<= 999999; // expected-warning {{shift count >= width of type}}
31*f4a2713aSLionel Sambuc   c >>= 999999; // expected-warning {{shift count >= width of type}}
32*f4a2713aSLionel Sambuc   c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}}
33*f4a2713aSLionel Sambuc   c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}}
34*f4a2713aSLionel Sambuc   c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
35*f4a2713aSLionel Sambuc   c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
36*f4a2713aSLionel Sambuc   (void)((long)c << CHAR_BIT);
37*f4a2713aSLionel Sambuc 
38*f4a2713aSLionel Sambuc   int i;
39*f4a2713aSLionel Sambuc   i = 1 << (WORD_BIT - 2);
40*f4a2713aSLionel Sambuc   i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
41*f4a2713aSLionel Sambuc   i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}
42*f4a2713aSLionel Sambuc   i = -1 << (WORD_BIT - 1);
43*f4a2713aSLionel Sambuc   i = 0 << (WORD_BIT - 1);
44*f4a2713aSLionel Sambuc   i = (char)1 << (WORD_BIT - 2);
45*f4a2713aSLionel Sambuc 
46*f4a2713aSLionel Sambuc   unsigned u;
47*f4a2713aSLionel Sambuc   u = 1U << (WORD_BIT - 1);
48*f4a2713aSLionel Sambuc   u = 5U << (WORD_BIT - 1);
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc   long long int lli;
51*f4a2713aSLionel Sambuc   lli = INT_MIN << 2; // expected-warning {{bits to represent, but 'int' only has}}
52*f4a2713aSLionel Sambuc   lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
53*f4a2713aSLionel Sambuc }
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc #define a 0
56*f4a2713aSLionel Sambuc #define ashift 8
57*f4a2713aSLionel Sambuc enum { b = (a << ashift) };
58*f4a2713aSLionel Sambuc 
59*f4a2713aSLionel Sambuc // Don't warn for negative shifts in code that is unreachable.
60*f4a2713aSLionel Sambuc void test_pr5544() {
61*f4a2713aSLionel Sambuc   (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
62*f4a2713aSLionel Sambuc }
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc void test_shift_too_much(char x) {
65*f4a2713aSLionel Sambuc   if (0)
66*f4a2713aSLionel Sambuc     (void) (x >> 80); // no-warning
67*f4a2713aSLionel Sambuc   (void) (x >> 80); // expected-warning {{shift count >= width of type}}
68*f4a2713aSLionel Sambuc }
69