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]

Re: PR c++/35711 bad text in -Wcast-qual warning


I have updated the patch to a recent revision, bootstrapped and
regression tested again.

OK for trunk?

Manuel.

2008/8/29 Manuel López-Ibáñez <lopezibanez@gmail.com>:
> The message mentions constness but it means any qualifiers.
> Unfortunately, with the current code is not obvious which qualifiers
> are casted away, so it is better to just say "qualifiers".
>
> Bootstrapped and regression tested on x86_64-unknown-linux-gnu with
> --enable-languages=all
>
> OK for trunk?
>
> 2008-08-26  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
>
>        PR c++/35711
> cp/
>        * typeck.c (check_for_casting_away_constness): We diagnose casting
>        away any qualifiers not just constness.
>        (casts_away_constness): Mention that it handles more than just
>        constness.
> testsuite/
>        * g++.dg/warn/pr35711.C: New.
>        * g++.dg/conversion/ptrmem2.C: Update.
>
Index: gcc/testsuite/g++.dg/conversion/ptrmem2.C
===================================================================
--- gcc/testsuite/g++.dg/conversion/ptrmem2.C	(revision 141210)
+++ gcc/testsuite/g++.dg/conversion/ptrmem2.C	(working copy)
@@ -29,11 +29,11 @@ int D::*p8 = static_cast<int D::*>(&A::x
 // Valid conversions which increase cv-qualification.
 const int B::*p9 = static_cast<const int B::*>(&D::x);
 const int D::*p10 = static_cast<const int D::*>(&B::x);
 
 // Invalid conversions which decrease cv-qualification.
-int B::*p11 = static_cast<int B::*>(p10); // { dg-error "casts away constness" }
-int D::*p12 = static_cast<int D::*>(p9);  // { dg-error "casts away constness" }
+int B::*p11 = static_cast<int B::*>(p10); // { dg-error "casts away qualifiers" }
+int D::*p12 = static_cast<int D::*>(p9);  // { dg-error "casts away qualifiers" }
 
 // Attempts to change member type.
 float B::*p13 = static_cast<float B::*>(&D::x); // { dg-error "" }
 float D::*p14 = static_cast<float D::*>(&B::x); // { dg-error "" }
Index: gcc/testsuite/g++.dg/warn/pr35711.C
===================================================================
--- gcc/testsuite/g++.dg/warn/pr35711.C	(revision 0)
+++ gcc/testsuite/g++.dg/warn/pr35711.C	(revision 0)
@@ -0,0 +1,8 @@
+// PR 35711
+// { dg-do compile }
+// { dg-options "-Wcast-qual" }
+
+int* foo (volatile int *p)
+{
+  return (int*)p; // { dg-warning "cast from type 'volatile int\\*' to type 'int\\*' casts away qualifiers" }
+}
Index: gcc/cp/typeck.c
===================================================================
--- gcc/cp/typeck.c	(revision 141210)
+++ gcc/cp/typeck.c	(working copy)
@@ -4932,42 +4932,49 @@ cp_build_compound_expr (tree lhs, tree r
 
   return build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
 }
 
 /* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
-   casts away constness.  CAST gives the type of cast.  */
+   casts away constness.  CAST gives the type of cast.  
+
+   ??? This function warns for casting away any qualifier not just
+   const.  We would like to specify exactly what qualifiers are casted
+   away.
+*/
 
 static void
 check_for_casting_away_constness (tree src_type, tree dest_type,
 				  enum tree_code cast)
 {
   /* C-style casts are allowed to cast away constness.  With
      WARN_CAST_QUAL, we still want to issue a warning.  */
   if (cast == CAST_EXPR && !warn_cast_qual)
       return;
   
-  if (casts_away_constness (src_type, dest_type))
-    switch (cast)
-      {
-      case CAST_EXPR:
-	warning (OPT_Wcast_qual, 
-                 "cast from type %qT to type %qT casts away constness",
-		 src_type, dest_type);
-	return;
-	
-      case STATIC_CAST_EXPR:
-	error ("static_cast from type %qT to type %qT casts away constness",
-	       src_type, dest_type);
-	return;
-	
-      case REINTERPRET_CAST_EXPR:
-	error ("reinterpret_cast from type %qT to type %qT casts away constness",
+  if (!casts_away_constness (src_type, dest_type))
+    return;
+
+  switch (cast)
+    {
+    case CAST_EXPR:
+      warning (OPT_Wcast_qual, 
+	       "cast from type %qT to type %qT casts away qualifiers",
 	       src_type, dest_type);
-	return;
-      default:
-	gcc_unreachable();
-      }
+      return;
+      
+    case STATIC_CAST_EXPR:
+      error ("static_cast from type %qT to type %qT casts away qualifiers",
+	     src_type, dest_type);
+      return;
+      
+    case REINTERPRET_CAST_EXPR:
+      error ("reinterpret_cast from type %qT to type %qT casts away qualifiers",
+	     src_type, dest_type);
+      return;
+    default:
+      gcc_unreachable();
+    }
 }
 
 /* Convert EXPR (an expression with pointer-to-member type) to TYPE
    (another pointer-to-member type in the same hierarchy) and return
    the converted expression.  If ALLOW_INVERSE_P is permitted, a
@@ -7295,11 +7302,16 @@ casts_away_constness_r (tree *t1, tree *
   *t1 = cp_build_qualified_type (*t1, quals1);
   *t2 = cp_build_qualified_type (*t2, quals2);
 }
 
 /* Returns nonzero if casting from TYPE1 to TYPE2 casts away
-   constness.  */
+   constness.  
+
+   ??? This function returns non-zero if casting away qualifiers not
+   just const.  We would like to return to the caller exactly which
+   qualifiers are casted away to give more accurate diagnostics.
+*/
 
 static bool
 casts_away_constness (tree t1, tree t2)
 {
   if (TREE_CODE (t2) == REFERENCE_TYPE)

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