This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCHES: PR 24667


When I rewrote C-style cast handling to make it standard-conformant, I
broke -Wcast-qual for C++.  Fixed with this patch.

Tested on x86_64-unknown-linux-gnu, applied on the mainline and on the
4.0 branch.

--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

2005-11-15  Mark Mitchell  <mark@codesourcery.com>

	PR c++/24667
	* typeck.c (check_for_casting_away_constness): Use the diag_fn.
	(build_const_cast_1): Call it, for C-style casts.

2005-11-15  Mark Mitchell  <mark@codesourcery.com>

	PR c++/24667
	* g++.dg/warn/Wcast-qual1.C: New test.

Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 106883)
+++ gcc/cp/typeck.c	(working copy)
@@ -4559,8 +4559,8 @@ check_for_casting_away_constness (tree s
 				  const char *description)
 {
   if (diag_fn && casts_away_constness (src_type, dest_type))
-    error ("%s from type %qT to type %qT casts away constness",
-	   description, src_type, dest_type);
+    diag_fn ("%s from type %qT to type %qT casts away constness",
+	     description, src_type, dest_type);
 }
 
 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
@@ -5085,9 +5085,9 @@ build_reinterpret_cast (tree type, tree 
 /* Perform a const_cast from EXPR to TYPE.  If the cast is valid,
    return an appropriate expression.  Otherwise, return
    error_mark_node.  If the cast is not valid, and COMPLAIN is true,
-   then a diagnostic will be issued.  If VALID_P is non-NULL, its
-   value upon return will indicate whether or not the conversion
-   succeeded.  */
+   then a diagnostic will be issued.  If VALID_P is non-NULL, we are
+   performing a C-style cast, its value upon return will indicate
+   whether or not the conversion succeeded.  */
 
 static tree
 build_const_cast_1 (tree dst_type, tree expr, bool complain,
@@ -5163,7 +5163,15 @@ build_const_cast_1 (tree dst_type, tree 
       && comp_ptr_ttypes_const (dst_type, src_type))
     {
       if (valid_p)
-	*valid_p = true;
+	{
+	  *valid_p = true;
+	  /* This cast is actually a C-style cast.  Issue a warning if
+	     the user is making a potentially unsafe cast.  */
+	  if (warn_cast_qual)
+	    check_for_casting_away_constness (src_type, dst_type,
+					      warning0,
+					      "cast");
+	}
       if (reference_type)
 	{
 	  expr = build_unary_op (ADDR_EXPR, expr, 0);
Index: gcc/testsuite/g++.dg/warn/Wcast-qual1.C
===================================================================
--- gcc/testsuite/g++.dg/warn/Wcast-qual1.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/Wcast-qual1.C	(revision 0)
@@ -0,0 +1,7 @@
+// PR c++/24667
+// { dg-options "-Wcast-qual" }
+
+int main(int, char**) {
+  const int foo[2] = {1,1};
+  ((int*)foo)[0] = 0; // { dg-warning "cast" }
+}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]