1 // Test this without pch.
2 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include %S/cxx-templates.h -verify %s
3 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include %S/cxx-templates.h %s -emit-llvm -o - -DNO_ERRORS | FileCheck %s
4
5 // Test with pch.
6 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -x c++-header -emit-pch -o %t %S/cxx-templates.h
7 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include-pch %t -verify %s
8 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include-pch %t %s -emit-llvm -o - -error-on-deserialized-decl doNotDeserialize -DNO_ERRORS | FileCheck %s
9
10 // Test with modules.
11 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fmodules -x c++-header -emit-pch -o %t %S/cxx-templates.h
12 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fmodules -include-pch %t -verify %s
13 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fmodules -include-pch %t %s -emit-llvm -o - -error-on-deserialized-decl doNotDeserialize -DNO_ERRORS -fmodules-ignore-macro=NO_ERRORS | FileCheck %s
14
15 // Test with pch and delayed template parsing.
16 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fdelayed-template-parsing -fexceptions -x c++-header -emit-pch -o %t %S/cxx-templates.h
17 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fdelayed-template-parsing -fexceptions -include-pch %t -verify %s
18 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fdelayed-template-parsing -fexceptions -include-pch %t %s -emit-llvm -o - -DNO_ERRORS | FileCheck %s
19
20 // Test with pch and template instantiation in the pch.
21 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fpch-instantiate-templates -x c++-header -emit-pch -o %t %S/cxx-templates.h
22 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include-pch %t -verify %s
23 // RUN: %clang_cc1 -std=c++17 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include-pch %t %s -emit-llvm -o - -DNO_ERRORS | FileCheck %s
24
25 // CHECK: define weak_odr {{.*}}void @_ZN2S4IiE1mEv
26 // CHECK: define linkonce_odr {{.*}}void @_ZN2S3IiE1mEv
27
28 struct A {
29 typedef int type;
30 static void my_f();
31 template <typename T>
my_templfA32 static T my_templf(T x) { return x; }
33 };
34
test(const int (& a6)[17])35 void test(const int (&a6)[17]) {
36 int x = templ_f<int, 5>(3);
37
38 S<char, float>::templ();
39 S<int, char>::partial();
40 S<int, float>::explicit_special();
41
42 Dep<A>::Ty ty;
43 Dep<A> a;
44 a.f();
45
46 S3<int> s3;
47 s3.m();
48
49 TS5 ts(0);
50
51 S6<const int[17]>::t2 b6 = a6;
52 }
53
54 template struct S4<int>;
55
56 S7<int[5]> s7_5;
57
58 namespace ZeroLengthExplicitTemplateArgs {
59 template void f<X>(X*);
60 }
61
62 // This used to overwrite memory and crash.
63 namespace Test1 {
64 struct StringHasher {
createHashTest1::StringHasher65 template<typename T, char Converter(T)> static inline unsigned createHash(const T*, unsigned) {
66 return 0;
67 }
68 };
69
70 struct CaseFoldingHash {
foldCaseTest1::CaseFoldingHash71 static inline char foldCase(char) {
72 return 0;
73 }
74
hashTest1::CaseFoldingHash75 static unsigned hash(const char* data, unsigned length) {
76 return StringHasher::createHash<char, foldCase>(data, length);
77 }
78 };
79 }
80
81 template< typename D >
operator =(const Foo & other)82 Foo< D >& Foo< D >::operator=( const Foo& other )
83 {
84 return *this;
85 }
86
87 namespace TestNestedExpansion {
88 struct Int {
89 Int(int);
90 friend Int operator+(Int, Int);
91 };
92 Int &g(Int, int, double);
93 Int &test = NestedExpansion<char, char, char>().f(0, 1, 2, Int(3), 4, 5.0);
94 }
95
96 namespace rdar13135282 {
test()97 void test() {
98 __mt_alloc<> mt = __mt_alloc<>();
99 }
100 }
101
CallDependentSpecializedFunc(DependentSpecializedFuncClass<int> & x)102 void CallDependentSpecializedFunc(DependentSpecializedFuncClass<int> &x) {
103 DependentSpecializedFunc(x);
104 }
105
106 namespace cyclic_module_load {
107 extern std::valarray<int> x;
108 std::valarray<int> y(x);
109 }
110
111 #ifndef NO_ERRORS
112 // expected-error@cxx-templates.h:305 {{incomplete}}
113 template int local_extern::f<int[]>(); // expected-note {{in instantiation of}}
114 #endif
115 template int local_extern::g<int[]>();
116
117 namespace MemberSpecializationLocation {
118 #ifndef NO_ERRORS
119 // expected-note@cxx-templates.h:* {{previous}}
120 template<> float A<int>::n; // expected-error {{redeclaration of 'n' with a different type}}
121 #endif
122 int k = A<int>::n;
123 }
124
125 // https://bugs.llvm.org/show_bug.cgi?id=34728
126 namespace PR34728 {
test()127 int test() {
128 // Verify with several TemplateParmDecl kinds, using PCH (incl. modules).
129 int z1 = func1(/*ignored*/2.718);
130 int z2 = func2(/*ignored*/3.142);
131 int tmp3 = 30;
132 Container<int> c = func3(tmp3);
133 int z3 = c.item;
134
135 // Return value is meaningless. Just "use" all these values to avoid
136 // warning about unused vars / values.
137 return z1 + z2 + z3;
138 }
139 } // end namespace PR34728
140
141 namespace ClassScopeExplicitSpecializations {
142 // FIXME: It's unclear these warnings (and the behavior they're describing)
143 // are desirable. These explicit instantiations could meaningfully
144 // instantiate the explicit specializations defined in the primary template.
145 template int A<3>::f<0>() const; // expected-warning {{has no effect}}
146 template int A<3>::f<1>() const;
147 template int A<4>::f<0>() const; // expected-warning {{has no effect}}
148 template int A<4>::f<1>() const;
149 // expected-note@cxx-templates.h:403 2{{here}}
150
151 static_assert(A<0>().f<0>() == 4, "");
152 static_assert(A<0>().f<1>() == 5, "");
153 static_assert(A<0>().f<2>() == 3, "");
154 static_assert(A<1>().f<0>() == 2, "");
155 static_assert(A<1>().f<1>() == 1, "");
156 static_assert(A<1>().f<2>() == 1, "");
157 static_assert(A<2>().f<0>() == 2, "");
158 static_assert(A<2>().f<1>() == 1, "");
159 static_assert(A<3>().f<0>() == 2, "");
160 static_assert(A<3>().f<1>() == 1, "");
161 static_assert(A<4>().f<0>() == 2, "");
162 static_assert(A<4>().f<1>() == 1, "");
163 }
164
165 namespace DependentMemberExpr {
166 #ifndef NO_ERRORS
167 // This used to mark 'f' invalid without producing any diagnostic. That's a
168 // little hard to detect, but we can make sure that constexpr evaluation
169 // fails when it should.
170 static_assert(A<int>().f() == 1); // expected-error {{static_assert failed}}
171 #endif
172 }
173
174 namespace DependentTemplateName {
175 struct HasMember {
176 template <class T> struct Member;
177 };
178
test()179 void test() {
180 getWithIdentifier<HasMember>();
181 }
182 }
183