From b7e20b53f60d973debcd771c882cc44b13db38b1 Mon Sep 17 00:00:00 2001 From: Gabriel Dos Reis Date: Thu, 9 Jun 2005 22:21:48 +0000 Subject: [PATCH] re PR c/21759 (Implement warning for codes at the intersection of C and C++) 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/ * gcc.dg/Wcxx-compat-1.c: New. From-SVN: r100806 --- gcc/ChangeLog | 8 ++++++++ gcc/c-typeck.c | 12 ++++++++++++ gcc/c.opt | 5 +++++ gcc/doc/invoke.texi | 7 ++++++- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/Wcxx-compat-1.c | 11 +++++++++++ 6 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/Wcxx-compat-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 77b50d8218ea..3c9b760cb970 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,11 @@ +2005-06-09 Gabriel Dos Reis + + 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*. + 2005-06-09 Gabriel Dos Reis * machmode.h (to_machine_mode): New. diff --git a/gcc/c-typeck.c b/gcc/c-typeck.c index 9429b73172c4..918afa0464f7 100644 --- a/gcc/c-typeck.c +++ b/gcc/c-typeck.c @@ -3779,6 +3779,18 @@ convert_for_assignment (tree type, tree rhs, enum impl_conv errtype, || 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 (OPT_Wc___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. diff --git a/gcc/c.opt b/gcc/c.opt index a055216e7063..ef8705db2e0a 100644 --- a/gcc/c.opt +++ b/gcc/c.opt @@ -128,6 +128,11 @@ Wbad-function-cast 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 diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index eef9a0f9c14f..95846f162739 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -219,7 +219,7 @@ Objective-C and Objective-C++ Dialects}. @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 +-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 @@ -2993,6 +2993,11 @@ to functions. 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 diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 92f5a6789aa5..7fc18e98ed69 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2005-06-09 Gabriel Dos Reis + + * gcc.dg/Wcxx-compat-1.c: New. + 2005-06-09 Thomas Koenig PR libfortran/21480 diff --git a/gcc/testsuite/gcc.dg/Wcxx-compat-1.c b/gcc/testsuite/gcc.dg/Wcxx-compat-1.c new file mode 100644 index 000000000000..91500421e852 --- /dev/null +++ b/gcc/testsuite/gcc.dg/Wcxx-compat-1.c @@ -0,0 +1,11 @@ +/* PR c/21759 */ +/* { dg-options "-Wc++-compat" } */ + +int +main(void) +{ + void *p = 0; + int *q = p; /* { dg-warning "not permitted" } */ + double* t = (void *)0; + return p != q; +} -- 2.43.5