xref: /llvm-project/clang/test/SemaTemplate/instantiate-pure-virtual-function.cpp (revision 0c92f8646a765174b68797ad3c0842215c77752f)
1 // RUN: %clang_cc1 -fsyntax-only -verify -Wundefined-func-template %s
2 
3 namespace GH74016 {
4   template <typename T> class B {
5   public:
foo(const T &)6     constexpr void foo(const T &) { bar(1); }
7     virtual constexpr void bar(unsigned int) = 0;
8   };
9 
10   template <typename T> class D : public B<T> {
11   public:
bar(unsigned int)12     constexpr void bar(unsigned int) override {}
13   };
14 
test()15   void test() {
16     auto t = D<int>();
17     t.foo(0);
18   }
19 };
20 
21 namespace call_pure_virtual_function_from_virtual {
22   template <typename T> class B {
23   public:
foo(const T &)24     const void foo(const T &) { B::bar(1); } // expected-warning {{instantiation of function 'call_pure_virtual_function_from_virtual::B<int>::bar' required here, but no definition is available}}
25     // expected-note@-1 {{add an explicit instantiation declaration to suppress this warning if 'call_pure_virtual_function_from_virtual::B<int>::bar' is explicitly instantiated in another translation unit}}
26     virtual const void bar(unsigned int) = 0; // expected-note {{forward declaration of template entity is here}}
27   };
28 
29   template <typename T> class D : public B<T> {
30   public:
bar(unsigned int)31     const void bar(unsigned int) override {}
32   };
33 
test()34   void test() {
35     auto t = D<int>();
36     t.foo(0); // expected-note {{in instantiation of member function 'call_pure_virtual_function_from_virtual::B<int>::foo' requested here}}
37   }
38 };
39 
40 namespace non_pure_virtual_function {
41   template <typename T> class B {
42   public:
foo(const T &)43     constexpr void foo(const T &) { bar(1); }
44 
45     virtual constexpr void bar(unsigned int); // expected-warning {{inline function 'non_pure_virtual_function::B<int>::bar' is not defined}}
46     // expected-note@-1 {{forward declaration of template entity is here}}
47     // expected-note@-2 {{forward declaration of template entity is here}}
48     // expected-note@-3 {{forward declaration of template entity is here}}
49   };
50 
51   template <typename T> class D : public B<T> { // expected-warning {{instantiation of function 'non_pure_virtual_function::B<int>::bar' required here, but no definition is available}}
52 // expected-warning@-1 {{instantiation of function 'non_pure_virtual_function::B<int>::bar' required here, but no definition is available}}
53 // expected-warning@-2 {{instantiation of function 'non_pure_virtual_function::B<int>::bar' required here, but no definition is available}}
54 // expected-note@-3 {{add an explicit instantiation declaration to suppress this warning if 'non_pure_virtual_function::B<int>::bar' is explicitly instantiated in another translation unit}}
55 // expected-note@-4 {{add an explicit instantiation declaration to suppress this warning if 'non_pure_virtual_function::B<int>::bar' is explicitly instantiated in another translation unit}}
56 // expected-note@-5 {{add an explicit instantiation declaration to suppress this warning if 'non_pure_virtual_function::B<int>::bar' is explicitly instantiated in another translation unit}}
57 // expected-note@-6 {{used here}}
58 
59   public:
bar(unsigned int)60     constexpr void bar(unsigned int) override { }
61   };
62 
test()63   void test() {
64     auto t = D<int>();
65     t.foo(0);
66   }
67 };
68