Debian Codegraph @

clang/test/Modules/cxx-templates.cpp

raw
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
7
8 #ifdef EARLY_IMPORT
9 #include "cxx-templates-textual.h"
10 #endif
11
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;
17
18 template<typename, char> struct Tmpl_T_C {};
19 template<typename, int, int> struct Tmpl_T_I_I {};
20
21 template<typename A, typename B, A> struct Tmpl_T_T_A {};
22 template<typename A, typename B, B> struct Tmpl_T_T_B {};
23
24 template<int> struct UseInt {};
25
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}}
33
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}}
40
41 template_param_kinds_1<0>(); // ok, from cxx-templates-a.h
42 template_param_kinds_1<int>(); // ok, from cxx-templates-b.h
43
44 template_param_kinds_2<Tmpl_T_C>(); // expected-error {{no matching function for call}}
45 // expected-note@Inputs/cxx-templates-a.h:11 {{candidate}}
46 // expected-note@Inputs/cxx-templates-b.h:11 {{candidate}}
47
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}}
51
52 template_param_kinds_3<Tmpl_T_T_A>();
53 template_param_kinds_3<Tmpl_T_T_B>();
54
55 // Trigger the instantiation of a template in 'a' that uses a type defined in
56 // 'common'. That type is not visible here.
57 PerformDelayedLookup(defined_in_common);
58
59 // Likewise, but via a default argument.
60 PerformDelayedLookupInDefaultArgument(defined_in_common);
61
62 // Trigger the instantiation of a template in 'b' that uses a type defined in
63 // 'b_impl'. That type is not visible here.
64 UseDefinedInBImpl<int>();
65
66 // Trigger the instantiation of a template in 'a' that uses a type defined in
67 // 'b_impl', via a template defined in 'b'. Since the type is visible from
68 // within 'b', the instantiation succeeds.
69 UseDefinedInBImplIndirectly(defined_in_b_impl);
70
71 // Trigger the instantiation of a template in 'a' that uses a type defined in
72 // 'b_impl'. That type is not visible here, nor in 'a'. This fails; there is
73 // no reason why DefinedInBImpl should be visible here.
74 //
75 // We turn off error recovery for modules in this test (so we don't get an
76 // implicit import of cxx_templates_b_impl), and that results in us producing
77 // a big spew of errors here.
78 //
79 // expected-error@Inputs/cxx-templates-a.h:19 {{definition of 'DefinedInBImpl' must be imported}}
80 // expected-note@Inputs/cxx-templates-b-impl.h:1 +{{definition here is not reachable}}
81 // expected-error@Inputs/cxx-templates-a.h:19 +{{}}
82 // expected-error@Inputs/cxx-templates-a.h:20 +{{}}
83 PerformDelayedLookup(defined_in_b_impl); // expected-note {{in instantiation of}}
84
85 merge_templates_a = merge_templates_b; // ok, same type
86
87 using T = decltype(enum_a_from_a);
88 using T = decltype(enum_b_from_b);
89 T e = true ? enum_a_from_a : enum_b_from_b;
90
91 UseRedeclaredEnum<int>(UseInt<1>());
92 // FIXME: Reintroduce this once we merge function template specializations.
93 //static_assert(UseRedeclaredEnumA == UseRedeclaredEnumB, "");
94 //static_assert(UseRedeclaredEnumA == UseRedeclaredEnum<int>, "");
95 //static_assert(UseRedeclaredEnumB == UseRedeclaredEnum<int>, "");
96 static_assert(enum_c_from_a == enum_c_from_b, "");
97 CommonTemplate<int> cti;
98 CommonTemplate<int>::E eee = CommonTemplate<int>::c;
99
100 TemplateInstantiationVisibility<char[1]> tiv1;
101 TemplateInstantiationVisibility<char[2]> tiv2;
102 TemplateInstantiationVisibility<char[3]> tiv3; // expected-error 5{{must be imported from module 'cxx_templates_b_impl'}}
103 // [email protected]:10 3{{explicit specialization declared here is not reachable}}
104 // [email protected]:10 2{{definition here is not reachable}}
105 TemplateInstantiationVisibility<char[4]> tiv4;
106
107 int &p = WithPartialSpecializationUse().f();
108 int &q = WithExplicitSpecializationUse().inner_template<int>();
109 int *r = PartiallyInstantiatePartialSpec<int*>::bar();
110
111 (void)&WithImplicitSpecialMembers<int>::n;
112
113 MergeClassTemplateSpecializations_string s;
114
115 extern TestInjectedClassName::A *use_a;
116 extern TestInjectedClassName::C *use_c;
117 TestInjectedClassName::UseD();
118 }
119
120 static_assert(Outer<int>::Inner<int>::f() == 1, "");
121 static_assert(Outer<int>::Inner<int>::g() == 2, "");
122
123 static_assert(MergeTemplateDefinitions<int>::f() == 1, "");
124 static_assert(MergeTemplateDefinitions<int>::g() == 2, "");
125
126 RedeclaredAsFriend<int> raf1;
127 RedeclareTemplateAsFriend<double> rtaf;
128 RedeclaredAsFriend<double> raf2;
129
130 MergeSpecializations<int*>::partially_specialized_in_a spec_in_a_1;
131 MergeSpecializations<int&>::partially_specialized_in_b spec_in_b_1;
132 MergeSpecializations<int[]>::partially_specialized_in_c spec_in_c_1;
133 MergeSpecializations<char>::explicitly_specialized_in_a spec_in_a_2;
134 MergeSpecializations<double>::explicitly_specialized_in_b spec_in_b_2;
135 MergeSpecializations<bool>::explicitly_specialized_in_c spec_in_c_2;
136
137 MergeAnonUnionMember<> maum_main;
138 typedef DontWalkPreviousDeclAfterMerging<int> dwpdam_typedef_2;
139 dwpdam_typedef::type dwpdam_typedef_use;
140 DontWalkPreviousDeclAfterMerging<int>::Inner::type dwpdam;
141
142 using AliasTemplateMergingTest = WithAliasTemplate<int>::X<char>;
143
144 int AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD, WithAnonymousDecls<char> WADC) {
145 return InstantiateWithAnonymousDeclsA(WAD) +
146 InstantiateWithAnonymousDeclsB(WAD) +
147 InstantiateWithAnonymousDeclsB2(WADC) +
148 InstantiateWithAnonymousDeclsD(WADC);
149 }
150
151 @import cxx_templates_common;
152
153 typedef SomeTemplate<int*> SomeTemplateIntPtr;
154 typedef SomeTemplate<int&> SomeTemplateIntRef;
155 SomeTemplate<char*> some_template_char_ptr;
156 SomeTemplate<char&> some_template_char_ref;
157
158 void testImplicitSpecialMembers(SomeTemplate<char[1]> &a,
159 const SomeTemplate<char[1]> &b,
160 SomeTemplate<char[2]> &c,
161 const SomeTemplate<char[2]> &d) {
162 a = b;
163 c = d;
164 }
165
166 bool testFriendInClassTemplate(Std::WithFriend<int> wfi) {
167 return wfi != wfi;
168 }
169
170 namespace hidden_specializations {
171 // [email protected]:* 1+{{here}}
172 void test() {
173 // For functions, uses that would trigger instantiations of definitions are
174 // not allowed.
175 fn<void>(); // ok
176 fn<char>(); // ok
177 fn<int>(); // expected-error 1+{{explicit specialization of 'fn<int>' must be imported}}
178 cls<void>::nested_fn(); // expected-error 1+{{explicit specialization of 'nested_fn' must be imported}}
179 cls<void>::nested_fn_t<int>(); // expected-error 1+{{explicit specialization of 'nested_fn_t' must be imported}}
180 cls<void>::nested_fn_t<char>(); // expected-error 1+{{explicit specialization of 'nested_fn_t' must be imported}}
181
182 // For classes, uses that would trigger instantiations of definitions are
183 // not allowed.
184 cls<void> *k0; // ok
185 cls<char> *k1; // ok
186 cls<int> *k2; // ok
187 cls<int*> *k3; // ok
188 cls<void>::nested_cls *nk1; // ok
189 cls<void>::nested_cls_t<int> *nk2; // ok
190 cls<void>::nested_cls_t<char> *nk3; // ok
191 cls<int> uk1; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
192 cls<int*> uk3; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}} expected-error 1+{{definition of}}
193 cls<char*> uk4; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}} expected-error 1+{{definition of}}
194 cls<void>::nested_cls unk1; // expected-error 1+{{explicit specialization of 'nested_cls' must be imported}} expected-error 1+{{definition of}}
195 cls<void>::nested_cls_t<int> unk2; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}} expected-error 1+{{definition of}}
196 // [email protected]:29 {{explicit specialization of 'nested_cls_t' must be imported}}
197 // expected-note@-2 {{in evaluation of exception specification}}
198 cls<void>::nested_cls_t<char> unk3; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}}
199
200 // For enums, uses that would trigger instantiations of definitions are not
201 // allowed.
202 cls<void>::nested_enum e; // ok
203 (void)cls<void>::nested_enum::e; // expected-error 1+{{definition of 'nested_enum' must be imported}} expected-error 1+{{declaration of 'e'}}
204
205 // For variable template specializations, no uses are allowed because
206 // specializations can change the type.
207 (void)sizeof(var<void>); // ok
208 (void)sizeof(var<char>); // ok
209 (void)sizeof(var<int>); // expected-error 1+{{explicit specialization of 'var<int>' must be imported}}
210 (void)sizeof(var<int*>); // expected-error 1+{{partial specialization of 'var<T *>' must be imported}}
211 (void)sizeof(var<char*>); // expected-error 1+{{partial specialization of 'var<T *>' must be imported}}
212 (void)sizeof(cls<void>::nested_var); // ok
213 (void)cls<void>::nested_var; // expected-error 1+{{explicit specialization of 'nested_var' must be imported}}
214 (void)sizeof(cls<void>::nested_var_t<int>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
215 (void)sizeof(cls<void>::nested_var_t<char>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
216 }
217
218 void cls<int>::nested_fn() {} // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
219 struct cls<int>::nested_cls {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
220 int cls<int>::nested_var; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
221 enum cls<int>::nested_enum : int {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
222
223 template<typename T> void cls<T*>::nested_fn() {} // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
224 template<typename T> struct cls<T*>::nested_cls {}; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
225 template<typename T> int cls<T*>::nested_var; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
226 template<typename T> enum cls<T*>::nested_enum : int {}; // expected-error 1+{{partial specialization of 'cls<T *>' must be imported}}
227 }
228
229 namespace Std {
230 void g(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
231 // [email protected]:21 {{previous}}
232 }
233
234 // CHECK-GLOBAL: DeclarationName 'f'
235 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
236 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
237 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
238 // CHECK-GLOBAL-NEXT: `-FunctionTemplate {{.*}} 'f'
239
240 // CHECK-NAMESPACE-N: DeclarationName 'f'
241 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
242 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
243 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
244 // CHECK-NAMESPACE-N-NEXT: `-FunctionTemplate {{.*}} 'f'
245
246 // CHECK-DUMP: ClassTemplateDecl {{.*}} <{{.*[/\\]}}cxx-templates-common.h:1:1, {{.*}}> col:{{.*}} in cxx_templates_common SomeTemplate
247 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
248 // CHECK-DUMP-NEXT: TemplateArgument type 'char[{{1|2}}]'
249 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
250 // CHECK-DUMP-NEXT: DefinitionData
251 // CHECK-DUMP-NEXT: DefaultConstructor
252 // CHECK-DUMP-NEXT: CopyConstructor
253 // CHECK-DUMP-NEXT: MoveConstructor
254 // CHECK-DUMP-NEXT: CopyAssignment
255 // CHECK-DUMP-NEXT: MoveAssignment
256 // CHECK-DUMP-NEXT: Destructor
257 // CHECK-DUMP-NEXT: TemplateArgument type 'char[{{1|2}}]'
258 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
259 // CHECK-DUMP-NEXT: TemplateArgument type 'char[{{1|2}}]'
260 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
261 // CHECK-DUMP-NEXT: DefinitionData
262 // CHECK-DUMP-NEXT: DefaultConstructor
263 // CHECK-DUMP-NEXT: CopyConstructor
264 // CHECK-DUMP-NEXT: MoveConstructor
265 // CHECK-DUMP-NEXT: CopyAssignment
266 // CHECK-DUMP-NEXT: MoveAssignment
267 // CHECK-DUMP-NEXT: Destructor
268 // CHECK-DUMP-NEXT: TemplateArgument type 'char[{{1|2}}]'