From patchwork Fri Aug 16 21:29:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 1148466 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-507153-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="cu0+9EkE"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 469Gfd4SPBz9sML for ; Sat, 17 Aug 2019 07:29:13 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=Np1xTDCSFHpYN5oA8zbFsh5qmC6cKX2Vv2dzHz9jEI1GXiJXHPvS0 LrdfqHFZam+q+w8EN1V8y10Qgj3f8pxbvGfWVNI4GEwaZJdPIb77u3jbzHzokuVF Rg6r3tHuVyt3ToX+yTgF+DulIxVjRDDOB1F9TRLF6oB03O9ybta2Yo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=pb7foBvai7++tShgboBg6w2os1M=; b=cu0+9EkE9cIGk7m9OVqN O0+KLfgBSL4RKT1z6FFKLGhBz+VXtjK43i/d/3IPacBzKrc+/bOHiltZi/KMfzdG CdI2HolLOTk+5BQfdR4U8TJCdsCdmMv6WSynRvaMeKwkWAScV07bF0JGaR1UsLTj wK1r89JMJM8PHbCf4BEuKx8= Received: (qmail 108535 invoked by alias); 16 Aug 2019 21:29:05 -0000 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 Received: (qmail 108527 invoked by uid 89); 16 Aug 2019 21:29:05 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.6 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=instantiations X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 16 Aug 2019 21:29:03 +0000 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BA1F0C057EC6 for ; Fri, 16 Aug 2019 21:29:02 +0000 (UTC) Received: from redhat.com (ovpn-122-82.rdu2.redhat.com [10.10.122.82]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 16FBE1001944; Fri, 16 Aug 2019 21:29:01 +0000 (UTC) Date: Fri, 16 Aug 2019 17:29:00 -0400 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if Message-ID: <20190816212900.GV14737@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.12.1 (2019-06-15) This patch is an attempt to fix the annoying -Wunused-but-set-* warnings that tend to occur with constexpr if. When we have something like template < typename T > int f(T v){ if constexpr(sizeof(T) == sizeof(int)){ return v; }else{ return 0; } } and call f('a'), then the condition is false, meaning that we won't instantiate the then-branch, as per tsubst_expr/IF_STMT: 17284 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) 17285 /* Don't instantiate the THEN_CLAUSE. */; so we'll never get round to mark_exp_read-ing the decls used in the then-branch, causing finish_function to emit "parameter set but not used" warnings. It's unclear how to best deal with this. Marking the decls DECL_READ_P while parsing doesn't seem like a viable approach, and since in this testcase the decl is type-dependent, doing something similiar to the fix for c++/85827 is out too. One option is the below, still don't instantiate anything in such a dead clause, but mark any use of a decl in it as a read. Which means that "set but unused" won't be warned about, but that's still better than bogus warnings. (Clang doesn't seem to handle that, either.) retrieve_local_specialization is there because we need to set the flag on the aready instantiated PARM/VAR_DECL, not the template itself. The good news is that when a param/decl is really unused, we still get the warning. And it works with lambdas too. Better ideas, anyone? (If anyone's wondering, constexpr function templates don't have this problem.) Bootstrapped/regtested on x86_64-linux, ok for trunk? 2019-08-16 Marek Polacek PR c++/81676 - bogus -Wunused warnings in constexpr if. * pt.c (maybe_mark_exp_read_r): New function. (tsubst_expr) : Call it for an uninstantiated THEN_CLAUSE and ELSE_CLAUSE. * g++.dg/cpp1z/constexpr-if30.C: New test. * g++.dg/cpp1z/constexpr-if31.C: New test. diff --git gcc/cp/pt.c gcc/cp/pt.c index 17585119bce..e146ad38443 100644 --- gcc/cp/pt.c +++ gcc/cp/pt.c @@ -16995,6 +16995,23 @@ lookup_init_capture_pack (tree decl) return r; } +/* Callback for cp_walk_tree to mark all instantiations of {VAR,PARM}_DECLs + in a tree as read. The point is to mark the parameters or local variables + of a template function as read to prevent various -Wunused warnings. */ + +static tree +maybe_mark_exp_read_r (tree *tp, int *, void *) +{ + tree t = *tp; + if (VAR_P (t) || TREE_CODE (t) == PARM_DECL) + { + tree s = retrieve_local_specialization (t); + if (s) + mark_exp_read (s); + } + return NULL_TREE; +} + /* Like tsubst_copy for expressions, etc. but also does semantic processing. */ @@ -17282,7 +17299,10 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, break; } if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp)) - /* Don't instantiate the THEN_CLAUSE. */; + /* Don't instantiate the THEN_CLAUSE. But mark the PARM_DECLs and + VAR_DECLs used in the THEN_CLAUSE as read, so as to suppress + -Wunused-but-set-{variable,parameter} warnings. */ + cp_walk_tree (&THEN_CLAUSE (t), maybe_mark_exp_read_r, NULL, NULL); else { tree folded = fold_non_dependent_expr (tmp, complain); @@ -17296,7 +17316,10 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl, finish_then_clause (stmt); if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp)) - /* Don't instantiate the ELSE_CLAUSE. */; + /* Don't instantiate the ELSE_CLAUSE. But mark the PARM_DECLs and + VAR_DECLs used in the ELSE_CLAUSE as read, so as to suppress + -Wunused-but-set-{variable,parameter} warnings. */ + cp_walk_tree (&ELSE_CLAUSE (t), maybe_mark_exp_read_r, NULL, NULL); else if (ELSE_CLAUSE (t)) { tree folded = fold_non_dependent_expr (tmp, complain); diff --git gcc/testsuite/g++.dg/cpp1z/constexpr-if30.C gcc/testsuite/g++.dg/cpp1z/constexpr-if30.C new file mode 100644 index 00000000000..26e0c6f39a6 --- /dev/null +++ gcc/testsuite/g++.dg/cpp1z/constexpr-if30.C @@ -0,0 +1,79 @@ +// PR c++/81676 - bogus -Wunused warnings in constexpr if. +// { dg-do compile { target c++17 } } +// { dg-options "-Wunused-variable -Wunused-parameter" } + +template int +f1 (T v) +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return v + x; + else + return 0; +} + +template int +f2 (T v) // { dg-warning "unused parameter .v." } +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return x; + else + return 0; +} + +template int +f3 (T v) +{ + T x = 0; // { dg-warning "unused variable .x." } + if constexpr(sizeof(T) == sizeof(int)) + return v; + else + return 0; +} + +template int +f4 (T v) +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return 0; + else + return v + x; +} + +template int +f5 (T v) // { dg-warning "unused parameter .v." } +{ + T x = 0; + if constexpr(sizeof(T) == sizeof(int)) + return 0; + else + return x; +} + +template int +f6 (T v) +{ + T x = 0; // { dg-warning "unused variable .x." } + if constexpr(sizeof(T) == sizeof(int)) + return 0; + else + return v; +} + +int main() +{ + f1(0); + f1('a'); + f2(0); + f2('a'); + f3(0); + f3('a'); + f4(0); + f4('a'); + f5(0); + f5('a'); + f6(0); + f6('a'); +} diff --git gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C new file mode 100644 index 00000000000..a1db91a5b40 --- /dev/null +++ gcc/testsuite/g++.dg/cpp1z/constexpr-if31.C @@ -0,0 +1,16 @@ +// PR c++/81676 - bogus -Wunused warnings in constexpr if. +// { dg-do compile { target c++17 } } +// { dg-options "-Wunused-variable -Wunused-parameter" } + +int main() +{ + auto f = [](auto a, auto b) { + if constexpr (sizeof(b) == 1) { + return a; + } else { + return b; + } + }; + + return f(1, 1) + f(1, 'a'); +}