diff mbox

Add -Wshift-count-negative and -Wshift-count-overflow

Message ID 546103F4.4050105@oracle.com
State New
Headers show

Commit Message

Paolo Carlini Nov. 10, 2014, 6:29 p.m. UTC
Hi again,

thus the below only adds the warnings. Would be mainline only of course.

Thanks!
Paolo.

////////////////////////////
2014-11-10  Paolo Carlini  <paolo.carlini@oracle.com>

	* doc/invoke.texi ([-Wshift-count-negative, -Wshift-count-overflow]):
	Add.

/c-family
2014-11-10  Paolo Carlini  <paolo.carlini@oracle.com>

	* c.opt ([Wshift-count-negative, Wshift-count-overflow]): Add.

/cp
2014-11-10  Paolo Carlini  <paolo.carlini@oracle.com>

	* typeck.c (cp_build_binary_op): Use OPT_Wshift_count_negative and
	OPT_Wshift_count_overflow in the warnings.

/c
2014-11-10  Paolo Carlini  <paolo.carlini@oracle.com>

	* c-typeck.c (build_binary_op): Use OPT_Wshift_count_negative and
	OPT_Wshift_count_overflow in the warnings.

/testsuite
2014-11-10  Paolo Carlini  <paolo.carlini@oracle.com>

	* c-c++-common/Wshift-count-overflow-1.c: New.
	* c-c++-common/Wshift-count-overflow-2.c: Likewise.
	* c-c++-common/Wshift-count-negative-1.c: Likewise.
	* c-c++-common/Wshift-count-negative-2.c: Likewise.

Comments

Jason Merrill Nov. 10, 2014, 6:40 p.m. UTC | #1
OK.

Jason
diff mbox

Patch

Index: c/c-typeck.c
===================================================================
--- c/c-typeck.c	(revision 217282)
+++ c/c-typeck.c	(working copy)
@@ -10491,7 +10491,8 @@  build_binary_op (location_t location, enum tree_co
 		{
 		  int_const = false;
 		  if (c_inhibit_evaluation_warnings == 0)
-		    warning_at (location, 0, "right shift count is negative");
+		    warning_at (location, OPT_Wshift_count_negative,
+				"right shift count is negative");
 		}
 	      else
 		{
@@ -10502,8 +10503,8 @@  build_binary_op (location_t location, enum tree_co
 		    {
 		      int_const = false;
 		      if (c_inhibit_evaluation_warnings == 0)
-			warning_at (location, 0, "right shift count >= width "
-				    "of type");
+			warning_at (location, OPT_Wshift_count_overflow,
+				    "right shift count >= width of type");
 		    }
 		}
 	    }
@@ -10545,7 +10546,8 @@  build_binary_op (location_t location, enum tree_co
 		{
 		  int_const = false;
 		  if (c_inhibit_evaluation_warnings == 0)
-		    warning_at (location, 0, "left shift count is negative");
+		    warning_at (location, OPT_Wshift_count_negative,
+				"left shift count is negative");
 		}
 
 	      else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
@@ -10552,8 +10554,8 @@  build_binary_op (location_t location, enum tree_co
 		{
 		  int_const = false;
 		  if (c_inhibit_evaluation_warnings == 0)
-		    warning_at (location, 0, "left shift count >= width of "
-				"type");
+		    warning_at (location, OPT_Wshift_count_overflow,
+				"left shift count >= width of type");
 		}
 	    }
 
Index: c-family/c.opt
===================================================================
--- c-family/c.opt	(revision 217282)
+++ c-family/c.opt	(working copy)
@@ -760,14 +760,22 @@  Wselector
 ObjC ObjC++ Var(warn_selector) Warning
 Warn if a selector has multiple methods
 
+Wsequence-point
+C ObjC C++ ObjC++ Var(warn_sequence_point) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
+Warn about possible violations of sequence point rules
+
 Wshadow-ivar
 ObjC ObjC++ Var(warn_shadow_ivar) EnabledBy(Wshadow) Init(1) Warning
 Warn if a local declaration hides an instance variable
 
-Wsequence-point
-C ObjC C++ ObjC++ Var(warn_sequence_point) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall)
-Warn about possible violations of sequence point rules
+Wshift-count-negative
+C ObjC C++ ObjC++ Var(warn_shift_count_negative) Init(1) Warning
+Warn if shift count is negative
 
+Wshift-count-overflow
+C ObjC C++ ObjC++ Var(warn_shift_count_overflow) Init(1) Warning
+Warn if shift count >= width of type
+
 Wsign-compare
 C ObjC C++ ObjC++ Var(warn_sign_compare) Warning LangEnabledBy(C++ ObjC++,Wall)
 Warn about signed-unsigned comparisons
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 217302)
+++ cp/typeck.c	(working copy)
@@ -4280,7 +4280,8 @@  cp_build_binary_op (location_t location,
 		{
 		  if ((complain & tf_warning)
 		      && c_inhibit_evaluation_warnings == 0)
-		    warning (0, "right shift count is negative");
+		    warning (OPT_Wshift_count_negative,
+			     "right shift count is negative");
 		}
 	      else
 		{
@@ -4287,7 +4288,8 @@  cp_build_binary_op (location_t location,
 		  if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
 		      && (complain & tf_warning)
 		      && c_inhibit_evaluation_warnings == 0)
-		    warning (0, "right shift count >= width of type");
+		    warning (OPT_Wshift_count_overflow,
+			     "right shift count >= width of type");
 		}
 	    }
 	  /* Convert the shift-count to an integer, regardless of
@@ -4328,7 +4330,8 @@  cp_build_binary_op (location_t location,
 		{
 		  if ((complain & tf_warning)
 		      && c_inhibit_evaluation_warnings == 0)
-		    warning (0, "left shift count is negative");
+		    warning (OPT_Wshift_count_negative,
+			     "left shift count is negative");
 		}
 	      else if (compare_tree_int (const_op1,
 					 TYPE_PRECISION (type0)) >= 0)
@@ -4335,7 +4338,8 @@  cp_build_binary_op (location_t location,
 		{
 		  if ((complain & tf_warning)
 		      && c_inhibit_evaluation_warnings == 0)
-		    warning (0, "left shift count >= width of type");
+		    warning (OPT_Wshift_count_overflow,
+			     "left shift count >= width of type");
 		}
 	    }
 	  /* Convert the shift-count to an integer, regardless of
Index: doc/invoke.texi
===================================================================
--- doc/invoke.texi	(revision 217282)
+++ doc/invoke.texi	(working copy)
@@ -269,6 +269,7 @@  Objective-C and Objective-C++ Dialects}.
 -Wpointer-arith  -Wno-pointer-to-int-cast @gol
 -Wredundant-decls  -Wno-return-local-addr @gol
 -Wreturn-type  -Wsequence-point  -Wshadow  -Wno-shadow-ivar @gol
+-Wshift-count-negative -Wshift-count-overflow @gol
 -Wsign-compare  -Wsign-conversion -Wfloat-conversion @gol
 -Wsizeof-pointer-memaccess  -Wsizeof-array-argument @gol
 -Wstack-protector -Wstack-usage=@var{len} -Wstrict-aliasing @gol
@@ -3825,6 +3826,16 @@  exceptions are @samp{main} and functions defined i
 
 This warning is enabled by @option{-Wall}.
 
+@item -Wshift-count-negative
+@opindex Wshift-count-negative
+@opindex Wno-shift-count-negative
+Warn if shift count is negative. This warning is enabled by default.
+
+@item -Wshift-count-overflow
+@opindex Wshift-count-overflow
+@opindex Wno-shift-count-overflow
+Warn if shift count >= width of type. This warning is enabled by default.
+
 @item -Wswitch
 @opindex Wswitch
 @opindex Wno-switch
Index: testsuite/c-c++-common/Wshift-count-negative-1.c
===================================================================
--- testsuite/c-c++-common/Wshift-count-negative-1.c	(revision 0)
+++ testsuite/c-c++-common/Wshift-count-negative-1.c	(working copy)
@@ -0,0 +1,8 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wshift-count-negative" } */
+
+void foo()
+{
+  unsigned i1 = 1U << -1; /* { dg-warning "left shift count is negative" } */
+  unsigned i2 = 1U >> -1; /* { dg-warning "right shift count is negative" } */
+}
Index: testsuite/c-c++-common/Wshift-count-negative-2.c
===================================================================
--- testsuite/c-c++-common/Wshift-count-negative-2.c	(revision 0)
+++ testsuite/c-c++-common/Wshift-count-negative-2.c	(working copy)
@@ -0,0 +1,8 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wno-shift-count-negative" } */
+
+void foo()
+{
+  unsigned i1 = 1U << -1;
+  unsigned i2 = 1U >> -1;
+}
Index: testsuite/c-c++-common/Wshift-count-overflow-1.c
===================================================================
--- testsuite/c-c++-common/Wshift-count-overflow-1.c	(revision 0)
+++ testsuite/c-c++-common/Wshift-count-overflow-1.c	(working copy)
@@ -0,0 +1,8 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wshift-count-overflow" } */
+
+void foo()
+{
+  unsigned i1 = 1U << (sizeof(unsigned) * __CHAR_BIT__); /* { dg-warning "left shift count >= width of type" } */
+  unsigned i2 = 1U >> (sizeof(unsigned) * __CHAR_BIT__); /* { dg-warning "right shift count >= width of type" } */
+}
Index: testsuite/c-c++-common/Wshift-count-overflow-2.c
===================================================================
--- testsuite/c-c++-common/Wshift-count-overflow-2.c	(revision 0)
+++ testsuite/c-c++-common/Wshift-count-overflow-2.c	(working copy)
@@ -0,0 +1,8 @@ 
+/* { dg-do compile } */
+/* { dg-options "-Wno-shift-count-overflow" } */
+
+void foo()
+{
+  unsigned i1 = 1U << (sizeof(unsigned) * __CHAR_BIT__);
+  unsigned i2 = 1U >> (sizeof(unsigned) * __CHAR_BIT__);
+}