From patchwork Sat Dec 10 09:33:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 31995 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp1219857wrr; Sat, 10 Dec 2022 01:34:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf6Bpet+BddknTzu2WP1r8E6spreIBedGOjgZvJeC1bI3iRj7WwPTpBkfAHpHgI6uCsx0JsD X-Received: by 2002:a17:906:9f1d:b0:7ad:b9a0:8764 with SMTP id fy29-20020a1709069f1d00b007adb9a08764mr8991058ejc.6.1670664881178; Sat, 10 Dec 2022 01:34:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670664881; cv=none; d=google.com; s=arc-20160816; b=Dir80MOdrZp94SAcpE58GR4Ep2inDEy0gPGk31OVkiMq+0lOIHsbzZehy7Vtbi6I1m a+tHps8heAkRXF7l7VEPv3yPQ//ooqv/keX2KU/gsUTJYpTxtfSB+aiax0bfwl4202wb qnZpoSXMC0DA3vK7IMKdcF6rseDyEvZ8yOTbCCGlu+/8SXgxOzjgC3PCtLKYIPctrpHJ KSeD4eAJQh6a+joBVw87A/azomqja/qnYDAlokm/CDzdg4v5nYpMkKIBnwrzty4PZ+0X ncY68g9dcoIUMEV7A+XL2pJBmdiXWxur1dcROJrgy2dYQpFXjf6nxywH9jXjh9XcBRDg ReOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:cc:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=k5KlE2VlZpt4az2x1BHS82ppYaLM4lWVY1dHpmEV+o0=; b=FNFInVvwAvjaLNrTNSllaYK1qcUTDcAJa4/wfabOksW2LA6eAE7b6CYMiTPF1DKDRZ 1gGSAf7Ro6rnbKb2pzNahPnZawJv3ODLNPtpND2fFc3Uj/pGEN5hbusOeMZe683FUU1Z BxxxXo3REsHanaVdW3SFeW3Svm2B67dUwvvKTqMn0PS4Xa6ziSIBZmV9hCFHhJPLnYVD pPVHarCmIigxLbxvo4en5BHkzWAC5RGw4owGbn8/Bd9zAsQkgF4HmdNyR1w6AGmKAOrp E3uNeLIvkRZrT0e5BrEjyBYtu8tZ6YxSRnUGmxC87BvVhfd4PJLGITmbdn+7rhPXWrt1 porg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=l6L5puR0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id dz10-20020a0564021d4a00b0046c8ce8deccsi4120794edb.591.2022.12.10.01.34.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Dec 2022 01:34:41 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=l6L5puR0; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 21E26384DD9F for ; Sat, 10 Dec 2022 09:34:40 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 21E26384DD9F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670664880; bh=k5KlE2VlZpt4az2x1BHS82ppYaLM4lWVY1dHpmEV+o0=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=l6L5puR0ep/2cCFDRzODVbaLzCuddmfT4zPmknWDlgPLi55Yo2hv18oum2e9/yeY9 t2562Wy5A76hgSoV8FnsrR3BSjQcZcMx8+WZDtBpEnnn7F2fRHxYj5+I2zdoAM0NLo fJEWiGH21ZQ4H+h5fvYBGePZhniJDl5bJJh6RZx8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4D2E43850B0A for ; Sat, 10 Dec 2022 09:33:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4D2E43850B0A Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-70-ZFsjQPRaORmTXCek2qSuMQ-1; Sat, 10 Dec 2022 04:33:52 -0500 X-MC-Unique: ZFsjQPRaORmTXCek2qSuMQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D9B303C01D9E for ; Sat, 10 Dec 2022 09:33:51 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.195.114]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 949E8C15BA8; Sat, 10 Dec 2022 09:33:51 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 2BA9XkJG3888013 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 10 Dec 2022 10:33:47 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 2BA9Xkrb3888012; Sat, 10 Dec 2022 10:33:46 +0100 Date: Sat, 10 Dec 2022 10:33:46 +0100 To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Ensure !!var is not an lvalue [PR107065] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_TRUTHINESS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751819098919896384?= X-GMAIL-MSGID: =?utf-8?q?1751819098919896384?= Hi! The TRUTH_NOT_EXPR case in cp_build_unary_op is one of the spots where we somewhat fold immediately using invert_truthvalue_loc. I've tried using return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg); in there instead, but unfortunately that regressed Wlogical-not-parentheses-*.c pr49706.c pr62199.c pr65120.c sequence-pt-1.C tests, so at least for backporting that doesn't seem to be a way to go. So, this patch instead wraps it into NON_LVALUE_EXPR if needed (which also need a tweak for some tests in the pr47906.c test, but nothing major), with the intent to make it backportable, and later I'll try to do further steps to avoid folding here prematurely. Most of the problems with build1 TRUTH_NOT_EXPR are that it doesn't even invert comparisons as most common case and lots of warning code isn't able to deal with ! around comparisons; so perhaps one way to do this would be fold by hand only invertable comparisons and for the rest create TRUTH_NOT_EXPR. Anyway, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk (for now) and for release branches (after a while on the trunk)? 2022-12-10 Jakub Jelinek PR c++/107065 gcc/cp/ * typeck.cc (cp_build_unary_op) : If invert_truthvalue_loc returns obvalue_p, wrap it into NON_LVALUE_EXPR. * parser.cc (cp_parser_binary_expression): Don't call warn_logical_not_parentheses if current.lhs is a NON_LVALUE_EXPR of a decl with boolean type. gcc/testsuite/ * g++.dg/cpp0x/pr107065.C: New test. Jakub --- gcc/cp/typeck.cc.jj 2022-11-30 10:29:42.024701797 +0100 +++ gcc/cp/typeck.cc 2022-12-09 17:47:54.132856233 +0100 @@ -7396,9 +7396,13 @@ cp_build_unary_op (enum tree_code code, build_zero_cst (TREE_TYPE (arg)), complain); arg = perform_implicit_conversion (boolean_type_node, arg, complain); - val = invert_truthvalue_loc (location, arg); if (arg != error_mark_node) - return val; + { + val = invert_truthvalue_loc (location, arg); + if (obvalue_p (val)) + val = non_lvalue_loc (location, val); + return val; + } errstring = _("in argument to unary !"); break; --- gcc/cp/parser.cc.jj 2022-12-09 11:02:35.871444993 +0100 +++ gcc/cp/parser.cc 2022-12-09 19:14:26.698847734 +0100 @@ -10224,7 +10224,10 @@ cp_parser_binary_expression (cp_parser* || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0))) != BOOLEAN_TYPE)))) /* Avoid warning for !!b == y where b is boolean. */ - && (!DECL_P (tree_strip_any_location_wrapper (current.lhs)) + && (!(DECL_P (tree_strip_any_location_wrapper (current.lhs)) + || (TREE_CODE (current.lhs) == NON_LVALUE_EXPR + && DECL_P (tree_strip_any_location_wrapper + (TREE_OPERAND (current.lhs, 0))))) || TREE_TYPE (current.lhs) == NULL_TREE || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE)) warn_logical_not_parentheses (current.loc, current.tree_type, --- gcc/testsuite/g++.dg/cpp0x/pr107065.C.jj 2022-12-09 16:22:59.686548071 +0100 +++ gcc/testsuite/g++.dg/cpp0x/pr107065.C 2022-12-09 16:22:59.686548071 +0100 @@ -0,0 +1,14 @@ +// PR c++/107065 +// { dg-do compile { target c++11 } } + +template struct is_same { static constexpr bool value = false; }; +template struct is_same { static constexpr bool value = true; }; + +int +main () +{ + bool b = true; + static_assert (is_same::value, ""); + auto bb = (!(!b)); + static_assert (is_same::value, ""); +}