diff options
author | Mark Mitchell <mark@codesourcery.com> | 2006-10-12 04:35:56 +0000 |
---|---|---|
committer | Mark Mitchell <mmitchel@gcc.gnu.org> | 2006-10-12 04:35:56 +0000 |
commit | f724eac4c554e4c2b29e0009e3e0dd8b6e87c71e (patch) | |
tree | 96f4f1c81af000b0bbabeeff0e01537893b6f984 | |
parent | 8c166806c82b6dab5be1db904867f9a0cd315807 (diff) | |
download | gcc-f724eac4c554e4c2b29e0009e3e0dd8b6e87c71e.zip gcc-f724eac4c554e4c2b29e0009e3e0dd8b6e87c71e.tar.gz gcc-f724eac4c554e4c2b29e0009e3e0dd8b6e87c71e.tar.bz2 |
re PR c++/29175 (ICE on invalid C++ variable length array)
PR c++/29175
* decl.c (check_initializer): Issue errors about trying to
initialize arrays whose elements have variable size.
PR c++/29175
* g++.dg/init/array24.C: New test.
From-SVN: r117658
-rw-r--r-- | gcc/cp/ChangeLog | 6 | ||||
-rw-r--r-- | gcc/cp/decl.c | 48 | ||||
-rw-r--r-- | gcc/testsuite/ChangeLog | 5 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/init/array24.C | 7 |
4 files changed, 49 insertions, 17 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 4c7fe49..1ac313b 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2006-10-11 Mark Mitchell <mark@codesourcery.com> + + PR c++/29175 + * decl.c (check_initializer): Issue errors about trying to + initialize arrays whose elements have variable size. + 2006-10-11 Lee Millward <lee.millward@codesourcery.com> PR c++/29024 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index befd4e8..e95870d 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -4703,32 +4703,46 @@ check_initializer (tree decl, tree init, int flags, tree *cleanup) if (type == error_mark_node) /* We will have already complained. */ - init = NULL_TREE; - else if (init && COMPLETE_TYPE_P (type) - && !TREE_CONSTANT (TYPE_SIZE (type))) - { - error ("variable-sized object %qD may not be initialized", decl); - init = NULL_TREE; - } - else if (TREE_CODE (type) == ARRAY_TYPE - && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) + return NULL_TREE; + + if (TREE_CODE (type) == ARRAY_TYPE) { - error ("elements of array %q#D have incomplete type", decl); - init = NULL_TREE; + tree element_type = TREE_TYPE (type); + + /* The array type itself need not be complete, because the + initializer may tell us how many elements are in the array. + But, the elements of the array must be complete. */ + if (!COMPLETE_TYPE_P (complete_type (element_type))) + { + error ("elements of array %q#D have incomplete type", decl); + return NULL_TREE; + } + /* It is not valid to initialize an a VLA. */ + if (init + && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) + || !TREE_CONSTANT (TYPE_SIZE (element_type)))) + { + error ("variable-sized object %qD may not be initialized", decl); + return NULL_TREE; + } } - else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type)) + else if (!COMPLETE_TYPE_P (type)) { error ("%qD has incomplete type", decl); TREE_TYPE (decl) = error_mark_node; - init = NULL_TREE; + return NULL_TREE; } - else if (!CP_AGGREGATE_TYPE_P (type) - && init && BRACE_ENCLOSED_INITIALIZER_P (init) - && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1) + else + /* There is no way to make a variable-sized class type in GNU C++. */ + gcc_assert (TREE_CONSTANT (TYPE_SIZE (type))); + + if (!CP_AGGREGATE_TYPE_P (type) + && init && BRACE_ENCLOSED_INITIALIZER_P (init) + && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1) { error ("scalar object %qD requires one element in initializer", decl); TREE_TYPE (decl) = error_mark_node; - init = NULL_TREE; + return NULL_TREE; } if (TREE_CODE (decl) == CONST_DECL) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 879d1d0..3de03fb 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2006-10-11 Mark Mitchell <mark@codesourcery.com> + + PR c++/29175 + * g++.dg/init/array24.C: New test. + 2006-10-11 Kaveh R. Ghazi <ghazi@caip.rutgers.edu> * gcc.dg/builtins-config.h: Move Solaris section after inclusion diff --git a/gcc/testsuite/g++.dg/init/array24.C b/gcc/testsuite/g++.dg/init/array24.C new file mode 100644 index 0000000..ed4c1d8 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array24.C @@ -0,0 +1,7 @@ +// PR c++/29175 +// { dg-options "" } + +void foo(int i) +{ + int x[][i] = { 0 }; // { dg-error "variable-sized|storage size" } +} |