blob: df6b1141bdfc7c82d81a28efb3daa426662c4dc7 [file] [log] [blame]
Chandler Carruth21206d52011-02-23 23:34:11 +00001// RUN: %clang -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -Xclang -verify %s
Ryan Flynnd0439682009-08-07 16:20:20 +00002
3#include <limits.h>
4
Chandler Carruth21206d52011-02-23 23:34:11 +00005#define WORD_BIT (sizeof(int) * CHAR_BIT)
6
Ryan Flynnd0439682009-08-07 16:20:20 +00007enum {
8 X = 1 << 0,
9 Y = 1 << 1,
10 Z = 1 << 2
11};
Chris Lattner1dcf2c82007-12-13 07:28:16 +000012
13void test() {
14 char c;
Ryan Flynnd0439682009-08-07 16:20:20 +000015
16 c = 0 << 0;
Ryan Flynn8045c732009-08-08 19:18:23 +000017 c = 0 << 1;
Ryan Flynnd0439682009-08-07 16:20:20 +000018 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;
Ryan Flynn8045c732009-08-08 19:18:23 +000024 c <<= 0;
25 c >>= 0;
Ryan Flynnd0439682009-08-07 16:20:20 +000026 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);
Chandler Carruth21206d52011-02-23 23:34:11 +000037
38 int i;
39 i = 1 << (WORD_BIT - 2);
Chandler Carruth5fa05cb2011-02-24 17:13:15 +000040 i = 2 << (WORD_BIT - 1); // expected-warning {{bits to represent, but 'int' only has}}
41 i = 1 << (WORD_BIT - 1); // expected-warning {{overrides the sign bit of the shift expression}}
Chandler Carruth21206d52011-02-23 23:34:11 +000042 i = -1 << (WORD_BIT - 1);
43 i = 0 << (WORD_BIT - 1);
44 i = (char)1 << (WORD_BIT - 2);
45
46 unsigned u;
47 u = 1U << (WORD_BIT - 1);
48 u = 5U << (WORD_BIT - 1);
49
50 long long int lli;
Chandler Carruth5fa05cb2011-02-24 17:13:15 +000051 lli = INT_MIN << 2; // expected-warning {{bits to represent, but 'int' only has}}
Chandler Carruth21206d52011-02-23 23:34:11 +000052 lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
Chris Lattner1dcf2c82007-12-13 07:28:16 +000053}
Ryan Flynn8045c732009-08-08 19:18:23 +000054
55#define a 0
56#define ashift 8
57enum { b = (a << ashift) };
58