xref: /minix3/external/bsd/llvm/dist/clang/test/PCH/cxx1y-variable-templates.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // No PCH:
2*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -pedantic -std=c++1y -include %s -include %s -verify %s -DNONPCH
3*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -pedantic -std=c++1y -include %s -include %s -verify %s -DNONPCH -DERROR
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // With PCH:
6*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -pedantic -std=c++1y -emit-pch %s -o %t.a -DHEADER1
7*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -pedantic -std=c++1y -include-pch %t.a -emit-pch %s -o %t.b -DHEADER2
8*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -pedantic -std=c++1y -include-pch %t.b -verify %s -DHEADERUSE
9*f4a2713aSLionel Sambuc 
10*f4a2713aSLionel Sambuc #ifndef ERROR
11*f4a2713aSLionel Sambuc // expected-no-diagnostics
12*f4a2713aSLionel Sambuc #endif
13*f4a2713aSLionel Sambuc 
14*f4a2713aSLionel Sambuc #ifdef NONPCH
15*f4a2713aSLionel Sambuc #if !defined(HEADER1)
16*f4a2713aSLionel Sambuc #define HEADER1
17*f4a2713aSLionel Sambuc #undef HEADER2
18*f4a2713aSLionel Sambuc #undef HEADERUSE
19*f4a2713aSLionel Sambuc #elif !defined(HEADER2)
20*f4a2713aSLionel Sambuc #define HEADER2
21*f4a2713aSLionel Sambuc #undef HEADERUSE
22*f4a2713aSLionel Sambuc #else
23*f4a2713aSLionel Sambuc #define HEADERUSE
24*f4a2713aSLionel Sambuc #undef HEADER1
25*f4a2713aSLionel Sambuc #undef HEADER2
26*f4a2713aSLionel Sambuc #endif
27*f4a2713aSLionel Sambuc #endif
28*f4a2713aSLionel Sambuc 
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc // *** HEADER1: First header file
31*f4a2713aSLionel Sambuc #if defined(HEADER1) && !defined(HEADER2) && !defined(HEADERUSE)
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc template<typename T> T var0a = T();
34*f4a2713aSLionel Sambuc template<typename T> extern T var0b;
35*f4a2713aSLionel Sambuc 
36*f4a2713aSLionel Sambuc namespace join {
37*f4a2713aSLionel Sambuc   template<typename T> T va = T(100);
38*f4a2713aSLionel Sambuc   template<typename T> extern T vb;
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc   namespace diff_types {
41*f4a2713aSLionel Sambuc #ifdef ERROR
42*f4a2713aSLionel Sambuc     template<typename T> extern float err0;
43*f4a2713aSLionel Sambuc     template<typename T> extern T err1;
44*f4a2713aSLionel Sambuc #endif
45*f4a2713aSLionel Sambuc     template<typename T> extern T def;
46*f4a2713aSLionel Sambuc   }
47*f4a2713aSLionel Sambuc 
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc namespace spec {
51*f4a2713aSLionel Sambuc   template<typename T> constexpr T va = T(10);
52*f4a2713aSLionel Sambuc   template<> constexpr float va<float> = 1.5;
53*f4a2713aSLionel Sambuc   template constexpr int va<int>;
54*f4a2713aSLionel Sambuc 
55*f4a2713aSLionel Sambuc   template<typename T> T vb = T();
56*f4a2713aSLionel Sambuc   template<> constexpr float vb<float> = 1.5;
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   template<typename T> T vc = T();
59*f4a2713aSLionel Sambuc 
60*f4a2713aSLionel Sambuc   template<typename T> constexpr T vd = T(10);
61*f4a2713aSLionel Sambuc   template<typename T> T* vd<T*> = new T();
62*f4a2713aSLionel Sambuc }
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc namespace spec_join1 {
65*f4a2713aSLionel Sambuc   template<typename T> T va = T(10);
66*f4a2713aSLionel Sambuc   template<> extern float va<float>;
67*f4a2713aSLionel Sambuc   extern template int va<int>;
68*f4a2713aSLionel Sambuc 
69*f4a2713aSLionel Sambuc   template<typename T> T vb = T(10);
70*f4a2713aSLionel Sambuc   template<> extern float vb<float>;
71*f4a2713aSLionel Sambuc 
72*f4a2713aSLionel Sambuc   template<typename T> T vc = T(10);
73*f4a2713aSLionel Sambuc 
74*f4a2713aSLionel Sambuc   template<typename T> T vd = T(10);
75*f4a2713aSLionel Sambuc   template<typename T> extern T* vd<T*>;
76*f4a2713aSLionel Sambuc }
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc #endif
79*f4a2713aSLionel Sambuc 
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc // *** HEADER2: Second header file -- including HEADER1
82*f4a2713aSLionel Sambuc #if defined(HEADER2) && !defined(HEADERUSE)
83*f4a2713aSLionel Sambuc 
84*f4a2713aSLionel Sambuc namespace join {
85*f4a2713aSLionel Sambuc   template<typename T> extern T va;
86*f4a2713aSLionel Sambuc   template<> constexpr float va<float> = 2.5;
87*f4a2713aSLionel Sambuc 
88*f4a2713aSLionel Sambuc   template<typename T> T vb = T(100);
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc   namespace diff_types {
91*f4a2713aSLionel Sambuc #ifdef ERROR
92*f4a2713aSLionel Sambuc     template<typename T> extern T err0; // expected-error {{redefinition of 'err0' with a different type: 'T' vs 'float'}}  // expected-note@42 {{previous definition is here}}
93*f4a2713aSLionel Sambuc     template<typename T> extern float err1; // expected-error {{redefinition of 'err1' with a different type: 'float' vs 'T'}} // expected-note@43 {{previous definition is here}}
94*f4a2713aSLionel Sambuc #endif
95*f4a2713aSLionel Sambuc     template<typename T> extern T def;
96*f4a2713aSLionel Sambuc   }
97*f4a2713aSLionel Sambuc }
98*f4a2713aSLionel Sambuc 
99*f4a2713aSLionel Sambuc namespace spec_join1 {
100*f4a2713aSLionel Sambuc   template<typename T> extern T va;
101*f4a2713aSLionel Sambuc   template<> float va<float> = 1.5;
102*f4a2713aSLionel Sambuc   extern template int va<int>;
103*f4a2713aSLionel Sambuc 
104*f4a2713aSLionel Sambuc   template<> float vb<float> = 1.5;
105*f4a2713aSLionel Sambuc   template int vb<int>;
106*f4a2713aSLionel Sambuc 
107*f4a2713aSLionel Sambuc   template<> float vc<float> = 1.5;
108*f4a2713aSLionel Sambuc   template int vc<int>;
109*f4a2713aSLionel Sambuc 
110*f4a2713aSLionel Sambuc   template<typename T> extern T vd;
111*f4a2713aSLionel Sambuc   template<typename T> T* vd<T*> = new T();
112*f4a2713aSLionel Sambuc }
113*f4a2713aSLionel Sambuc 
114*f4a2713aSLionel Sambuc #endif
115*f4a2713aSLionel Sambuc 
116*f4a2713aSLionel Sambuc // *** HEADERUSE: File using both header files -- including HEADER2
117*f4a2713aSLionel Sambuc #ifdef HEADERUSE
118*f4a2713aSLionel Sambuc 
119*f4a2713aSLionel Sambuc template int var0a<int>;
120*f4a2713aSLionel Sambuc float fvara = var0a<float>;
121*f4a2713aSLionel Sambuc 
122*f4a2713aSLionel Sambuc template<typename T> extern T var0a;
123*f4a2713aSLionel Sambuc 
124*f4a2713aSLionel Sambuc template<typename T> T var0b = T();
125*f4a2713aSLionel Sambuc template int var0b<int>;
126*f4a2713aSLionel Sambuc float fvarb = var0b<float>;
127*f4a2713aSLionel Sambuc 
128*f4a2713aSLionel Sambuc namespace join {
129*f4a2713aSLionel Sambuc   template const int va<const int>;
130*f4a2713aSLionel Sambuc   template<> const int va<int> = 50;
131*f4a2713aSLionel Sambuc   static_assert(va<float> == 2.5, "");
132*f4a2713aSLionel Sambuc   static_assert(va<int> == 50, "");
133*f4a2713aSLionel Sambuc 
134*f4a2713aSLionel Sambuc   template<> constexpr float vb<float> = 2.5;
135*f4a2713aSLionel Sambuc   template const int vb<const int>;
136*f4a2713aSLionel Sambuc   static_assert(vb<float> == 2.5, "");
137*f4a2713aSLionel Sambuc   static_assert(vb<const int> == 100, "");
138*f4a2713aSLionel Sambuc 
139*f4a2713aSLionel Sambuc   namespace diff_types {
140*f4a2713aSLionel Sambuc     template<typename T> T def = T();
141*f4a2713aSLionel Sambuc   }
142*f4a2713aSLionel Sambuc 
143*f4a2713aSLionel Sambuc }
144*f4a2713aSLionel Sambuc 
145*f4a2713aSLionel Sambuc namespace spec {
146*f4a2713aSLionel Sambuc   static_assert(va<float> == 1.5, "");
147*f4a2713aSLionel Sambuc   static_assert(va<int> == 10, "");
148*f4a2713aSLionel Sambuc 
149*f4a2713aSLionel Sambuc   template<typename T> T* vb<T*> = new T();
150*f4a2713aSLionel Sambuc   int* intpb = vb<int*>;
151*f4a2713aSLionel Sambuc   static_assert(vb<float> == 1.5, "");
152*f4a2713aSLionel Sambuc 
153*f4a2713aSLionel Sambuc   template<typename T> T* vc<T*> = new T();
154*f4a2713aSLionel Sambuc   template<> constexpr float vc<float> = 1.5;
155*f4a2713aSLionel Sambuc   int* intpc = vc<int*>;
156*f4a2713aSLionel Sambuc   static_assert(vc<float> == 1.5, "");
157*f4a2713aSLionel Sambuc 
158*f4a2713aSLionel Sambuc   char* intpd = vd<char*>;
159*f4a2713aSLionel Sambuc }
160*f4a2713aSLionel Sambuc 
161*f4a2713aSLionel Sambuc namespace spec_join1 {
162*f4a2713aSLionel Sambuc   template int va<int>;
163*f4a2713aSLionel Sambuc   int a = va<int>;
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc   template<typename T> extern T vb;
166*f4a2713aSLionel Sambuc   int b = vb<int>;
167*f4a2713aSLionel Sambuc 
168*f4a2713aSLionel Sambuc   int* intpb = vd<int*>;
169*f4a2713aSLionel Sambuc }
170*f4a2713aSLionel Sambuc 
171*f4a2713aSLionel Sambuc #endif
172