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