From 4e6bf0b9dd5585df1a1472d6a93b9fff72fe2524 Mon Sep 17 00:00:00 2001 From: Martin Uecker Date: Wed, 17 Nov 2021 14:20:59 +0100 Subject: Fix ICE when mixing VLAs and statement expressions [PR91038] When returning VM-types from statement expressions, this can lead to an ICE when declarations from the statement expression are referred to later. Most of these issues can be addressed by gimplifying the base expression earlier in gimplify_compound_lval. Another issue is fixed by wrapping the pointer expression in pointer_int_sum. This fixes PR91038 and some of the test cases from PR29970 (structs with VLA members need further work). gcc/ PR c/91038 PR c/29970 * gimplify.c (gimplify_var_or_parm_decl): Update comment. (gimplify_compound_lval): Gimplify base expression first. (gimplify_target_expr): Add comment. gcc/c-family/ PR c/91038 PR c/29970 * c-common.c (pointer_int_sum): Make sure pointer expressions are evaluated first when the size expression depends on for variably-modified types. gcc/testsuite/ PR c/91038 PR c/29970 * gcc.dg/vla-stexp-3.c: New test. * gcc.dg/vla-stexp-4.c: New test. * gcc.dg/vla-stexp-5.c: New test. * gcc.dg/vla-stexp-6.c: New test. * gcc.dg/vla-stexp-7.c: New test. * gcc.dg/vla-stexp-8.c: New test. * gcc.dg/vla-stexp-9.c: New test. --- gcc/c-family/c-common.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'gcc/c-family/c-common.c') diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index 90e8ec8..ca7d69c 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -3306,7 +3306,19 @@ pointer_int_sum (location_t loc, enum tree_code resultcode, TREE_TYPE (result_type))) size_exp = integer_one_node; else - size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type)); + { + size_exp = size_in_bytes_loc (loc, TREE_TYPE (result_type)); + /* Wrap the pointer expression in a SAVE_EXPR to make sure it + is evaluated first when the size expression may depend + on it for VM types. */ + if (TREE_SIDE_EFFECTS (size_exp) + && TREE_SIDE_EFFECTS (ptrop) + && variably_modified_type_p (TREE_TYPE (ptrop), NULL)) + { + ptrop = save_expr (ptrop); + size_exp = build2 (COMPOUND_EXPR, TREE_TYPE (intop), ptrop, size_exp); + } + } /* We are manipulating pointer values, so we don't need to warn about relying on undefined signed overflow. We disable the -- cgit v1.1