From patchwork Fri Mar 20 08:31:13 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1258708 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=nQGJ7Uiy; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48kH7165Cjz9sPF for ; Fri, 20 Mar 2020 19:31:24 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 966753945C30; Fri, 20 Mar 2020 08:31:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 966753945C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1584693081; bh=B/gQEckasz+NE2H1zi26pnHIoCYX1K2P4pCnkRIPsuM=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=nQGJ7Uiyvd5LbWvnvfkhdU1iwJoqRlJJ2BulUQizSy9gIZzO454TQIZWR98yOviQp grQNFHZ8VdZLgosFVFlrvmXKkiFgKbAeUH1sJuLl15dseaAwG/z0vHdHVFFT8OVeb9 sPY79dEZl3GueACAFE5zq1rS8L9V44+M0v4ZhoMQ= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-74.mimecast.com (us-smtp-delivery-74.mimecast.com [216.205.24.74]) by sourceware.org (Postfix) with ESMTP id 833383945C1B for ; Fri, 20 Mar 2020 08:31:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 833383945C1B Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-349-BUx--8R8PrCFX-Kl7mqWDA-1; Fri, 20 Mar 2020 04:31:17 -0400 X-MC-Unique: BUx--8R8PrCFX-Kl7mqWDA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2A814477 for ; Fri, 20 Mar 2020 08:31:16 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-112-22.ams2.redhat.com [10.36.112.22]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A04DC91290; Fri, 20 Mar 2020 08:31:15 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id 02K8VDv4028477; Fri, 20 Mar 2020 09:31:14 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id 02K8VDEY028476; Fri, 20 Mar 2020 09:31:13 +0100 Date: Fri, 20 Mar 2020 09:31:13 +0100 To: Jason Merrill Subject: [PATCH] c++: Handle COMPOUND_EXPRs in get_narrower and warnings_for_convert_and_check [PR91993] Message-ID: <20200320083113.GU2156@tucnak> References: <20200305075141.GX2156@tucnak> MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/1.11.3 (2019-02-01) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" On Wed, Mar 18, 2020 at 05:37:26PM -0400, Jason Merrill via Gcc-patches wrote: > How about improving get_narrower to handle COMPOUND_EXPR? I'd think that > would do the trick without affecting evaluation order. Not completely, had to change also warnings_for_convert_and_check, but with that it works. The float-cast-overflow* changes are needed because now with -O1+ we emit lots of -Woverflow warnings on the testcase, but we do emit those warnings on the testcase even when compiling just with -O1 and without -fsanitize=float-cast-overflow, so it seems to me a change in the right direction, having -fsanitize= or explicit comma expressions smaller effect on the warnings that are emitted. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2020-03-20 Jakub Jelinek PR c++/91993 * tree.c (get_narrower): Handle COMPOUND_EXPR by recursing on ultimate rhs and if returned something different, reconstructing the COMPOUND_EXPRs. * c-warn.c (warnings_for_convert_and_check): For expr and/or result being COMPOUND_EXPRs, skip to ultimate rhs. * g++.dg/warn/Wconversion-pr91993.C: New test. * g++.dg/ubsan/pr91993.C: New test. * c-c++-common/ubsan/float-cast-overflow-1.c: Add -Wno-overflow to dg-options. * c-c++-common/ubsan/float-cast-overflow-2.c: Likewise. * c-c++-common/ubsan/float-cast-overflow-4.c: Likewise. Jakub --- gcc/tree.c.jj 2020-03-17 13:50:52.382941623 +0100 +++ gcc/tree.c 2020-03-19 18:43:18.405499531 +0100 @@ -8862,6 +8862,21 @@ get_narrower (tree op, int *unsignedp_pt tree win = op; bool integral_p = INTEGRAL_TYPE_P (TREE_TYPE (op)); + if (TREE_CODE (op) == COMPOUND_EXPR) + { + while (TREE_CODE (op) == COMPOUND_EXPR) + op = TREE_OPERAND (op, 1); + tree ret = get_narrower (op, unsignedp_ptr); + if (ret == op) + return win; + op = win; + for (tree *p = &win; TREE_CODE (op) == COMPOUND_EXPR; + op = TREE_OPERAND (op, 1), p = &TREE_OPERAND (*p, 1)) + *p = build2_loc (EXPR_LOCATION (op), COMPOUND_EXPR, + TREE_TYPE (ret), TREE_OPERAND (op, 0), + ret); + return win; + } while (TREE_CODE (op) == NOP_EXPR) { int bitschange --- gcc/c-family/c-warn.c.jj 2020-01-23 19:17:24.072286895 +0100 +++ gcc/c-family/c-warn.c 2020-03-19 19:21:43.389730189 +0100 @@ -1359,6 +1359,11 @@ warnings_for_convert_and_check (location { loc = expansion_point_location_if_in_system_header (loc); + while (TREE_CODE (expr) == COMPOUND_EXPR) + expr = TREE_OPERAND (expr, 1); + while (TREE_CODE (result) == COMPOUND_EXPR) + result = TREE_OPERAND (result, 1); + bool cst = TREE_CODE_CLASS (TREE_CODE (result)) == tcc_constant; tree exprtype = TREE_TYPE (expr); --- gcc/testsuite/g++.dg/warn/Wconversion-pr91993.C.jj 2020-03-19 18:21:02.635049452 +0100 +++ gcc/testsuite/g++.dg/warn/Wconversion-pr91993.C 2020-03-19 18:21:02.635049452 +0100 @@ -0,0 +1,17 @@ +// PR c++/91993 +// { dg-do compile } +// { dg-options "-Wconversion" } + +typedef unsigned char uc; + +int +foo (const uc &a, const uc &b, const uc &c) +{ + return static_cast(static_cast(a << 1U) | b) | c; // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" } +} + +int +bar (const uc &a, const uc &b, const uc &c, int &d) +{ + return static_cast(static_cast((d++, a) << 1U) | b) | c; // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" } +} --- gcc/testsuite/g++.dg/ubsan/pr91993.C.jj 2020-03-19 18:21:02.635049452 +0100 +++ gcc/testsuite/g++.dg/ubsan/pr91993.C 2020-03-19 18:21:02.635049452 +0100 @@ -0,0 +1,17 @@ +// PR c++/91993 +// { dg-do compile } +// { dg-options "-Wconversion -fsanitize=undefined" } + +typedef unsigned char uc; + +int +foo (const uc &a, const uc &b, const uc &c) +{ + return static_cast(static_cast(a << 1U) | b) | c; // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" } +} + +int +bar (const uc &a, const uc &b, const uc &c, int &d) +{ + return static_cast(static_cast((d++, a) << 1U) | b) | c; // { dg-bogus "conversion from 'int' to 'unsigned char' may change value" } +} --- gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-1.c.jj 2020-01-12 11:54:37.029404115 +0100 +++ gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-1.c 2020-03-20 00:40:57.403648603 +0100 @@ -1,5 +1,5 @@ /* { dg-do run { target { lp64 || ilp32 } } } */ -/* { dg-options "-fsanitize=float-cast-overflow" } */ +/* { dg-options "-fsanitize=float-cast-overflow -Wno-overflow" } */ /* { dg-additional-options "-ffloat-store" { target { ia32 } } } */ /* { dg-additional-options "-mieee" { target { { alpha*-*-* } || { sh*-*-* } } } } */ --- gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-2.c.jj 2020-01-12 11:54:37.029404115 +0100 +++ gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-2.c 2020-03-20 00:41:21.757289152 +0100 @@ -1,6 +1,6 @@ /* { dg-do run } */ /* { dg-require-effective-target int128 } */ -/* { dg-options "-fsanitize=float-cast-overflow" } */ +/* { dg-options "-fsanitize=float-cast-overflow -Wno-overflow" } */ #include "float-cast.h" --- gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-4.c.jj 2020-01-12 11:54:37.029404115 +0100 +++ gcc/testsuite/c-c++-common/ubsan/float-cast-overflow-4.c 2020-03-20 00:41:32.053137185 +0100 @@ -1,5 +1,5 @@ /* { dg-do run { target { lp64 } } } */ -/* { dg-options "-fsanitize=float-cast-overflow" } */ +/* { dg-options "-fsanitize=float-cast-overflow -Wno-overflow" } */ #include #include "float-cast.h"