diff mbox

[C++] Improve -std=c++98 static var initializers with sizeof (PR c++/55081)

Message ID 20121026194124.GE1752@tucnak.redhat.com
State New
Headers show

Commit Message

Jakub Jelinek Oct. 26, 2012, 7:41 p.m. UTC
Hi!

The SIZEOF_EXPR delayed folding caused another problem as the following
testcase shows for -std=c++98.  Fixed by always performing
maybe_constant_init, even for C++98.  Bootstrapped/regtested on x86_64-linux
and i686-linux, ok for trunk?

2012-10-26  Jakub Jelinek  <jakub@redhat.com>

	PR c++/55081
	* typeck2.c (store_init_value): Call fold_non_dependent_expr
	and maybe_constant_init even for C++98.

	* g++.dg/opt/pr55081.C: New test.


	Jakub

Comments

Jason Merrill Oct. 26, 2012, 8:27 p.m. UTC | #1
OK.

Jason
diff mbox

Patch

--- gcc/cp/typeck2.c.jj	2012-09-25 11:59:43.000000000 +0200
+++ gcc/cp/typeck2.c	2012-10-26 17:46:22.935974382 +0200
@@ -709,11 +709,9 @@  store_init_value (tree decl, tree init,
 
   /* In C++0x constant expression is a semantic, not syntactic, property.
      In C++98, make sure that what we thought was a constant expression at
-     template definition time is still constant.  */
-  if ((cxx_dialect >= cxx0x
-       || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl))
-      && (decl_maybe_constant_var_p (decl)
-	  || TREE_STATIC (decl)))
+     template definition time is still constant and otherwise perform this
+     as optimization, e.g. to fold SIZEOF_EXPRs in the initializer.  */
+  if (decl_maybe_constant_var_p (decl) || TREE_STATIC (decl))
     {
       bool const_init;
       value = fold_non_dependent_expr (value);
--- gcc/testsuite/g++.dg/opt/pr55081.C.jj	2012-10-26 17:44:03.869797146 +0200
+++ gcc/testsuite/g++.dg/opt/pr55081.C	2012-10-26 17:42:41.000000000 +0200
@@ -0,0 +1,17 @@ 
+// PR c++/55081
+// { dg-do compile }
+
+struct R { int field; } r;
+
+__UINTPTR_TYPE__ *
+foo ()
+{
+  static __UINTPTR_TYPE__ array[] = {
+    sizeof (char),
+    (reinterpret_cast <__UINTPTR_TYPE__>(&r.field)
+     - reinterpret_cast <__UINTPTR_TYPE__>(&r)) + 1
+  };
+  return array;
+}
+
+// { dg-final { scan-assembler-not "_ZGVZ3foovE5array" } }