Patchwork [C] Add -Wincompatible-pointer-types option (PR c/58286)

login
register
mail settings
Submitter Marek Polacek
Date June 30, 2014, 6:30 p.m.
Message ID <20140630183056.GD20427@redhat.com>
Download mbox | patch
Permalink /patch/365713/
State New
Headers show

Comments

Marek Polacek - June 30, 2014, 6:30 p.m.
This patch adds the -Wincompatible-pointer-types option for a warning
we already have, so it's possible to suppress this specific warning, or
use it with -Werror= and so on.
As a followup change, I'm considering printing the types of the pointers;
saying merely e.g. "assignment from incompatible pointer type" seems to be too
austere.  (We say "expected T but argument is of type U" when passing arguments.)
This is for C/ObjC only, since in C++, we'd issue "cannot convert" error.

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2014-06-30  Marek Polacek  <polacek@redhat.com>

	PR c/58286
	* doc/invoke.texi: Document -Wincompatible-pointer-types.
c-family/
	* c.opt (Wincompatible-pointer-types): New option.
c/
	* c-typeck.c (convert_for_assignment): Pass
	OPT_Wincompatible_pointer_types instead of 0 to WARN_FOR_ASSIGNMENT.
testsuite/
	* gcc.dg/Wincompatible-pointer-types.c: New test.


	Marek
Joseph S. Myers - June 30, 2014, 8:14 p.m.
On Mon, 30 Jun 2014, Marek Polacek wrote:

> This patch adds the -Wincompatible-pointer-types option for a warning
> we already have, so it's possible to suppress this specific warning, or
> use it with -Werror= and so on.
> As a followup change, I'm considering printing the types of the pointers;
> saying merely e.g. "assignment from incompatible pointer type" seems to be too
> austere.  (We say "expected T but argument is of type U" when passing arguments.)
> This is for C/ObjC only, since in C++, we'd issue "cannot convert" error.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?

OK with the documentation amended to make clear this is for the cases not 
covered by -Wno-pointer-sign (which in ISO C terms are just as 
incompatible as the cases covered by the new option).

Patch

diff --git gcc/c-family/c.opt gcc/c-family/c.opt
index 1d02bae..6448b1b 100644
--- gcc/c-family/c.opt
+++ gcc/c-family/c.opt
@@ -443,6 +443,10 @@  Wignored-qualifiers
 C C++ Var(warn_ignored_qualifiers) Warning EnabledBy(Wextra)
 Warn whenever type qualifiers are ignored.
 
+Wincompatible-pointer-types
+C ObjC Var(warn_incompatible_pointer_types) Init(1) Warning
+Warn when there is a conversion between pointers that have incompatible types
+
 Winit-self
 C ObjC C++ ObjC++ Var(warn_init_self) Warning LangEnabledBy(C++ ObjC++,Wall)
 Warn about variables which are initialized to themselves
diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index b62e830..fff26a3 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -6189,7 +6189,8 @@  convert_for_assignment (location_t location, location_t expr_loc, tree type,
       else
 	/* Avoid warning about the volatile ObjC EH puts on decls.  */
 	if (!objc_ok)
-	  WARN_FOR_ASSIGNMENT (location, expr_loc, 0,
+	  WARN_FOR_ASSIGNMENT (location, expr_loc,
+			       OPT_Wincompatible_pointer_types,
 			       G_("passing argument %d of %qE from "
 				  "incompatible pointer type"),
 			       G_("assignment from incompatible pointer type"),
diff --git gcc/doc/invoke.texi gcc/doc/invoke.texi
index dbc1132..dfae4f0 100644
--- gcc/doc/invoke.texi
+++ gcc/doc/invoke.texi
@@ -251,7 +251,7 @@  Objective-C and Objective-C++ Dialects}.
 -Wno-format-contains-nul -Wno-format-extra-args -Wformat-nonliteral @gol
 -Wformat-security  -Wformat-signedness  -Wformat-y2k @gol
 -Wframe-larger-than=@var{len} -Wno-free-nonheap-object -Wjump-misses-init @gol
--Wignored-qualifiers @gol
+-Wignored-qualifiers  -Wincompatible-pointer-types @gol
 -Wimplicit  -Wimplicit-function-declaration  -Wimplicit-int @gol
 -Winit-self  -Winline @gol
 -Wno-int-to-pointer-cast -Wno-invalid-offsetof @gol
@@ -4199,14 +4199,19 @@  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
+@item -Wno-discarded-qualifiers @r{(C and Objective-C only)}
 @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.
+can be used to suppress such a warning.
+
+@item -Wno-incompatible-pointer-types @r{(C and Objective-C only)}
+@opindex Wno-incompatible-pointer-types
+@opindex Wincompatible-pointer-types
+Do not warn when there is a conversion between pointers that have incompatible
+types.
 
 @item -Wno-div-by-zero
 @opindex Wno-div-by-zero
diff --git gcc/testsuite/gcc.dg/Wincompatible-pointer-types.c gcc/testsuite/gcc.dg/Wincompatible-pointer-types.c
index e69de29..e765b27 100644
--- gcc/testsuite/gcc.dg/Wincompatible-pointer-types.c
+++ gcc/testsuite/gcc.dg/Wincompatible-pointer-types.c
@@ -0,0 +1,21 @@ 
+/* PR c/58286 */
+/* { dg-do compile } */
+/* { dg-options "-Wno-incompatible-pointer-types" } */
+
+void
+fn2 (short *s, long *l)
+{
+}
+
+unsigned *
+fn1 (void)
+{
+  int (*fpi) (int);
+  int (*fpd) (double) = fpi;
+  fpi = fpd;
+  char *di;
+  float *dp = &di;
+  di = dp;
+  fn2 (dp, di);
+  return dp;
+}