[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / Modules / cxx-templates.cpp
blobb7d5741e69af61b384c1b03e63716a280f82f443
1 // RUN: rm -rf %t
2 // RUN: not %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++14 -ast-dump-lookups 2>/dev/null | FileCheck %s --check-prefix=CHECK-GLOBAL
3 // RUN: not %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++14 -ast-dump-lookups -ast-dump-filter N 2>/dev/null | FileCheck %s --check-prefix=CHECK-NAMESPACE-N
4 // RUN: not %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -std=c++14 -ast-dump -ast-dump-filter SomeTemplate 2>/dev/null | FileCheck %s --check-prefix=CHECK-DUMP
5 // RUN: %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++14
6 // RUN: %clang_cc1 -x objective-c++ -fmodules -fimplicit-module-maps -fno-modules-error-recovery -fmodules-cache-path=%t -I %S/Inputs %s -verify -std=c++14 -DEARLY_IMPORT
8 #ifdef EARLY_IMPORT
9 #include "cxx-templates-textual.h"
10 #endif
12 @import cxx_templates_a;
13 @import cxx_templates_b;
14 @import cxx_templates_c;
15 @import cxx_templates_d;
16 @import cxx_templates_common;
18 template<typename, char> struct Tmpl_T_C {};
19 template<typename, int, int> struct Tmpl_T_I_I {};
21 template<typename A, typename B, A> struct Tmpl_T_T_A {};
22 template<typename A, typename B, B> struct Tmpl_T_T_B {};
24 template<int> struct UseInt {};
26 void g() {
27 f(0);
28 f<double>(1.0);
29 f<int>();
30 f(); // expected-error {{no matching function}}
31 // expected-note@Inputs/cxx-templates-a.h:3 {{couldn't infer template argument}}
32 // expected-note@Inputs/cxx-templates-a.h:4 {{requires 1 argument}}
34 N::f(0);
35 N::f<double>(1.0);
36 N::f<int>();
37 N::f(); // expected-error {{no matching function}}
38 // expected-note@Inputs/cxx-templates-b.h:6 {{couldn't infer template argument}}
39 // expected-note@Inputs/cxx-templates-b.h:7 {{requires single argument}}
41 template_param_kinds_1<0>(); // ok, from cxx-templates-a.h
42 template_param_kinds_1<int>(); // ok, from cxx-templates-b.h
44 template_param_kinds_2<Tmpl_T_C>(); // expected-error {{no matching function}}
45 // expected-note@Inputs/cxx-templates-a.h:11 {{invalid explicitly-specified argument}}
46 // expected-note@Inputs/cxx-templates-b.h:11 {{invalid explicitly-specified argument}}
48 template_param_kinds_2<Tmpl_T_I_I>(); // expected-error {{ambiguous}}
49 // expected-note@Inputs/cxx-templates-a.h:11 {{candidate}}
50 // expected-note@Inputs/cxx-templates-b.h:11 {{candidate}}
52 // FIXME: This should be valid, but we incorrectly match the template template
53 // argument against both template template parameters.
54 template_param_kinds_3<Tmpl_T_T_A>(); // expected-error {{ambiguous}}
55 // expected-note@Inputs/cxx-templates-a.h:12 {{candidate}}
56 // expected-note@Inputs/cxx-templates-b.h:12 {{candidate}}
57 template_param_kinds_3<Tmpl_T_T_B>(); // expected-error {{ambiguous}}
58 // expected-note@Inputs/cxx-templates-a.h:12 {{candidate}}
59 // expected-note@Inputs/cxx-templates-b.h:12 {{candidate}}
61 // Trigger the instantiation of a template in 'a' that uses a type defined in
62 // 'common'. That type is not visible here.
63 PerformDelayedLookup(defined_in_common);
65 // Likewise, but via a default argument.
66 PerformDelayedLookupInDefaultArgument(defined_in_common);
68 // Trigger the instantiation of a template in 'b' that uses a type defined in
69 // 'b_impl'. That type is not visible here.
70 UseDefinedInBImpl<int>();
72 // Trigger the instantiation of a template in 'a' that uses a type defined in
73 // 'b_impl', via a template defined in 'b'. Since the type is visible from
74 // within 'b', the instantiation succeeds.
75 UseDefinedInBImplIndirectly(defined_in_b_impl);
77 // Trigger the instantiation of a template in 'a' that uses a type defined in
78 // 'b_impl'. That type is not visible here, nor in 'a'. This fails; there is
79 // no reason why DefinedInBImpl should be visible here.
81 // We turn off error recovery for modules in this test (so we don't get an
82 // implicit import of cxx_templates_b_impl), and that results in us producing
83 // a big spew of errors here.
85 // expected-error@Inputs/cxx-templates-a.h:19 {{definition of 'DefinedInBImpl' must be imported}}
86 // expected-note@Inputs/cxx-templates-b-impl.h:1 +{{definition here is not reachable}}
87 // expected-error@Inputs/cxx-templates-a.h:19 +{{}}
88 // expected-error@Inputs/cxx-templates-a.h:20 +{{}}
89 PerformDelayedLookup(defined_in_b_impl); // expected-note {{in instantiation of}}
91 merge_templates_a = merge_templates_b; // ok, same type
93 using T = decltype(enum_a_from_a);
94 using T = decltype(enum_b_from_b);
95 T e = true ? enum_a_from_a : enum_b_from_b;
97 UseRedeclaredEnum<int>(UseInt<1>());
98 // FIXME: Reintroduce this once we merge function template specializations.
99 //static_assert(UseRedeclaredEnumA == UseRedeclaredEnumB, "");
100 //static_assert(UseRedeclaredEnumA == UseRedeclaredEnum<int>, "");
101 //static_assert(UseRedeclaredEnumB == UseRedeclaredEnum<int>, "");
102 static_assert(enum_c_from_a == enum_c_from_b, "");
103 CommonTemplate<int> cti;
104 CommonTemplate<int>::E eee = CommonTemplate<int>::c;
106 TemplateInstantiationVisibility<char[1]> tiv1;
107 TemplateInstantiationVisibility<char[2]> tiv2;
108 TemplateInstantiationVisibility<char[3]> tiv3; // expected-error 5{{must be imported from module 'cxx_templates_b_impl'}}
109 // expected-note@cxx-templates-b-impl.h:10 3{{explicit specialization declared here is not reachable}}
110 // expected-note@cxx-templates-b-impl.h:10 2{{definition here is not reachable}}
111 TemplateInstantiationVisibility<char[4]> tiv4;
113 int &p = WithPartialSpecializationUse().f();
114 int &q = WithExplicitSpecializationUse().inner_template<int>();
115 int *r = PartiallyInstantiatePartialSpec<int*>::bar();
117 (void)&WithImplicitSpecialMembers<int>::n;
119 MergeClassTemplateSpecializations_string s;
121 extern TestInjectedClassName::A *use_a;
122 extern TestInjectedClassName::C *use_c;
123 TestInjectedClassName::UseD();
126 static_assert(Outer<int>::Inner<int>::f() == 1, "");
127 static_assert(Outer<int>::Inner<int>::g() == 2, "");
129 static_assert(MergeTemplateDefinitions<int>::f() == 1, "");
130 static_assert(MergeTemplateDefinitions<int>::g() == 2, "");
132 RedeclaredAsFriend<int> raf1;
133 RedeclareTemplateAsFriend<double> rtaf;
134 RedeclaredAsFriend<double> raf2;
136 MergeSpecializations<int*>::partially_specialized_in_a spec_in_a_1;
137 MergeSpecializations<int&>::partially_specialized_in_b spec_in_b_1;
138 MergeSpecializations<int[]>::partially_specialized_in_c spec_in_c_1;
139 MergeSpecializations<char>::explicitly_specialized_in_a spec_in_a_2;
140 MergeSpecializations<double>::explicitly_specialized_in_b spec_in_b_2;
141 MergeSpecializations<bool>::explicitly_specialized_in_c spec_in_c_2;
143 MergeAnonUnionMember<> maum_main;
144 typedef DontWalkPreviousDeclAfterMerging<int> dwpdam_typedef_2;
145 dwpdam_typedef::type dwpdam_typedef_use;
146 DontWalkPreviousDeclAfterMerging<int>::Inner::type dwpdam;
148 using AliasTemplateMergingTest = WithAliasTemplate<int>::X<char>;
150 int AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD, WithAnonymousDecls<char> WADC) {
151 return InstantiateWithAnonymousDeclsA(WAD) +
152 InstantiateWithAnonymousDeclsB(WAD) +
153 InstantiateWithAnonymousDeclsB2(WADC) +
154 InstantiateWithAnonymousDeclsD(WADC);
157 @import cxx_templates_common;
159 typedef SomeTemplate<int*> SomeTemplateIntPtr;
160 typedef SomeTemplate<int&> SomeTemplateIntRef;
161 SomeTemplate<char*> some_template_char_ptr;
162 SomeTemplate<char&> some_template_char_ref;
164 void testImplicitSpecialMembers(SomeTemplate<char[1]> &a,
165 const SomeTemplate<char[1]> &b,
166 SomeTemplate<char[2]> &c,
167 const SomeTemplate<char[2]> &d) {
168 a = b;
169 c = d;
172 bool testFriendInClassTemplate(Std::WithFriend<int> wfi) {
173 return wfi != wfi;
176 namespace hidden_specializations {
177 // expected-note@cxx-templates-unimported.h:* 1+{{here}}
178 void test() {
179 // For functions, uses that would trigger instantiations of definitions are
180 // not allowed.
181 fn<void>(); // ok
182 fn<char>(); // ok
183 fn<int>(); // expected-error 1+{{explicit specialization of 'fn<int>' must be imported}}
184 cls<void>::nested_fn(); // expected-error 1+{{explicit specialization of 'nested_fn' must be imported}}
185 cls<void>::nested_fn_t<int>(); // expected-error 1+{{explicit specialization of 'nested_fn_t' must be imported}}
186 cls<void>::nested_fn_t<char>(); // expected-error 1+{{explicit specialization of 'nested_fn_t' must be imported}}
188 // For classes, uses that would trigger instantiations of definitions are
189 // not allowed.
190 cls<void> *k0; // ok
191 cls<char> *k1; // ok
192 cls<int> *k2; // ok
193 cls<int*> *k3; // ok
194 cls<void>::nested_cls *nk1; // ok
195 cls<void>::nested_cls_t<int> *nk2; // ok
196 cls<void>::nested_cls_t<char> *nk3; // ok
197 cls<int> uk1; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
198 cls<int*> uk3; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}} expected-error 1+{{definition of}}
199 cls<char*> uk4; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}} expected-error 1+{{definition of}}
200 cls<void>::nested_cls unk1; // expected-error 1+{{explicit specialization of 'nested_cls' must be imported}} expected-error 1+{{definition of}}
201 cls<void>::nested_cls_t<int> unk2; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}} expected-error 1+{{definition of}}
202 // expected-error@cxx-templates-unimported.h:29 {{explicit specialization of 'nested_cls_t' must be imported}}
203 // expected-note@-2 {{in evaluation of exception specification}}
204 cls<void>::nested_cls_t<char> unk3; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}}
206 // For enums, uses that would trigger instantiations of definitions are not
207 // allowed.
208 cls<void>::nested_enum e; // ok
209 (void)cls<void>::nested_enum::e; // expected-error 1+{{definition of 'nested_enum' must be imported}} expected-error 1+{{declaration of 'e'}}
211 // For variable template specializations, no uses are allowed because
212 // specializations can change the type.
213 (void)sizeof(var<void>); // ok
214 (void)sizeof(var<char>); // ok
215 (void)sizeof(var<int>); // expected-error 1+{{explicit specialization of 'var<int>' must be imported}}
216 (void)sizeof(var<int*>); // expected-error 1+{{partial specialization of 'var<T *>' must be imported}}
217 (void)sizeof(var<char*>); // expected-error 1+{{partial specialization of 'var<T *>' must be imported}}
218 (void)sizeof(cls<void>::nested_var); // ok
219 (void)cls<void>::nested_var; // expected-error 1+{{explicit specialization of 'nested_var' must be imported}}
220 (void)sizeof(cls<void>::nested_var_t<int>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
221 (void)sizeof(cls<void>::nested_var_t<char>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
224 void cls<int>::nested_fn() {} // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
225 struct cls<int>::nested_cls {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
226 int cls<int>::nested_var; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
227 enum cls<int>::nested_enum : int {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
229 template<typename T> void cls<T*>::nested_fn() {} // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
230 template<typename T> struct cls<T*>::nested_cls {}; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
231 template<typename T> int cls<T*>::nested_var; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
232 template<typename T> enum cls<T*>::nested_enum : int {}; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
235 namespace Std {
236 void g(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
237 // expected-note@cxx-templates-common.h:21 {{previous}}
240 // CHECK-GLOBAL: DeclarationName 'f'
241 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
242 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
243 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
244 // CHECK-GLOBAL-NEXT: `-FunctionTemplate {{.*}} 'f'
246 // CHECK-NAMESPACE-N: DeclarationName 'f'
247 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
248 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
249 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
250 // CHECK-NAMESPACE-N-NEXT: `-FunctionTemplate {{.*}} 'f'
252 // CHECK-DUMP: ClassTemplateDecl {{.*}} <{{.*[/\\]}}cxx-templates-common.h:1:1, {{.*}}> col:{{.*}} in cxx_templates_common SomeTemplate
253 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
254 // CHECK-DUMP-NEXT: TemplateArgument type 'char[2]'
255 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
256 // CHECK-DUMP-NEXT: DefinitionData
257 // CHECK-DUMP-NEXT: DefaultConstructor
258 // CHECK-DUMP-NEXT: CopyConstructor
259 // CHECK-DUMP-NEXT: MoveConstructor
260 // CHECK-DUMP-NEXT: CopyAssignment
261 // CHECK-DUMP-NEXT: MoveAssignment
262 // CHECK-DUMP-NEXT: Destructor
263 // CHECK-DUMP-NEXT: TemplateArgument type 'char[2]'
264 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
265 // CHECK-DUMP-NEXT: TemplateArgument type 'char[1]'
266 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
267 // CHECK-DUMP-NEXT: DefinitionData
268 // CHECK-DUMP-NEXT: DefaultConstructor
269 // CHECK-DUMP-NEXT: CopyConstructor
270 // CHECK-DUMP-NEXT: MoveConstructor
271 // CHECK-DUMP-NEXT: CopyAssignment
272 // CHECK-DUMP-NEXT: MoveAssignment
273 // CHECK-DUMP-NEXT: Destructor
274 // CHECK-DUMP-NEXT: TemplateArgument type 'char[1]'