Commit f724eac4 by Mark Mitchell Committed by Mark Mitchell

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
parent 8c166806
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> 2006-10-11 Lee Millward <lee.millward@codesourcery.com>
PR c++/29024 PR c++/29024
......
...@@ -4703,32 +4703,46 @@ check_initializer (tree decl, tree init, int flags, tree *cleanup) ...@@ -4703,32 +4703,46 @@ check_initializer (tree decl, tree init, int flags, tree *cleanup)
if (type == error_mark_node) if (type == error_mark_node)
/* We will have already complained. */ /* We will have already complained. */
init = NULL_TREE; return NULL_TREE;
else if (init && COMPLETE_TYPE_P (type)
&& !TREE_CONSTANT (TYPE_SIZE (type))) if (TREE_CODE (type) == ARRAY_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))))
{ {
error ("elements of array %q#D have incomplete type", decl); tree element_type = TREE_TYPE (type);
init = NULL_TREE;
/* 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); error ("%qD has incomplete type", decl);
TREE_TYPE (decl) = error_mark_node; TREE_TYPE (decl) = error_mark_node;
init = NULL_TREE; return NULL_TREE;
} }
else if (!CP_AGGREGATE_TYPE_P (type) else
&& init && BRACE_ENCLOSED_INITIALIZER_P (init) /* There is no way to make a variable-sized class type in GNU C++. */
&& VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1) 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); error ("scalar object %qD requires one element in initializer", decl);
TREE_TYPE (decl) = error_mark_node; TREE_TYPE (decl) = error_mark_node;
init = NULL_TREE; return NULL_TREE;
} }
if (TREE_CODE (decl) == CONST_DECL) if (TREE_CODE (decl) == CONST_DECL)
......
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> 2006-10-11 Kaveh R. Ghazi <ghazi@caip.rutgers.edu>
* gcc.dg/builtins-config.h: Move Solaris section after inclusion * gcc.dg/builtins-config.h: Move Solaris section after inclusion
// PR c++/29175
// { dg-options "" }
void foo(int i)
{
int x[][i] = { 0 }; // { dg-error "variable-sized|storage size" }
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment