]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/Sema/shift.c
Vendor import of clang release_40 branch r292732:
[FreeBSD/FreeBSD.git] / test / Sema / shift.c
1 // RUN: %clang_cc1 -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -verify %s
2
3 #include <limits.h>
4
5 #define WORD_BIT (sizeof(int) * CHAR_BIT)
6
7 enum {
8   X = 1 << 0,
9   Y = 1 << 1,
10   Z = 1 << 2
11 };
12
13 void test() {
14   char c;
15
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 << c;
24   c <<= 0;
25   c >>= 0;
26   c <<= 1;
27   c >>= 1;
28   c <<= -1; // expected-warning {{shift count is negative}}
29   c >>= -1; // expected-warning {{shift count is negative}}
30   c <<= 999999; // expected-warning {{shift count >= width of type}}
31   c >>= 999999; // expected-warning {{shift count >= width of type}}
32   c <<= CHAR_BIT; // expected-warning {{shift count >= width of type}}
33   c >>= CHAR_BIT; // expected-warning {{shift count >= width of type}}
34   c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
35   c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
36   (void)((long)c << CHAR_BIT);
37
38   int i;
39   i = 1 << (WORD_BIT - 2);
40   i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
41   i = 1 << (WORD_BIT - 1); // expected-warning {{sets the sign bit of the shift expression}}
42   i = -1 << (WORD_BIT - 1); // expected-warning {{shifting a negative signed value is undefined}}
43   i = -1 << 0; // expected-warning {{shifting a negative signed value is undefined}}
44   i = 0 << (WORD_BIT - 1);
45   i = (char)1 << (WORD_BIT - 2);
46
47   unsigned u;
48   u = 1U << (WORD_BIT - 1);
49   u = 5U << (WORD_BIT - 1);
50
51   long long int lli;
52   lli = INT_MIN << 2; // expected-warning {{shifting a negative signed value is undefined}}
53   lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
54 }
55
56 #define a 0
57 #define ashift 8
58 enum { b = (a << ashift) };
59
60 // Don't warn for negative shifts in code that is unreachable.
61 void test_pr5544() {
62   (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
63 }
64
65 void test_shift_too_much(char x) {
66   if (0)
67     (void) (x >> 80); // no-warning
68   (void) (x >> 80); // expected-warning {{shift count >= width of type}}
69 }
70
71 typedef unsigned vec16 __attribute__((vector_size(16)));
72 typedef unsigned vec8 __attribute__((vector_size(8)));
73
74 void vect_shift_1(vec16 *x) { *x = *x << 4; }
75
76 void vect_shift_2(vec16 *x, vec16 y) { *x = *x << y; }
77
78 void vect_shift_3(vec16 *x, vec8 y) {
79   *x = *x << y; // expected-error {{vector operands do not have the same number of elements}}
80 }