From patchwork Tue Mar 8 05:21:55 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 85903 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 AA399B6EE8 for ; Tue, 8 Mar 2011 16:22:14 +1100 (EST) Received: (qmail 12366 invoked by alias); 8 Mar 2011 05:22:11 -0000 Received: (qmail 12357 invoked by uid 22791); 8 Mar 2011 05:22:09 -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; Tue, 08 Mar 2011 05:21:58 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id p285Lv5o012471 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 8 Mar 2011 00:21:57 -0500 Received: from [127.0.0.1] (ovpn-113-31.phx2.redhat.com [10.3.113.31]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id p285Luok008467 for ; Tue, 8 Mar 2011 00:21:56 -0500 Message-ID: <4D75BCF3.4050705@redhat.com> Date: Tue, 08 Mar 2011 00:21:55 -0500 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.7 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH for c++/48003 (allow integer overflow in template args with -fpermissive) 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 This patch changes convert_nontype_argument to defer to cxx_constant_value on whether or not to go ahead with a semi-constant template argument, and whether or not to give a diagnostic. Making this change revealed a bug in potential_constant_expression_1, which was failing to check for overflow. Tested x86_64-pc-linux-gnu, applying to trunk. commit 0e5ec26f6aeca92278e290181b3bd66f9344953f Author: Jason Merrill Date: Mon Mar 7 16:17:21 2011 -0500 PR c++/48003 * pt.c (convert_nontype_argument): Fix -fpermissive allowing integer overflow. * semantics.c (potential_constant_expression_1): Check TREE_OVERFLOW. diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index dfc9728..076224c 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -5402,11 +5402,19 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) { if (complain & tf_error) { - error ("%qE is not a valid template argument for type %qT " - "because it is a non-constant expression", expr, type); - cxx_constant_value (expr); + int errs = errorcount, warns = warningcount; + expr = cxx_constant_value (expr); + if (errorcount > errs || warningcount > warns) + inform (EXPR_LOC_OR_HERE (expr), + "in template argument for type %qT ", type); + if (expr == error_mark_node) + return NULL_TREE; + /* else cxx_constant_value complained but gave us + a real constant, so go ahead. */ + gcc_assert (TREE_CODE (expr) == INTEGER_CST); } - return NULL_TREE; + else + return NULL_TREE; } } /* [temp.arg.nontype]/5, bullet 2 diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index 52a962d..a0f48c0e 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7275,7 +7275,20 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags) return false; } if (CONSTANT_CLASS_P (t)) - return true; + { + if (TREE_OVERFLOW (t)) + { + if (flags & tf_error) + { + permerror (EXPR_LOC_OR_HERE (t), + "overflow in constant expression"); + if (flag_permissive) + return true; + } + return false; + } + return true; + } switch (TREE_CODE (t)) { diff --git a/gcc/testsuite/g++.dg/init/member1.C b/gcc/testsuite/g++.dg/init/member1.C index aededf2..88f9b31 100644 --- a/gcc/testsuite/g++.dg/init/member1.C +++ b/gcc/testsuite/g++.dg/init/member1.C @@ -12,7 +12,7 @@ template struct C { static const int i = A::i; // { dg-error "incomplete" } static const int j = i; - B b; // { dg-error "not a valid template arg" } + B b; }; C c; diff --git a/gcc/testsuite/g++.dg/parse/constant4.C b/gcc/testsuite/g++.dg/parse/constant4.C index 4d9814f..a1be5dd 100644 --- a/gcc/testsuite/g++.dg/parse/constant4.C +++ b/gcc/testsuite/g++.dg/parse/constant4.C @@ -18,7 +18,7 @@ void Foo () static const unsigned J = X::J; // { dg-message "not initialized with a constant expression" } - Y j; // { dg-error "constant" "" } + Y j; // { dg-error "constant|template argument" "" } } struct A diff --git a/gcc/testsuite/g++.dg/template/nontype20.C b/gcc/testsuite/g++.dg/template/nontype20.C new file mode 100644 index 0000000..e4aba32 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/nontype20.C @@ -0,0 +1,11 @@ +// PR c++/48003 +// { dg-options "-fpermissive -w" } +// Test that we allow integer overflow in constant exprs with -fpermissive + +template +struct test +{ + typedef test prior; +}; + +test<-2147483647-1> f; diff --git a/gcc/testsuite/g++.dg/template/qualified-id3.C b/gcc/testsuite/g++.dg/template/qualified-id3.C index bbfb51e..c769a52 100644 --- a/gcc/testsuite/g++.dg/template/qualified-id3.C +++ b/gcc/testsuite/g++.dg/template/qualified-id3.C @@ -3,7 +3,7 @@ template struct A { }; template struct B { static const int c; // { dg-message "not initialized with a constant expression" } - typedef A::c> C; // { dg-error "constant expression" } + typedef A::c> C; // { dg-error "constant expression|template argument" } }; template const int B::c = sizeof (T); diff --git a/gcc/testsuite/g++.old-deja/g++.pt/crash10.C b/gcc/testsuite/g++.old-deja/g++.pt/crash10.C index af0e919..86f3861 100644 --- a/gcc/testsuite/g++.old-deja/g++.pt/crash10.C +++ b/gcc/testsuite/g++.old-deja/g++.pt/crash10.C @@ -5,6 +5,7 @@ class GCD { public: enum { val = (N == 0) ? M : GCD::val }; // { dg-warning "division" "division" } // { dg-error "constant expression" "valid" { target *-*-* } 6 } +// { dg-message "template argument" "valid" { target *-*-* } 6 } }; int main() { diff --git a/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow_neg.cc b/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow_neg.cc index 51dcdac..ca91e46 100644 --- a/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow_neg.cc +++ b/libstdc++-v3/testsuite/20_util/ratio/cons/cons_overflow_neg.cc @@ -51,7 +51,4 @@ test04() // { dg-error "instantiated from here" "" { target *-*-* } 46 } // { dg-error "denominator cannot be zero" "" { target *-*-* } 155 } // { dg-error "out of range" "" { target *-*-* } 156 } -// { dg-error "non-constant expression" "" { target *-*-* } 61 } -// { dg-error "overflow in constant expression" "" { target *-*-* } 61 } -// { dg-error "not a member" "" { target *-*-* } 164 } -// { dg-error "not a valid template argument" "" { target *-*-* } 166 } +// { dg-error "overflow in constant expression" "" { target *-*-* } 74 }