xref: /aosp_15_r20/external/clang/test/PCH/cxx-templates.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // Test this without pch.
2*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include %S/cxx-templates.h -verify %s -ast-dump -o -
3*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include %S/cxx-templates.h %s -emit-llvm -o - -DNO_ERRORS | FileCheck %s
4*67e74705SXin Li 
5*67e74705SXin Li // Test with pch.
6*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -x c++-header -emit-pch -o %t %S/cxx-templates.h
7*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include-pch %t -verify %s -ast-dump  -o -
8*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -include-pch %t %s -emit-llvm -o - -error-on-deserialized-decl doNotDeserialize -DNO_ERRORS | FileCheck %s
9*67e74705SXin Li 
10*67e74705SXin Li // Test with modules.
11*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fmodules -x c++-header -emit-pch -o %t %S/cxx-templates.h
12*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fexceptions -fmodules -include-pch %t -verify %s -ast-dump  -o -
13*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -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*67e74705SXin Li 
15*67e74705SXin Li // Test with pch and delayed template parsing.
16*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fdelayed-template-parsing -fexceptions -x c++-header -emit-pch -o %t %S/cxx-templates.h
17*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fdelayed-template-parsing -fexceptions -include-pch %t -verify %s -ast-dump  -o -
18*67e74705SXin Li // RUN: %clang_cc1 -std=c++11 -triple %itanium_abi_triple -fcxx-exceptions -fdelayed-template-parsing -fexceptions -include-pch %t %s -emit-llvm -o - -DNO_ERRORS | FileCheck %s
19*67e74705SXin Li 
20*67e74705SXin Li // CHECK: define weak_odr {{.*}}void @_ZN2S4IiE1mEv
21*67e74705SXin Li // CHECK: define linkonce_odr {{.*}}void @_ZN2S3IiE1mEv
22*67e74705SXin Li 
23*67e74705SXin Li struct A {
24*67e74705SXin Li   typedef int type;
25*67e74705SXin Li   static void my_f();
26*67e74705SXin Li   template <typename T>
my_templfA27*67e74705SXin Li   static T my_templf(T x) { return x; }
28*67e74705SXin Li };
29*67e74705SXin Li 
test(const int (& a6)[17])30*67e74705SXin Li void test(const int (&a6)[17]) {
31*67e74705SXin Li   int x = templ_f<int, 5>(3);
32*67e74705SXin Li 
33*67e74705SXin Li   S<char, float>::templ();
34*67e74705SXin Li   S<int, char>::partial();
35*67e74705SXin Li   S<int, float>::explicit_special();
36*67e74705SXin Li 
37*67e74705SXin Li   Dep<A>::Ty ty;
38*67e74705SXin Li   Dep<A> a;
39*67e74705SXin Li   a.f();
40*67e74705SXin Li 
41*67e74705SXin Li   S3<int> s3;
42*67e74705SXin Li   s3.m();
43*67e74705SXin Li 
44*67e74705SXin Li   TS5 ts(0);
45*67e74705SXin Li 
46*67e74705SXin Li   S6<const int[17]>::t2 b6 = a6;
47*67e74705SXin Li }
48*67e74705SXin Li 
49*67e74705SXin Li template struct S4<int>;
50*67e74705SXin Li 
51*67e74705SXin Li S7<int[5]> s7_5;
52*67e74705SXin Li 
53*67e74705SXin Li namespace ZeroLengthExplicitTemplateArgs {
54*67e74705SXin Li   template void f<X>(X*);
55*67e74705SXin Li }
56*67e74705SXin Li 
57*67e74705SXin Li // This used to overwrite memory and crash.
58*67e74705SXin Li namespace Test1 {
59*67e74705SXin Li   struct StringHasher {
createHashTest1::StringHasher60*67e74705SXin Li     template<typename T, char Converter(T)> static inline unsigned createHash(const T*, unsigned) {
61*67e74705SXin Li       return 0;
62*67e74705SXin Li     }
63*67e74705SXin Li   };
64*67e74705SXin Li 
65*67e74705SXin Li   struct CaseFoldingHash {
foldCaseTest1::CaseFoldingHash66*67e74705SXin Li     static inline char foldCase(char) {
67*67e74705SXin Li       return 0;
68*67e74705SXin Li     }
69*67e74705SXin Li 
hashTest1::CaseFoldingHash70*67e74705SXin Li     static unsigned hash(const char* data, unsigned length) {
71*67e74705SXin Li       return StringHasher::createHash<char, foldCase>(data, length);
72*67e74705SXin Li     }
73*67e74705SXin Li   };
74*67e74705SXin Li }
75*67e74705SXin Li 
76*67e74705SXin Li template< typename D >
operator =(const Foo & other)77*67e74705SXin Li Foo< D >& Foo< D >::operator=( const Foo& other )
78*67e74705SXin Li {
79*67e74705SXin Li    return *this;
80*67e74705SXin Li }
81*67e74705SXin Li 
82*67e74705SXin Li namespace TestNestedExpansion {
83*67e74705SXin Li   struct Int {
84*67e74705SXin Li     Int(int);
85*67e74705SXin Li     friend Int operator+(Int, Int);
86*67e74705SXin Li   };
87*67e74705SXin Li   Int &g(Int, int, double);
88*67e74705SXin Li   Int &test = NestedExpansion<char, char, char>().f(0, 1, 2, Int(3), 4, 5.0);
89*67e74705SXin Li }
90*67e74705SXin Li 
91*67e74705SXin Li namespace rdar13135282 {
test()92*67e74705SXin Li   void test() {
93*67e74705SXin Li     __mt_alloc<> mt = __mt_alloc<>();
94*67e74705SXin Li   }
95*67e74705SXin Li }
96*67e74705SXin Li 
CallDependentSpecializedFunc(DependentSpecializedFuncClass<int> & x)97*67e74705SXin Li void CallDependentSpecializedFunc(DependentSpecializedFuncClass<int> &x) {
98*67e74705SXin Li   DependentSpecializedFunc(x);
99*67e74705SXin Li }
100*67e74705SXin Li 
101*67e74705SXin Li namespace cyclic_module_load {
102*67e74705SXin Li   extern std::valarray<int> x;
103*67e74705SXin Li   std::valarray<int> y(x);
104*67e74705SXin Li }
105*67e74705SXin Li 
106*67e74705SXin Li #ifndef NO_ERRORS
107*67e74705SXin Li // [email protected]:305 {{incomplete}}
108*67e74705SXin Li template int local_extern::f<int[]>(); // expected-note {{in instantiation of}}
109*67e74705SXin Li #endif
110*67e74705SXin Li template int local_extern::g<int[]>();
111