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