From patchwork Fri Oct 21 10:27:04 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 120974 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 B51381007D4 for ; Fri, 21 Oct 2011 21:29:20 +1100 (EST) Received: (qmail 4602 invoked by alias); 21 Oct 2011 10:29:18 -0000 Received: (qmail 4540 invoked by uid 22791); 21 Oct 2011 10:29:17 -0000 X-SWARE-Spam-Status: No, hits=-2.4 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; Fri, 21 Oct 2011 10:29:04 +0000 Received: from acsinet21.oracle.com (acsinet21.oracle.com [141.146.126.237]) by acsinet15.oracle.com (Switch-3.4.4/Switch-3.4.4) with ESMTP id p9LAT1mT027952 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 21 Oct 2011 10:29:03 GMT Received: from acsmt357.oracle.com (acsmt357.oracle.com [141.146.40.157]) by acsinet21.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id p9LAT0f2019864 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Fri, 21 Oct 2011 10:29:01 GMT Received: from abhmt114.oracle.com (abhmt114.oracle.com [141.146.116.66]) by acsmt357.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id p9LAStd8008526; Fri, 21 Oct 2011 05:28:55 -0500 Received: from [192.168.1.4] (/79.51.11.57) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Fri, 21 Oct 2011 03:28:55 -0700 Message-ID: <4EA148F8.40404@oracle.com> Date: Fri, 21 Oct 2011 12:27:04 +0200 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 / RFC] PR 45385 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 one is a bit subtler. It's actually a regression due to the fix for PR35602, which was about a bogus warning for: struct c { ~c(); c(); }; int main() { c x[0UL][0UL] = // { dg-bogus "warning: conversion to .long unsigned int. from .long int. may change the sign of the result" } { }; } The way we did it, we added a check at the beginning of c-common.c:conversion_warning: /* If any operand is artificial, then this expression was generated by the compiler and we do not warn. */ for (i = 0; i < expr_num_operands; i++) { tree op = TREE_OPERAND (expr, i); if (op && DECL_P (op) && DECL_ARTIFICIAL (op)) return; } which catches the artificial (only) operand of the expr (expr is a BIT_NOT_EXPR and the operand a VAR_DECL). Now, however, for testcases like PR45385, where member functions are involved, we easily fail to produce warnings, simply because the this pointer is artificial! Thus I had the idea of restricting the above check to the single operand case which matters for PR35602: for sure it's a safe change, and passes the testsuite, but I cannot exclude that more complex situations can occur for which the loop would avoid more bogus warnings... What do you think, is the change good enough for now? Thanks, Paolo. //////////////////// /c-family 2011-10-21 Paolo Carlini PR c++/45385 * c-common.c (conversion_warning): Early return only if the only operand is DECL_ARTIFICIAL. testsuite/ 2011-10-21 Paolo Carlini PR c++/45385 * g++.dg/warn/Wconversion4.C: New. Index: c-family/c-common.c =================================================================== --- c-family/c-common.c (revision 180288) +++ c-family/c-common.c (working copy) @@ -2121,19 +2121,17 @@ unsafe_conversion_p (tree type, tree expr, bool pr static void conversion_warning (tree type, tree expr) { - int i; - const int expr_num_operands = TREE_OPERAND_LENGTH (expr); tree expr_type = TREE_TYPE (expr); location_t loc = EXPR_LOC_OR_HERE (expr); if (!warn_conversion && !warn_sign_conversion) return; - /* If any operand is artificial, then this expression was generated - by the compiler and we do not warn. */ - for (i = 0; i < expr_num_operands; i++) + /* If the only operand is artificial, then the expression was generated + by the compiler and we do not warn. ???? */ + if (TREE_OPERAND_LENGTH (expr) == 1) { - tree op = TREE_OPERAND (expr, i); + tree op = TREE_OPERAND (expr, 0); if (op && DECL_P (op) && DECL_ARTIFICIAL (op)) return; } Index: testsuite/g++.dg/warn/Wconversion4.C =================================================================== --- testsuite/g++.dg/warn/Wconversion4.C (revision 0) +++ testsuite/g++.dg/warn/Wconversion4.C (revision 0) @@ -0,0 +1,17 @@ +// PR c++/45385 +// { dg-options "-Wconversion" } + +void foo(unsigned char); + +class Test +{ + void eval() + { + foo(bar()); // { dg-warning "may alter its value" } + } + + unsigned int bar() const + { + return __INT_MAX__ * 2U + 1; + } +};