1 // RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx17 -std=c++17 %s
2 // RUN: %clang_cc1 -Wall -Wno-unused-but-set-variable -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify=expected,cxx2a -std=c++2a %s
6 #define WORD_BIT (sizeof(int) * CHAR_BIT)
22 c
= 1 << -1; // expected-warning {{shift count is negative}}
23 c
= 1 >> -1; // expected-warning {{shift count is negative}}
24 c
= 1 << (unsigned)-1; // expected-warning {{shift count >= width of type}}
25 // expected-warning@-1 {{implicit conversion}}
26 c
= 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}
32 c
<<= -1; // expected-warning {{shift count is negative}}
33 c
>>= -1; // expected-warning {{shift count is negative}}
34 c
<<= 999999; // expected-warning {{shift count >= width of type}}
35 c
>>= 999999; // expected-warning {{shift count >= width of type}}
36 c
<<= CHAR_BIT
; // expected-warning {{shift count >= width of type}}
37 c
>>= CHAR_BIT
; // expected-warning {{shift count >= width of type}}
38 c
<<= CHAR_BIT
+1; // expected-warning {{shift count >= width of type}}
39 c
>>= CHAR_BIT
+1; // expected-warning {{shift count >= width of type}}
40 (void)((long)c
<< CHAR_BIT
);
43 i
= 1 << (WORD_BIT
- 2);
44 i
= 2 << (WORD_BIT
- 1); // cxx17-warning {{bits to represent, but 'int' only has}}
45 i
= 1 << (WORD_BIT
- 1); // cxx17-warning {{sets the sign bit of the shift expression}}
46 i
= -1 << (WORD_BIT
- 1); // cxx17-warning {{shifting a negative signed value is undefined}}
47 i
= -1 << 0; // cxx17-warning {{shifting a negative signed value is undefined}}
48 i
= 0 << (WORD_BIT
- 1);
49 i
= (char)1 << (WORD_BIT
- 2);
52 u
= 1U << (WORD_BIT
- 1);
53 u
= 5U << (WORD_BIT
- 1);
56 lli
= INT_MIN
<< 2; // cxx17-warning {{shifting a negative signed value is undefined}}
57 lli
= 1LL << (sizeof(long long) * CHAR_BIT
- 2);
62 enum { b
= (a
<< ashift
) };
64 // Don't warn for negative shifts in code that is unreachable.
66 (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
69 void test_shift_too_much(char x
) {
71 (void) (x
>> 80); // no-warning
72 (void) (x
>> 80); // expected-warning {{shift count >= width of type}}
75 typedef unsigned vec16
__attribute__((vector_size(16)));
76 typedef unsigned vec8
__attribute__((vector_size(8)));
78 void vect_shift_1(vec16
*x
) { *x
= *x
<< 4; }
80 void vect_shift_2(vec16
*x
, vec16 y
) { *x
= *x
<< y
; }
82 void vect_shift_3(vec16
*x
, vec8 y
) {
83 *x
= *x
<< y
; // expected-error {{vector operands do not have the same number of elements}}