This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
PATCH to C front-end: PR 21759
- From: Gabriel Dos Reis <gdr at integrable-solutions dot net>
- To: gcc-patches at gcc dot gnu dot org
- Cc: Joseph Myers <jsm at polyomino dot org dot uk>, Richard Henderson <rth at redhat dot com>, bkoz at redhat dot com
- Date: 09 Jun 2005 18:30:34 +0200
- Subject: PATCH to C front-end: PR 21759
Hi,
As suggested here
http://gcc.gnu.org/ml/gcc/2005-05/msg01342.html
this patch is the first in a series for warning about ISO C constructs
that are outside the common subset of C and C++. This particular
patch warns about the implicit conversion void* -> T*, which is absent
from C++. I've made a special case for the null pointer constant
int *p = NULL; // #1
int *q = (void *) 0; // #2
because #1 is quite common, and in C++ NULL will expand to the "right"
expression and it is desired to keep the false positives as minimum as
possible. (If the C front-end used the GNU C++ trick of defining
NULL to __null, we would remove that special case altogether).
Bootstrapped and regtested on an i686-pc-linux-gnu. OK?
-- Gaby
2005-06-09 Gabriel Dos Reis <gdr@integrable-solutions.net>
PR c/21759
* c.opt (Wc++-compat): New.
* doc/invoke.texi (-Wc++-compat): Document.
* c-typeck.c (convert_for_assignment): Check for implicit
conversion void* -> T*.
testsuite:
2005-06-09 Gabriel Dos Reis <gdr@integrable-solutions.net>
* gcc.dg/Wcxx-compat-1.c: New.
Index: c-typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c-typeck.c,v
retrieving revision 1.451
diff -p -r1.451 c-typeck.c
*** c-typeck.c 9 Jun 2005 11:35:46 -0000 1.451
--- c-typeck.c 9 Jun 2005 15:07:25 -0000
*************** convert_for_assignment (tree type, tree
*** 3779,3784 ****
--- 3779,3796 ----
|| targetm.vector_opaque_p (rhstype))
&& TREE_CODE (ttl) == VECTOR_TYPE
&& TREE_CODE (ttr) == VECTOR_TYPE;
+
+ /* C++ does not allow the implicit conversion void* -> T*. However,
+ for the purpose of reducing the number of false positives, we
+ tolerate the special case of
+
+ int *p = NULL;
+
+ where NULL is typically defined in C to be '(void *) 0'. */
+ if (warn_cxx_compat && VOID_TYPE_P (ttr) && rhs != null_pointer_node
+ && !VOID_TYPE_P (ttl))
+ warning (OPT_Wc___compat, "request for implicit conversion from "
+ "%qT to %qT not permitted in C++", rhstype, type);
/* Any non-function converts to a [const][volatile] void *
and vice versa; otherwise, targets must be the same.
Index: c.opt
===================================================================
RCS file: /cvs/gcc/gcc/gcc/c.opt,v
retrieving revision 1.45
diff -p -r1.45 c.opt
*** c.opt 25 May 2005 03:58:57 -0000 1.45
--- c.opt 9 Jun 2005 15:07:25 -0000
*************** Wbad-function-cast
*** 128,133 ****
--- 128,138 ----
C ObjC Var(warn_bad_function_cast)
Warn about casting functions to incompatible types
+ Wc++-compat
+ C Var(warn_cxx_compat)
+ Warn about C constructs that are not in the common subset of C and C++
+
+
Wcast-qual
C ObjC C++ ObjC++ Var(warn_cast_qual)
Warn about casts which discard qualifiers
Index: doc/invoke.texi
===================================================================
RCS file: /cvs/gcc/gcc/gcc/doc/invoke.texi,v
retrieving revision 1.632
diff -p -r1.632 invoke.texi
*** doc/invoke.texi 6 Jun 2005 02:32:27 -0000 1.632
--- doc/invoke.texi 9 Jun 2005 15:07:36 -0000
*************** Objective-C and Objective-C++ Dialects}.
*** 219,225 ****
@xref{Warning Options,,Options to Request or Suppress Warnings}.
@gccoptlist{-fsyntax-only -pedantic -pedantic-errors @gol
-w -Wextra -Wall -Waggregate-return -Wno-attributes @gol
! -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment @gol
-Wconversion -Wno-deprecated-declarations @gol
-Wdisabled-optimization -Wno-div-by-zero -Wno-endif-labels @gol
-Werror -Werror-implicit-function-declaration @gol
--- 219,225 ----
@xref{Warning Options,,Options to Request or Suppress Warnings}.
@gccoptlist{-fsyntax-only -pedantic -pedantic-errors @gol
-w -Wextra -Wall -Waggregate-return -Wno-attributes @gol
! -Wc++-compat -Wcast-align -Wcast-qual -Wchar-subscripts -Wcomment @gol
-Wconversion -Wno-deprecated-declarations @gol
-Wdisabled-optimization -Wno-div-by-zero -Wno-endif-labels @gol
-Werror -Werror-implicit-function-declaration @gol
*************** to functions.
*** 2993,2998 ****
--- 2993,3003 ----
Warn whenever a function call is cast to a non-matching type.
For example, warn if @code{int malloc()} is cast to @code{anything *}.
+ @item -Wc++-compat
+ Warn about ISO C constructs that are outside of the common subset of
+ ISO C and ISO C++, e.g. request for implicit conversion from
+ @code{void *} to a pointer to non-@code{void} type.
+
@item -Wcast-qual
@opindex Wcast-qual
Warn whenever a pointer is cast so as to remove a type qualifier from
Index: testsuite/gcc.dg/Wcxx-compat-1.c
===================================================================
RCS file: testsuite/gcc.dg/Wcxx-compat-1.c
diff -N testsuite/gcc.dg/Wcxx-compat-1.c
*** /dev/null 1 Jan 1970 00:00:00 -0000
--- testsuite/gcc.dg/Wcxx-compat-1.c 9 Jun 2005 15:07:52 -0000
***************
*** 0 ****
--- 1,10 ----
+ /* PR c/21759 */
+ /* { dg-options "-Wc++-compat" } */
+
+ int
+ main(void)
+ {
+ void *p = 0;
+ int *q = p; /* { dg-warning "not permitted" } */
+ return p != q;
+ }