File tree Expand file tree Collapse file tree 4 files changed +62
-0
lines changed
test/Interop/Cxx/class/inheritance Expand file tree Collapse file tree 4 files changed +62
-0
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,17 @@ void IRGenModule::emitClangDecl(const clang::Decl *decl) {
201
201
}
202
202
}
203
203
204
+ // If something from a C++ class is used, emit all virtual methods of this
205
+ // class because they might be emitted in the vtable even if not used
206
+ // directly from Swift.
207
+ if (auto *record = dyn_cast<clang::CXXRecordDecl>(next->getDeclContext ())) {
208
+ for (auto *method : record->methods ()) {
209
+ if (method->isVirtual ()) {
210
+ callback (method);
211
+ }
212
+ }
213
+ }
214
+
204
215
if (auto var = dyn_cast<clang::VarDecl>(next))
205
216
if (!var->isFileVarDecl ())
206
217
continue ;
Original file line number Diff line number Diff line change @@ -13,3 +13,8 @@ module SubTypes {
13
13
module TypeAliases {
14
14
header "type-aliases.h"
15
15
}
16
+
17
+ module VirtualMethods {
18
+ header "virtual-methods.h"
19
+ requires cplusplus
20
+ }
Original file line number Diff line number Diff line change
1
+ extern " C" void puts (const char *);
2
+
3
+ inline void testFunctionCollected () {
4
+ puts (" test\n " );
5
+ }
6
+
7
+ struct Base {
8
+ virtual void foo () = 0;
9
+ };
10
+
11
+ template <class T >
12
+ struct Derived : Base {
13
+ inline void foo () override {
14
+ testFunctionCollected ();
15
+ }
16
+
17
+ void callMe () {
18
+ }
19
+ };
20
+
21
+ using DerivedInt = Derived<int >;
22
+
23
+ template <class T >
24
+ struct Unused : Base {
25
+ inline void foo () override {
26
+ }
27
+ };
28
+
29
+ using UnusedInt = Unused<int >;
Original file line number Diff line number Diff line change
1
+ // RUN: %target-swift-emit-ir -I %S/Inputs -enable-experimental-cxx-interop %s -validate-tbd-against-ir=none | %FileCheck %s
2
+
3
+ // FIXME: enable on Windows
4
+ // XFAIL: OS=windows-msvc
5
+
6
+ import VirtualMethods
7
+
8
+ var x = DerivedInt ( )
9
+ x. callMe ( )
10
+
11
+ // CHECK: define {{.*}}void @{{_ZN7DerivedIiE3fooEv|"\?foo@\?$Derived@H@@UEAAXXZ"}}
12
+ // CHECK: call void @{{_Z21testFunctionCollectedv|"\?testFunctionCollected@@YAXXZ"}}
13
+
14
+ // CHECK: define {{.*}}void @{{_Z21testFunctionCollectedv|"\?testFunctionCollected@@YAXXZ"}}
15
+
16
+ // CHECK-NOT: _ZN6UnusedIiE3fooEv
17
+ // CHECK-NOT: "\?foo@\?$Unused@H@@UEAAXXZ"
You can’t perform that action at this time.
0 commit comments