blob: 6367e6d2f250bbce643cef291514af664217c1fb [file] [log] [blame]
Michael Benfieldcf49cae2021-06-01 14:49:34 -07001// 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
Chandler Carruth60ed89d2011-02-24 00:03:53 +00003
4#include <limits.h>
5
6#define WORD_BIT (sizeof(int) * CHAR_BIT)
7
Richard Smith7939ba02019-06-25 01:45:26 +00008enum {
9 X = 1 << 0,
10 Y = 1 << 1,
11 Z = 1 << 2
12};
Chandler Carruth60ed89d2011-02-24 00:03:53 +000013
14void test() {
Richard Smith7939ba02019-06-25 01:45:26 +000015 char c;
16
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}}
Budimir Aranđeloviće4163c02024-07-11 14:21:21 +020025 // expected-warning@-1 {{implicit conversion from 'int' to 'char' changes value from -2147483648 to 0}}
Richard Smith7939ba02019-06-25 01:45:26 +000026 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);
41
42 int i;
43 i = 1 << (WORD_BIT - 2);
Shivam Gupta48e18292022-06-14 17:59:13 +053044 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}}
Richard Smith7939ba02019-06-25 01:45:26 +000046 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);
50
51 unsigned u;
52 u = 1U << (WORD_BIT - 1);
53 u = 5U << (WORD_BIT - 1);
54
55 long long int lli;
Shivam Gupta48e18292022-06-14 17:59:13 +053056 lli = INT_MIN << 2; // cxx17-warning {{shifting a negative signed value is undefined}}
Richard Smith7939ba02019-06-25 01:45:26 +000057 lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
58}
59
60#define a 0
61#define ashift 8
62enum { b = (a << ashift) };
63
64// Don't warn for negative shifts in code that is unreachable.
65void test_pr5544() {
66 (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
67}
68
69void 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}}
73}
74
75typedef unsigned vec16 __attribute__((vector_size(16)));
76typedef unsigned vec8 __attribute__((vector_size(8)));
77
78void vect_shift_1(vec16 *x) { *x = *x << 4; }
79
80void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; }
81
82void vect_shift_3(vec16 *x, vec8 y) {
83 *x = *x << y; // expected-error {{vector operands do not have the same number of elements}}
Chandler Carruth60ed89d2011-02-24 00:03:53 +000084}