thread-specifier.c 5.2 KB

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