From: mpolacek Date: Wed, 27 Jan 2016 14:26:38 +0000 (+0000) Subject: PR c++/69496 X-Git-Tag: upstream/6.1~1426 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5cb23e3ccf0f6a577666542edf7e2457618ebe2a;p=platform%2Fupstream%2Flinaro-gcc.git PR c++/69496 * constexpr.c (cxx_eval_array_reference): Evaluate the number of elements of the array. * g++.dg/ext/constexpr-vla1.C: New test. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@232875 138bc75d-0d04-0410-961f-82ee72b054a4 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 0c490d5..9f8d91a 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2016-01-27 Marek Polacek + + PR c++/69496 + * constexpr.c (cxx_eval_array_reference): Evaluate the number of + elements of the array. + 2016-01-26 Jason Merrill PR c++/68949 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index bbb8ccf..fd80000 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1846,7 +1846,12 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, if (!found) { - if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary)))) + tree nelts = array_type_nelts_top (TREE_TYPE (ary)); + /* For VLAs, the number of elements won't be an integer constant. */ + nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, + overflow_p); + VERIFY_CONSTANT (nelts); + if (tree_int_cst_lt (index, nelts)) { if (TREE_CODE (ary) == CONSTRUCTOR && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8559a78..2ff4f7d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-01-27 Marek Polacek + + PR c++/69496 + * g++.dg/ext/constexpr-vla1.C: New test. + 2016-01-20 Christian Bruel PR target/69245 diff --git a/gcc/testsuite/g++.dg/ext/constexpr-vla1.C b/gcc/testsuite/g++.dg/ext/constexpr-vla1.C new file mode 100644 index 0000000..a5615bb --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/constexpr-vla1.C @@ -0,0 +1,30 @@ +// PR c++/69496 +// { dg-do compile { target c++14 } } + +constexpr int +fn_ok (int n) +{ + __extension__ int a[n] = { }; + int z = 0; + + for (unsigned i = 0; i < sizeof (a) / sizeof (int); ++i) + z += a[i]; + + return z; +} + + +constexpr int +fn_not_ok (int n) +{ + __extension__ int a[n] = { }; + int z = 0; + + for (unsigned i = 0; i < sizeof (a); ++i) + z += a[i]; + + return z; +} + +constexpr int n1 = fn_ok (3); +constexpr int n2 = fn_not_ok (3); // { dg-error "array subscript out of bound" }