1 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic %s -DGNU
2 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic -x c++ %s -DGNU -std=c++98
3 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify -pedantic %s -std=c11 -DC11 -D__thread=_Thread_local
4 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify=expected,thread-local -pedantic -x c++ %s -DC11 -D__thread=_Thread_local -std=c++98
5 // 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
6 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify=expected,thread-local -pedantic -x c++ %s -DC11 -D__thread=_Thread_local -std=c++11 -Wno-deprecated
7 // RUN: %clang_cc1 -triple i686-pc-linux-gnu -fsyntax-only -Wno-private-extern -verify=expected,thread-local -pedantic %s -std=c99 -D__thread=_Thread_local -DC99
8 
9 #ifdef __cplusplus
10 // In C++, we define __private_extern__ to extern.
11 #undef __private_extern__
12 #endif
13 
14 __thread int t1; // thread-local-warning {{'_Thread_local' is a C11 extension}}
15 __thread extern int t2; // thread-local-warning {{'_Thread_local' is a C11 extension}}
16 __thread static int t3; // thread-local-warning {{'_Thread_local' is a C11 extension}}
17 #ifdef GNU
18 // expected-warning@-3 {{'__thread' before 'extern'}}
19 // expected-warning@-3 {{'__thread' before 'static'}}
20 #endif
21 
22 __thread __private_extern__ int t4; // thread-local-warning {{'_Thread_local' is a C11 extension}}
23 struct t5 { __thread int x; }; // thread-local-warning {{'_Thread_local' is a C11 extension}}
24 #ifdef __cplusplus
25 // expected-error-re@-2 {{'{{__thread|_Thread_local|thread_local}}' is only allowed on variable declarations}}
26 #else
27 // FIXME: The 'is only allowed on variable declarations' diagnostic is better here.
28 // expected-error@-5 {{type name does not allow storage class to be specified}}
29 #endif
30 
31 __thread int t6(); // thread-local-warning {{'_Thread_local' is a C11 extension}}
32 #if defined(GNU)
33 // expected-error@-2 {{'__thread' is only allowed on variable declarations}}
34 #elif defined(C11) || defined(C99)
35 // expected-error@-4 {{'_Thread_local' is only allowed on variable declarations}}
36 #else
37 // expected-error@-6 {{'thread_local' is only allowed on variable declarations}}
38 #endif
39 
f(__thread int t7)40 int f(__thread int t7) { // expected-error {{' is only allowed on variable declarations}} \
41                          // thread-local-warning {{'_Thread_local' is a C11 extension}}
42   __thread int t8; // thread-local-warning {{'_Thread_local' is a C11 extension}}
43 #if defined(GNU)
44   // expected-error@-2 {{'__thread' variables must have global storage}}
45 #elif defined(C11) || defined(C99)
46   // expected-error@-4 {{'_Thread_local' variables must have global storage}}
47 #endif
48   extern __thread int t9; // thread-local-warning {{'_Thread_local' is a C11 extension}}
49   static __thread int t10; // thread-local-warning {{'_Thread_local' is a C11 extension}}
50   __thread __private_extern__ int t11; // thread-local-warning {{'_Thread_local' is a C11 extension}}
51 #if __cplusplus < 201103L
52   __thread auto int t12a; // expected-error-re {{cannot combine with previous '{{__thread|_Thread_local}}' declaration specifier}} \
53                           // thread-local-warning {{'_Thread_local' is a C11 extension}}
54   auto __thread int t12b; // expected-error {{cannot combine with previous 'auto' declaration specifier}} \
55                           // thread-local-warning {{'_Thread_local' is a C11 extension}}
56 #elif !defined(CXX11)
57   __thread auto t12a = 0; // expected-error {{'_Thread_local' variables must have global storage}} \
58                           // thread-local-warning {{'_Thread_local' is a C11 extension}}
59   auto __thread t12b = 0; // expected-error {{'_Thread_local' variables must have global storage}} \
60                           // thread-local-warning {{'_Thread_local' is a C11 extension}}
61 #endif
62   __thread register int t13a; // expected-error-re {{cannot combine with previous '{{__thread|_Thread_local|thread_local}}' declaration specifier}} \
63                               // thread-local-warning {{'_Thread_local' is a C11 extension}}
64   register __thread int t13b; // expected-error {{cannot combine with previous 'register' declaration specifier}} \
65                               // thread-local-warning {{'_Thread_local' is a C11 extension}}
66 }
67 
68 __thread typedef int t14; // expected-error-re {{cannot combine with previous '{{__thread|_Thread_local|thread_local}}' declaration specifier}} \
69                           // thread-local-warning {{'_Thread_local' is a C11 extension}}
70 __thread int t15; // expected-note {{previous definition is here}} \
71                   // thread-local-warning {{'_Thread_local' is a C11 extension}}
72 extern int t15; // expected-error {{non-thread-local declaration of 't15' follows thread-local declaration}}
73 extern int t16; // expected-note {{previous declaration is here}}
74 __thread int t16; // expected-error {{thread-local declaration of 't16' follows non-thread-local declaration}} \
75                   // thread-local-warning {{'_Thread_local' is a C11 extension}}
76 
77 #ifdef CXX11
78 extern thread_local int t17; // expected-note {{previous declaration is here}}
79 _Thread_local int t17; // expected-error {{thread-local declaration of 't17' with static initialization follows declaration with dynamic initialization}} \
80                        // expected-warning {{'_Thread_local' is a C11 extension}}
81 extern _Thread_local int t18; // expected-note {{previous declaration is here}} \
82                               // expected-warning {{'_Thread_local' is a C11 extension}}
83 thread_local int t18; // expected-error {{thread-local declaration of 't18' with dynamic initialization follows declaration with static initialization}}
84 #endif
85 
86 // PR13720
87 __thread int thread_int; // thread-local-warning {{'_Thread_local' is a C11 extension}}
88 int *thread_int_ptr = &thread_int;
89 #ifndef __cplusplus
90 // expected-error@-2 {{initializer element is not a compile-time constant}}
91 #endif
g()92 void g() {
93   int *p = &thread_int; // This is perfectly fine, though.
94 }
95 #if __cplusplus >= 201103L
96 constexpr int *thread_int_ptr_2 = &thread_int; // expected-error {{must be initialized by a constant expression}}
97 #endif
98 
99 int non_const();
100 __thread int non_const_init = non_const(); // thread-local-warning {{'_Thread_local' is a C11 extension}}
101 #if !defined(__cplusplus)
102 // expected-error@-2 {{initializer element is not a compile-time constant}}
103 #elif !defined(CXX11)
104 // expected-error@-4 {{initializer for thread-local variable must be a constant expression}}
105 #if __cplusplus >= 201103L
106 // expected-note@-6 {{use 'thread_local' to allow this}}
107 #endif
108 #endif
109 
110 #ifdef __cplusplus
111 struct S {
112   ~S();
113 };
114 __thread S s; // thread-local-warning {{'_Thread_local' is a C11 extension}}
115 #if !defined(CXX11)
116 // expected-error@-2 {{type of thread-local variable has non-trivial destruction}}
117 #if __cplusplus >= 201103L
118 // expected-note@-4 {{use 'thread_local' to allow this}}
119 #endif
120 #endif
121 #endif
122 
123 #ifdef __cplusplus
124 struct HasCtor {
125   HasCtor();
126 };
127 __thread HasCtor var_with_ctor; // thread-local-warning {{'_Thread_local' is a C11 extension}}
128 #if !defined(CXX11)
129 // expected-error@-2 {{initializer for thread-local variable must be a constant expression}}
130 #if __cplusplus >= 201103L
131 // expected-note@-4 {{use 'thread_local' to allow this}}
132 #endif
133 #endif
134 #endif
135 
136 __thread int aggregate[10] = {0}; // thread-local-warning {{'_Thread_local' is a C11 extension}}
137