[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / CXX / drs / cwg22xx.cpp
blob0614d4f34ad5e459075a133723c9742d98c6902e
1 // RUN: %clang_cc1 -std=c++98 -triple x86_64-unknown-unknown %s -verify=expected -fexceptions -fcxx-exceptions -pedantic-errors
2 // RUN: %clang_cc1 -std=c++11 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
3 // RUN: %clang_cc1 -std=c++14 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
4 // RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
5 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
6 // RUN: %clang_cc1 -std=c++23 -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
7 // RUN: %clang_cc1 -std=c++2c -triple x86_64-unknown-unknown %s -verify=expected,since-cxx11 -fexceptions -fcxx-exceptions -pedantic-errors
10 #if __cplusplus >= 201103L
11 namespace cwg2211 { // cwg2211: 8
12 void f() {
13 int a;
14 auto f = [a](int a) { (void)a; };
15 // since-cxx11-error@-1 {{a lambda parameter cannot shadow an explicitly captured entity}}
16 // since-cxx11-note@-2 {{variable 'a' is explicitly captured here}}
17 auto g = [=](int a) { (void)a; };
20 #endif
22 namespace cwg2213 { // cwg2213: yes
23 template <typename T, typename U>
24 struct A;
26 template <typename U>
27 struct A<int, U>;
28 } // namespace cwg2213
30 namespace cwg2229 { // cwg2229: 7
31 struct AnonBitfieldQualifiers {
32 const unsigned : 1;
33 // expected-error@-1 {{anonymous bit-field cannot have qualifiers}}
34 volatile unsigned : 1;
35 // expected-error@-1 {{anonymous bit-field cannot have qualifiers}}
36 const volatile unsigned : 1;
37 // expected-error@-1 {{anonymous bit-field cannot have qualifiers}}
39 unsigned : 1;
40 const unsigned i1 : 1;
41 volatile unsigned i2 : 1;
42 const volatile unsigned i3 : 1;
46 namespace cwg2233 { // cwg2233: 11
47 #if __cplusplus >= 201103L
48 template <typename... T>
49 void f(int i = 0, T... args) {}
51 template <typename... T>
52 void g(int i = 0, T... args, T... args2) {}
54 template <typename... T>
55 void h(int i = 0, T... args, int j = 1) {}
57 template <typename... T, typename... U>
58 void i(int i = 0, T... args, int j = 1, U... args2) {}
60 template <class... Ts>
61 void j(int i = 0, Ts... ts) {}
63 template <>
64 void j<int>(int i, int j) {}
66 template
67 void j(int, int, int);
69 extern template
70 void j(int, int, int, int);
72 // PR23029
73 // Ensure instantiating the templates works.
74 void use() {
75 f();
76 f(0, 1);
77 f<int>(1, 2);
78 g<int>(1, 2, 3);
79 h(0, 1);
80 i();
81 i(3);
82 i<int>(3, 2);
83 i<int>(3, 2, 1);
84 i<int, int>(1, 2, 3, 4, 5);
85 j();
86 j(1);
87 j(1, 2);
88 j<int>(1, 2);
91 namespace MultilevelSpecialization {
92 template<typename ...T> struct A {
93 template <T... V> void f(int i = 0, int (&... arr)[V]);
95 template<> template<>
96 void A<int, int>::f<1, 1>(int i, int (&arr1a)[1], int (&arr2a)[1]) {}
98 // FIXME: I believe this example is valid, at least up to the first explicit
99 // specialization, but Clang can't cope with explicit specializations that
100 // expand packs into a sequence of parameters. If we ever start accepting
101 // that, we'll need to decide whether it's OK for arr1a to be missing its
102 // default argument -- how far back do we look when determining whether a
103 // parameter was expanded from a pack?
104 // -- zygoloid 2020-06-02
105 template<typename ...T> struct B { // #cwg2233-B
106 template <T... V> void f(int i = 0, int (&... arr)[V]);
108 template<> template<int a, int b>
109 void B<int, int>::f(int i, int (&arr1)[a], int (&arr2)[b]) {}
110 // since-cxx11-error@-1 {{out-of-line definition of 'f' does not match any declaration in 'cwg2233::MultilevelSpecialization::B<int, int>'}}
111 // since-cxx11-note@#cwg2233-B {{defined here}}
112 template<> template<>
113 void B<int, int>::f<1, 1>(int i, int (&arr1a)[1], int (&arr2a)[1]) {}
116 namespace CheckAfterMerging1 {
117 template <typename... T> void f() {
118 void g(int, int = 0);
119 void g(int = 0, T...);
120 g();
122 void h() { f<int>(); }
125 namespace CheckAfterMerging2 {
126 template <typename... T> void f() {
127 void g(int = 0, T...);
128 void g(int, int = 0);
129 g();
131 void h() { f<int>(); }
133 #endif
134 } // namespace cwg2233
136 namespace cwg2267 { // cwg2267: no
137 #if __cplusplus >= 201103L
138 struct A {} a;
139 struct B { explicit B(const A&); }; // #cwg2267-struct-B
141 struct D { D(); };
142 struct C { explicit operator D(); } c;
144 B b1(a);
145 const B &b2{a}; // FIXME ill-formed
146 const B &b3(a);
147 // since-cxx11-error@-1 {{no viable conversion from 'struct A' to 'const B'}}
148 // since-cxx11-note@#cwg2267-struct-B {{candidate constructor (the implicit copy constructor) not viable: no known conversion from 'struct A' to 'const B &' for 1st argument}}
149 // since-cxx11-note@#cwg2267-struct-B {{candidate constructor (the implicit move constructor) not viable: no known conversion from 'struct A' to 'B &&' for 1st argument}}
150 // since-cxx11-note@#cwg2267-struct-B {{explicit constructor is not a candidate}}
152 D d1(c);
153 const D &d2{c}; // FIXME ill-formed
154 const D &d3(c); // FIXME ill-formed
155 #endif
158 namespace cwg2273 { // cwg2273: 3.3
159 #if __cplusplus >= 201103L
160 struct A {
161 A(int = 0) = delete; // #cwg2273-A
164 struct B : A { // #cwg2273-B
165 using A::A;
168 B b;
169 // since-cxx11-error@-1 {{call to implicitly-deleted default constructor of 'B'}}
170 // since-cxx11-note@#cwg2273-B {{default constructor of 'B' is implicitly deleted because base class 'A' has a deleted default constructor}}
171 // since-cxx11-note@#cwg2273-A {{'A' has been explicitly marked deleted here}}
172 #endif
175 namespace cwg2277 { // cwg2277: partial
176 #if __cplusplus >= 201103L
177 struct A {
178 A(int, int = 0);
179 void f(int, int = 0); // #cwg2277-A-f
181 struct B : A {
182 B(int);
183 using A::A;
185 void f(int); // #cwg2277-B-f
186 using A::f;
189 void g() {
190 B b{0};
191 b.f(0); // FIXME: this is well-formed for the same reason as initialization of 'b' above
192 // since-cxx11-error@-1 {{call to member function 'f' is ambiguous}}
193 // since-cxx11-note@#cwg2277-A-f {{candidate function}}
194 // since-cxx11-note@#cwg2277-B-f {{candidate function}}
196 #endif
199 namespace cwg2292 { // cwg2292: 9
200 #if __cplusplus >= 201103L
201 template<typename T> using id = T;
202 void test(int *p) {
203 p->template id<int>::~id<int>();
205 #endif