aboutsummaryrefslogtreecommitdiff
path: root/gcc/c/c-decl.cc
diff options
context:
space:
mode:
authorMartin Uecker <uecker@tugraz.at>2023-04-13 19:37:12 +0200
committerMartin Uecker <uecker@tugraz.at>2023-05-23 22:04:49 +0200
commitf9b5be322358ee63798e02a9103b6bbe459e7aea (patch)
treee280f4418c443ba88255bfddf9624a08c1980c89 /gcc/c/c-decl.cc
parent8a20b4bc50bdc8d61610974d60d5851f3fd8b70f (diff)
downloadgcc-f9b5be322358ee63798e02a9103b6bbe459e7aea.zip
gcc-f9b5be322358ee63798e02a9103b6bbe459e7aea.tar.gz
gcc-f9b5be322358ee63798e02a9103b6bbe459e7aea.tar.bz2
Fix ICEs related to VM types in C 1/2 [PR70418, PR107557, PR108423]
Size expressions were sometimes lost and not gimplified correctly, leading to ICEs and incorrect evaluation order. Fix this by 1) not recursing into pointers when gimplifying parameters in the middle-end (the code is merged with gimplify_type_sizes), which is incorrect because it might access variables declared later for incomplete structs, and 2) tracking size expressions for struct/union members correctly, 3) emitting code to evaluate size expressions for missing cases (nested functions, empty declarations, and structs/unions). PR c/70418 PR c/106465 PR c/107557 PR c/108423 gcc/c/ * c-decl.cc (start_decl): Make sure size expression are evaluated only in correct context. (grokdeclarator): Size expression in fields may need a bind expression, make sure DECL_EXPR is always created. (grokfield, declspecs_add_type): Pass along size expressions. (finish_struct): Remove unneeded DECL_EXPR. (start_function): Evaluate size expressions for nested functions. * c-parser.cc (c_parser_struct_declarations, c_parser_struct_or_union_specifier): Pass along size expressions. (c_parser_declaration_or_fndef): Evaluate size expression. (c_parser_objc_at_property_declaration, c_parser_objc_class_instance_variables): Adapt. * c-tree.h (grokfield): Adapt declaration. gcc/testsuite/ * gcc.dg/nested-vla-1.c: New test. * gcc.dg/nested-vla-2.c: New test. * gcc.dg/nested-vla-3.c: New test. * gcc.dg/pr70418.c: New test. * gcc.dg/pr106465.c: New test. * gcc.dg/pr107557-1.c: New test. * gcc.dg/pr107557-2.c: New test. * gcc.dg/pr108423-1.c: New test. * gcc.dg/pr108423-2.c: New test. * gcc.dg/pr108423-3.c: New test. * gcc.dg/pr108423-4.c: New test. * gcc.dg/pr108423-5.c: New test. * gcc.dg/pr108423-6.c: New test. * gcc.dg/typename-vla-2.c: New test. * gcc.dg/typename-vla-3.c: New test. * gcc.dg/typename-vla-4.c: New test. * gcc.misc-tests/gcov-pr85350.c: Adapt.
Diffstat (limited to 'gcc/c/c-decl.cc')
-rw-r--r--gcc/c/c-decl.cc39
1 files changed, 22 insertions, 17 deletions
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 94ce760..494d3cf 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -5364,7 +5364,8 @@ start_decl (struct c_declarator *declarator, struct c_declspecs *declspecs,
if (lastdecl != error_mark_node)
*lastloc = DECL_SOURCE_LOCATION (lastdecl);
- if (expr)
+ /* Make sure the size expression is evaluated at this point. */
+ if (expr && !current_scope->parm_flag)
add_stmt (fold_convert (void_type_node, expr));
if (TREE_CODE (decl) != FUNCTION_DECL && MAIN_NAME_P (DECL_NAME (decl))
@@ -7498,7 +7499,8 @@ grokdeclarator (const struct c_declarator *declarator,
&& c_type_variably_modified_p (type))
{
tree bind = NULL_TREE;
- if (decl_context == TYPENAME || decl_context == PARM)
+ if (decl_context == TYPENAME || decl_context == PARM
+ || decl_context == FIELD)
{
bind = build3 (BIND_EXPR, void_type_node, NULL_TREE,
NULL_TREE, NULL_TREE);
@@ -7507,10 +7509,11 @@ grokdeclarator (const struct c_declarator *declarator,
push_scope ();
}
tree decl = build_decl (loc, TYPE_DECL, NULL_TREE, type);
- DECL_ARTIFICIAL (decl) = 1;
pushdecl (decl);
- finish_decl (decl, loc, NULL_TREE, NULL_TREE, NULL_TREE);
+ DECL_ARTIFICIAL (decl) = 1;
+ add_stmt (build_stmt (DECL_SOURCE_LOCATION (decl), DECL_EXPR, decl));
TYPE_NAME (type) = decl;
+
if (bind)
{
pop_scope ();
@@ -8709,7 +8712,7 @@ start_struct (location_t loc, enum tree_code code, tree name,
tree
grokfield (location_t loc,
struct c_declarator *declarator, struct c_declspecs *declspecs,
- tree width, tree *decl_attrs)
+ tree width, tree *decl_attrs, tree *expr)
{
tree value;
@@ -8766,7 +8769,7 @@ grokfield (location_t loc,
}
value = grokdeclarator (declarator, declspecs, FIELD, false,
- width ? &width : NULL, decl_attrs, NULL, NULL,
+ width ? &width : NULL, decl_attrs, expr, NULL,
DEPRECATED_NORMAL);
finish_decl (value, loc, NULL_TREE, NULL_TREE, NULL_TREE);
@@ -9424,13 +9427,6 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
finish_incomplete_vars (incomplete_vars, toplevel);
- /* If we're inside a function proper, i.e. not file-scope and not still
- parsing parameters, then arrange for the size of a variable sized type
- to be bound now. */
- if (building_stmt_list_p () && c_type_variably_modified_p(t))
- add_stmt (build_stmt (loc,
- DECL_EXPR, build_decl (loc, TYPE_DECL, NULL, t)));
-
if (warn_cxx_compat)
warn_cxx_compat_finish_struct (fieldlist, TREE_CODE (t), loc);
@@ -10056,6 +10052,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
tree restype, resdecl;
location_t loc;
location_t result_loc;
+ tree expr = NULL;
current_function_returns_value = 0; /* Assume, until we see it does. */
current_function_returns_null = 0;
@@ -10067,7 +10064,7 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
in_statement = 0;
decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, true, NULL,
- &attributes, NULL, NULL, DEPRECATED_NORMAL);
+ &attributes, &expr, NULL, DEPRECATED_NORMAL);
invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1);
/* If the declarator is not suitable for a function definition,
@@ -10076,6 +10073,11 @@ start_function (struct c_declspecs *declspecs, struct c_declarator *declarator,
|| TREE_CODE (decl1) != FUNCTION_DECL)
return false;
+ /* Nested functions may have variably modified (return) type.
+ Make sure the size expression is evaluated at this point. */
+ if (expr && !current_scope->parm_flag)
+ add_stmt (fold_convert (void_type_node, expr));
+
loc = DECL_SOURCE_LOCATION (decl1);
/* A nested function is not global. */
@@ -12284,10 +12286,13 @@ declspecs_add_type (location_t loc, struct c_declspecs *specs,
}
else
{
- if (TREE_CODE (type) != ERROR_MARK && spec.kind == ctsk_typeof)
+ if (TREE_CODE (type) != ERROR_MARK)
{
- specs->typedef_p = true;
- specs->locations[cdw_typedef] = loc;
+ if (spec.kind == ctsk_typeof)
+ {
+ specs->typedef_p = true;
+ specs->locations[cdw_typedef] = loc;
+ }
if (spec.expr)
{
if (specs->expr)