From patchwork Wed Sep 8 08:10:51 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 64117 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id DF466B6F06 for ; Wed, 8 Sep 2010 18:10:48 +1000 (EST) Received: (qmail 17774 invoked by alias); 8 Sep 2010 08:10:42 -0000 Received: (qmail 17757 invoked by uid 22791); 8 Sep 2010 08:10:36 -0000 X-SWARE-Spam-Status: No, hits=-6.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_HI, SPF_HELO_PASS, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 08 Sep 2010 08:10:27 +0000 Received: from int-mx03.intmail.prod.int.phx2.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o888AP0f013590 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 8 Sep 2010 04:10:26 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [10.16.42.4]) by int-mx03.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o888AOH2017314 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 8 Sep 2010 04:10:25 -0400 Received: from tyan-ft48-01.lab.bos.redhat.com (tyan-ft48-01.lab.bos.redhat.com [127.0.0.1]) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4) with ESMTP id o888AqWl031780; Wed, 8 Sep 2010 10:10:52 +0200 Received: (from jakub@localhost) by tyan-ft48-01.lab.bos.redhat.com (8.14.4/8.14.4/Submit) id o888Ap85031778; Wed, 8 Sep 2010 10:10:51 +0200 Date: Wed, 8 Sep 2010 10:10:51 +0200 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Fix -Wunused-but-set-* on const var used as array size in template (PR c++/45588) Message-ID: <20100908081051.GM1269@tyan-ft48-01.lab.bos.redhat.com> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-12-10) X-IsSubscribed: yes 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 Hi! Attached are two alternative patches to fix the attached testcase. For non-templates a similar issue has been fixed before by calling mark_rvalue_use in compute_array_index_type, but, but for templates the const var is changed for an INTEGER_CST already before it reaches that function, by fold_decl_constant_value. I have no idea when fold_decl_constant_value actually needs to loop, perhaps just the second patch should be sufficient (it is for the testcase, and also for template void foo () { const int i = S::k; const int j = i; const int k = j; unsigned char a[k]; bar (a); } foo body instead). Jakub 2010-09-08 Jakub Jelinek PR c++/45588 * pt.c (fold_decl_constant_value): Call mark_rvalue_use before calling integral_constant_value. * g++.dg/warn/Wunused-var-15.C: New test. 2010-09-08 Jakub Jelinek PR c++/45588 * pt.c (tsubst) : Call mark_rvalue_use before calling fold_decl_constant_value. * g++.dg/warn/Wunused-var-15.C: New test. --- gcc/cp/pt.c.jj 2010-09-06 08:41:35.000000000 +0200 +++ gcc/cp/pt.c 2010-09-08 09:54:39.000000000 +0200 @@ -10116,6 +10116,7 @@ tsubst (tree t, tree args, tsubst_flags_ && !TREE_TYPE (max)) TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0)); + max = mark_rvalue_use (max); max = fold_decl_constant_value (max); /* If we're in a partial instantiation, preserve the magic NOP_EXPR --- gcc/testsuite/g++.dg/warn/Wunused-var-15.C.jj 2010-09-08 09:32:24.000000000 +0200 +++ gcc/testsuite/g++.dg/warn/Wunused-var-15.C 2010-09-08 09:31:44.000000000 +0200 @@ -0,0 +1,29 @@ +// PR c++/45588 +// { dg-do compile } +// { dg-options "-Wunused" } + +void bar (unsigned char *); + +template +struct S +{ + static const int k = 6; +}; + +template +const int S::k; + +template +void +foo () +{ + const int i = S::k; + unsigned char a[i]; + bar (a); +} + +void +baz () +{ + foo<0> (); +} --- gcc/cp/pt.c.jj 2010-09-06 08:41:35.000000000 +0200 +++ gcc/cp/pt.c 2010-09-08 09:11:28.000000000 +0200 @@ -4854,6 +4854,7 @@ fold_decl_constant_value (tree expr) do { expr = fold_non_dependent_expr (const_expr); + expr = mark_rvalue_use (expr); const_expr = integral_constant_value (expr); } while (expr != const_expr); --- gcc/testsuite/g++.dg/warn/Wunused-var-15.C.jj 2010-09-08 09:32:24.000000000 +0200 +++ gcc/testsuite/g++.dg/warn/Wunused-var-15.C 2010-09-08 09:31:44.000000000 +0200 @@ -0,0 +1,29 @@ +// PR c++/45588 +// { dg-do compile } +// { dg-options "-Wunused" } + +void bar (unsigned char *); + +template +struct S +{ + static const int k = 6; +}; + +template +const int S::k; + +template +void +foo () +{ + const int i = S::k; + unsigned char a[i]; + bar (a); +} + +void +baz () +{ + foo<0> (); +}