This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH] Fix PR c++/30759: Initializer-list accepted for object of non-POD type


Hello Gabriel, Mark.

On Mon, 28 May 2007, Mark Mitchell wrote:
[...]
| Better to say "in C++98" instead of "except in C++0x", since presumably
| this will also be allowed in C++1x, C++2x, etc. :-)

I agree; and I hope the test for the flag is adjusted accordingly.
I've looked for an equivalent of flag_cpp0x for C++98 and I did not find
one.

The attached patch introduces a flag called flag_cpp98 that specifies
that we're using C++98, and uses flag_cpp98 instead of !flag_cpp0x to
determine if the error must be issued or not.

I've successfully regtested it on i386-apple-darwin8.9.1. Is it OK?

Best regards,
Simon



2007-05-28  Simon Martin  <simartin@users.sourceforge.net>

	* c-common.h (flag_cpp98): New flag to specify that we are using
	C++98.
	* c-common.c (flag_cpp98): Define it.
	* c-opts.c (set_std_cxx98): Set flag_cpp98. 
	(set_std_cxx0x): Reset flag_cpp98.


2007-05-28  Simon Martin  <simartin@users.sourceforge.net>

	PR c++/30759
	* decl.c (check_initializer): Report an error when a brace enclosed
	initializer is used for a non-aggregate type in C++98.


Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c	(revision 125143)
+++ gcc/cp/decl.c	(working copy)
@@ -4777,14 +4777,29 @@ check_initializer (tree decl, tree init,
   else
     /* There is no way to make a variable-sized class type in GNU C++.  */
     gcc_assert (TREE_CONSTANT (TYPE_SIZE (type)));
-  
-  if (!CP_AGGREGATE_TYPE_P (type)
-      && init && BRACE_ENCLOSED_INITIALIZER_P (init)
-      && VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init)) != 1)
-    {
-      error ("scalar object %qD requires one element in initializer", decl);
-      TREE_TYPE (decl) = error_mark_node;
-      return NULL_TREE;
+
+  if (init && BRACE_ENCLOSED_INITIALIZER_P (init))
+    {
+      int init_len = VEC_length (constructor_elt, CONSTRUCTOR_ELTS (init));
+      if (SCALAR_TYPE_P (type))
+	{
+	  if (init_len != 1)
+	    {
+	      error ("scalar object %qD requires one element in initializer",
+		     decl);
+	      TREE_TYPE (decl) = error_mark_node;
+	      return NULL_TREE;
+	    }
+	}
+      else if (flag_cpp98 && !CP_AGGREGATE_TYPE_P (type))
+	{
+	  /* A non-aggregate that is not a scalar cannot be initialized
+	     via an initializer-list in C++98.  */
+	  error ("braces around initializer for non-aggregate type %qT",
+		 type);
+	  TREE_TYPE (decl) = error_mark_node;
+	  return NULL_TREE;
+	}
     }
 
   if (TREE_CODE (decl) == CONST_DECL)
Index: gcc/c-opts.c
===================================================================
--- gcc/c-opts.c	(revision 125143)
+++ gcc/c-opts.c	(working copy)
@@ -1610,6 +1610,8 @@ set_std_cxx98 (int iso)
   flag_no_gnu_keywords = iso;
   flag_no_nonansi_builtin = iso;
   flag_iso = iso;
+  flag_cpp98 = 1;
+  flag_cpp0x = 0;
 }
 
 /* Set the C++ 0x working draft "standard" (without GNU extensions if ISO).  */
@@ -1620,6 +1622,7 @@ set_std_cxx0x (int iso)
   flag_no_gnu_keywords = iso;
   flag_no_nonansi_builtin = iso;
   flag_iso = iso;
+  flag_cpp98 = 0;
   flag_cpp0x = 1;
 }
 
Index: gcc/c-common.c
===================================================================
--- gcc/c-common.c	(revision 125143)
+++ gcc/c-common.c	(working copy)
@@ -406,6 +406,9 @@ int flag_access_control = 1;
 
 int flag_check_new;
 
+/* Nonzero means use C++98. This is the default.  */
+int flag_cpp98 = 1;
+
 /* Nonzero if we want to allow the use of experimental features that
    are likely to become part of C++0x. */
 
Index: gcc/c-common.h
===================================================================
--- gcc/c-common.h	(revision 125143)
+++ gcc/c-common.h	(working copy)
@@ -543,6 +543,10 @@ extern int flag_access_control;
 
 extern int flag_check_new;
 
+/* Nonzero means use C++98.  */
+
+extern int flag_cpp98;
+
 /* Nonzero if we want to allow the use of experimental features that
    are likely to become part of C++0x. */
 



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]