This patch fixes a defect in -Wshift-overflow. We should only warn
about left-shifting 1 into the sign bit when -Wshift-overflow=2. But
this doesn't apply only for 1 << 31, but also for 2 << 30, etc.
In C++14, never warn about this.
Neither existing tests nor documentation require updating, I think.
Bootstrapped/regtested on x86_64-linux, ok for trunk?
2015-08-12 Marek Polacek
PR c++/55095
* c-common.c (maybe_warn_shift_overflow): Properly handle
left-shifting 1 into the sign bit.
* c-c++-common/Wshift-overflow-6.c: New test.
* c-c++-common/Wshift-overflow-7.c: New test.
* g++.dg/cpp1y/left-shift-2.C: New test.
diff --git gcc/c-family/c-common.c gcc/c-family/c-common.c
index f6c5ddd..13175d8 100644
--- gcc/c-family/c-common.c
+++ gcc/c-family/c-common.c
@@ -12442,9 +12442,10 @@ maybe_warn_shift_overflow (location_t loc, tree op0,
tree op1)
if (TYPE_UNSIGNED (type0))
return false;
+ unsigned int min_prec = (wi::min_precision (op0, SIGNED)
+ + TREE_INT_CST_LOW (op1));
/* Handle the left-shifting 1 into the sign bit case. */
- if (integer_onep (op0)
- && compare_tree_int (op1, prec0 - 1) == 0)
+ if (min_prec == prec0 + 1)
{
/* Never warn for C++14 onwards. */
if (cxx_dialect >= cxx14)
@@ -12456,8 +12457,6 @@ maybe_warn_shift_overflow (location_t loc, tree op0,
tree op1)
return true;
}
- unsigned int min_prec = (wi::min_precision (op0, SIGNED)
- + TREE_INT_CST_LOW (op1));
bool overflowed = min_prec > prec0;
if (overflowed && c_inhibit_evaluation_warnings == 0)
warning_at (loc, OPT_Wshift_overflow_,
diff --git gcc/testsuite/c-c++-common/Wshift-overflow-6.c
gcc/testsuite/c-c++-common/Wshift-overflow-6.c
index e69de29..fed79f8 100644
--- gcc/testsuite/c-c++-common/Wshift-overflow-6.c
+++ gcc/testsuite/c-c++-common/Wshift-overflow-6.c
@@ -0,0 +1,36 @@
+/* PR c++/55095 */
+/* { dg-do compile { target int32 } } */
+/* { dg-options "-Wshift-overflow=1" } */
+/* { dg-additional-options "-std=c++11" { target c++ } } */
+
+int i00 = 0b1 << 31;
+int i01 = 0b10 << 30;
+int i02 = 0b100 << 29;
+int i03 = 0b1000 << 28;
+int i04 = 0b1 << 27;
+int i05 = 0b10 << 26;
+int i06 = 0b100 << 25;
+int i07 = 0b1000 << 24;
+int i08 = 0b1 << 23;
+int i09 = 0b10 << 22;
+int i10 = 0b100 << 21;
+int i11 = 0b1000 << 20;
+int i12 = 0b1 << 19;
+int i13 = 0b10 << 18;
+int i14 = 0b100 << 17;
+int i15 = 0b1000 << 16;
+int i16 = 0b1 << 15;
+int i17 = 0b10 << 14;
+int i18 = 0b100 << 13;
+int i19 = 0b1000 << 12;
+int i20 = 0b1 << 11;
+int i21 = 0b10 << 10;
+int i22 = 0b100 << 9;
+int i23 = 0b1000 << 8;
+int i24 = 0b1 << 7;
+int i25 = 0b10 << 6;
+int i26 = 0b100 << 5;
+int i27 = 0b1000 << 4;
+int i28 = 0b1 << 3;
+int i29 = 0b10 << 2;
+int i30 = 0b100 << 1;
diff --git gcc/testsuite/c-c++-common/Wshift-overflow-7.c
gcc/testsuite/c-c++-common/Wshift-overflow-7.c
index e69de29..0eb1fef 100644
--- gcc/testsuite/c-c++-common/Wshift-overflow-7.c
+++ gcc/testsuite/c-c++-common/Wshift-overflow-7.c
@@ -0,0 +1,36 @@
+/* PR c++/55095 */
+/* { dg-do compile { target int32 } } */
+/* { dg-options "-Wshift-overflow=2" } */
+/* { dg-additional-options "-std=c++11" { target c++ } } */
+
+int i00 = 0b1 << 31; /* { dg-warning "requires 33 bits to represent" } */
+int i01 = 0b10 << 30; /* { dg-warning "requires 33 bits to represent" } */
+int i02 = 0b100 << 29; /* { dg-warning "requires 33 bits to represent" } */
+int i03 = 0b1000 << 28; /* { dg-warning "requires 33 bits to represent" } */
+int i04 = 0b1 << 27; /* { dg-warning "requires 33 bits to represent" } */
+int i05 = 0b10 << 26; /* { dg-warning "requires 33 bits to represent" } */
+int i06 = 0b100 << 25; /* { dg-warning "requires 33 bits to represent" } */
+int i07 = 0b1000 << 24; /* { dg-warning "requires 33 bits to represent" }
*/
+int i08 = 0b1 << 23; /* { dg-warning "requires 33 bits to represent" }
*/
+int i09 = 0b10 << 22; /* { dg-warning "requires 33 bits to represent"
} */
+int i10 = 0b100 << 21; /* { dg-warning "requires 33 bits to represent"
} */
+int i11 = 0b1000 << 20; /* { dg-warning "requires 33 bits to
represent" } */
+int i12 = 0b1 << 19; /* { dg-warning "requires 33 bits to
represent" } */
+int i13 = 0b10 << 18; /* { dg-warning "requires 33 bits to
represent" } */
+int i14 = 0b100 << 17; /* { dg-warning "requires 33 bits to
represent" } */
+int i15 = 0b1000 << 16; /* { dg-warning "requires 33 bits to
re