public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: [C++ PATCH] Fix compound literal diagnostics (take 2, PR c++/36023)
Date: Wed, 21 May 2008 16:39:00 -0000	[thread overview]
Message-ID: <20080521155056.GH29975@devserv.devel.redhat.com> (raw)
In-Reply-To: <48343A0A.10108@redhat.com>

On Wed, May 21, 2008 at 11:04:42AM -0400, Jason Merrill wrote:
> Jakub Jelinek wrote:
> >I think this is uglification rather than cleanup, especially given the
> >size of the duplicated code and that the error messages need to be
> >different, but certainly if you prefer it that way, I can write a patch. 
> 
> I think I do prefer it that way, so that if we add additional checks in 
> the future it will be obvious that we need to handle both cases.  If 
> checks are in two different places changes in one tend not to be 
> reflected in the other.

Ok, here it is lightly tested, ok for trunk/4.3 if full bootstrap/regression
testing succeeds?

2008-05-21  Jakub Jelinek  <jakub@redhat.com>

	PR c++/36023
	* cp-tree.h (check_array_initializer): New prototype.
	* decl.c (check_array_initializer): New function.
	(check_initializer): Call it.
	* semantics.c (finish_compound_literal): Call it for ARRAY_TYPEs.

	* g++.dg/ext/complit10.C: New test.

--- gcc/cp/cp-tree.h.jj	2008-04-24 13:36:03.000000000 +0200
+++ gcc/cp/cp-tree.h	2008-05-21 17:18:40.000000000 +0200
@@ -4211,6 +4211,7 @@ extern tree shadow_tag				(cp_decl_speci
 extern tree groktypename			(cp_decl_specifier_seq *, const cp_declarator *);
 extern tree start_decl				(const cp_declarator *, cp_decl_specifier_seq *, int, tree, tree, tree *);
 extern void start_decl_1			(tree, bool);
+extern bool check_array_initializer		(tree, tree, tree);
 extern void cp_finish_decl			(tree, tree, bool, tree, int);
 extern void finish_decl				(tree, tree, tree);
 extern int cp_complete_array_type		(tree *, tree, bool);
--- gcc/cp/decl.c.jj	2008-05-08 01:06:18.000000000 +0200
+++ gcc/cp/decl.c	2008-05-21 17:39:11.000000000 +0200
@@ -4894,6 +4894,38 @@ reshape_init (tree type, tree init)
   return new_init;
 }
 
+/* Verify array initializer.  Returns true if errors have been reported.  */
+
+bool
+check_array_initializer (tree decl, tree type, tree init)
+{
+  tree element_type = TREE_TYPE (type);
+
+  /* The array type itself need not be complete, because the
+     initializer may tell us how many elements are in the array.
+     But, the elements of the array must be complete.  */
+  if (!COMPLETE_TYPE_P (complete_type (element_type)))
+    {
+      if (decl)
+	error ("elements of array %q#D have incomplete type", decl);
+      else
+	error ("elements of array %q#T have incomplete type", type);
+      return true;
+    }
+  /* It is not valid to initialize a VLA.  */
+  if (init
+      && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
+	  || !TREE_CONSTANT (TYPE_SIZE (element_type))))
+    {
+      if (decl)
+	error ("variable-sized object %qD may not be initialized", decl);
+      else
+	error ("variable-sized compound literal");
+      return true;
+    }
+  return false;
+}
+
 /* Verify INIT (the initializer for DECL), and record the
    initialization in DECL_INITIAL, if appropriate.  CLEANUP is as for
    grok_reference_init.
@@ -4917,24 +4949,8 @@ check_initializer (tree decl, tree init,
 
   if (TREE_CODE (type) == ARRAY_TYPE)
     {
-      tree element_type = TREE_TYPE (type);
-
-      /* The array type itself need not be complete, because the
-	 initializer may tell us how many elements are in the array.
-	 But, the elements of the array must be complete.  */
-      if (!COMPLETE_TYPE_P (complete_type (element_type)))
-	{
-	  error ("elements of array %q#D have incomplete type", decl);
-	  return NULL_TREE;
-	}
-      /* It is not valid to initialize a VLA.  */
-      if (init
-	  && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type)))
-	      || !TREE_CONSTANT (TYPE_SIZE (element_type))))
-	{
-	  error ("variable-sized object %qD may not be initialized", decl);
-	  return NULL_TREE;
-	}
+      if (check_array_initializer (decl, type, init))
+	return NULL_TREE;
     }
   else if (!COMPLETE_TYPE_P (type))
     {
--- gcc/cp/semantics.c.jj	2008-05-18 22:13:50.000000000 +0200
+++ gcc/cp/semantics.c	2008-05-21 17:40:01.000000000 +0200
@@ -2123,6 +2123,9 @@ finish_compound_literal (tree type, VEC(
     }
 
   type = complete_type (type);
+  if (TREE_CODE (type) == ARRAY_TYPE
+      && check_array_initializer (NULL_TREE, type, compound_literal))
+    return error_mark_node;
   compound_literal = reshape_init (type, compound_literal);
   if (TREE_CODE (type) == ARRAY_TYPE)
     cp_complete_array_type (&type, compound_literal, false);
--- gcc/testsuite/g++.dg/ext/complit10.C.jj	2008-05-21 17:20:50.000000000 +0200
+++ gcc/testsuite/g++.dg/ext/complit10.C	2008-05-21 17:20:45.000000000 +0200
@@ -0,0 +1,20 @@
+// PR c++/36023
+// { dg-do compile }
+// { dg-options "" }
+
+struct A;
+
+void
+f1 (int i)
+{
+  (int[i]) { 1 };	// { dg-error "variable-sized compound literal" }
+  (A[5]) { 1 };		// { dg-error "have incomplete type" }
+  (A[i]) { 1 };		// { dg-error "have incomplete type" }
+}
+
+void
+f2 ()
+{
+  (int[]) { 1 };
+  (int[1]) { 1 };
+}

	Jakub

  reply	other threads:[~2008-05-21 15:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-05-07  8:38 Patch ping Jakub Jelinek
2008-05-07 14:59 ` Jason Merrill
2008-05-21 15:05   ` Jakub Jelinek
2008-05-21 15:51     ` Jason Merrill
2008-05-21 16:39       ` Jakub Jelinek [this message]
2008-05-21 20:36         ` [C++ PATCH] Fix compound literal diagnostics (take 2, PR c++/36023) Jason Merrill
2008-05-10 19:23 ` Patch ping Diego Novillo

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=20080521155056.GH29975@devserv.devel.redhat.com \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /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).