[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / shift.cpp
blob6367e6d2f250bbce643cef291514af664217c1fb
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
4 #include <limits.h>
6 #define WORD_BIT (sizeof(int) * CHAR_BIT)
8 enum {
9 X = 1 << 0,
10 Y = 1 << 1,
11 Z = 1 << 2
14 void test() {
15 char c;
17 c = 0 << 0;
18 c = 0 << 1;
19 c = 1 << 0;
20 c = 1 << -0;
21 c = 1 >> -0;
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 from 'int' to 'char' changes value from -2147483648 to 0}}
26 c = 1 >> (unsigned)-1; // expected-warning {{shift count >= width of type}}
27 c = 1 << c;
28 c <<= 0;
29 c >>= 0;
30 c <<= 1;
31 c >>= 1;
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);
42 int i;
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);
51 unsigned u;
52 u = 1U << (WORD_BIT - 1);
53 u = 5U << (WORD_BIT - 1);
55 long long int lli;
56 lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}}
57 lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
60 #define a 0
61 #define ashift 8
62 enum { b = (a << ashift) };
64 // Don't warn for negative shifts in code that is unreachable.
65 void test_pr5544() {
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) {
70 if (0)
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}}