public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C/C++ PATCH] Fix -Wshift-overflow with sign bit
@ 2015-08-12 15:39 Marek Polacek
  2015-08-12 17:10 ` Jeff Law
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Marek Polacek @ 2015-08-12 15:39 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill, Joseph Myers, Jeff Law

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  <polacek@redhat.com>

	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 = 0b10000 << 27;
+int i05 = 0b100000 << 26;
+int i06 = 0b1000000 << 25;
+int i07 = 0b10000000 << 24;
+int i08 = 0b100000000 << 23;
+int i09 = 0b1000000000 << 22;
+int i10 = 0b10000000000 << 21;
+int i11 = 0b100000000000 << 20;
+int i12 = 0b1000000000000 << 19;
+int i13 = 0b10000000000000 << 18;
+int i14 = 0b100000000000000 << 17;
+int i15 = 0b1000000000000000 << 16;
+int i16 = 0b10000000000000000 << 15;
+int i17 = 0b100000000000000000 << 14;
+int i18 = 0b1000000000000000000 << 13;
+int i19 = 0b10000000000000000000 << 12;
+int i20 = 0b100000000000000000000 << 11;
+int i21 = 0b1000000000000000000000 << 10;
+int i22 = 0b10000000000000000000000 << 9;
+int i23 = 0b100000000000000000000000 << 8;
+int i24 = 0b1000000000000000000000000 << 7;
+int i25 = 0b10000000000000000000000000 << 6;
+int i26 = 0b100000000000000000000000000 << 5;
+int i27 = 0b1000000000000000000000000000 << 4;
+int i28 = 0b10000000000000000000000000000 << 3;
+int i29 = 0b100000000000000000000000000000 << 2;
+int i30 = 0b1000000000000000000000000000000 << 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 = 0b10000 << 27; /* { dg-warning "requires 33 bits to represent" } */
+int i05 = 0b100000 << 26; /* { dg-warning "requires 33 bits to represent" } */
+int i06 = 0b1000000 << 25; /* { dg-warning "requires 33 bits to represent" } */
+int i07 = 0b10000000 << 24; /* { dg-warning "requires 33 bits to represent" } */
+int i08 = 0b100000000 << 23; /* { dg-warning "requires 33 bits to represent" } */
+int i09 = 0b1000000000 << 22; /* { dg-warning "requires 33 bits to represent" } */
+int i10 = 0b10000000000 << 21; /* { dg-warning "requires 33 bits to represent" } */
+int i11 = 0b100000000000 << 20; /* { dg-warning "requires 33 bits to represent" } */
+int i12 = 0b1000000000000 << 19; /* { dg-warning "requires 33 bits to represent" } */
+int i13 = 0b10000000000000 << 18; /* { dg-warning "requires 33 bits to represent" } */
+int i14 = 0b100000000000000 << 17; /* { dg-warning "requires 33 bits to represent" } */
+int i15 = 0b1000000000000000 << 16; /* { dg-warning "requires 33 bits to represent" } */
+int i16 = 0b10000000000000000 << 15; /* { dg-warning "requires 33 bits to represent" } */
+int i17 = 0b100000000000000000 << 14; /* { dg-warning "requires 33 bits to represent" } */
+int i18 = 0b1000000000000000000 << 13; /* { dg-warning "requires 33 bits to represent" } */
+int i19 = 0b10000000000000000000 << 12; /* { dg-warning "requires 33 bits to represent" } */
+int i20 = 0b100000000000000000000 << 11; /* { dg-warning "requires 33 bits to represent" } */
+int i21 = 0b1000000000000000000000 << 10; /* { dg-warning "requires 33 bits to represent" } */
+int i22 = 0b10000000000000000000000 << 9; /* { dg-warning "requires 33 bits to represent" } */
+int i23 = 0b100000000000000000000000 << 8; /* { dg-warning "requires 33 bits to represent" } */
+int i24 = 0b1000000000000000000000000 << 7; /* { dg-warning "requires 33 bits to represent" } */
+int i25 = 0b10000000000000000000000000 << 6; /* { dg-warning "requires 33 bits to represent" } */
+int i26 = 0b100000000000000000000000000 << 5; /* { dg-warning "requires 33 bits to represent" } */
+int i27 = 0b1000000000000000000000000000 << 4; /* { dg-warning "requires 33 bits to represent" } */
+int i28 = 0b10000000000000000000000000000 << 3; /* { dg-warning "requires 33 bits to represent" } */
+int i29 = 0b100000000000000000000000000000 << 2; /* { dg-warning "requires 33 bits to represent" } */
+int i30 = 0b1000000000000000000000000000000 << 1; /* { dg-warning "requires 33 bits to represent" } */
diff --git gcc/testsuite/g++.dg/cpp1y/left-shift-2.C gcc/testsuite/g++.dg/cpp1y/left-shift-2.C
index e69de29..342f9072 100644
--- gcc/testsuite/g++.dg/cpp1y/left-shift-2.C
+++ gcc/testsuite/g++.dg/cpp1y/left-shift-2.C
@@ -0,0 +1,36 @@
+// PR c++/55095
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wshift-overflow=2" }
+// { dg-require-effective-target int32 }
+
+int i00 = 0b1 << 31;
+int i01 = 0b10 << 30;
+int i02 = 0b100 << 29;
+int i03 = 0b1000 << 28;
+int i04 = 0b10000 << 27;
+int i05 = 0b100000 << 26;
+int i06 = 0b1000000 << 25;
+int i07 = 0b10000000 << 24;
+int i08 = 0b100000000 << 23;
+int i09 = 0b1000000000 << 22;
+int i10 = 0b10000000000 << 21;
+int i11 = 0b100000000000 << 20;
+int i12 = 0b1000000000000 << 19;
+int i13 = 0b10000000000000 << 18;
+int i14 = 0b100000000000000 << 17;
+int i15 = 0b1000000000000000 << 16;
+int i16 = 0b10000000000000000 << 15;
+int i17 = 0b100000000000000000 << 14;
+int i18 = 0b1000000000000000000 << 13;
+int i19 = 0b10000000000000000000 << 12;
+int i20 = 0b100000000000000000000 << 11;
+int i21 = 0b1000000000000000000000 << 10;
+int i22 = 0b10000000000000000000000 << 9;
+int i23 = 0b100000000000000000000000 << 8;
+int i24 = 0b1000000000000000000000000 << 7;
+int i25 = 0b10000000000000000000000000 << 6;
+int i26 = 0b100000000000000000000000000 << 5;
+int i27 = 0b1000000000000000000000000000 << 4;
+int i28 = 0b10000000000000000000000000000 << 3;
+int i29 = 0b100000000000000000000000000000 << 2;
+int i30 = 0b1000000000000000000000000000000 << 1;

	Marek

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [C/C++ PATCH] Fix -Wshift-overflow with sign bit
  2015-08-12 15:39 [C/C++ PATCH] Fix -Wshift-overflow with sign bit Marek Polacek
@ 2015-08-12 17:10 ` Jeff Law
  2015-08-12 17:28 ` Toon Moene
  2015-08-23  8:50 ` Jason Merrill
  2 siblings, 0 replies; 7+ messages in thread
From: Jeff Law @ 2015-08-12 17:10 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Jason Merrill, Joseph Myers

On 08/12/2015 09:39 AM, Marek Polacek wrote:
> 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  <polacek@redhat.com>
>
> 	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.
I didn't realize C++14 fixed this bit of lameness.  I'd read that it was 
under consideration when I was looking at Mikael's sext_hwi patch, but 
didn't follow up on the current status.

OK for the trunk.

jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [C/C++ PATCH] Fix -Wshift-overflow with sign bit
  2015-08-12 15:39 [C/C++ PATCH] Fix -Wshift-overflow with sign bit Marek Polacek
  2015-08-12 17:10 ` Jeff Law
@ 2015-08-12 17:28 ` Toon Moene
  2015-08-12 17:33   ` Jeff Law
  2015-08-23  8:50 ` Jason Merrill
  2 siblings, 1 reply; 7+ messages in thread
From: Toon Moene @ 2015-08-12 17:28 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Jason Merrill, Joseph Myers, Jeff Law

On 08/12/2015 05:39 PM, Marek Polacek wrote:

> 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.

And then there's this:

https://gcc.gnu.org/ml/gcc-testresults/2015-08/msg01036.html

[ Yes, that's at run time, not compile time ... ]

-- 
Toon Moene - e-mail: toon@moene.org - phone: +31 346 214290
Saturnushof 14, 3738 XG  Maartensdijk, The Netherlands
At home: http://moene.org/~toon/; weather: http://moene.org/~hirlam/
Progress of GNU Fortran: http://gcc.gnu.org/wiki/GFortran#news

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [C/C++ PATCH] Fix -Wshift-overflow with sign bit
  2015-08-12 17:28 ` Toon Moene
@ 2015-08-12 17:33   ` Jeff Law
  2015-08-12 17:40     ` Marek Polacek
  0 siblings, 1 reply; 7+ messages in thread
From: Jeff Law @ 2015-08-12 17:33 UTC (permalink / raw)
  To: Toon Moene, Marek Polacek, GCC Patches, Jason Merrill, Joseph Myers

On 08/12/2015 11:28 AM, Toon Moene wrote:
> On 08/12/2015 05:39 PM, Marek Polacek wrote:
>
>> 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.
>
> And then there's this:
>
> https://gcc.gnu.org/ml/gcc-testresults/2015-08/msg01036.html
>
> [ Yes, that's at run time, not compile time ... ]
Hoping some of those are fixed by the sext_hwi changes from Mikael.

jeff
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [C/C++ PATCH] Fix -Wshift-overflow with sign bit
  2015-08-12 17:33   ` Jeff Law
@ 2015-08-12 17:40     ` Marek Polacek
  2015-08-12 17:46       ` Jeff Law
  0 siblings, 1 reply; 7+ messages in thread
From: Marek Polacek @ 2015-08-12 17:40 UTC (permalink / raw)
  To: Jeff Law; +Cc: Toon Moene, GCC Patches, Jason Merrill, Joseph Myers

On Wed, Aug 12, 2015 at 11:33:21AM -0600, Jeff Law wrote:
> On 08/12/2015 11:28 AM, Toon Moene wrote:
> >https://gcc.gnu.org/ml/gcc-testresults/2015-08/msg01036.html
> >
> >[ Yes, that's at run time, not compile time ... ]
> Hoping some of those are fixed by the sext_hwi changes from Mikael.

I hope too ;).  But they have nothing to do with the -Wshift-overflow IMHO.

	Marek

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [C/C++ PATCH] Fix -Wshift-overflow with sign bit
  2015-08-12 17:40     ` Marek Polacek
@ 2015-08-12 17:46       ` Jeff Law
  0 siblings, 0 replies; 7+ messages in thread
From: Jeff Law @ 2015-08-12 17:46 UTC (permalink / raw)
  To: Marek Polacek; +Cc: Toon Moene, GCC Patches, Jason Merrill, Joseph Myers

On 08/12/2015 11:40 AM, Marek Polacek wrote:
> On Wed, Aug 12, 2015 at 11:33:21AM -0600, Jeff Law wrote:
>> On 08/12/2015 11:28 AM, Toon Moene wrote:
>>> https://gcc.gnu.org/ml/gcc-testresults/2015-08/msg01036.html
>>>
>>> [ Yes, that's at run time, not compile time ... ]
>> Hoping some of those are fixed by the sext_hwi changes from Mikael.
>
> I hope too ;).  But they have nothing to do with the -Wshift-overflow IMHO.
Right.  Toon was just pointing out that we're getting the left-shift 
errors from ubsan all over the place.  Those are totally independent if 
your -Wshift-overflow stuff.

Jeff

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [C/C++ PATCH] Fix -Wshift-overflow with sign bit
  2015-08-12 15:39 [C/C++ PATCH] Fix -Wshift-overflow with sign bit Marek Polacek
  2015-08-12 17:10 ` Jeff Law
  2015-08-12 17:28 ` Toon Moene
@ 2015-08-23  8:50 ` Jason Merrill
  2 siblings, 0 replies; 7+ messages in thread
From: Jason Merrill @ 2015-08-23  8:50 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches, Joseph Myers, Jeff Law

OK.

Jason

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-08-23  4:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-12 15:39 [C/C++ PATCH] Fix -Wshift-overflow with sign bit Marek Polacek
2015-08-12 17:10 ` Jeff Law
2015-08-12 17:28 ` Toon Moene
2015-08-12 17:33   ` Jeff Law
2015-08-12 17:40     ` Marek Polacek
2015-08-12 17:46       ` Jeff Law
2015-08-23  8:50 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).