[clang][lex] NFCI: Use DirectoryEntryRef in ModuleMap::inferFrameworkModule()
[llvm-project.git] / clang / test / SemaTemplate / dependent-names.cpp
blob641ec950054f5731cf33ec3a8d1d9f6a85399071
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
3 typedef double A;
4 template<typename T> class B {
5 typedef int A;
6 };
8 template<typename T> struct X : B<T> {
9 static A a;
12 int a0[sizeof(X<int>::a) == sizeof(double) ? 1 : -1];
14 // PR4365.
15 template<class T> class Q;
16 template<class T> class R : Q<T> {T current;};
19 namespace test0 {
20 template <class T> class Base {
21 public:
22 void instance_foo();
23 static void static_foo();
24 class Inner {
25 public:
26 void instance_foo();
27 static void static_foo();
31 template <class T> class Derived1 : Base<T> {
32 public:
33 void test0() {
34 Base<T>::static_foo();
35 Base<T>::instance_foo();
38 void test1() {
39 Base<T>::Inner::static_foo();
40 Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
43 static void test2() {
44 Base<T>::static_foo();
45 Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
48 static void test3() {
49 Base<T>::Inner::static_foo();
50 Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
54 template <class T> class Derived2 : Base<T>::Inner {
55 public:
56 void test0() {
57 Base<T>::static_foo();
58 Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
61 void test1() {
62 Base<T>::Inner::static_foo();
63 Base<T>::Inner::instance_foo();
66 static void test2() {
67 Base<T>::static_foo();
68 Base<T>::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
71 static void test3() {
72 Base<T>::Inner::static_foo();
73 Base<T>::Inner::instance_foo(); // expected-error {{call to non-static member function without an object argument}}
77 void test0() {
78 Derived1<int> d1;
79 d1.test0();
80 d1.test1(); // expected-note {{in instantiation of member function}}
81 d1.test2(); // expected-note {{in instantiation of member function}}
82 d1.test3(); // expected-note {{in instantiation of member function}}
84 Derived2<int> d2;
85 d2.test0(); // expected-note {{in instantiation of member function}}
86 d2.test1();
87 d2.test2(); // expected-note {{in instantiation of member function}}
88 d2.test3(); // expected-note {{in instantiation of member function}}
92 namespace test1 {
93 template <class T> struct Base {
94 void foo(T); // expected-note {{member is declared here}}
97 template <class T> struct Derived : Base<T> {
98 void doFoo(T v) {
99 foo(v); // expected-error {{explicit qualification required to use member 'foo' from dependent base class}}
103 template struct Derived<int>; // expected-note {{requested here}}
106 namespace PR8966 {
107 template <class T>
108 class MyClassCore
112 template <class T>
113 class MyClass : public MyClassCore<T>
115 public:
116 enum {
120 // static member declaration
121 static const char* array [N];
123 void f() {
124 MyClass<T>::InBase = 17;
128 // static member definition
129 template <class T>
130 const char* MyClass<T>::array [MyClass<T>::N] = { "A", "B", "C" };
133 namespace std {
134 inline namespace v1 {
135 template<typename T> struct basic_ostream;
137 namespace inner {
138 template<typename T> struct vector {};
140 using inner::vector;
141 template<typename T, typename U> struct pair {};
142 typedef basic_ostream<char> ostream;
143 extern ostream cout;
144 std::ostream &operator<<(std::ostream &out, const char *);
147 namespace PR10053 {
148 template<typename T> struct A {
149 T t;
150 A() {
151 f(t); // expected-error {{call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup}}
155 void f(int&); // expected-note {{'f' should be declared prior to the call site}}
157 A<int> a; // expected-note {{in instantiation of member function}}
160 namespace N {
161 namespace M {
162 template<typename T> int g(T t) {
163 f(t); // expected-error {{call to function 'f' that is neither visible in the template definition nor found by argument-dependent lookup}}
167 void f(char&); // expected-note {{'f' should be declared prior to the call site}}
170 void f(char&);
172 int k = N::M::g<char>(0);; // expected-note {{in instantiation of function}}
175 namespace O {
176 int f(char&); // expected-note {{candidate function not viable}}
178 template<typename T> struct C {
179 static const int n = f(T()); // expected-error {{no matching function}}
183 int f(double); // no note, shadowed by O::f
184 O::C<double> c; // expected-note {{requested here}}
187 // Example from www/compatibility.html
188 namespace my_file {
189 template <typename T> T Squared(T x) {
190 return Multiply(x, x); // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
193 int Multiply(int x, int y) { // expected-note {{should be declared prior to the call site}}
194 return x * y;
197 int main() {
198 Squared(5); // expected-note {{here}}
202 // Example from www/compatibility.html
203 namespace my_file2 {
204 template<typename T>
205 void Dump(const T& value) {
206 std::cout << value << "\n"; // expected-error {{neither visible in the template definition nor found by argument-dependent lookup}}
209 namespace ns {
210 struct Data {};
213 std::ostream& operator<<(std::ostream& out, ns::Data data) { // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2::ns'}}
214 return out << "Some data";
217 void Use() {
218 Dump(ns::Data()); // expected-note {{here}}
222 namespace my_file2_a {
223 template<typename T>
224 void Dump(const T &value) {
225 print(std::cout, value); // expected-error 4{{neither visible in the template definition nor found by argument-dependent lookup}}
228 namespace ns {
229 struct Data {};
231 namespace ns2 {
232 struct Data {};
235 std::ostream &print(std::ostream &out, int); // expected-note-re {{should be declared prior to the call site{{$}}}}
236 std::ostream &print(std::ostream &out, ns::Data); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2_a::ns'}}
237 std::ostream &print(std::ostream &out, std::vector<ns2::Data>); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::my_file2_a::ns2'}}
238 std::ostream &print(std::ostream &out, std::pair<ns::Data, ns2::Data>); // expected-note {{should be declared prior to the call site or in an associated namespace of one of its arguments}}
240 void Use() {
241 Dump(0); // expected-note {{requested here}}
242 Dump(ns::Data()); // expected-note {{requested here}}
243 Dump(std::vector<ns2::Data>()); // expected-note {{requested here}}
244 Dump(std::pair<ns::Data, ns2::Data>()); // expected-note {{requested here}}
248 namespace unary {
249 template<typename T>
250 T Negate(const T& value) {
251 return !value; // expected-error {{call to function 'operator!' that is neither visible in the template definition nor found by argument-dependent lookup}}
254 namespace ns {
255 struct Data {};
258 ns::Data operator!(ns::Data); // expected-note {{should be declared prior to the call site or in namespace 'PR10053::unary::ns'}}
260 void Use() {
261 Negate(ns::Data()); // expected-note {{requested here}}
266 namespace PR10187 {
267 namespace A1 {
268 template<typename T>
269 struct S {
270 void f() {
271 for (auto &a : e)
272 __range(a); // expected-error {{undeclared identifier '__range'}}
274 int e[10];
278 namespace A2 {
279 template<typename T>
280 struct S {
281 void f() {
282 for (auto &a : e)
283 __range(a); // expected-error {{undeclared identifier '__range'}}
285 T e[10];
287 void g() {
288 S<int>().f(); // expected-note {{here}}
290 struct X {};
291 void __range(X);
292 void h() {
293 S<X>().f();
297 namespace B {
298 template<typename T> void g(); // expected-note {{not viable}}
299 template<typename T> void f() {
300 g<int>(T()); // expected-error {{no matching function}}
303 namespace {
304 struct S {};
306 void g(S);
308 template void f<S>(); // expected-note {{here}}
312 namespace rdar11242625 {
314 template <typename T>
315 struct Main {
316 struct default_names {
317 typedef int id;
320 template <typename T2 = typename default_names::id>
321 struct TS {
322 T2 q;
326 struct Sub : public Main<int> {
327 TS<> ff;
330 int arr[sizeof(Sub)];
334 namespace PR11421 {
335 template < unsigned > struct X {
336 static const unsigned dimension = 3;
337 template<unsigned dim=dimension>
338 struct Y: Y<dim> { }; // expected-error{{circular inheritance between 'Y<dim>' and 'Y<dim>'}}
340 typedef X<3> X3;
341 X3::Y<>::iterator it; // expected-error {{no type named 'iterator' in 'PR11421::X<3>::Y<>'}}
344 namespace rdar12629723 {
345 template<class T>
346 struct X {
347 struct C : public C { }; // expected-error{{circular inheritance between 'C' and 'rdar12629723::X::C'}}
349 struct B;
351 struct A : public B { // expected-note{{'A' declared here}}
352 virtual void foo() { }
355 struct D : T::foo { };
356 struct E : D { };
359 template<class T>
360 struct X<T>::B : public A { // expected-error{{circular inheritance between 'A' and 'rdar12629723::X::B'}}
361 virtual void foo() { }
365 namespace test_reserved_identifiers {
366 template<typename A, typename B> void tempf(A a, B b) {
367 a + b; // expected-error{{call to function 'operator+' that is neither visible in the template definition nor found by argument-dependent lookup}}
369 namespace __gnu_cxx { struct X {}; }
370 namespace ns { struct Y {}; }
371 void operator+(__gnu_cxx::X, ns::Y); // expected-note{{or in namespace 'test_reserved_identifiers::ns'}}
372 void test() {
373 __gnu_cxx::X x;
374 ns::Y y;
375 tempf(x, y); // expected-note{{in instantiation of}}
379 // This test must live in the global namespace.
380 struct PR14695_X {};
381 // FIXME: This note is bogus; it is the using directive which would need to move
382 // to prior to the call site to fix the problem.
383 namespace PR14695_A { void PR14695_f(PR14695_X); } // expected-note {{'PR14695_f' should be declared prior to the call site or in the global namespace}}
384 template<typename T> void PR14695_g(T t) { PR14695_f(t); } // expected-error {{call to function 'PR14695_f' that is neither visible in the template definition nor found by argument-dependent lookup}}
385 using namespace PR14695_A;
386 template void PR14695_g(PR14695_X); // expected-note{{requested here}}
388 namespace OperatorNew {
389 template<typename T> void f(T t) {
390 operator new(100, t); // expected-error{{call to function 'operator new' that is neither visible in the template definition nor found by argument-dependent lookup}}
391 // FIXME: This should give the same error.
392 new (t) int;
394 struct X {};
396 using size_t = decltype(sizeof(0));
397 void *operator new(size_t, OperatorNew::X); // expected-note-re {{should be declared prior to the call site{{$}}}}
398 template void OperatorNew::f(OperatorNew::X); // expected-note {{instantiation of}}
400 namespace PR19936 {
401 template<typename T> decltype(*T()) f() {} // expected-note {{previous}}
402 template<typename T> decltype(T() * T()) g() {} // expected-note {{previous}}
404 // Create some overloaded operators so we build an overload operator call
405 // instead of a builtin operator call for the dependent expression.
406 enum E {};
407 int operator*(E);
408 int operator*(E, E);
410 // Check that they still profile the same.
411 template<typename T> decltype(*T()) f() {} // expected-error {{redefinition}}
412 template<typename T> decltype(T() * T()) g() {} // expected-error {{redefinition}}
415 template <typename> struct CT2 {
416 template <class U> struct X;
418 template <typename T> int CT2<int>::X<>; // expected-error {{template parameter list matching the non-templated nested type 'CT2<int>' should be empty}}
420 namespace DependentTemplateIdWithNoArgs {
421 template<typename T> void f() { T::template f(); }
422 struct X {
423 template<int = 0> static void f();
425 void g() { f<X>(); }
428 namespace DependentUnresolvedUsingTemplate {
429 template<typename T>
430 struct X : T {
431 using T::foo;
432 void f() { this->template foo(); } // expected-error {{does not refer to a template}}
433 void g() { this->template foo<>(); } // expected-error {{does not refer to a template}}
434 void h() { this->template foo<int>(); } // expected-error {{does not refer to a template}}
436 struct A { template<typename = int> int foo(); };
437 struct B { int foo(); }; // expected-note 3{{non-template here}}
438 void test(X<A> xa, X<B> xb) {
439 xa.f();
440 xa.g();
441 xa.h();
442 xb.f(); // expected-note {{instantiation of}}
443 xb.g(); // expected-note {{instantiation of}}
444 xb.h(); // expected-note {{instantiation of}}
448 namespace PR37680 {
449 template <class a> struct b : a {
450 using a::add;
451 template<int> int add() { return this->template add(0); }
453 struct a {
454 template<typename T = void> int add(...);
455 void add(int);
457 int f(b<a> ba) { return ba.add<0>(); }