Implement a warning for known shift overflows on constant shift
expressions. Consider the code:

  int64_t i = 10 << 30;

This compiles fine, but most developers expect it to produce the value
for 10 gigs, not -2 gigs. This is actually undefined behavior because
the LHS is a signed integer type.

The warning is currently gated behind -Wshift-overflow.

There is a special case where only the sign bit is overridden that gets
a custom error message and is by default ignored. This case is much less
likely to cause observed buggy behavior, it's just undefined behavior
according to the spec. This warning can be enabled with
-Wshift-sign-overflow.

Original patch by Oleg Slezberg, with style tweaks and some correctness
fixes by me.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@126342 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/Sema/shift.c b/test/Sema/shift.c
index 4273cab..63c97fe 100644
--- a/test/Sema/shift.c
+++ b/test/Sema/shift.c
@@ -1,7 +1,9 @@
-// RUN: %clang -Wall -ffreestanding -fsyntax-only -Xclang -verify %s
+// RUN: %clang -Wall -Wshift-sign-overflow -ffreestanding -fsyntax-only -Xclang -verify %s
 
 #include <limits.h>
 
+#define WORD_BIT (sizeof(int) * CHAR_BIT)
+
 enum {
   X = 1 << 0,
   Y = 1 << 1,
@@ -32,6 +34,22 @@
   c <<= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
   c >>= CHAR_BIT+1; // expected-warning {{shift count >= width of type}}
   (void)((long)c << CHAR_BIT);
+
+  int i;
+  i = 1 << (WORD_BIT - 2);
+  i = 2 << (WORD_BIT - 1); // expected-warning {{the promoted type of the shift expression is 'int'}}
+  i = 1 << (WORD_BIT - 1); // expected-warning {{overrides the sign bit of the promoted type of the shift expression ('int')}}
+  i = -1 << (WORD_BIT - 1);
+  i = 0 << (WORD_BIT - 1);
+  i = (char)1 << (WORD_BIT - 2);
+
+  unsigned u;
+  u = 1U << (WORD_BIT - 1);
+  u = 5U << (WORD_BIT - 1);
+
+  long long int lli;
+  lli = INT_MIN << 2; // expected-warning {{the promoted type of the shift expression is 'int'}}
+  lli = 1LL << (sizeof(long long) * CHAR_BIT - 2);
 }
 
 #define a 0