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
g()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}}
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}}
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 // 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}}
60
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);
64
65 // Likewise, but via a default argument.
66 PerformDelayedLookupInDefaultArgument(defined_in_common);
67
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>();
71
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);
76
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.
80 //
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.
84 //
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 is here}}
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}}
90
91 merge_templates_a = merge_templates_b; // ok, same type
92
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;
96
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;
105
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}}
110 // expected-note@cxx-templates-b-impl.h:10 2{{previous definition is here}}
111 TemplateInstantiationVisibility<char[4]> tiv4;
112
113 int &p = WithPartialSpecializationUse().f();
114 int &q = WithExplicitSpecializationUse().inner_template<int>();
115 int *r = PartiallyInstantiatePartialSpec<int*>::bar();
116
117 (void)&WithImplicitSpecialMembers<int>::n;
118
119 MergeClassTemplateSpecializations_string s;
120
121 extern TestInjectedClassName::A *use_a;
122 extern TestInjectedClassName::C *use_c;
123 TestInjectedClassName::UseD();
124 }
125
126 static_assert(Outer<int>::Inner<int>::f() == 1, "");
127 static_assert(Outer<int>::Inner<int>::g() == 2, "");
128
129 static_assert(MergeTemplateDefinitions<int>::f() == 1, "");
130 static_assert(MergeTemplateDefinitions<int>::g() == 2, "");
131
132 RedeclaredAsFriend<int> raf1;
133 RedeclareTemplateAsFriend<double> rtaf;
134 RedeclaredAsFriend<double> raf2;
135
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;
142
143 MergeAnonUnionMember<> maum_main;
144 typedef DontWalkPreviousDeclAfterMerging<int> dwpdam_typedef_2;
145 dwpdam_typedef::type dwpdam_typedef_use;
146 DontWalkPreviousDeclAfterMerging<int>::Inner::type dwpdam;
147
148 using AliasTemplateMergingTest = WithAliasTemplate<int>::X<char>;
149
AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD,WithAnonymousDecls<char> WADC)150 int AnonymousDeclsMergingTest(WithAnonymousDecls<int> WAD, WithAnonymousDecls<char> WADC) {
151 return InstantiateWithAnonymousDeclsA(WAD) +
152 InstantiateWithAnonymousDeclsB(WAD) +
153 InstantiateWithAnonymousDeclsB2(WADC) +
154 InstantiateWithAnonymousDeclsD(WADC);
155 }
156
157 @import cxx_templates_common;
158
159 typedef SomeTemplate<int*> SomeTemplateIntPtr;
160 typedef SomeTemplate<int&> SomeTemplateIntRef;
161 SomeTemplate<char*> some_template_char_ptr;
162 SomeTemplate<char&> some_template_char_ref;
163
testImplicitSpecialMembers(SomeTemplate<char[1]> & a,const SomeTemplate<char[1]> & b,SomeTemplate<char[2]> & c,const SomeTemplate<char[2]> & d)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;
170 }
171
testFriendInClassTemplate(Std::WithFriend<int> wfi)172 bool testFriendInClassTemplate(Std::WithFriend<int> wfi) {
173 return wfi != wfi;
174 }
175
176 namespace hidden_specializations {
177 // expected-note@cxx-templates-unimported.h:* 1+{{here}}
test()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}}
187
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<type-parameter-0-0 *>' must be imported}} expected-error 1+{{definition of}}
199 cls<char*> uk4; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' 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 cls<void>::nested_cls_t<char> unk3; // expected-error 1+{{explicit specialization of 'nested_cls_t' must be imported}}
203
204 // For enums, uses that would trigger instantiations of definitions are not
205 // allowed.
206 cls<void>::nested_enum e; // ok
207 (void)cls<void>::nested_enum::e; // expected-error 1+{{definition of 'nested_enum' must be imported}} expected-error 1+{{declaration of 'e'}}
208
209 // For variable template specializations, no uses are allowed because
210 // specializations can change the type.
211 (void)sizeof(var<void>); // ok
212 (void)sizeof(var<char>); // ok
213 (void)sizeof(var<int>); // expected-error 1+{{explicit specialization of 'var<int>' must be imported}}
214 (void)sizeof(var<int*>); // expected-error 1+{{partial specialization of 'var<type-parameter-0-0 *>' must be imported}}
215 (void)sizeof(var<char*>); // expected-error 1+{{partial specialization of 'var<type-parameter-0-0 *>' must be imported}}
216 (void)sizeof(cls<void>::nested_var); // ok
217 (void)cls<void>::nested_var; // expected-error 1+{{explicit specialization of 'nested_var' must be imported}}
218 (void)sizeof(cls<void>::nested_var_t<int>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
219 (void)sizeof(cls<void>::nested_var_t<char>); // expected-error 1+{{explicit specialization of 'nested_var_t' must be imported}}
220 }
221
nested_fn()222 void cls<int>::nested_fn() {} // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
223 struct cls<int>::nested_cls {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
224 int cls<int>::nested_var; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
225 enum cls<int>::nested_enum : int {}; // expected-error 1+{{explicit specialization of 'cls<int>' must be imported}} expected-error 1+{{definition of}}
226
nested_fn()227 template<typename T> void cls<T*>::nested_fn() {} // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
228 template<typename T> struct cls<T*>::nested_cls {}; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
229 template<typename T> int cls<T*>::nested_var; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
230 template<typename T> enum cls<T*>::nested_enum : int {}; // expected-error 1+{{partial specialization of 'cls<type-parameter-0-0 *>' must be imported}}
231 }
232
233 namespace Std {
234 void g(); // expected-error {{functions that differ only in their return type cannot be overloaded}}
235 // expected-note@cxx-templates-common.h:21 {{previous}}
236 }
237
238 // CHECK-GLOBAL: DeclarationName 'f'
239 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
240 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
241 // CHECK-GLOBAL-NEXT: |-FunctionTemplate {{.*}} 'f'
242 // CHECK-GLOBAL-NEXT: `-FunctionTemplate {{.*}} 'f'
243
244 // CHECK-NAMESPACE-N: DeclarationName 'f'
245 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
246 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
247 // CHECK-NAMESPACE-N-NEXT: |-FunctionTemplate {{.*}} 'f'
248 // CHECK-NAMESPACE-N-NEXT: `-FunctionTemplate {{.*}} 'f'
249
250 // CHECK-DUMP: ClassTemplateDecl {{.*}} <{{.*[/\\]}}cxx-templates-common.h:1:1, {{.*}}> col:{{.*}} in cxx_templates_common SomeTemplate
251 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
252 // CHECK-DUMP-NEXT: TemplateArgument type 'char [1]'
253 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
254 // CHECK-DUMP-NEXT: TemplateArgument type 'char [1]'
255 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} prev {{.*}} SomeTemplate
256 // CHECK-DUMP-NEXT: TemplateArgument type 'char [2]'
257 // CHECK-DUMP: ClassTemplateSpecializationDecl {{.*}} SomeTemplate definition
258 // CHECK-DUMP-NEXT: TemplateArgument type 'char [2]'
259