diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index b943e310ffa69f602c89456a6150d6313749847e..9b3083a4ecbabda3c40224321472ab27c42c196d 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -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) diff --git a/test/Sema/thread-specifier.c b/test/Sema/thread-specifier.c index 01bb8f7773f9e9386afd0a006ed48208d3662845..9d516e8f14130a6c79fa8ffabbdfefa234b94a75 100644 --- a/test/Sema/thread-specifier.c +++ b/test/Sema/thread-specifier.c @@ -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/test/SemaCXX/cxx11-thread-local.cpp b/test/SemaCXX/cxx11-thread-local.cpp new file mode 100644 index 0000000000000000000000000000000000000000..011c8862523f4c2f369fa1b04a9255ef599027b6 --- /dev/null +++ b/test/SemaCXX/cxx11-thread-local.cpp @@ -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}} +}