From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 277243858C52 for ; Fri, 1 Apr 2022 18:07:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 277243858C52 Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-313--KiGIY4-Oh6ML8uUx115yw-1; Fri, 01 Apr 2022 14:07:23 -0400 X-MC-Unique: -KiGIY4-Oh6ML8uUx115yw-1 Received: by mail-qt1-f199.google.com with SMTP id o15-20020ac8698f000000b002e1db0c88d0so2491721qtq.17 for ; Fri, 01 Apr 2022 11:07:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=HfRff14qWAq7Qa5MulEcyT6y78a748kThOlxLRpfM3g=; b=eio2wwJ3j5JHF9Jg29VDNoHUtlDxNtw8LNj1QkgQrsu6UlRngi7IQmEa/L5GVYlaGY ulzPY2T3SRZRPKyFH6Vhpzkmg+8RIhe3LVgjw2w+00rVDYRwSwk/wfVM85CcpnM3NPro WxyuRaDdwdy3uIioaFnmhWfokGzSbWoRLulGNHZ0cCSsZ5GKrcDt9T1QxqLvdXv2NuLN TJekC+1HOGiJVAHhl0o9c1TRu0Q/UKi1WKAXuFsnw9w3NOpgxgvPkG0+3WYXo7ZM4dfA RnGFekew2XZfoNfBT8nXSIUtZJRK5yIlw/uBiXlidHiVDJg3EwvyuSIKDv8bU3BgtZHD FCGg== X-Gm-Message-State: AOAM533/yiJMjdHRc7N5Un3RUs1kvry6YaSmeK/5NErsvkq8q3VxQSkF MHFcEY+jzVNM/I4kWecdINLwOwN0+0DmnQwpwXHScS4ja96QWmGRRaTbOF5dTwI11Zqdo3MhIlb WUVORvaoW/xV4aru1zA== X-Received: by 2002:ac8:5894:0:b0:2e1:c997:a61e with SMTP id t20-20020ac85894000000b002e1c997a61emr9367082qta.388.1648836443167; Fri, 01 Apr 2022 11:07:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw2NND4TsC6m50KPSFOc9MnjwoxbPtHC0aTgczY3ABuxpZCP8eX9gEOvTizEQ//MAJPVn7mbQ== X-Received: by 2002:ac8:5894:0:b0:2e1:c997:a61e with SMTP id t20-20020ac85894000000b002e1c997a61emr9367050qta.388.1648836442772; Fri, 01 Apr 2022 11:07:22 -0700 (PDT) Received: from [192.168.1.149] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id g21-20020ac85815000000b002e06e2623a7sm2171311qtg.0.2022.04.01.11.07.21 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Apr 2022 11:07:22 -0700 (PDT) Message-ID: <19a161d8-8308-785a-ef40-c91093a3d0f4@redhat.com> Date: Fri, 1 Apr 2022 14:07:21 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH] c-family: Tweak -Woverflow diagnostic To: Marek Polacek , GCC Patches , Joseph Myers References: <20220330222853.762944-1-polacek@redhat.com> From: Jason Merrill In-Reply-To: <20220330222853.762944-1-polacek@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 01 Apr 2022 18:07:26 -0000 On 3/30/22 18:28, Marek Polacek wrote: > When g++ emits > > warning: overflow in conversion from 'int' to 'char' changes value from '300' to '','' > > for code like "char c = 300;" it might raise a few eyebrows. With this > warning we're not interested in the ASCII representation of the char, only > the numerical value, so convert constants of type char to int. It looks > like this conversion only needs to be done for char_type_node. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? I'm also happy > to defer this to GCC 13. OK for stage 1. > gcc/c-family/ChangeLog: > > * c-warn.cc (warnings_for_convert_and_check): Convert constants of type > char to int. > > gcc/testsuite/ChangeLog: > > * c-c++-common/Wconversion-1.c: New test. > --- > gcc/c-family/c-warn.cc | 16 +++++++++++----- > gcc/testsuite/c-c++-common/Wconversion-1.c | 14 ++++++++++++++ > 2 files changed, 25 insertions(+), 5 deletions(-) > create mode 100644 gcc/testsuite/c-c++-common/Wconversion-1.c > > diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc > index f24ac5d0539..cae89294aea 100644 > --- a/gcc/c-family/c-warn.cc > +++ b/gcc/c-family/c-warn.cc > @@ -1404,8 +1404,14 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr, > result = TREE_OPERAND (result, 1); > > bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant; > - > tree exprtype = TREE_TYPE (expr); > + tree result_diag; > + /* We're interested in the actual numerical value here, not its ASCII > + representation. */ > + if (cst && TYPE_MAIN_VARIANT (TREE_TYPE (result)) == char_type_node) > + result_diag = fold_convert (integer_type_node, result); > + else > + result_diag = result; > > if (TREE_CODE (expr) == INTEGER_CST > && (TREE_CODE (type) == INTEGER_TYPE > @@ -1430,7 +1436,7 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr, > "changes value from %qE to %qE") > : G_("unsigned conversion from %qT to %qT " > "changes value from %qE to %qE")), > - exprtype, type, expr, result); > + exprtype, type, expr, result_diag); > else > warning_at (loc, OPT_Woverflow, > (TYPE_UNSIGNED (exprtype) > @@ -1449,7 +1455,7 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr, > warning_at (loc, OPT_Woverflow, > "overflow in conversion from %qT to %qT " > "changes value from %qE to %qE", > - exprtype, type, expr, result); > + exprtype, type, expr, result_diag); > else > warning_at (loc, OPT_Woverflow, > "overflow in conversion from %qT to %qT " > @@ -1466,7 +1472,7 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr, > warning_at (loc, OPT_Woverflow, > "overflow in conversion from %qT to %qT " > "changes value from %qE to %qE", > - exprtype, type, expr, result); > + exprtype, type, expr, result_diag); > else > warning_at (loc, OPT_Woverflow, > "overflow in conversion from %qT to %qT " > @@ -1483,7 +1489,7 @@ warnings_for_convert_and_check (location_t loc, tree type, tree expr, > warning_at (loc, OPT_Woverflow, > "overflow in conversion from %qT to %qT " > "changes value from %qE to %qE", > - exprtype, type, expr, result); > + exprtype, type, expr, result_diag); > else > warning_at (loc, OPT_Woverflow, > "overflow in conversion from %qT to %qT " > diff --git a/gcc/testsuite/c-c++-common/Wconversion-1.c b/gcc/testsuite/c-c++-common/Wconversion-1.c > new file mode 100644 > index 00000000000..ed65918c70f > --- /dev/null > +++ b/gcc/testsuite/c-c++-common/Wconversion-1.c > @@ -0,0 +1,14 @@ > +/* { dg-do compile } */ > +/* { dg-options "-Wconversion" } */ > + > +typedef char T; > + > +void g() > +{ > + char c = 300; /* { dg-warning "conversion from .int. to .char. changes value from .300. to .44." } */ > + T t = 300; /* { dg-warning "conversion from .int. to .T. {aka .char.} changes value from .300. to .44." } */ > + signed char sc = 300; /* { dg-warning "conversion from .int. to .signed char. changes value from .300. to .44." } */ > + unsigned char uc = 300; /* { dg-warning "conversion from .int. to .unsigned char. changes value from .300. to .44." } */ > + unsigned char uc2 = 300u; /* { dg-warning "conversion from .unsigned int. to .unsigned char. changes value from .300. to .44." } */ > + char c2 = (double)1.0 + 200; /* { dg-warning "overflow in conversion from .double. to .char. changes value from .2.01e\\+2. to .127." } */ > +} > > base-commit: b4e4b35f4ebe561826489bed971324efc99c5423