public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-288] c: Fix up error-recovery on non-empty VLA initializers [PR109409]
Date: Thu, 27 Apr 2023 09:37:25 +0000 (GMT)	[thread overview]
Message-ID: <20230427093725.43E283858D32@sourceware.org> (raw)

https://gcc.gnu.org/g:d8842271ebf9a81128df9ae80e1d3b688749eac8

commit r14-288-gd8842271ebf9a81128df9ae80e1d3b688749eac8
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Apr 27 11:36:54 2023 +0200

    c: Fix up error-recovery on non-empty VLA initializers [PR109409]
    
    On the following testcase we ICE, because after we emit the
    variable-sized object may not be initialized except with an empty initializer
    error we don't really reset the initializer to error_mark_node and then at
    -Wformat checking time we ICE on seeing STRING_CST initializer for a VLA.
    
    The following patch just arranges for error_mark_node to be returned after
    the error diagnostics.
    
    2023-04-27  Jakub Jelinek  <jakub@redhat.com>
    
            PR c/109409
            * c-parser.cc (c_parser_initializer): Move diagnostics about
            initialization of variable sized object with non-empty initializer
            after c_parser_expr_no_commas call and ret.set_error (); after it.
    
            * gcc.dg/pr109409.c: New test.

Diff:
---
 gcc/c/c-parser.cc               | 11 +++++++----
 gcc/testsuite/gcc.dg/pr109409.c | 10 ++++++++++
 2 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 9398c7a5271..c9f06930e3a 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -5677,11 +5677,14 @@ c_parser_initializer (c_parser *parser, tree decl)
     {
       struct c_expr ret;
       location_t loc = c_parser_peek_token (parser)->location;
-      if (decl != error_mark_node && C_DECL_VARIABLE_SIZE (decl))
-	error_at (loc,
-		  "variable-sized object may not be initialized except "
-		  "with an empty initializer");
       ret = c_parser_expr_no_commas (parser, NULL);
+      if (decl != error_mark_node && C_DECL_VARIABLE_SIZE (decl))
+	{
+	  error_at (loc,
+		    "variable-sized object may not be initialized except "
+		    "with an empty initializer");
+	  ret.set_error ();
+	}
       /* This is handled mostly by gimplify.cc, but we have to deal with
 	 not warning about int x = x; as it is a GCC extension to turn off
 	 this warning but only if warn_init_self is zero.  */
diff --git a/gcc/testsuite/gcc.dg/pr109409.c b/gcc/testsuite/gcc.dg/pr109409.c
new file mode 100644
index 00000000000..097a511c07e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr109409.c
@@ -0,0 +1,10 @@
+/* PR c/109409 */
+/* { dg-do compile } */
+/* { dg-options "-Wall" } */
+
+void
+foo (int n)
+{
+  const char c[n] = "1";	/* { dg-error "variable-sized object may not be initialized except with an empty initializer" } */
+  __builtin_printf (c);
+}

                 reply	other threads:[~2023-04-27  9:37 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=20230427093725.43E283858D32@sourceware.org \
    --to=jakub@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).