Patchwork C++ PATCH for c++/56859 (alignas and value-dependent expressions)

login
register
mail settings
Submitter Jason Merrill
Date April 25, 2013, 4:18 p.m.
Message ID <5179575C.7020805@redhat.com>
Download mbox | patch
Permalink /patch/239561/
State New
Headers show

Comments

Jason Merrill - April 25, 2013, 4:18 p.m.
Leaving value-dependent expressions alone doesn't work if you then 
silently turn them into error_mark_node because they aren't INTEGER_CST. 
  And while I'm in this code, there's no need to check the result of 
cxx_constant_value, as it already returns error_mark_node for 
non-constant expressions.

Tested x86_64-pc-linux-gnu, applying to trunk and 4.8.

Patch

commit 66d07cda5155f2713de7f5e85a37062210c7c0b4
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 24 21:13:50 2013 -0400

    	PR c++/56859
    	* typeck.c (cxx_alignas_expr): Handle value-dependence properly.

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index 84da5de..b761dd5 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -1725,15 +1725,19 @@  cxx_alignas_expr (tree e)
        
 	   When the alignment-specifier is of the form
 	   alignas(type-id ), it shall have the same effect as
-	   alignas( alignof(type-id )).  */
+	   alignas(alignof(type-id )).  */
 
     return cxx_sizeof_or_alignof_type (e, ALIGNOF_EXPR, false);
   
-
   /* If we reach this point, it means the alignas expression if of
      the form "alignas(assignment-expression)", so we should follow
      what is stated by [dcl.align]/2.  */
 
+  if (value_dependent_expression_p (e))
+    /* Leave value-dependent expression alone for now. */
+    return e;
+
+  e = fold_non_dependent_expr (e);
   e = mark_rvalue_use (e);
 
   /* [dcl.align]/2 says:
@@ -1741,18 +1745,7 @@  cxx_alignas_expr (tree e)
          the assignment-expression shall be an integral constant
 	 expression.  */
   
-  e = fold_non_dependent_expr (e);
-  if (value_dependent_expression_p (e))
-    /* Leave value-dependent expression alone for now. */;
-  else
-    e = cxx_constant_value (e);
-
-  if (e == NULL_TREE
-      || e == error_mark_node
-      || TREE_CODE (e) != INTEGER_CST)
-    return error_mark_node;
-
-  return e;
+  return cxx_constant_value (e);
 }
 
 
diff --git a/gcc/testsuite/g++.dg/cpp0x/gen-attrs-54.C b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-54.C
new file mode 100644
index 0000000..45aa8e4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/gen-attrs-54.C
@@ -0,0 +1,14 @@ 
+// PR c++/56859
+// { dg-require-effective-target c++11 }
+
+template<unsigned size, unsigned alignment>
+struct aligned_storage
+{
+  using type = struct { alignas(alignment) unsigned char data[size]; };
+};
+
+#define SA(X) static_assert((X),#X)
+SA(alignof(aligned_storage<8,1>::type) == 1);
+SA(alignof(aligned_storage<8,2>::type) == 2);
+SA(alignof(aligned_storage<8,4>::type) == 4);
+SA(alignof(aligned_storage<8,8>::type) == 8);