From patchwork Thu Jun 27 02:34:06 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 254930 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]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "localhost", Issuer "www.qmailtoaster.com" (not verified)) by ozlabs.org (Postfix) with ESMTPS id 8AF9B2C0092 for ; Thu, 27 Jun 2013 12:34:20 +1000 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:subject:content-type; q= dns; s=default; b=CIePpnn7IBn1X+q10FDkdIzOhZ5JST4K3XvT0s8Fr+paaO XCvdGhkpLM/+rgr6zRV9rU/i3gE0+BdRvMRmioGpvZnzFDM8aP42Hr96bSW+qN0H w3n1gwk0acruo5KOfG8Dlw8p/GldX+Vss0pWrUCQ80wexTIrHVgcc4YaDDb8Q= 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 :message-id:date:from:mime-version:to:subject:content-type; s= default; bh=ZvG3kvMWbAyUtUmvJezIyikhLEA=; b=EkCSeSTeRIuGak5XG/WW IQV1i4gi1G2fOiHkln45qAHz2MG9MzTkqGsyuoe8ZOoutosDLKuMCyskxBZ+yRSX t1qSQ9Vzewxq5lpulKbUrWjPtfa+cJ0L4LmYpomcK5Wm21sIBjnM60WlnToYLm0U zn+QgwQlvKDAy9PwEubFJJ4= Received: (qmail 7731 invoked by alias); 27 Jun 2013 02:34:14 -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 7692 invoked by uid 89); 27 Jun 2013 02:34:09 -0000 X-Spam-SWARE-Status: No, score=-7.0 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_W, RCVD_IN_HOSTKARMA_WL, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.1 Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.84/v0.84-167-ge50287c) with ESMTP; Thu, 27 Jun 2013 02:34:08 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r5R2Y72b013284 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 26 Jun 2013 22:34:07 -0400 Received: from [10.3.113.42] (ovpn-113-42.phx2.redhat.com [10.3.113.42]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id r5R2Y6DO030257 for ; Wed, 26 Jun 2013 22:34:06 -0400 Message-ID: <51CBA49E.2000400@redhat.com> Date: Wed, 26 Jun 2013 22:34:06 -0400 From: Jason Merrill User-Agent: Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20100101 Thunderbird/24.0a2 MIME-Version: 1.0 To: gcc-patches List Subject: C++ PATCH to handling of overflow in in-class static member initializers X-Virus-Found: No A question from a student of a committee member led me to notice that we were handling overflow in a class model of Fibonacci numbers oddly: C++11 says that arithmetic overflow causes an expression to not be constant, but instead of enforcing that on the actual affected variable initialization, we were initializing it fine and then preventing other expressions using that variable from being constant. Checking this directly in store_init_value means we don't need to check in cp_finish_decl anymore. Tested x86_64-pc-linux-gnu, applying to trunk. commit 0bc5f82462c2a625608de5a69dcc54dbc08699c2 Author: Jason Merrill Date: Sun Jun 23 06:15:20 2013 -0400 * typeck2.c (store_init_value): Diagnose a non-constant initializer for in-class static. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index f562546..047fd77 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6346,25 +6346,6 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, cleanups = make_tree_vector (); init = check_initializer (decl, init, flags, &cleanups); - /* Check that the initializer for a static data member was a - constant. Although we check in the parser that the - initializer is an integral constant expression, we do not - simplify division-by-zero at the point at which it - occurs. Therefore, in: - - struct S { static const int i = 7 / 0; }; - - we issue an error at this point. It would - probably be better to forbid division by zero in - integral constant expressions. */ - if (DECL_EXTERNAL (decl) && init) - { - error ("%qD cannot be initialized by a non-constant expression" - " when being declared", decl); - DECL_INITIALIZED_IN_CLASS_P (decl) = 0; - init = NULL_TREE; - } - /* Handle: [dcl.init] diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c index a447893..7932939 100644 --- a/gcc/cp/typeck2.c +++ b/gcc/cp/typeck2.c @@ -775,7 +775,8 @@ store_init_value (tree decl, tree init, vec** cleanups, int flags) bool const_init; value = fold_non_dependent_expr (value); value = maybe_constant_init (value); - if (DECL_DECLARED_CONSTEXPR_P (decl)) + if (DECL_DECLARED_CONSTEXPR_P (decl) + || DECL_IN_AGGR_P (decl)) { /* Diagnose a non-constant initializer for constexpr. */ if (processing_template_decl diff --git a/gcc/testsuite/g++.dg/cpp0x/overflow1.C b/gcc/testsuite/g++.dg/cpp0x/overflow1.C new file mode 100644 index 0000000..7033e9c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/overflow1.C @@ -0,0 +1,23 @@ +template +struct Fib +{ + static const long long value // { dg-error "overflow" } + = Fib::value + Fib::value; +}; + +template <> +struct Fib<0> +{ + static const long long value = 0; +}; + +template <> +struct Fib<1> +{ + static const long long value = 1; +}; + +int main() +{ + return Fib<95>::value; +} diff --git a/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc b/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc index f0dcdec..f2f2330 100644 --- a/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc +++ b/libstdc++-v3/testsuite/20_util/ratio/operations/ops_overflow_neg.cc @@ -46,5 +46,6 @@ test02() // { dg-error "overflow in multiplication" "" { target *-*-* } 97 } // { dg-error "overflow in multiplication" "" { target *-*-* } 99 } // { dg-error "overflow in multiplication" "" { target *-*-* } 101 } +// { dg-error "overflow in constant expression" "" { target *-*-* } 108 } // { dg-prune-output "out of range" } // { dg-prune-output "not usable in a constant expression" }