r179517 - Local thread_local variables are implicitly 'static'. (This doesn't apply to _Thread_local nor __thread.)

Richard Smith richard-llvm at metafoo.co.uk
Mon Apr 15 01:33:22 PDT 2013


Author: rsmith
Date: Mon Apr 15 03:33:22 2013
New Revision: 179517

URL: http://llvm.org/viewvc/llvm-project?rev=179517&view=rev
Log:
Local thread_local variables are implicitly 'static'. (This doesn't apply to _Thread_local nor __thread.)

Added:
    cfe/trunk/test/SemaCXX/cxx11-thread-local.cpp
Modified:
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/Sema/thread-specifier.c

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=179517&r1=179516&r2=179517&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Apr 15 03:33:22 2013
@@ -4687,8 +4687,7 @@ Sema::ActOnVariableDeclarator(Scope *S,
          "Parser allowed 'typedef' as storage class VarDecl.");
   VarDecl::StorageClass SC = StorageClassSpecToVarDeclStorageClass(SCSpec);
 
-  if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16)
-  {
+  if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp16) {
     // OpenCL v1.2 s6.1.1.1: reject declaring variables of the half and
     // half array type (unless the cl_khr_fp16 extension is enabled).
     if (Context.getBaseElementType(R)->isHalfType()) {
@@ -4705,6 +4704,16 @@ Sema::ActOnVariableDeclarator(Scope *S,
     SC = SC_None;
   }
 
+  // C++11 [dcl.stc]p4:
+  //   When thread_local is applied to a variable of block scope the
+  //   storage-class-specifier static is implied if it does not appear
+  //   explicitly.
+  // Core issue: 'static' is not implied if the variable is declared 'extern'.
+  if (SCSpec == DeclSpec::SCS_unspecified &&
+      D.getDeclSpec().getThreadStorageClassSpec() ==
+          DeclSpec::TSCS_thread_local && DC->isFunctionOrMethod())
+    SC = SC_Static;
+
   IdentifierInfo *II = Name.getAsIdentifierInfo();
   if (!II) {
     Diag(D.getIdentifierLoc(), diag::err_bad_variable_name)

Modified: cfe/trunk/test/Sema/thread-specifier.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/thread-specifier.c?rev=179517&r1=179516&r2=179517&view=diff
==============================================================================
--- cfe/trunk/test/Sema/thread-specifier.c (original)
+++ cfe/trunk/test/Sema/thread-specifier.c Mon Apr 15 03:33:22 2013
@@ -42,8 +42,6 @@ int f(__thread int t7) { // expected-err
   // expected-error at -2 {{'__thread' variables must have global storage}}
 #elif defined(C11)
   // expected-error at -4 {{'_Thread_local' variables must have global storage}}
-#else
-  // expected-error at -6 {{'thread_local' variables must have global storage}}
 #endif
   extern __thread int t9;
   static __thread int t10;
@@ -51,9 +49,9 @@ int f(__thread int t7) { // expected-err
 #if __cplusplus < 201103L
   __thread auto int t12a; // expected-error-re {{cannot combine with previous '(__thread|_Thread_local)' declaration specifier}}
   auto __thread int t12b; // expected-error {{cannot combine with previous 'auto' declaration specifier}}
-#else
-  __thread auto t12a = 0; // expected-error-re {{'(t|_T)hread_local' variables must have global storage}}
-  auto __thread t12b = 0; // expected-error-re {{'(t|_T)hread_local' variables must have global storage}}
+#elif !defined(CXX11)
+  __thread auto t12a = 0; // expected-error-re {{'_Thread_local' variables must have global storage}}
+  auto __thread t12b = 0; // expected-error-re {{'_Thread_local' variables must have global storage}}
 #endif
   __thread register int t13a; // expected-error-re {{cannot combine with previous '(__thread|_Thread_local|thread_local)' declaration specifier}}
   register __thread int t13b; // expected-error {{cannot combine with previous 'register' declaration specifier}}

Added: cfe/trunk/test/SemaCXX/cxx11-thread-local.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx11-thread-local.cpp?rev=179517&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/cxx11-thread-local.cpp (added)
+++ cfe/trunk/test/SemaCXX/cxx11-thread-local.cpp Mon Apr 15 03:33:22 2013
@@ -0,0 +1,23 @@
+// RUN: %clang_cc1 -std=c++11 -verify %s
+
+struct S {
+  static thread_local int a;
+  static int b; // expected-note {{here}}
+  thread_local int c; // expected-error {{'thread_local' is only allowed on variable declarations}}
+  static thread_local int d; // expected-note {{here}}
+};
+
+thread_local int S::a;
+thread_local int S::b; // expected-error {{thread-local declaration of 'b' follows non-thread-local declaration}}
+thread_local int S::c; // expected-error {{non-static data member defined out-of-line}}
+int S::d; // expected-error {{non-thread-local declaration of 'd' follows thread-local declaration}}
+
+thread_local int x[3];
+thread_local int y[3];
+thread_local int z[3]; // expected-note {{previous}}
+
+void f() {
+  thread_local int x;
+  static thread_local int y;
+  extern thread_local int z; // expected-error {{redefinition of 'z' with a different type}}
+}





More information about the cfe-commits mailing list