blob: 142d83c43d92b98938ebf4792afb60db54ee4ddd [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}}
Ted Kremenekfa821382011-06-15 00:54:52 +000041 i = 1 << (WORD_BIT - 1); // expected-warning {{sets 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
Ted Kremenek082bf7a2011-03-01 18:09:31 +000059// Don't warn for negative shifts in code that is unreachable.
60void test_pr5544() {
61 (void) (((1) > 63 && (1) < 128 ? (((unsigned long long) 1)<<((1)-64)) : (unsigned long long) 0)); // no-warning
62}
Ted Kremenek425a31e2011-03-01 19:13:22 +000063
64void test_shift_too_much(char x) {
65 if (0)
66 (void) (x >> 80); // no-warning
67 (void) (x >> 80); // expected-warning {{shift count >= width of type}}
68}