Local thread_local variables are implicitly 'static'. (This doesn't apply to _Thread_...
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 15 Apr 2013 08:33:22 +0000 (08:33 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 15 Apr 2013 08:33:22 +0000 (08:33 +0000)
llvm-svn: 179517

clang/lib/Sema/SemaDecl.cpp
clang/test/Sema/thread-specifier.c
clang/test/SemaCXX/cxx11-thread-local.cpp [new file with mode: 0644]

index b943e31..9b3083a 100644 (file)
@@ -4687,8 +4687,7 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC,
          "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, Declarator &D, DeclContext *DC,
     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)
index 01bb8f7..9d516e8 100644 (file)
@@ -42,8 +42,6 @@ int f(__thread int t7) { // expected-error {{' is only allowed on variable decla
   // expected-error@-2 {{'__thread' variables must have global storage}}
 #elif defined(C11)
   // expected-error@-4 {{'_Thread_local' variables must have global storage}}
-#else
-  // expected-error@-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-error {{' is only allowed on variable decla
 #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}}
diff --git a/clang/test/SemaCXX/cxx11-thread-local.cpp b/clang/test/SemaCXX/cxx11-thread-local.cpp
new file mode 100644 (file)
index 0000000..011c886
--- /dev/null
@@ -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}}
+}