Patchwork [C++,Patch/RFC] PR 55951

login
register
mail settings
Submitter Paolo Carlini
Date March 26, 2013, 12:09 p.m.
Message ID <51519007.9010708@oracle.com>
Download mbox | patch
Permalink /patch/231183/
State New
Headers show

Comments

Paolo Carlini - March 26, 2013, 12:09 p.m.
Hi,

in mainline and 4.8, at variance with 4.7, for:

enum { A };

static const char *a[] = {
   [A] = "a"
};

check_array_designated_initializer is called by reshape_init* with 
ce->index a CONST_DECL, not an INTEGER_CST. Thus I wondered if in such 
cases it's just matter of using integral_constant_value on it, thus 
something like the below (which passes testing on x86_64-linux).

Thanks,
Paolo.

//////////////////////
Jason Merrill - March 26, 2013, 12:32 p.m.
On 03/26/2013 08:09 AM, Paolo Carlini wrote:
> check_array_designated_initializer is called by reshape_init* with
> ce->index a CONST_DECL, not an INTEGER_CST. Thus I wondered if in such
> cases it's just matter of using integral_constant_value on it, thus
> something like the below (which passes testing on x86_64-linux).

Any constant-expression can go there, so I think I'd use 
cxx_constant_value unconditionally.

Jason

Patch

Index: cp/decl.c
===================================================================
--- cp/decl.c	(revision 197097)
+++ cp/decl.c	(working copy)
@@ -4766,15 +4766,18 @@  check_array_designated_initializer (const construc
   /* Designated initializers for array elements are not supported.  */
   if (ce->index)
     {
+      tree ce_index = (TREE_CODE (ce->index) == CONST_DECL
+		       ? integral_constant_value (ce->index) : ce->index);
+
       /* The parser only allows identifiers as designated
 	 initializers.  */
       if (ce->index == error_mark_node)
 	error ("name used in a GNU-style designated "
 	       "initializer for an array");
-      else if (TREE_CODE (ce->index) == INTEGER_CST)
+      else if (TREE_CODE (ce_index) == INTEGER_CST)
 	{
 	  /* A C99 designator is OK if it matches the current index.  */
-	  if (TREE_INT_CST_LOW (ce->index) == index)
+	  if (TREE_INT_CST_LOW (ce_index) == index)
 	    return true;
 	  else
 	    sorry ("non-trivial designated initializers not supported");
Index: testsuite/g++.dg/ext/desig5.C
===================================================================
--- testsuite/g++.dg/ext/desig5.C	(revision 0)
+++ testsuite/g++.dg/ext/desig5.C	(working copy)
@@ -0,0 +1,7 @@ 
+// PR c++/55951
+
+enum { A };
+
+static const char *a[] = {
+  [A] = "a"
+};