1ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic %s -DGNU 2ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DGNU 3ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic %s -DC11 -D__thread=_Thread_local 4ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DC11 -D__thread=_Thread_local 579f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DCXX11 -D__thread=thread_local -std=c++11 -Wno-deprecated 679f4bb7aad1b7c53f8a3bc43d89de0efdef8286dRichard Smith// RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DC11 -D__thread=_Thread_local -std=c++11 -Wno-deprecated 7ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith 8ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#ifdef __cplusplus 9ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// In C++, we define __private_extern__ to extern. 10ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#undef __private_extern__ 11ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 12f22f77f4e9149499a987e2363a0be01d0bc2d426Eli Friedman 13f22f77f4e9149499a987e2363a0be01d0bc2d426Eli Friedman__thread int t1; 14ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith__thread extern int t2; 15ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith__thread static int t3; 16ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#ifdef GNU 17ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-warning@-3 {{'__thread' before 'extern'}} 18ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-warning@-3 {{'__thread' before 'static'}} 19ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 20ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith 21f22f77f4e9149499a987e2363a0be01d0bc2d426Eli Friedman__thread __private_extern__ int t4; 22ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smithstruct t5 { __thread int x; }; 23ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#ifdef __cplusplus 24651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines// expected-error-re@-2 {{'{{__thread|_Thread_local|thread_local}}' is only allowed on variable declarations}} 25ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#else 26ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// FIXME: The 'is only allowed on variable declarations' diagnostic is better here. 27ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-error@-5 {{type name does not allow storage class to be specified}} 28ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 29ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith 30ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith__thread int t6(); 31ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#if defined(GNU) 32ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-error@-2 {{'__thread' is only allowed on variable declarations}} 33ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#elif defined(C11) 34ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-error@-4 {{'_Thread_local' is only allowed on variable declarations}} 35ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#else 36ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-error@-6 {{'thread_local' is only allowed on variable declarations}} 37ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 383af16fdfddc63cf6c611cc1eb2c7822d901bd470Hans Wennborg 39ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smithint f(__thread int t7) { // expected-error {{' is only allowed on variable declarations}} 40ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith __thread int t8; 41ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#if defined(GNU) 42ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith // expected-error@-2 {{'__thread' variables must have global storage}} 43ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#elif defined(C11) 44ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith // expected-error@-4 {{'_Thread_local' variables must have global storage}} 45ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 463af16fdfddc63cf6c611cc1eb2c7822d901bd470Hans Wennborg extern __thread int t9; 473af16fdfddc63cf6c611cc1eb2c7822d901bd470Hans Wennborg static __thread int t10; 48f22f77f4e9149499a987e2363a0be01d0bc2d426Eli Friedman __thread __private_extern__ int t11; 49ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#if __cplusplus < 201103L 50651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines __thread auto int t12a; // expected-error-re {{cannot combine with previous '{{__thread|_Thread_local}}' declaration specifier}} 51ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith auto __thread int t12b; // expected-error {{cannot combine with previous 'auto' declaration specifier}} 52df4cc0a0c75ff9d92ca501ecd40c5413b97506d6Richard Smith#elif !defined(CXX11) 53651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines __thread auto t12a = 0; // expected-error {{'_Thread_local' variables must have global storage}} 54651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines auto __thread t12b = 0; // expected-error {{'_Thread_local' variables must have global storage}} 55ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 56651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines __thread register int t13a; // expected-error-re {{cannot combine with previous '{{__thread|_Thread_local|thread_local}}' declaration specifier}} 57ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith register __thread int t13b; // expected-error {{cannot combine with previous 'register' declaration specifier}} 58f22f77f4e9149499a987e2363a0be01d0bc2d426Eli Friedman} 593af16fdfddc63cf6c611cc1eb2c7822d901bd470Hans Wennborg 60651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines__thread typedef int t14; // expected-error-re {{cannot combine with previous '{{__thread|_Thread_local|thread_local}}' declaration specifier}} 61c568f1e98938584c0ef0b12ae5018ff7d90a4072Stephen Hines__thread int t15; // expected-note {{previous definition is here}} 62ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smithextern int t15; // expected-error {{non-thread-local declaration of 't15' follows thread-local declaration}} 6338afbc7361d861968232defaeaf8e302af75b5eeRichard Smithextern int t16; // expected-note {{previous declaration is here}} 64f22f77f4e9149499a987e2363a0be01d0bc2d426Eli Friedman__thread int t16; // expected-error {{thread-local declaration of 't16' follows non-thread-local declaration}} 650f87dd74fe1f4fc2bafe16f708500d68ed04d442Hans Wennborg 6638afbc7361d861968232defaeaf8e302af75b5eeRichard Smith#ifdef CXX11 6738afbc7361d861968232defaeaf8e302af75b5eeRichard Smithextern thread_local int t17; // expected-note {{previous declaration is here}} 6838afbc7361d861968232defaeaf8e302af75b5eeRichard Smith_Thread_local int t17; // expected-error {{thread-local declaration of 't17' with static initialization follows declaration with dynamic initialization}} 6938afbc7361d861968232defaeaf8e302af75b5eeRichard Smithextern _Thread_local int t18; // expected-note {{previous declaration is here}} 7038afbc7361d861968232defaeaf8e302af75b5eeRichard Smiththread_local int t18; // expected-error {{thread-local declaration of 't18' with dynamic initialization follows declaration with static initialization}} 7138afbc7361d861968232defaeaf8e302af75b5eeRichard Smith#endif 7238afbc7361d861968232defaeaf8e302af75b5eeRichard Smith 730f87dd74fe1f4fc2bafe16f708500d68ed04d442Hans Wennborg// PR13720 740f87dd74fe1f4fc2bafe16f708500d68ed04d442Hans Wennborg__thread int thread_int; 75ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smithint *thread_int_ptr = &thread_int; 76ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#ifndef __cplusplus 77ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith// expected-error@-2 {{initializer element is not a compile-time constant}} 78ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 790f87dd74fe1f4fc2bafe16f708500d68ed04d442Hans Wennborgvoid g() { 800f87dd74fe1f4fc2bafe16f708500d68ed04d442Hans Wennborg int *p = &thread_int; // This is perfectly fine, though. 810f87dd74fe1f4fc2bafe16f708500d68ed04d442Hans Wennborg} 82ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#if __cplusplus >= 201103L 83ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smithconstexpr int *thread_int_ptr_2 = &thread_int; // expected-error {{must be initialized by a constant expression}} 84ec64244f5939fa81596fbeddad966cca4b4a4c51Richard Smith#endif 856a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith 866a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smithint non_const(); 876a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith__thread int non_const_init = non_const(); 886a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#if !defined(__cplusplus) 896a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith// expected-error@-2 {{initializer element is not a compile-time constant}} 906a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#elif !defined(CXX11) 916a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith// expected-error@-4 {{initializer for thread-local variable must be a constant expression}} 926a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#if __cplusplus >= 201103L 936a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith// expected-note@-6 {{use 'thread_local' to allow this}} 946a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#endif 956a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#endif 966a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith 976a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#ifdef __cplusplus 986a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smithstruct S { 996a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith ~S(); 1006a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith}; 1016a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith__thread S s; 1026a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#if !defined(CXX11) 1036a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith// expected-error@-2 {{type of thread-local variable has non-trivial destruction}} 1046a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#if __cplusplus >= 201103L 1056a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith// expected-note@-4 {{use 'thread_local' to allow this}} 1066a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#endif 1076a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#endif 1086a570f610c101054e79af2fcdb73a10f8e3f337dRichard Smith#endif 109b6b127f279f89d992e0713866f17fed267147a79Richard Smith 1106bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#ifdef __cplusplus 1116bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hinesstruct HasCtor { 1126bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines HasCtor(); 1136bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines}; 1146bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines__thread HasCtor var_with_ctor; 1156bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if !defined(CXX11) 1166bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines// expected-error@-2 {{initializer for thread-local variable must be a constant expression}} 1176bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#if __cplusplus >= 201103L 1186bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines// expected-note@-4 {{use 'thread_local' to allow this}} 1196bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif 1206bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif 1216bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines#endif 1226bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89Stephen Hines 123b6b127f279f89d992e0713866f17fed267147a79Richard Smith__thread int aggregate[10] = {0}; 124