From b75bf8b188f71f129f1b97da93e3246637c384e8 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Fri, 7 Mar 2014 15:00:14 -0500 Subject: [PATCH] decl.c (create_array_type_for_decl): Only warn about invalid C++1y VLA if flag_iso or warn_vla>0. * decl.c (create_array_type_for_decl): Only warn about invalid C++1y VLA if flag_iso or warn_vla>0. (grokdeclarator): Likewise. * pt.c (tsubst): Likewise. * semantics.c (finish_decltype_type): Likewise. * typeck.c (cxx_sizeof_or_alignof_type): Likewise. (cp_build_addr_expr_1): Likewise. * init.c (build_new_1): Improve diagnostics. From-SVN: r208411 --- gcc/cp/ChangeLog | 11 +++++++++++ gcc/cp/decl.c | 9 ++++++--- gcc/cp/init.c | 18 ++++++++++++++---- gcc/cp/pt.c | 3 ++- gcc/cp/semantics.c | 3 ++- gcc/cp/typeck.c | 6 ++++-- gcc/testsuite/g++.dg/ext/vla1.C | 2 +- gcc/testsuite/g++.dg/ext/vla5.C | 2 +- gcc/testsuite/g++.dg/ext/vla8.C | 2 +- gcc/testsuite/g++.dg/init/new35.C | 2 +- gcc/testsuite/g++.dg/init/new37.C | 2 +- 11 files changed, 44 insertions(+), 16 deletions(-) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index fd0b4d2e240e..988c3bf264f7 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,14 @@ +2014-03-07 Jason Merrill + + * decl.c (create_array_type_for_decl): Only warn about invalid + C++1y VLA if flag_iso or warn_vla>0. + (grokdeclarator): Likewise. + * pt.c (tsubst): Likewise. + * semantics.c (finish_decltype_type): Likewise. + * typeck.c (cxx_sizeof_or_alignof_type): Likewise. + (cp_build_addr_expr_1): Likewise. + * init.c (build_new_1): Improve diagnostics. + 2014-03-07 Paolo Carlini PR c++/58609 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 389ed1a9890d..4eb3e69af795 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -8530,7 +8530,8 @@ create_array_type_for_decl (tree name, tree type, tree size) return error_mark_node; } - if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)) + if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type) + && (flag_iso || warn_vla > 0)) pedwarn (input_location, OPT_Wvla, "array of array of runtime bound"); /* Figure out the index type for the array. */ @@ -9762,7 +9763,8 @@ grokdeclarator (const cp_declarator *declarator, : G_("cannot declare pointer to qualified function type %qT"), type); - if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)) + if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type) + && (flag_iso || warn_vla > 0)) pedwarn (input_location, OPT_Wvla, declarator->kind == cdk_reference ? G_("reference to array of runtime bound") @@ -10110,7 +10112,8 @@ grokdeclarator (const cp_declarator *declarator, type = error_mark_node; } - if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)) + if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type) + && (flag_iso || warn_vla > 0)) pedwarn (input_location, OPT_Wvla, "typedef naming array of runtime bound"); diff --git a/gcc/cp/init.c b/gcc/cp/init.c index 3ae2b5c61927..7f5d04539a5e 100644 --- a/gcc/cp/init.c +++ b/gcc/cp/init.c @@ -2285,6 +2285,7 @@ build_new_1 (vec **placement, tree type, tree nelts, is therefore reusable. */ tree data_addr; tree init_preeval_expr = NULL_TREE; + tree orig_type = type; if (nelts) { @@ -2330,7 +2331,7 @@ build_new_1 (vec **placement, tree type, tree nelts, if (complain & tf_error) { error_at (EXPR_LOC_OR_LOC (inner_nelts, input_location), - "array size in operator new must be constant"); + "array size in new-expression must be constant"); cxx_constant_value(inner_nelts); } nelts = error_mark_node; @@ -2344,7 +2345,7 @@ build_new_1 (vec **placement, tree type, tree nelts, if (variably_modified_type_p (elt_type, NULL_TREE) && (complain & tf_error)) { - error ("variably modified type not allowed in operator new"); + error ("variably modified type not allowed in new-expression"); return error_mark_node; } @@ -2357,8 +2358,17 @@ build_new_1 (vec **placement, tree type, tree nelts, && !TREE_CONSTANT (maybe_constant_value (outer_nelts))) { if (complain & tf_warning_or_error) - pedwarn(EXPR_LOC_OR_LOC (outer_nelts, input_location), OPT_Wvla, - "ISO C++ does not support variable-length array types"); + { + const char *msg; + if (typedef_variant_p (orig_type)) + msg = ("non-constant array new length must be specified " + "directly, not by typedef"); + else + msg = ("non-constant array new length must be specified " + "without parentheses around the type-id"); + pedwarn (EXPR_LOC_OR_LOC (outer_nelts, input_location), + OPT_Wvla, msg); + } else return error_mark_node; } diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 8126905941e4..6476d8aae56e 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -11954,7 +11954,8 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) if (cxx_dialect >= cxx1y && !(TREE_CODE (t) == REFERENCE_TYPE && REFERENCE_VLA_OK (t)) - && array_of_runtime_bound_p (type)) + && array_of_runtime_bound_p (type) + && (flag_iso || warn_vla > 0)) { if (complain & tf_warning_or_error) pedwarn diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c index d2c453fc658e..b9c1271e3898 100644 --- a/gcc/cp/semantics.c +++ b/gcc/cp/semantics.c @@ -7031,7 +7031,8 @@ finish_decltype_type (tree expr, bool id_expression_or_member_access_p, } } - if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)) + if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type) + && (flag_iso || warn_vla > 0)) { if (complain & tf_warning_or_error) pedwarn (input_location, OPT_Wvla, diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c index ae2e5038e18a..c91612c76754 100644 --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -1552,7 +1552,8 @@ cxx_sizeof_or_alignof_type (tree type, enum tree_code op, bool complain) return value; } - if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type)) + if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (type) + && (flag_iso || warn_vla > 0)) { if (complain & tf_warning_or_error) pedwarn (input_location, OPT_Wvla, @@ -5471,7 +5472,8 @@ cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain) if (argtype != error_mark_node) { - if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype)) + if (cxx_dialect >= cxx1y && array_of_runtime_bound_p (argtype) + && (flag_iso || warn_vla > 0)) { if (complain & tf_warning_or_error) pedwarn (input_location, OPT_Wvla, diff --git a/gcc/testsuite/g++.dg/ext/vla1.C b/gcc/testsuite/g++.dg/ext/vla1.C index f3725354fc3e..55ae5c8af0a3 100644 --- a/gcc/testsuite/g++.dg/ext/vla1.C +++ b/gcc/testsuite/g++.dg/ext/vla1.C @@ -9,7 +9,7 @@ class A { A (int); }; A::A (int i) { - int ar[1][i]; // { dg-error "variable length array" } + int ar[1][i]; // { dg-error "array" } ar[0][0] = 0; } diff --git a/gcc/testsuite/g++.dg/ext/vla5.C b/gcc/testsuite/g++.dg/ext/vla5.C index 2457e34f1833..ca583091769f 100644 --- a/gcc/testsuite/g++.dg/ext/vla5.C +++ b/gcc/testsuite/g++.dg/ext/vla5.C @@ -6,5 +6,5 @@ void test (int a) { - new (char[a]); // { dg-warning "variable-length array" } + new (char[a]); // { dg-warning "parentheses" } } diff --git a/gcc/testsuite/g++.dg/ext/vla8.C b/gcc/testsuite/g++.dg/ext/vla8.C index 1c6000fa3b78..9e2d6bdad436 100644 --- a/gcc/testsuite/g++.dg/ext/vla8.C +++ b/gcc/testsuite/g++.dg/ext/vla8.C @@ -8,7 +8,7 @@ struct AvlTreeIter AvlTreeIter() { - new (void* [Num()]); // { dg-warning "variable-length array" } + new (void* [Num()]); // { dg-warning "parentheses" } } }; diff --git a/gcc/testsuite/g++.dg/init/new35.C b/gcc/testsuite/g++.dg/init/new35.C index c5f79aa2f802..7d07cf57f865 100644 --- a/gcc/testsuite/g++.dg/init/new35.C +++ b/gcc/testsuite/g++.dg/init/new35.C @@ -5,7 +5,7 @@ int main (int argc, char **argv) { typedef char A[argc]; - new A; // { dg-warning "variable-length array types|not a constant" } + new A; // { dg-warning "array" } new A[0]; // { dg-error "must be constant|not a constant" } new A[5]; // { dg-error "must be constant|not a constant" } new (A[0]); // { dg-error "must be constant|not a constant" } diff --git a/gcc/testsuite/g++.dg/init/new37.C b/gcc/testsuite/g++.dg/init/new37.C index 82ca18b7aeba..eab785421745 100644 --- a/gcc/testsuite/g++.dg/init/new37.C +++ b/gcc/testsuite/g++.dg/init/new37.C @@ -3,7 +3,7 @@ void nonconst(int n) { - new (long[n][n]); // { dg-error "variable length|array size|not a constant" } + new (long[n][n]); // { dg-error "variable length|array size|not a constant|runtime bound" } new long[n][n]; // { dg-error "variable length|array size|not a constant" } } -- 2.47.2