Evaluate type arguments of sizeof for all types of variable size
and not just for VLAs. This fixes PR101838 and some issues related
to PR29970 where statement expressions need to be evaluated so that
the size is well defined.
2021-08-12 Martin Uecker <muecker@gwdg.de>
gcc/c/
PR c/101838
PR c/29970
* c-typeck.c (c_expr_sizeof_type): Evaluate
size expressions for structs of variable size.
gcc/testsuite/
PR c/101838
* gcc.dg/vla-stexp-2.c: New test.
c_last_sizeof_loc = loc;
ret.original_code = SIZEOF_EXPR;
ret.original_type = NULL;
+ if (type == error_mark_node)
+ {
+ ret.value = error_mark_node;
+ ret.original_code = ERROR_MARK;
+ }
+ else
if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
- && c_vla_type_p (type))
+ && C_TYPE_VARIABLE_SIZE (type))
{
/* If the type is a [*] array, it is a VLA but is represented as
having a size of zero. In such a case we must ensure that
--- /dev/null
+/* PR101838 */
+/* { dg-do run } */
+/* { dg-options "-Wpedantic -O0" } */
+
+
+int bar0(
+ int (*a)[*],
+ int (*b)[sizeof(*a)]
+);
+
+
+int bar(
+ struct f { /* { dg-warning "will not be visible outside of this definition" } */
+ int a[*]; } v, /* { dg-warning "variably modified type" } */
+ int (*b)[sizeof(struct f)] // should not warn about zero size
+);
+
+int foo(void)
+{
+ int n = 0;
+ return sizeof(typeof(*({ n = 10; struct foo { /* { dg-warning "braced-groups" } */
+ int x[n]; /* { dg-warning "variably modified type" } */
+ } x; &x; })));
+}
+
+
+int main()
+{
+ if (sizeof(struct foo { int x[10]; }) != foo())
+ __builtin_abort();
+
+ return 0;
+}