Skip to content

Commit 8990cd9

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 8990cd9

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-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

@@ -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/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"
@@ -1464,8 +1465,14 @@ ClangImporter::create(ASTContext &ctx,
14641465
importer.get(), importerOpts, VFS, *swiftTargetClangArgs);
14651466
if (!swiftTargetClangInvocation)
14661467
return nullptr;
1467-
importer->Impl.setSwiftTargetInfo(clang::TargetInfo::CreateTargetInfo(
1468-
clangDiags, swiftTargetClangInvocation->TargetOpts));
1468+
auto targetInfo = clang::TargetInfo::CreateTargetInfo(
1469+
clangDiags, swiftTargetClangInvocation->TargetOpts);
1470+
// Ensure the target info has configured target-specific defines
1471+
std::string defineBuffer;
1472+
llvm::raw_string_ostream predefines(defineBuffer);
1473+
clang::MacroBuilder builder(predefines);
1474+
targetInfo->getTargetDefines(instance.getLangOpts(), builder);
1475+
importer->Impl.setSwiftTargetInfo(targetInfo);
14691476
importer->Impl.setSwiftCodeGenOptions(new clang::CodeGenOptions(
14701477
swiftTargetClangInvocation->getCodeGenOpts()));
14711478
} 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.
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// CHECK: (undefined) external _funcBarButOlder
22+
23+
//--- map.json.template
24+
[
25+
{
26+
"moduleName": "Bar",
27+
"clangModulePath": "INPUTSDIR/Bar.pcm",
28+
"clangModuleMapPath": "CHEADERSDIR/module.modulemap"
29+
},
30+
{
31+
"moduleName": "Swift",
32+
"modulePath": "STDLIBMOD",
33+
"isFramework": false
34+
},
35+
{
36+
"moduleName": "SwiftOnoneSupport",
37+
"modulePath": "ONONEMOD",
38+
"isFramework": false
39+
},
40+
{
41+
"moduleName": "SwiftShims",
42+
"isFramework": false,
43+
"clangModuleMapPath": "SWIFTLIBDIR/swift/shims/module.modulemap",
44+
"clangModulePath": "INPUTSDIR/SwiftShims.pcm"
45+
}]
46+
47+
//--- cheaders/module.modulemap
48+
module Bar {
49+
header "Bar.h"
50+
export *
51+
}
52+
53+
//--- cheaders/Bar.h
54+
#pragma clang attribute push (__attribute__((availability(macOS, introduced=15.0))), apply_to=function)
55+
extern int funcBar(void);
56+
#pragma clang attribute pop
57+
58+
#pragma clang attribute push (__attribute__((availability(macOS, introduced=14.0))), apply_to=function)
59+
extern int funcBarButOlder(void);
60+
#pragma clang attribute pop
61+
62+
//--- test.swift
63+
import Bar
64+
public func foo() {
65+
let _ = funcBarButOlder()
66+
if #available(macOS 15.0, *), funcBar() != 0 {
67+
print("Hello, World!")
68+
}
69+
}

0 commit comments

Comments
 (0)