Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / PCH / cxx-templates.cpp
blob11ad401de23a8cf7d14dd1a0f4a1cdd35e186581
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
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
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
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
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
25 // CHECK: define weak_odr {{.*}}void @_ZN2S4IiE1mEv
26 // CHECK: define linkonce_odr {{.*}}void @_ZN2S3IiE1mEv
28 struct A {
29 typedef int type;
30 static void my_f();
31 template <typename T>
32 static T my_templf(T x) { return x; }
35 void test(const int (&a6)[17]) {
36 int x = templ_f<int, 5>(3);
38 S<char, float>::templ();
39 S<int, char>::partial();
40 S<int, float>::explicit_special();
42 Dep<A>::Ty ty;
43 Dep<A> a;
44 a.f();
46 S3<int> s3;
47 s3.m();
49 TS5 ts(0);
51 S6<const int[17]>::t2 b6 = a6;
54 template struct S4<int>;
56 S7<int[5]> s7_5;
58 namespace ZeroLengthExplicitTemplateArgs {
59 template void f<X>(X*);
62 // This used to overwrite memory and crash.
63 namespace Test1 {
64 struct StringHasher {
65 template<typename T, char Converter(T)> static inline unsigned createHash(const T*, unsigned) {
66 return 0;
70 struct CaseFoldingHash {
71 static inline char foldCase(char) {
72 return 0;
75 static unsigned hash(const char* data, unsigned length) {
76 return StringHasher::createHash<char, foldCase>(data, length);
81 template< typename D >
82 Foo< D >& Foo< D >::operator=( const Foo& other )
84 return *this;
87 namespace TestNestedExpansion {
88 struct Int {
89 Int(int);
90 friend Int operator+(Int, Int);
92 Int &g(Int, int, double);
93 Int &test = NestedExpansion<char, char, char>().f(0, 1, 2, Int(3), 4, 5.0);
96 namespace rdar13135282 {
97 void test() {
98 __mt_alloc<> mt = __mt_alloc<>();
102 void CallDependentSpecializedFunc(DependentSpecializedFuncClass<int> &x) {
103 DependentSpecializedFunc(x);
106 namespace cyclic_module_load {
107 extern std::valarray<int> x;
108 std::valarray<int> y(x);
111 #ifndef NO_ERRORS
112 // expected-error@cxx-templates.h:304 {{incomplete}}
113 template int local_extern::f<int[]>(); // expected-note {{in instantiation of}}
114 #endif
115 template int local_extern::g<int[]>();
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;
125 // https://bugs.llvm.org/show_bug.cgi?id=34728
126 namespace PR34728 {
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;
135 // Return value is meaningless. Just "use" all these values to avoid
136 // warning about unused vars / values.
137 return z1 + z2 + z3;
139 } // end namespace PR34728
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:402 2{{here}}
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, "");
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 assertion failed}} \
171 // expected-note {{evaluates to '0 == 1'}}
172 #endif
175 namespace DependentTemplateName {
176 struct HasMember {
177 template <class T> struct Member;
180 void test() {
181 getWithIdentifier<HasMember>();
185 namespace ClassTemplateCycle {
186 extern T t;
187 int k = M;