xref: /llvm-project/lldb/test/API/lang/cpp/virtual-functions/main.cpp (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1 // Tests virtual function calls. As virtual destructors influence
2 // vtables this tests also needs to cover all combinations of
3 // virtual destructors in the derived/base class.
4 
5 struct BaseWithVirtDtor {
~BaseWithVirtDtorBaseWithVirtDtor6   virtual ~BaseWithVirtDtor() {}
fooBaseWithVirtDtor7   virtual int foo() { return 1; }
8 };
9 
10 struct BaseWithoutVirtDtor {
fooBaseWithoutVirtDtor11   virtual int foo() { return 2; }
12 };
13 
14 struct DerivedWithVirtDtor : BaseWithVirtDtor {
~DerivedWithVirtDtorDerivedWithVirtDtor15   virtual ~DerivedWithVirtDtor() {}
fooDerivedWithVirtDtor16   virtual int foo() { return 3; }
17 };
18 
19 struct DerivedWithoutVirtDtor : BaseWithoutVirtDtor {
fooDerivedWithoutVirtDtor20   virtual int foo() { return 4; }
21 };
22 
23 struct DerivedWithBaseVirtDtor : BaseWithVirtDtor {
fooDerivedWithBaseVirtDtor24   virtual int foo() { return 5; }
25 };
26 
27 struct DerivedWithVirtDtorButNoBaseDtor : BaseWithoutVirtDtor {
~DerivedWithVirtDtorButNoBaseDtorDerivedWithVirtDtorButNoBaseDtor28   virtual ~DerivedWithVirtDtorButNoBaseDtor() {}
fooDerivedWithVirtDtorButNoBaseDtor29   virtual int foo() { return 6; }
30 };
31 
32 struct DerivedWithOverload : BaseWithVirtDtor {
~DerivedWithOverloadDerivedWithOverload33   virtual ~DerivedWithOverload() {}
fooDerivedWithOverload34   virtual int foo(int i) { return 7; }
35 };
36 
37 struct DerivedWithOverloadAndUsing : BaseWithVirtDtor {
~DerivedWithOverloadAndUsingDerivedWithOverloadAndUsing38   virtual ~DerivedWithOverloadAndUsing() {}
39   using BaseWithVirtDtor::foo;
fooDerivedWithOverloadAndUsing40   virtual int foo(int i) { return 8; }
41 };
42 
main()43 int main() {
44   // Declare base classes.
45   BaseWithVirtDtor base_with_dtor;
46   BaseWithoutVirtDtor base_without_dtor;
47 
48   // Declare all the derived classes.
49   DerivedWithVirtDtor derived_with_dtor;
50   DerivedWithoutVirtDtor derived_without_dtor;
51   DerivedWithBaseVirtDtor derived_with_base_dtor;
52   DerivedWithVirtDtorButNoBaseDtor derived_with_dtor_but_no_base_dtor;
53   DerivedWithOverload derived_with_overload;
54   DerivedWithOverloadAndUsing derived_with_overload_and_using;
55 
56   // The previous classes as their base class type.
57   BaseWithVirtDtor &derived_with_dtor_as_base = derived_with_dtor;
58   BaseWithoutVirtDtor &derived_without_as_base = derived_without_dtor;
59   BaseWithVirtDtor &derived_with_base_dtor_as_base = derived_with_base_dtor;
60   BaseWithoutVirtDtor &derived_with_dtor_but_no_base_dtor_as_base = derived_with_dtor_but_no_base_dtor;
61 
62   // Call functions so that they are compiled.
63   int i = base_with_dtor.foo() + base_without_dtor.foo() +
64           derived_with_dtor.foo() + derived_without_dtor.foo() +
65           derived_with_base_dtor.foo() + derived_with_overload.foo(1)
66           + derived_with_overload_and_using.foo(2)
67           + derived_with_overload_and_using.foo();
68 
69   return i; // break here
70 }
71