From patchwork Thu Jun 23 15:07:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1647217 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=vor9A2SS; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4LTNvD59h5z9sGP for ; Fri, 24 Jun 2022 01:09:12 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5221E385609E for ; Thu, 23 Jun 2022 15:09:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5221E385609E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1655996949; bh=Upwyivq4mVVhOj2AhiwVRO0tVtYBUKBAk2vmgG4/E1s=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=vor9A2SS7YM9OztNOyLsxldF6FjaHNz21Sk2KT1ZCzgARZgbnYt0EvXWWJ9bf6ubv HgjAKx0q1zHlauyoVW/L/AO2gJ33weKX1BiIO6Rla5n45FE1bGXvhs0kUtDnZdOKrl Jm+6+TuTw1dSrTjQ/gVpUVRRwqrCSFKgrKtRW//k= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 2867F38560B2 for ; Thu, 23 Jun 2022 15:07:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 2867F38560B2 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-441-wCeq9lVrNE2eG7hbgCgFSA-1; Thu, 23 Jun 2022 11:07:55 -0400 X-MC-Unique: wCeq9lVrNE2eG7hbgCgFSA-1 Received: by mail-qv1-f71.google.com with SMTP id g29-20020a0caadd000000b004702ed3c3f5so14934502qvb.11 for ; Thu, 23 Jun 2022 08:07:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Upwyivq4mVVhOj2AhiwVRO0tVtYBUKBAk2vmgG4/E1s=; b=NKULaIBp8KPcPObiiB//5Q32tmMt0s6js7b0d+6KlMIX1KlInKK+mIEOKVlByOtYxx fYlpprvcMq9kWtyhuU8FaQNlRYz+D2inGdxAF/aWa0bPgnXuSFqNwYklat1aj6rwivUd rh/x4+v0Dbe6o6FitD9Vhspb0TxenjLxnMGrmcRdX1x/hyoKWs1wETqc8U91HZMXK8rG QE9/+oXnRI5Vs9cjBN9nLCNu72CJMwM1VgRLUeOLEAMmf8sxgBtX3QT5In0k+dZeVGKo HSeL0UIPtB5qRCJA4yvG0DxL5St6preU2nDixgthqIQfQa+vYWi/FWM6QanSOtvJulWx M62Q== X-Gm-Message-State: AJIora/YeQ7m01/l0/KIaTpmOx2SJYz9jPE4mGL2LwyVUWAK6er/+eSw vLyfruI+tYahX9bCsuMCwefYD7YlWeYem9fTqGQE9RDrGOguTqXCR+91Q9EpsPSOOCleIhBtN++ 8VXkljTAOKfcsdaOGPqtog8GqlExsXmDZ6y86pYM7KmKBEoUncN6TaNumUyHSd53DGA== X-Received: by 2002:a05:620a:c55:b0:6a7:9f07:f41 with SMTP id u21-20020a05620a0c5500b006a79f070f41mr6867090qki.430.1655996874710; Thu, 23 Jun 2022 08:07:54 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uwwo3zGI95HHU2yQPLHGzM2BKwMX4IBLfZVebaoZzlu9KWKJfVkOiTsIPjBa7G2AX0eHKIpQ== X-Received: by 2002:a05:620a:c55:b0:6a7:9f07:f41 with SMTP id u21-20020a05620a0c5500b006a79f070f41mr6867010qki.430.1655996873738; Thu, 23 Jun 2022 08:07:53 -0700 (PDT) Received: from barrymore.redhat.com (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 t9-20020a37ea09000000b006a6be577535sm19565454qkj.85.2022.06.23.08.07.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 08:07:53 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: -Waddress and if constexpr [PR94554] Date: Thu, 23 Jun 2022 11:07:51 -0400 Message-Id: <20220623150751.114851-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20220623150659.114599-1-jason@redhat.com> References: <20220623150659.114599-1-jason@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Like we avoid various warnings for seemingly tautological expressions when substituting a template, we should avoid warning for the implicit conversion to bool in an if statement. I considered also doing this for the conditions in loop expressions, but that seems unnecessary, as a loop condition is unlikely to be a constant. The change to finish_if_stmt_cond isn't necessary since dependent_operand_p looks through IMPLICIT_CONV_EXPR, but makes it more constent with e.g. build_x_binary_op that determines the type of an expression and then builds it using the original operands. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/94554 gcc/cp/ChangeLog: * pt.cc (dependent_operand_p): Split out from... (tsubst_copy_and_build): ...here. (tsubst_expr) [IF_STMT]: Use it. * semantics.cc (finish_if_stmt_cond): Keep the pre-conversion condition in the template tree. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/constexpr-if38.C: New test. --- gcc/cp/pt.cc | 43 +++++++++++++++------ gcc/cp/semantics.cc | 10 +++-- gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C | 16 ++++++++ 3 files changed, 55 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C base-commit: 6e4d5300c1f62c3f0cd1bf859b0ee6bb4e31e434 diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 4535c14c26a..12a2b57d9e3 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -18547,6 +18547,29 @@ lookup_init_capture_pack (tree decl) return r; } +/* T is an operand of a template tree being substituted. Return whether + T is dependent such that we should suppress some warnings that would + make sense if the substituted expression were written directly, like + template bool f() { return I == 2; } + We don't want to warn when instantiating f that comparing two constants + always has the same value. + + This is a more limited concept of dependence than instantiation-dependent; + here we don't care whether substitution could fail. */ + +static bool +dependent_operand_p (tree t) +{ + while (TREE_CODE (t) == IMPLICIT_CONV_EXPR) + t = TREE_OPERAND (t, 0); + ++processing_template_decl; + bool r = (potential_constant_expression (t) + ? value_dependent_expression_p (t) + : type_dependent_expression_p (t)); + --processing_template_decl; + return r; +} + /* Like tsubst_copy for expressions, etc. but also does semantic processing. */ @@ -18872,8 +18895,13 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t); if (IF_STMT_CONSTEXPR_P (t)) args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args, complain, in_decl); - tmp = RECUR (IF_COND (t)); - tmp = finish_if_stmt_cond (tmp, stmt); + { + tree cond = IF_COND (t); + bool was_dep = dependent_operand_p (cond); + cond = RECUR (cond); + warning_sentinel s1(warn_address, was_dep); + tmp = finish_if_stmt_cond (cond, stmt); + } if (IF_STMT_CONSTEXPR_P (t) && instantiation_dependent_expression_p (tmp)) { @@ -20422,15 +20450,8 @@ tsubst_copy_and_build (tree t, warnings that depend on the range of the types involved. */ tree op0 = TREE_OPERAND (t, 0); tree op1 = TREE_OPERAND (t, 1); - auto dep_p = [](tree t) { - ++processing_template_decl; - bool r = (potential_constant_expression (t) - ? value_dependent_expression_p (t) - : type_dependent_expression_p (t)); - --processing_template_decl; - return r; - }; - const bool was_dep = dep_p (op0) || dep_p (op1); + const bool was_dep = (dependent_operand_p (op0) + || dependent_operand_p (op1)); op0 = RECUR (op0); op1 = RECUR (op1); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 24cca99e909..2344b5eea00 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -1029,9 +1029,9 @@ maybe_warn_for_constant_evaluated (tree cond, bool constexpr_if) IF_STMT. */ tree -finish_if_stmt_cond (tree cond, tree if_stmt) +finish_if_stmt_cond (tree orig_cond, tree if_stmt) { - cond = maybe_convert_cond (cond); + tree cond = maybe_convert_cond (orig_cond); if (IF_STMT_CONSTEXPR_P (if_stmt) && !type_dependent_expression_p (cond) && require_constant_expression (cond) @@ -1045,7 +1045,11 @@ finish_if_stmt_cond (tree cond, tree if_stmt) cond = cxx_constant_value (cond, NULL_TREE); } else - maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false); + { + maybe_warn_for_constant_evaluated (cond, /*constexpr_if=*/false); + if (processing_template_decl) + cond = orig_cond; + } finish_cond (&IF_COND (if_stmt), cond); add_stmt (if_stmt); THEN_CLAUSE (if_stmt) = push_stmt_list (); diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C new file mode 100644 index 00000000000..3e4fd446dca --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if38.C @@ -0,0 +1,16 @@ +// PR c++/94554 +// { dg-do compile { target c++17 } } +// { dg-additional-options -Wall } + +int meow() { return 1; } +void kitty(int); +template +void test() { + if constexpr (F) { + kitty(F()); + } else { + kitty(2); + } +} +template void test(); +template void test();