Skip to content

Commit 4ac9119

Browse files
committed
Modify clang declaration weakly-imported query to use Swift's code-gen target triple
Similarly to how #70564 configures 'ClangImporter's 'CodeGenerator' using Swift's compilation target triple, we must use the versioned version of the 'isWeakImported' query to determine linkage for imported Clang symbols.
1 parent ea4906d commit 4ac9119

File tree

4 files changed

+85
-6
lines changed

4 files changed

+85
-6
lines changed

lib/AST/Decl.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
#include "clang/Basic/CharInfo.h"
7777
#include "clang/Basic/Module.h"
78+
#include "clang/Basic/TargetInfo.h"
7879
#include "clang/AST/Attr.h"
7980
#include "clang/AST/DeclObjC.h"
8081

@@ -1464,9 +1465,9 @@ AvailabilityRange Decl::getAvailabilityForLinkage() const {
14641465

14651466
bool Decl::isAlwaysWeakImported() const {
14661467
// For a Clang declaration, trust Clang.
1467-
if (auto clangDecl = getClangDecl()) {
1468-
return clangDecl->isWeakImported();
1469-
}
1468+
if (auto clangDecl = getClangDecl())
1469+
return clangDecl->isWeakImported(
1470+
getASTContext().LangOpts.getMinPlatformVersion());
14701471

14711472
if (getAttrs().hasAttribute<WeakLinkedAttr>())
14721473
return true;

lib/ClangImporter/ClangImporter.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
#include "clang/Basic/FileEntry.h"
6767
#include "clang/Basic/IdentifierTable.h"
6868
#include "clang/Basic/LangStandard.h"
69+
#include "clang/Basic/MacroBuilder.h"
6970
#include "clang/Basic/Module.h"
7071
#include "clang/Basic/Specifiers.h"
7172
#include "clang/Basic/TargetInfo.h"
@@ -1488,8 +1489,14 @@ ClangImporter::create(ASTContext &ctx,
14881489
importer.get(), importerOpts, VFS, *swiftTargetClangArgs);
14891490
if (!swiftTargetClangInvocation)
14901491
return nullptr;
1491-
importer->Impl.setSwiftTargetInfo(clang::TargetInfo::CreateTargetInfo(
1492-
clangDiags, swiftTargetClangInvocation->TargetOpts));
1492+
auto targetInfo = clang::TargetInfo::CreateTargetInfo(
1493+
clangDiags, swiftTargetClangInvocation->TargetOpts);
1494+
// Ensure the target info has configured target-specific defines
1495+
std::string defineBuffer;
1496+
llvm::raw_string_ostream predefines(defineBuffer);
1497+
clang::MacroBuilder builder(predefines);
1498+
targetInfo->getTargetDefines(instance.getLangOpts(), builder);
1499+
importer->Impl.setSwiftTargetInfo(targetInfo);
14931500
importer->Impl.setSwiftCodeGenOptions(new clang::CodeGenOptions(
14941501
swiftTargetClangInvocation->getCodeGenOpts()));
14951502
} else {

lib/SIL/IR/SILFunction.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,8 @@ bool SILFunction::isWeakImported(ModuleDecl *module) const {
618618

619619
// For imported functions check the Clang declaration.
620620
if (ClangNodeOwner)
621-
return ClangNodeOwner->getClangDecl()->isWeakImported();
621+
return ClangNodeOwner->getClangDecl()->isWeakImported(
622+
getASTContext().LangOpts.getMinPlatformVersion());
622623

623624
// For native functions check a flag on the SILFunction
624625
// itself.
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// REQUIRES: objc_interop
2+
// REQUIRES: OS=macosx
3+
// RUN: %empty-directory(%t)
4+
// RUN: %empty-directory(%t/module-cache)
5+
// RUN: %empty-directory(%t/inputs)
6+
// RUN: %empty-directory(%t/cheaders)
7+
8+
// RUN: split-file %s %t
9+
// RUN: sed -e "s|INPUTSDIR|%/t/inputs|g" %t/map.json.template > %t/map.json.template1
10+
// RUN: sed -e "s|STDLIBMOD|%/stdlib_module|g" %t/map.json.template1 > %t/map.json.template2
11+
// RUN: sed -e "s|ONONEMOD|%/ononesupport_module|g" %t/map.json.template2 > %t/map.json.template3
12+
// RUN: sed -e "s|CHEADERSDIR|%t/cheaders|g" %t/map.json.template3 > %t/map.json.template4
13+
// RUN: sed -e "s|SWIFTLIBDIR|%swift-lib-dir|g" %t/map.json.template4 > %t/map.json
14+
15+
// RUN: %target-swift-emit-pcm -module-name Bar -o %t/inputs/Bar.pcm %t/cheaders/module.modulemap -target %target-cpu-apple-macosx15.0
16+
// RUN: %target-swift-emit-pcm -module-name SwiftShims %swift-lib-dir/swift/shims/module.modulemap -o %t/inputs/SwiftShims.pcm -target %target-cpu-apple-macosx15.0
17+
18+
// RUN: %target-swift-frontend -c -disable-implicit-swift-modules -disable-implicit-concurrency-module-import -disable-implicit-string-processing-module-import -module-cache-path %t/module-cache -explicit-swift-module-map-file %t/map.json -primary-file %t/test.swift -o %t/test.o -target %target-cpu-apple-macosx14.0 -clang-target %target-cpu-apple-macosx15.0
19+
20+
// RUN: %llvm-nm -m %t/test.o | %FileCheck %s
21+
// CHECK: (undefined) weak external _funcBar
22+
// CHECK: (undefined) external _funcBarButOlder
23+
24+
//--- map.json.template
25+
[
26+
{
27+
"moduleName": "Bar",
28+
"clangModulePath": "INPUTSDIR/Bar.pcm",
29+
"clangModuleMapPath": "CHEADERSDIR/module.modulemap"
30+
},
31+
{
32+
"moduleName": "Swift",
33+
"modulePath": "STDLIBMOD",
34+
"isFramework": false
35+
},
36+
{
37+
"moduleName": "SwiftOnoneSupport",
38+
"modulePath": "ONONEMOD",
39+
"isFramework": false
40+
},
41+
{
42+
"moduleName": "SwiftShims",
43+
"isFramework": false,
44+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
45+
"clangModulePath": "INPUTSDIR/SwiftShims.pcm"
46+
}]
47+
48+
//--- cheaders/module.modulemap
49+
module Bar {
50+
header "Bar.h"
51+
export *
52+
}
53+
54+
//--- cheaders/Bar.h
55+
#pragma clang attribute push (__attribute__((availability(macOS, introduced=15.0))), apply_to=function)
56+
extern int funcBar(void);
57+
#pragma clang attribute pop
58+
59+
#pragma clang attribute push (__attribute__((availability(macOS, introduced=14.0))), apply_to=function)
60+
extern int funcBarButOlder(void);
61+
#pragma clang attribute pop
62+
63+
//--- test.swift
64+
import Bar
65+
public func foo() {
66+
let _ = funcBarButOlder()
67+
if #available(macOS 15.0, *), funcBar() != 0 {
68+
print("Hello, World!")
69+
}
70+
}

0 commit comments

Comments
 (0)