[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CXX / drs / cwg9xx.cpp
blobd4f54bcdad6ea03fd4bfc9d3c9349386ef8844c9
1 // RUN: %clang_cc1 -std=c++98 %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
8 namespace std {
9 __extension__ typedef __SIZE_TYPE__ size_t;
11 template<typename T> struct initializer_list {
12 const T *p; size_t n;
13 initializer_list(const T *p, size_t n);
17 namespace cwg930 { // cwg930: 2.7
18 #if __cplusplus >= 201103L
19 static_assert(alignof(int[]) == alignof(int), "");
20 static_assert(alignof(int[][2]) == alignof(int[2]), "");
21 #endif
22 } // namespace cwg930
24 namespace cwg948 { // cwg948: 3.7
25 #if __cplusplus >= 201103L
26 class A {
27 public:
28 constexpr A(int v) : v(v) { }
29 constexpr operator int() const { return v; }
30 private:
31 int v;
34 constexpr int id(int x)
36 return x;
39 void f() {
40 if (constexpr int i = id(101)) { }
41 switch (constexpr int i = id(2)) { default: break; case 2: break; }
42 for (; constexpr int i = id(0); ) { }
43 while (constexpr int i = id(0)) { }
45 if (constexpr A i = 101) { }
46 switch (constexpr A i = 2) { default: break; case 2: break; }
47 for (; constexpr A i = 0; ) { }
48 while (constexpr A i = 0) { }
50 #endif
53 namespace cwg952 { // cwg952: 2.8
54 namespace example1 {
55 struct A {
56 typedef int I; // #cwg952-I
58 struct B : private A { // #cwg952-B
60 struct C : B {
61 void f() {
62 I i1;
63 // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
64 // expected-note@#cwg952-B {{constrained by private inheritance here}}
65 // expected-note@#cwg952-I {{member is declared here}}
67 I i2;
68 // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
69 // expected-note@#cwg952-B {{constrained by private inheritance here}}
70 // expected-note@#cwg952-I {{member is declared here}}
71 struct D {
72 I i3;
73 // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
74 // expected-note@#cwg952-B {{constrained by private inheritance here}}
75 // expected-note@#cwg952-I {{member is declared here}}
76 void g() {
77 I i4;
78 // expected-error@-1 {{'I' is a private member of 'cwg952::example1::A'}}
79 // expected-note@#cwg952-B {{constrained by private inheritance here}}
80 // expected-note@#cwg952-I {{member is declared here}}
84 } // namespace example1
85 namespace example2 {
86 struct A {
87 protected:
88 static int x;
90 struct B : A {
91 friend int get(B) { return x; }
93 } // namespace example2
94 } // namespace cwg952
96 namespace cwg960 { // cwg960: 3.0
97 struct a {};
98 class A {
99 #if __cplusplus >= 201103L
100 // Check lvalue ref vs rvalue ref vs pointer.
101 virtual a& rvalue_ref();
102 virtual a&& lvalue_ref();
103 virtual a& rvalue_vs_lvalue_ref(); // #cwg960-A-rvalue_vs_lvalue_ref
104 virtual a&& lvalue_vs_rvalue_ref(); // #cwg960-A-lvalue_vs_rvalue_ref
105 virtual a& rvalue_ref_vs_pointer(); // #cwg960-A-rvalue_ref_vs_pointer
106 virtual a* pointer_vs_rvalue_ref(); // #cwg960-A-pointer_vs_rvalue_ref
107 virtual a&& lvalue_ref_vs_pointer(); // #cwg960-A-lvalue_ref_vs_pointer
108 virtual a* pointer_vs_lvalue_ref(); // #cwg960-A-pointer_vs_lvalue_ref
109 #endif
112 class B : A {
113 #if __cplusplus >= 201103L
114 // Check lvalue ref vs rvalue ref vs pointer.
115 a& rvalue_ref() override;
116 a&& lvalue_ref() override;
118 a&& rvalue_vs_lvalue_ref() override;
119 // since-cxx11-error@-1 {{virtual function 'rvalue_vs_lvalue_ref' has a different return type ('a &&') than the function it overrides (which has return type 'a &')}}
120 // since-cxx11-note@#cwg960-A-rvalue_vs_lvalue_ref {{overridden virtual function is here}}
122 a& lvalue_vs_rvalue_ref() override;
123 // since-cxx11-error@-1 {{virtual function 'lvalue_vs_rvalue_ref' has a different return type ('a &') than the function it overrides (which has return type 'a &&')}}
124 // since-cxx11-note@#cwg960-A-lvalue_vs_rvalue_ref {{overridden virtual function is here}}
126 a* rvalue_ref_vs_pointer() override;
127 // since-cxx11-error@-1 {{virtual function 'rvalue_ref_vs_pointer' has a different return type ('a *') than the function it overrides (which has return type 'a &')}}
128 // since-cxx11-note@#cwg960-A-rvalue_ref_vs_pointer {{overridden virtual function is here}}
130 a& pointer_vs_rvalue_ref() override;
131 // since-cxx11-error@-1 {{virtual function 'pointer_vs_rvalue_ref' has a different return type ('a &') than the function it overrides (which has return type 'a *')}}
132 // since-cxx11-note@#cwg960-A-pointer_vs_rvalue_ref {{overridden virtual function is here}}
134 a* lvalue_ref_vs_pointer() override;
135 // since-cxx11-error@-1 {{virtual function 'lvalue_ref_vs_pointer' has a different return type ('a *') than the function it overrides (which has return type 'a &&')}}
136 // since-cxx11-note@#cwg960-A-lvalue_ref_vs_pointer {{overridden virtual function is here}}
138 a&& pointer_vs_lvalue_ref() override;
139 // since-cxx11-error@-1 {{virtual function 'pointer_vs_lvalue_ref' has a different return type ('a &&') than the function it overrides (which has return type 'a *')}}
140 // since-cxx11-note@#cwg960-A-pointer_vs_lvalue_ref {{overridden virtual function is here}}
141 #endif
144 } // namespace cwg960
146 namespace cwg974 { // cwg974: yes
147 #if __cplusplus >= 201103L
148 void test() {
149 auto lam = [](int x = 42) { return x; };
151 #endif
154 namespace cwg977 { // cwg977: yes
155 enum E { e = E() }; // #cwg977-E
156 #if !defined(_WIN32) || defined(__MINGW32__)
157 // expected-error@#cwg977-E {{invalid use of incomplete type 'E'}}
158 // expected-note@#cwg977-E {{definition of 'cwg977::E' is not complete until the closing '}'}}
159 #endif
160 #if __cplusplus >= 201103L
161 enum E2 : int { e2 = E2() };
162 enum struct E3 { e = static_cast<int>(E3()) };
163 enum struct E4 : int { e = static_cast<int>(E4()) };
164 #endif
165 } // namespace cwg977
167 namespace cwg990 { // cwg990: 3.5
168 #if __cplusplus >= 201103L
169 struct A { // #cwg990-A
170 A(std::initializer_list<int>); // #cwg990-A-init-list
172 struct B {
173 A a;
175 B b1 { };
176 B b2 { 1 };
177 // since-cxx11-error@-1 {{no viable conversion from 'int' to 'A'}}
178 // since-cxx11-note@#cwg990-A {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const A &' for 1st argument}}
179 // since-cxx11-note@#cwg990-A {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'A &&' for 1st argument}}
180 // since-cxx11-note@#cwg990-A-init-list {{candidate constructor not viable: no known conversion from 'int' to 'std::initializer_list<int>' for 1st argument}}
181 B b3 { { 1 } };
183 struct C {
184 C();
185 C(int);
186 C(std::initializer_list<int>) = delete; // #cwg990-deleted
188 C c1[3] { 1 }; // ok
189 C c2[3] { 1, {2} };
190 // since-cxx11-error@-1 {{call to deleted constructor of 'C'}}
191 // since-cxx11-note@#cwg990-deleted {{'C' has been explicitly marked deleted here}}
193 struct D {
194 D();
195 D(std::initializer_list<int>);
196 D(std::initializer_list<double>);
198 D d{};
199 #endif