[C PATCH] Add -Wdiscarded-qualifiers (PR c/43245)

Marek Polacek polacek@redhat.com
Tue Apr 29 21:34:00 GMT 2014


I think we should have an option to turn off various "discards
qualifier" warnings (only for C), or to make them hard errors.
This patch adds new option, called -Wdiscarded-qualifiers (as
suggested in the BZ) for that.

Regtested/bootstrapped on x86_64-linux, ok for trunk?

2014-04-29  Marek Polacek  <polacek@redhat.com>

	PR c/43245
	* doc/invoke.texi: Document -Wdiscarded-qualifiers.
c-family/
	* c.opt (Wdiscarded-qualifiers): Add.
c/
	* c-typeck.c (convert_for_assignment): Pass OPT_Wdiscarded_qualifiers
	instead of 0 to WARN_FOR_QUALIFIERS.
testsuite/
	* gcc.dg/pr43245.c: New test.

diff --git gcc/c-family/c.opt gcc/c-family/c.opt
index 9444708..1bbb81c 100644
--- gcc/c-family/c.opt
+++ gcc/c-family/c.opt
@@ -355,6 +355,10 @@ Wdeprecated
 C C++ ObjC ObjC++ Var(warn_deprecated) Init(1) Warning
 Warn if a deprecated compiler feature, class, method, or field is used
 
+Wdiscarded-qualifiers
+C ObjC Var(warn_discarded_qualifiers) Init(1) Warning
+Warn if type qualifiers on pointers are discarded
+
 Wdiv-by-zero
 C ObjC C++ ObjC++ Var(warn_div_by_zero) Init(1) Warning
 Warn about compile-time integer division by zero
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 62c72df..1bf20a4 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -5851,7 +5851,7 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type,
 		     vice-versa.  */
 		  if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
 		      & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
-		    WARN_FOR_QUALIFIERS (location, 0,
+		    WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
 					 G_("passing argument %d of %qE "
 					    "makes %q#v qualified function "
 					    "pointer from unqualified"),
@@ -5867,7 +5867,7 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type,
 		}
 	      else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
 		       & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
-		WARN_FOR_QUALIFIERS (location, 0,
+		WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
 				     G_("passing argument %d of %qE discards "
 					"%qv qualifier from pointer target type"),
 				     G_("assignment discards %qv qualifier "
@@ -6048,7 +6048,7 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type,
 	      if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
 		  & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
 		{
-		  WARN_FOR_QUALIFIERS (location, 0,
+		  WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
 				       G_("passing argument %d of %qE discards "
 					  "%qv qualifier from pointer target type"),
 				       G_("assignment discards %qv qualifier "
@@ -6085,7 +6085,7 @@ convert_for_assignment (location_t location, location_t expr_loc, tree type,
 		 where an ordinary one is wanted, but not vice-versa.  */
 	      if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
 		  & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
-		WARN_FOR_QUALIFIERS (location, 0,
+		WARN_FOR_QUALIFIERS (location, OPT_Wdiscarded_qualifiers,
 				     G_("passing argument %d of %qE makes "
 					"%q#v qualified function pointer "
 					"from unqualified"),
diff --git gcc/doc/invoke.texi gcc/doc/invoke.texi
index cc6d405..0eba1e0 100644
--- gcc/doc/invoke.texi
+++ gcc/doc/invoke.texi
@@ -242,6 +242,7 @@ Objective-C and Objective-C++ Dialects}.
 -Wchar-subscripts -Wclobbered  -Wcomment -Wconditionally-supported  @gol
 -Wconversion -Wcoverage-mismatch -Wdate-time -Wdelete-incomplete -Wno-cpp  @gol
 -Wno-deprecated -Wno-deprecated-declarations -Wdisabled-optimization  @gol
+-Wno-discarded-qualifiers @gol
 -Wno-div-by-zero -Wdouble-promotion -Wempty-body  -Wenum-compare @gol
 -Wno-endif-labels -Werror  -Werror=* @gol
 -Wfatal-errors  -Wfloat-equal  -Wformat  -Wformat=2 @gol
@@ -4182,6 +4183,15 @@ This option is only active when @option{-ftree-vrp} is active
 (default for @option{-O2} and above). It warns about subscripts to arrays
 that are always out of bounds. This warning is enabled by @option{-Wall}.
 
+@item -Wno-discarded-qualifiers
+@opindex Wno-discarded-qualifiers
+@opindex Wdiscarded-qualifiers
+Do not warn if type qualifiers on pointers are being discarded.
+Typically, the compiler will warn if a @code{const char *} variable is
+passed to a function that takes @code{char *} parameter.  This option
+can be used to suppress such a warning.  This warning is only supported
+for C.
+
 @item -Wno-div-by-zero
 @opindex Wno-div-by-zero
 @opindex Wdiv-by-zero
diff --git gcc/testsuite/gcc.dg/pr43245.c gcc/testsuite/gcc.dg/pr43245.c
index e69de29..85490ff 100644
--- gcc/testsuite/gcc.dg/pr43245.c
+++ gcc/testsuite/gcc.dg/pr43245.c
@@ -0,0 +1,18 @@
+/* PR c/43245 */
+/* { dg-do compile } */
+/* { dg-options "-Wno-discarded-qualifiers" } */
+
+void
+foo (char *p)
+{
+}
+
+char *
+bar (void)
+{
+  const char *s = "foo";
+  char *s1 = s;
+  s1 = s;
+  foo (s);
+  return s;
+}

	Marek



More information about the Gcc-patches mailing list