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