[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / warn-exit-time-destructors.cpp
blob55ae37d7368f884f6ce1a54a95f0d8f76927dc36
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wexit-time-destructors %s -verify=expected,cxx11
2 // RUN: %clang_cc1 -std=c++20 -fsyntax-only -Wexit-time-destructors %s -verify=expected
4 namespace test1 {
5 struct A { ~A(); };
6 A a; // expected-warning {{declaration requires an exit-time destructor}}
7 A b[10]; // expected-warning {{declaration requires an exit-time destructor}}
8 A c[10][10]; // expected-warning {{declaration requires an exit-time destructor}}
10 A &d = a;
11 A &e = b[5];
12 A &f = c[5][7];
15 namespace test2 {
16 void f() {
17 struct A { ~A() { } };
19 static A a; // expected-warning {{declaration requires an exit-time destructor}}
20 static A b[10]; // expected-warning {{declaration requires an exit-time destructor}}
21 static A c[10][10]; // expected-warning {{declaration requires an exit-time destructor}}
23 static A &d = a;
24 static A &e = b[5];
25 static A &f = c[5][7];
29 namespace test3 {
30 struct A { ~A() = default; };
31 A a;
33 struct B { ~B(); };
34 struct C : B { ~C() = default; };
35 C c; // expected-warning {{exit-time destructor}}
37 class D {
38 friend struct E;
39 ~D() = default;
41 struct E : D {
42 D d;
43 ~E() = default;
45 E e;
48 namespace test4 {
49 struct A { ~A(); };
50 [[clang::no_destroy]] A a; // no warning
53 namespace test5 {
54 struct A { ~A(); };
55 [[clang::always_destroy]] A a; // no warning
57 void func() {
58 [[clang::always_destroy]] static A a; // no warning
62 namespace test6 {
63 #if __cplusplus >= 202002L
64 #define CPP20_CONSTEXPR constexpr
65 #else
66 #define CPP20_CONSTEXPR
67 #endif
68 struct S {
69 CPP20_CONSTEXPR ~S() {}
71 S s; // cxx11-warning {{exit-time destructor}}
73 struct T {
74 CPP20_CONSTEXPR ~T() { if (b) {} }
75 bool b;
77 T t; // expected-warning {{exit-time destructor}}
78 #undef CPP20_CONSTEXPR