public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Uecker <uecker@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-2830] Evaluate arguments of sizeof that are structs of variable size.
Date: Tue, 10 Aug 2021 05:50:21 +0000 (GMT)	[thread overview]
Message-ID: <20210810055021.8A1693850409@sourceware.org> (raw)

https://gcc.gnu.org/g:0631faf87a197145acd833249bf8f20a1c4aaabf

commit r12-2830-g0631faf87a197145acd833249bf8f20a1c4aaabf
Author: Martin Uecker <muecker@gwdg.de>
Date:   Tue Aug 10 07:42:51 2021 +0200

    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.

Diff:
---
 gcc/c/c-typeck.c                   |  2 +-
 gcc/testsuite/gcc.dg/vla-stexp-1.c | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/gcc/c/c-typeck.c b/gcc/c/c-typeck.c
index 5d6565bdaa9..c5bf3372321 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 00000000000..97d66937e9a
--- /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;
+}


                 reply	other threads:[~2021-08-10  5:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210810055021.8A1693850409@sourceware.org \
    --to=uecker@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).