Skip to content

[cxx-interop] Fix linker errors when a template is used from different modules #42222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/IRGen/GenClangDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ class ClangDeclFinder
// the initializer of the variable.
clang::Decl *getDeclWithExecutableCode(clang::Decl *decl) {
if (auto fd = dyn_cast<clang::FunctionDecl>(decl)) {
// If this is a potentially not-yet-instanciated template, we might
// still have a body.
if (fd->getTemplateInstantiationPattern())
return fd;

const clang::FunctionDecl *definition;
if (fd->hasBody(definition)) {
return const_cast<clang::FunctionDecl *>(definition);
}

// If this is a potentially not-yet-instanciated template, we might
// still have a body.
if (fd->getTemplateInstantiationPattern())
return fd;
} else if (auto vd = dyn_cast<clang::VarDecl>(decl)) {
clang::VarDecl *initializingDecl = vd->getInitializingDeclaration();
if (initializingDecl) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_A_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_A_H

#include "wrapper.h"

WrapperInt getWrapperIntA() {
return WrapperInt();
}

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_A_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_B_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_B_H

#include "wrapper.h"

WrapperInt getWrapperIntB() {
return WrapperInt();
}

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_B_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Wrapper {
header "wrapper.h"
export *
requires cplusplus
}

module A {
header "a.h"
export *
requires cplusplus
}

module B {
header "b.h"
export *
requires cplusplus
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_WRAPPER_H
#define TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_WRAPPER_H

template <typename T>
struct Wrapper {
T t;
int i = 0;

void foo() { i++; }

Wrapper() { foo(); }
};

typedef Wrapper<int> WrapperInt;

#endif // TEST_INTEROP_CXX_TEMPLATES_INPUTS_MULTIPLE_MODULES_WRAPPER_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swift-emit-ir %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s

import A
import B
// Both A and B reference WrapperInt, and clang will instantiate two different redecls for WrapperInt.

let w = WrapperInt()
// The constructor of Wrapper calls Wrapper::foo, we need to make sure that Wrapper::foo is emitted,
// otherwise a linker error occurs.
// CHECK: define linkonce_odr{{( dso_local)?}} void @{{_ZN7WrapperIiE3fooEv|"\?foo@\?\$Wrapper@H@@QEAAXXZ"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop)
//
// REQUIRES: executable_test

import A
import B
import StdlibUnittest

var TemplatesTestSuite = TestSuite("TemplatesTestSuite")

TemplatesTestSuite.test("instantiation-in-multiple-modules") {
var w = WrapperInt()
// Make sure that WrapperInt::foo gets called.
expectEqual(w.i, 1)
}

runAllTests()