From patchwork Wed Nov 2 17:17:06 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 123301 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id A5F5DB6F8F for ; Thu, 3 Nov 2011 04:18:07 +1100 (EST) Received: (qmail 12462 invoked by alias); 2 Nov 2011 17:18:04 -0000 Received: (qmail 12445 invoked by uid 22791); 2 Nov 2011 17:18:02 -0000 X-SWARE-Spam-Status: No, hits=-2.7 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from acsinet15.oracle.com (HELO acsinet15.oracle.com) (141.146.126.227) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 02 Nov 2011 17:17:34 +0000 Received: from acsinet22.oracle.com (acsinet22.oracle.com [141.146.126.238]) by acsinet15.oracle.com (Switch-3.4.4/Switch-3.4.4) with ESMTP id pA2HHKJW019446 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 2 Nov 2011 17:17:29 GMT Received: from acsmt358.oracle.com (acsmt358.oracle.com [141.146.40.158]) by acsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id pA2HHKVK024991 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 2 Nov 2011 17:17:20 GMT Received: from abhmt101.oracle.com (abhmt101.oracle.com [141.146.116.53]) by acsmt358.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id pA2HHEJk015309; Wed, 2 Nov 2011 12:17:14 -0500 Received: from [192.168.1.4] (/79.51.11.246) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 02 Nov 2011 10:17:14 -0700 Message-ID: <4EB17B12.1020005@oracle.com> Date: Wed, 02 Nov 2011 18:17:06 +0100 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20110929 Thunderbird/7.0.1 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill Subject: [C++ Patch] PR 50956 X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Hi, this should restore -Wcast-qual to the 3.4.x (!) behavior, more or less. The diff seems large but the new code is essentially in the last 5 lines. When fixing this issue the most subtle existing testcase to get right, IMHO, is c-c++-common/Wcast-qual-1.c. Duplicate warnings should not be an issue because, in input, the valid_p parameter of build_const_cast_1 essentially plays the role of c_cast_p afterwards and all the following check_for_casting_away_constness calls are protected by !c_cast_p. Tested x86_64-linux. Ok for mainline? Thanks, Paolo. ///////////////////////// /cp 2011-11-02 Paolo Carlini PR c++/50956 * typeck.c (build_const_cast_1): Fix -Wcast-qual for false comp_ptr_ttypes_const. /testsuite 2011-11-02 Paolo Carlini PR c++/50956 * g++.dg/warn/Wcast-qual2.C: New. Index: testsuite/g++.dg/warn/Wcast-qual2.C =================================================================== --- testsuite/g++.dg/warn/Wcast-qual2.C (revision 0) +++ testsuite/g++.dg/warn/Wcast-qual2.C (revision 0) @@ -0,0 +1,4 @@ +// PR c++/50956 +// { dg-options "-Wcast-qual" } + +void* p = (void*)"txt"; // { dg-warning "cast" } Index: cp/typeck.c =================================================================== --- cp/typeck.c (revision 180778) +++ cp/typeck.c (working copy) @@ -6345,34 +6345,41 @@ build_const_cast_1 (tree dst_type, tree expr, tsub 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) + if (comp_ptr_ttypes_const (dst_type, src_type)) { - *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 (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); + } } - 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)