Clang Project

clang_source_code/test/SemaCXX/no_destroy.cpp
1// RUN: %clang_cc1 -DNO_DTORS -fno-c++-static-destructors -verify %s
2// RUN: %clang_cc1 -verify %s
3
4struct SecretDestructor {
5#ifndef NO_DTORS
6  // expected-note@+2 4 {{private}}
7#endif
8private: ~SecretDestructor(); // expected-note 2 {{private}}
9};
10
11SecretDestructor sd1;
12thread_local SecretDestructor sd2;
13void locals() {
14  static SecretDestructor sd3;
15  thread_local SecretDestructor sd4;
16}
17
18#ifndef NO_DTORS
19// SecretDestructor sd1;                  // expected-error@-8 {{private}}
20// thread_local SecretDestructor sd2;     // expected-error@-8 {{private}}
21// void locals() {
22//   static SecretDestructor sd3;         // expected-error@-8 {{private}}
23//   thread_local SecretDestructor sd4;   // expected-error@-8 {{private}}
24// }
25#endif
26
27[[clang::always_destroy]] SecretDestructor sd6; // expected-error{{private}}
28[[clang::always_destroy]] thread_local SecretDestructor sd7; // expected-error{{private}}
29
30[[clang::no_destroy]] SecretDestructor sd8;
31
32int main() {
33  [[clang::no_destroy]] int p; // expected-error{{no_destroy attribute can only be applied to a variable with static or thread storage duration}}
34  [[clang::always_destroy]] int p2; // expected-error{{always_destroy attribute can only be applied to a variable with static or thread storage duration}}
35  [[clang::no_destroy]] static int p3;
36  [[clang::always_destroy]] static int p4;
37}
38
39[[clang::always_destroy]] [[clang::no_destroy]] int p; // expected-error{{'no_destroy' and 'always_destroy' attributes are not compatible}} // expected-note{{here}}
40[[clang::no_destroy]] [[clang::always_destroy]] int p2; // expected-error{{'always_destroy' and 'no_destroy' attributes are not compatible}} // expected-note{{here}}
41
42[[clang::always_destroy]] void f() {} // expected-warning{{'always_destroy' attribute only applies to variables}}
43struct [[clang::no_destroy]] DoesntApply {};  // expected-warning{{'no_destroy' attribute only applies to variables}}
44
45[[clang::no_destroy(0)]] int no_args; // expected-error{{'no_destroy' attribute takes no arguments}}
46[[clang::always_destroy(0)]] int no_args2; // expected-error{{'always_destroy' attribute takes no arguments}}
47