Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / Sema / shift.c
blob1ebccb1d0b3acc798a8dea0a9b5d81ba64d9ab27
1 // RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s
3 #include <limits.h>
5 #define WORD_BIT (sizeof(int) * CHAR_BIT)
7 enum {
8 X = 1 << 0,
9 Y = 1 << 1,
10 Z = 1 << 2
13 void test(void) {
14 char c;
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);
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);
50 unsigned u;
51 u = 1U << (WORD_BIT - 1);
52 u = 5U << (WORD_BIT - 1);
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);
59 #define a 0
60 #define ashift 8
61 enum { b = (a << ashift) };
63 // Don't warn for negative shifts in code that is unreachable.
64 void test_pr5544(void) {
65 (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
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}}
74 typedef unsigned vec16 __attribute__((vector_size(16)));
75 typedef unsigned vec8 __attribute__((vector_size(8)));
77 void vect_shift_1(vec16 *x) { *x = *x << 4; }
79 void vect_shift_2(vec16 *x, vec16 y) { *x = *x << 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}}