re PR c++/35711 (bad text in -Wcast-qual warning (forgets volatile))
authorManuel López-Ibáñez <manu@gcc.gnu.org>
Tue, 21 Apr 2009 19:49:23 +0000 (19:49 +0000)
committerManuel López-Ibáñez <manu@gcc.gnu.org>
Tue, 21 Apr 2009 19:49:23 +0000 (19:49 +0000)
2009-04-21  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.

From-SVN: r146537

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/conversion/ptrmem2.C
gcc/testsuite/g++.dg/warn/pr35711.C [new file with mode: 0644]

index 5bca4b403dd75182b7cdeba68a9c3e7eadf6e0e0..99c78261654d96a4bafb23ca69acd864abb28bc3 100644 (file)
@@ -1,3 +1,11 @@
+2009-04-21  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
+
+       PR c++/35711
+       * 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.
+       
 2009-04-21  Joseph Myers  <joseph@codesourcery.com>
 
        * ChangeLog, ChangeLog-1993, ChangeLog-1994, ChangeLog-1995,
index 9084b5e10e0e66db933c99e345f01abaf6c0b12e..34a20b8af2a7b1b83125ab114134c2306d909901 100644 (file)
@@ -5038,7 +5038,12 @@ cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
 }
 
 /* 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,
@@ -5049,27 +5054,29 @@ check_for_casting_away_constness (tree src_type, tree dest_type,
   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
@@ -7362,7 +7369,12 @@ casts_away_constness_r (tree *t1, tree *t2)
 }
 
 /* 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)
index 7907ac5a96af743a9407435c648c0c5cb4577bb1..c144d658690139b2cd3cc85173767b7a83cfa92a 100644 (file)
@@ -1,3 +1,9 @@
+2009-04-21  Manuel Lopez-Ibanez  <manu@gcc.gnu.org>
+
+       PR c++/35711
+       * g++.dg/warn/pr35711.C: New.
+       * g++.dg/conversion/ptrmem2.C: Update.
+
 2009-04-21  Joseph Myers  <joseph@codesourcery.com>
 
        * ChangeLog, ChangeLog-1993-2007, ChangeLog-2008, ChangeLog.ptr,
index db39fc0345778b0956b7a6c2fe978aa8e2f62905..d46113db6c658f8d65a88608577046f187fce6c3 100644 (file)
@@ -31,8 +31,8 @@ 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 "" }
diff --git a/gcc/testsuite/g++.dg/warn/pr35711.C b/gcc/testsuite/g++.dg/warn/pr35711.C
new file mode 100644 (file)
index 0000000..653269c
--- /dev/null
@@ -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" }
+}