98a12cb
2017-01-30  Jakub Jelinek  <jakub@redhat.com>
98a12cb
98a12cb
	PR c++/79288
98a12cb
	* decl.c (grokdeclarator): For static data members, handle thread_p
98a12cb
	only after handling inline.
98a12cb
98a12cb
	* g++.dg/tls/pr79288.C: New test.
98a12cb
98a12cb
--- gcc/cp/decl.c.jj	2017-01-26 09:14:24.000000000 +0100
98a12cb
+++ gcc/cp/decl.c	2017-01-30 18:49:38.544438710 +0100
98a12cb
@@ -12049,14 +12049,6 @@ grokdeclarator (const cp_declarator *dec
98a12cb
 					    : input_location,
98a12cb
 					    VAR_DECL, unqualified_id, type);
98a12cb
 		set_linkage_for_static_data_member (decl);
98a12cb
-		if (thread_p)
98a12cb
-		  {
98a12cb
-		    CP_DECL_THREAD_LOCAL_P (decl) = true;
98a12cb
-		    if (!processing_template_decl)
98a12cb
-		      set_decl_tls_model (decl, decl_default_tls_model (decl));
98a12cb
-		    if (declspecs->gnu_thread_keyword_p)
98a12cb
-		      SET_DECL_GNU_TLS_P (decl);
98a12cb
-		  }
98a12cb
 		if (concept_p)
98a12cb
 		    error ("static data member %qE declared %<concept%>",
98a12cb
 			   unqualified_id);
98a12cb
@@ -12077,6 +12069,15 @@ grokdeclarator (const cp_declarator *dec
98a12cb
 		     definition is provided, unless this is an inline
98a12cb
 		     variable.  */
98a12cb
 		  DECL_EXTERNAL (decl) = 1;
98a12cb
+
98a12cb
+		if (thread_p)
98a12cb
+		  {
98a12cb
+		    CP_DECL_THREAD_LOCAL_P (decl) = true;
98a12cb
+		    if (!processing_template_decl)
98a12cb
+		      set_decl_tls_model (decl, decl_default_tls_model (decl));
98a12cb
+		    if (declspecs->gnu_thread_keyword_p)
98a12cb
+		      SET_DECL_GNU_TLS_P (decl);
98a12cb
+		  }
98a12cb
 	      }
98a12cb
 	    else
98a12cb
 	      {
98a12cb
--- gcc/testsuite/g++.dg/tls/pr79288.C.jj	2017-01-30 18:55:05.754282818 +0100
98a12cb
+++ gcc/testsuite/g++.dg/tls/pr79288.C	2017-01-30 18:54:52.000000000 +0100
98a12cb
@@ -0,0 +1,28 @@
98a12cb
+// PR c++/79288
98a12cb
+// { dg-do compile { target nonpic } }
98a12cb
+// { dg-require-effective-target tls }
98a12cb
+// { dg-options "-O2" }
98a12cb
+// { dg-final { scan-assembler-not "@tpoff" { target i?86-*-* x86_64-*-* } } }
98a12cb
+
98a12cb
+struct S
98a12cb
+{
98a12cb
+  static __thread int *p;
98a12cb
+};
98a12cb
+
98a12cb
+template <int N>
98a12cb
+struct T
98a12cb
+{
98a12cb
+  static __thread int *p;
98a12cb
+};
98a12cb
+
98a12cb
+int *
98a12cb
+foo ()
98a12cb
+{
98a12cb
+  return S::p;
98a12cb
+}
98a12cb
+
98a12cb
+int *
98a12cb
+bar ()
98a12cb
+{
98a12cb
+  return T<0>::p;
98a12cb
+}