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