aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorMartin Uecker <muecker@gwdg.de>2021-08-10 07:42:51 +0200
committerMartin Uecker <muecker@gwdg.de>2021-08-10 07:49:57 +0200
commit0631faf87a197145acd833249bf8f20a1c4aaabf (patch)
tree0ec94e5d7ad338e9051b5b6560b5521597d01687 /gcc
parent3d7ccbc1efbd475031a9a4a6110c531f71fbf631 (diff)
downloadgcc-0631faf87a197145acd833249bf8f20a1c4aaabf.zip
gcc-0631faf87a197145acd833249bf8f20a1c4aaabf.tar.gz
gcc-0631faf87a197145acd833249bf8f20a1c4aaabf.tar.bz2
Evaluate arguments of sizeof that are structs of variable size.
Evaluate arguments of sizeof for all types of variable size and not just for VLAs. This fixes some issues related to [PR29970] where statement expressions need to be evaluated so that the size is well defined. 2021-08-10 Martin Uecker <muecker@gwdg.de> gcc/c/ PR c/29970 * c-typeck.c (c_expr_sizeof_expr): Evaluate size expressions for structs of variable size. gcc/testsuite/ PR c/29970 * gcc.dg/vla-stexp-1.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/c/c-typeck.c2
-rw-r--r--gcc/testsuite/gcc.dg/vla-stexp-1.c18
2 files changed, 19 insertions, 1 deletions
diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 5d6565b..c5bf337 100644
--- a/gcc/c/c-typeck.c
+++ b/gcc/c/c-typeck.c
@@ -2992,7 +2992,7 @@ c_expr_sizeof_expr (location_t loc, struct c_expr expr)
c_last_sizeof_loc = loc;
ret.original_code = SIZEOF_EXPR;
ret.original_type = NULL;
- if (c_vla_type_p (TREE_TYPE (folded_expr)))
+ if (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)))
{
/* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
diff --git a/gcc/testsuite/gcc.dg/vla-stexp-1.c b/gcc/testsuite/gcc.dg/vla-stexp-1.c
new file mode 100644
index 0000000..97d6693
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vla-stexp-1.c
@@ -0,0 +1,18 @@
+/* PR29970*/
+/* { dg-do run } */
+/* { dg-options "-Wall -O0" } */
+
+int foo(void)
+{
+ int n = 0;
+ return sizeof(*({ n = 10; struct foo { int x[n]; } x; &x; }));
+}
+
+
+int main()
+{
+ if (sizeof(struct foo { int x[10]; }) != foo())
+ __builtin_abort();
+
+ return 0;
+}