diff mbox

C++ PATCH for c++/61343 (missing init for thread_local)

Message ID 5390A8C3.6030009@redhat.com
State New
Headers show

Commit Message

Jason Merrill June 5, 2014, 5:28 p.m. UTC
The bug here was that we were setting 
DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P for a variable where the 
explicitly written initializer is constant, but becomes a non-constant 
constructor call.  Fixed thus.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit 210aa8e75e0827163fd3041890404d39d485d23d
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 4 13:28:38 2014 -0400

    	PR c++/61343
    	* decl.c (check_initializer): Maybe clear
    	DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 3d4058c..b068df8 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -5856,6 +5856,13 @@  check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups)
   if (init && init != error_mark_node)
     init_code = build2 (INIT_EXPR, type, decl, init);
 
+  if (init_code)
+    {
+      /* We might have set these in cp_finish_decl.  */
+      DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false;
+      TREE_CONSTANT (decl) = false;
+    }
+
   if (init_code && DECL_IN_AGGR_P (decl))
     {
       static int explained = 0;
diff --git a/gcc/testsuite/g++.dg/tls/thread_local9.C b/gcc/testsuite/g++.dg/tls/thread_local9.C
new file mode 100644
index 0000000..c75528a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tls/thread_local9.C
@@ -0,0 +1,23 @@ 
+// PR c++/61343
+
+// { dg-do run { target c++11 } }
+// { dg-add-options tls }
+// { dg-require-effective-target tls_runtime }
+
+struct Foo {
+  int value;
+
+  Foo() noexcept {
+    value = 12;
+  }
+};
+
+static thread_local Foo a{};
+
+static __attribute__((noinline)) void UseA() {
+  if (a.value != 12) __builtin_abort();
+}
+
+int main() {
+  UseA();
+}