diff mbox

[C++] PR 43453

Message ID 538F7D2F.9080600@oracle.com
State New
Headers show

Commit Message

Paolo Carlini June 4, 2014, 8:10 p.m. UTC
Hi,

On 06/04/2014 09:32 PM, Jason Merrill wrote:
> On 06/04/2014 02:08 PM, Paolo Carlini wrote:
>>         /* An array of character type can be initialized from a
>> -     brace-enclosed string constant.
>> -
>> -     FIXME: this code is duplicated from reshape_init. Probably
>> -     we should just call reshape_init here?  */
>
> It strikes me as odd that we get here before we strip the parens. What 
> if we moved the paren code up out of store_init_value to 
> check_initializer before the call to maybe_deduce_size_from_array_init?
Ok... At first I was worried by the other 
maybe_deduce_size_from_array_init call, but I don't think the 
initializer of a char array can be dependent, right? Then I'm finishing 
testing the below.

Thanks!
Paolo.

///////////////////
diff mbox

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 211242)
+++ cp/decl.c	(working copy)
@@ -5758,6 +5758,13 @@  check_initializer (tree decl, tree init, int flags
 		check_narrowing (type, init);
 	    }
 	}
+      else if (TREE_CODE (type) == ARRAY_TYPE
+	       && TREE_CODE (init) == TREE_LIST
+	       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
+	       && list_length (init) == 1
+	       && TREE_CODE (TREE_VALUE (init)) == STRING_CST)
+	/* We get here with code like `char s[] ("abc");' */
+	init = TREE_VALUE (init);
 
       /* If DECL has an array type without a specific bound, deduce the
 	 array size from the initializer.  */
@@ -7176,20 +7183,21 @@  cp_complete_array_type (tree *ptype, tree initial_
       tree value;
 
       /* An array of character type can be initialized from a
-	 brace-enclosed string constant.
-
-	 FIXME: this code is duplicated from reshape_init. Probably
-	 we should just call reshape_init here?  */
-      if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype)))
-	  && TREE_CODE (initial_value) == CONSTRUCTOR
-	  && !vec_safe_is_empty (CONSTRUCTOR_ELTS (initial_value)))
+	 brace-enclosed string constant.  */
+      if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))))
 	{
-	  vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initial_value);
-	  tree value = (*v)[0].value;
+	  tree svalue = initial_value;
 
-	  if (TREE_CODE (value) == STRING_CST
-	      && v->length () == 1)
-	    initial_value = value;
+	  if (TREE_CODE (svalue) == CONSTRUCTOR
+	      && vec_safe_length (CONSTRUCTOR_ELTS (svalue)) == 1)
+	    svalue = (*CONSTRUCTOR_ELTS (svalue))[0].value;
+	  else if (TREE_CODE (svalue) == TREE_LIST
+		   && list_length (svalue) == 1)
+	    // We get here with code like `char s[] ("abc");'
+	    svalue = TREE_VALUE (svalue);
+
+	  if (TREE_CODE (svalue) == STRING_CST)
+	    initial_value = svalue;
 	}
 
       /* If any of the elements are parameter packs, we can't actually
Index: cp/typeck.c
===================================================================
--- cp/typeck.c	(revision 211242)
+++ cp/typeck.c	(working copy)
@@ -7511,6 +7511,18 @@  cp_build_modify_expr (tree lhs, enum tree_code mod
 	    return error_mark_node;
 	}
 
+      /* C++11 8.5/17: "If the destination type is an array of characters,
+	 an array of char16_t, an array of char32_t, or an array of wchar_t,
+	 and the initializer is a string literal...".  */
+      else if (TREE_CODE (newrhs) == STRING_CST
+	       && char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
+	       && modifycode == INIT_EXPR)
+	{
+	  newrhs = digest_init (lhstype, newrhs, complain);
+	  if (newrhs == error_mark_node)
+	    return error_mark_node;
+	}
+
       else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
 				     TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
 	{
Index: cp/typeck2.c
===================================================================
--- cp/typeck2.c	(revision 211242)
+++ cp/typeck2.c	(working copy)
@@ -785,16 +785,9 @@  store_init_value (tree decl, tree init, vec<tree,
     {
       gcc_assert (TREE_CODE (decl) != RESULT_DECL);
 
-      if (TREE_CODE (init) == TREE_LIST
-	       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
-	{
-	  error ("cannot initialize arrays using this syntax");
-	  return NULL_TREE;
-	}
-      else
-	/* We get here with code like `int a (2);' */
-	init = build_x_compound_expr_from_list (init, ELK_INIT,
-						tf_warning_or_error);
+      /* We get here with code like `int a (2);' */
+      init = build_x_compound_expr_from_list (init, ELK_INIT,
+					      tf_warning_or_error);
     }
 
   /* End of special C++ code.  */
Index: testsuite/g++.dg/init/pr43453.C
===================================================================
--- testsuite/g++.dg/init/pr43453.C	(revision 0)
+++ testsuite/g++.dg/init/pr43453.C	(working copy)
@@ -0,0 +1,33 @@ 
+// PR c++/43453
+
+struct A {
+  char x[4]; 
+  A() : x("bug") { };
+};
+
+char x [4] ("bug");
+
+struct CA {
+  const char cx[4]; 
+  CA() : cx("bug") { };
+};
+
+const char cx [4] ("bug");
+
+struct B {
+  char y[4]; 
+  B() : y("bu") { };
+};
+
+char y [4] ("bu");
+
+struct C {
+  char z[4]; 
+  C() : z("bugs") { };  // { dg-error "too long" }
+};
+
+char z [4] ("bugs");    // { dg-error "too long" }
+
+char k [] ("bug");
+
+const char ck [] ("bug");