]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR c++/50956 (-Wcast-qual does not work)
authorPaolo Carlini <paolo.carlini@oracle.com>
Wed, 2 Nov 2011 18:04:48 +0000 (18:04 +0000)
committerPaolo Carlini <paolo@gcc.gnu.org>
Wed, 2 Nov 2011 18:04:48 +0000 (18:04 +0000)
2011-11-02  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/50956
* builtins.c (fold_builtin_memchr): Fix cast.

/cp
2011-11-02  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/50956
* typeck.c (build_const_cast_1): Fix -Wcast-qual for false
comp_ptr_ttypes_const.

/testsuite
2011-11-02  Paolo Carlini  <paolo.carlini@oracle.com>

PR c++/50956
* g++.dg/warn/Wcast-qual2.C: New.

From-SVN: r180786

gcc/ChangeLog
gcc/builtins.c
gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/warn/Wcast-qual2.C [new file with mode: 0644]

index 70ae866463f989144949f50c98a55d5d261fa9f0..fe07de1b70fe8c3b9ffec3300d29b5c4008481c4 100644 (file)
@@ -1,3 +1,8 @@
+2011-11-02  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/50956
+       * builtins.c (fold_builtin_memchr): Fix cast.
+
 2011-11-02  Teresa Johnson  <tejohnson@google.com>
 
        * config/i386/predicates.md (promotable_binary_operator): Add minus
index 51d7b12e6875a5fecb40d9e6923a925b90aded3e..bad31659a83ae71a4d5d4ec99a82e1ea16e7abea 100644 (file)
@@ -8427,7 +8427,7 @@ fold_builtin_memchr (location_t loc, tree arg1, tree arg2, tree len, tree type)
          if (target_char_cast (arg2, &c))
            return NULL_TREE;
 
-         r = (char *) memchr (p1, c, tree_low_cst (len, 1));
+         r = (const char *) memchr (p1, c, tree_low_cst (len, 1));
 
          if (r == NULL)
            return build_int_cst (TREE_TYPE (arg1), 0);
index d246e5cc4ba29d22e6e4848ef8ccd217c7ab210d..90651e6e54061b4db32c890c29ea5b1996455982 100644 (file)
@@ -1,3 +1,9 @@
+2011-11-02  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/50956
+       * typeck.c (build_const_cast_1): Fix -Wcast-qual for false
+       comp_ptr_ttypes_const.
+
 2011-11-02  Rainer Orth  <ro@CeBiTec.Uni-Bielefeld.DE>
 
        * Make-lang.in (g++spec.o): Pass SHLIB instead of SHLIB_LINK.
index 8d70df5eb75f158c42b7ecf9018d12417dbac5f0..0b1f217a9085a68d391b126c070a417ef83e9dfe 100644 (file)
@@ -6345,34 +6345,41 @@ build_const_cast_1 (tree dst_type, tree expr, tsubst_flags_t complain,
        return error_mark_node;
     }
 
-  if ((TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
-      && comp_ptr_ttypes_const (dst_type, src_type))
+  if (TYPE_PTR_P (src_type) || TYPE_PTRMEM_P (src_type))
     {
-      if (valid_p)
-       {
-         *valid_p = true;
-         /* This cast is actually a C-style cast.  Issue a warning if
-            the user is making a potentially unsafe cast.  */
-         check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
-                                           complain);
-       }
-      if (reference_type)
+      if (comp_ptr_ttypes_const (dst_type, src_type))
        {
-         expr = cp_build_addr_expr (expr, complain);
-         expr = build_nop (reference_type, expr);
-         return convert_from_reference (expr);
-       }
-      else
-       {
-         expr = decay_conversion (expr);
-         /* build_c_cast puts on a NOP_EXPR to make the result not an
-            lvalue.  Strip such NOP_EXPRs if VALUE is being used in
-            non-lvalue context.  */
-         if (TREE_CODE (expr) == NOP_EXPR
-             && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
-           expr = TREE_OPERAND (expr, 0);
-         return build_nop (dst_type, expr);
+         if (valid_p)
+           {
+             *valid_p = true;
+             /* This cast is actually a C-style cast.  Issue a warning if
+                the user is making a potentially unsafe cast.  */
+             check_for_casting_away_constness (src_type, dst_type,
+                                               CAST_EXPR, complain);
+           }
+         if (reference_type)
+           {
+             expr = cp_build_addr_expr (expr, complain);
+             expr = build_nop (reference_type, expr);
+             return convert_from_reference (expr);
+           }
+         else
+           {
+             expr = decay_conversion (expr);
+             /* build_c_cast puts on a NOP_EXPR to make the result not an
+                lvalue.  Strip such NOP_EXPRs if VALUE is being used in
+                non-lvalue context.  */
+             if (TREE_CODE (expr) == NOP_EXPR
+                 && TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
+               expr = TREE_OPERAND (expr, 0);
+             return build_nop (dst_type, expr);
+           }
        }
+      else if (valid_p
+              && !at_least_as_qualified_p (TREE_TYPE (dst_type),
+                                           TREE_TYPE (src_type)))
+       check_for_casting_away_constness (src_type, dst_type, CAST_EXPR,
+                                         complain);
     }
 
   if (complain & tf_error)
index 057fd7c5973855b6fcd7ca89df9360fbc32a2b5a..aae9e36114492f4ab103a3bcaef4edc0e12163d0 100644 (file)
@@ -1,3 +1,8 @@
+2011-11-02  Paolo Carlini  <paolo.carlini@oracle.com>
+
+       PR c++/50956
+       * g++.dg/warn/Wcast-qual2.C: New.
+
 2011-11-02  Tom de Vries  <tom@codesourcery.com>
 
        PR tree-optimization/50763
diff --git a/gcc/testsuite/g++.dg/warn/Wcast-qual2.C b/gcc/testsuite/g++.dg/warn/Wcast-qual2.C
new file mode 100644 (file)
index 0000000..23dbb4d
--- /dev/null
@@ -0,0 +1,4 @@
+// PR c++/50956
+// { dg-options "-Wcast-qual" }
+
+void* p = (void*)"txt"; // { dg-warning "cast" }