Skip to content

Commit 427c0ce

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 ee6decc commit 427c0ce

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

include/swift/AST/ClangModuleLoader.h

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class ClangModuleLoader : public ModuleLoader {
140140
/// (The implementing `ClangImporter` class maintains separate Target info
141141
/// for use by IRGen/CodeGen clients)
142142
virtual clang::TargetInfo &getModuleAvailabilityTarget() const = 0;
143+
virtual clang::TargetInfo &getTargetInfo() const = 0;
143144

144145
virtual clang::ASTContext &getClangASTContext() const = 0;
145146
virtual clang::Preprocessor &getClangPreprocessor() const = 0;

include/swift/ClangImporter/ClangImporter.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ class ClangImporter final : public ClangModuleLoader {
570570
/// instead. To distinguish IRGen clients from module loading clients,
571571
/// `getModuleAvailabilityTarget` should be used instead by module-loading
572572
/// clients.
573-
clang::TargetInfo &getTargetInfo() const;
573+
clang::TargetInfo &getTargetInfo() const override;
574574
clang::CodeGenOptions &getCodeGenOpts() const;
575575

576576
std::string getClangModuleHash() const;

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

@@ -1468,9 +1469,9 @@ AvailabilityRange Decl::getAvailabilityForLinkage() const {
14681469

14691470
bool Decl::isAlwaysWeakImported() const {
14701471
// For a Clang declaration, trust Clang.
1471-
if (auto clangDecl = getClangDecl()) {
1472-
return clangDecl->isWeakImported();
1473-
}
1472+
if (auto clangDecl = getClangDecl())
1473+
return clangDecl->isWeakImported(
1474+
getASTContext().LangOpts.getMinPlatformVersion());
14741475

14751476
if (getAttrs().hasAttribute<WeakLinkedAttr>())
14761477
return true;

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

0 commit comments

Comments
 (0)