Skip to content

Commit c541666

Browse files
authored
Merge pull request #85023 from hnrklssn/swiftify-bridging-header
[ClangImporter] add implicit import of `Swift` for `__ObjC` module
2 parents b01436d + 314d3d5 commit c541666

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1563,9 +1563,20 @@ std::unique_ptr<ClangImporter> ClangImporter::create(
15631563
importer->Impl.setObjectForKeyedSubscript
15641564
= clangContext.Selectors.getSelector(2, setObjectForKeyedSubscriptIdents);
15651565

1566+
// Can't inherit implicit modules from main module, because it isn't loaded yet.
1567+
// Add the Swift module, because it is important for safe interop wrappers.
1568+
Identifier stdlibName =
1569+
importer->Impl.SwiftContext.getIdentifier(STDLIB_NAME);
1570+
ImportPath::Raw path =
1571+
importer->Impl.SwiftContext.AllocateCopy<Located<Identifier>>(
1572+
Located<Identifier>(stdlibName, SourceLoc()));
1573+
ImplicitImportInfo implicitImportInfo;
1574+
implicitImportInfo.AdditionalUnloadedImports.emplace_back(
1575+
UnloadedImportedModule(ImportPath(path), ImportKind::Module));
1576+
15661577
// Set up the imported header module.
15671578
auto *importedHeaderModule = ModuleDecl::create(
1568-
ctx.getIdentifier(CLANG_HEADER_MODULE_NAME), ctx,
1579+
ctx.getIdentifier(CLANG_HEADER_MODULE_NAME), ctx, implicitImportInfo,
15691580
[&](ModuleDecl *importedHeaderModule, auto addFile) {
15701581
importer->Impl.ImportedHeaderUnit = new (ctx)
15711582
ClangModuleUnit(*importedHeaderModule, importer->Impl, nullptr);
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// REQUIRES: swift_feature_SafeInteropWrappers
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: split-file %s %t
5+
6+
// RUN: %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -o %t/test.swiftmodule -I %t -import-objc-header %t/bridging.h -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast %t/test.swift -verify
7+
// RUN: %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -o %t/test.swiftmodule -I %t -import-objc-header %t/bridging.h -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast %t/test.swift -dump-macro-expansions 2>&1 | %FileCheck --dry-run > %t/macro-expansions.out
8+
// RUN: %diff %t/macro-expansions.out %t/macro-expansions.expected
9+
// RUN: %target-swift-frontend -typecheck -plugin-path %swift-plugin-dir -o %t/test.swiftmodule -I %t -import-objc-header %t/bridging.h -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror -Xcc -Wno-nullability-completeness -Xcc -Wno-div-by-zero -Xcc -Wno-pointer-to-int-cast %t/test.swift -dump-source-file-imports 2>&1 | %FileCheck --dry-run > %t/imports.out
10+
// RUN: %diff %t/imports.out %t/imports.expected
11+
12+
//--- imports.expected
13+
imports for TMP_DIR/test.swift:
14+
Swift
15+
__ObjC
16+
_StringProcessing
17+
_SwiftConcurrencyShims
18+
_Concurrency
19+
imports for __ObjC.foo:
20+
imports for @__swiftmacro_So3foo15_SwiftifyImportfMp_.swift:
21+
__ObjC
22+
Swift
23+
24+
//--- macro-expansions.expected
25+
@__swiftmacro_So3foo15_SwiftifyImportfMp_.swift
26+
------------------------------
27+
/// This is an auto-generated wrapper for safer interop
28+
@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_disfavoredOverload public func foo(_ p: Span<Int32>) {
29+
let len = Int32(exactly: p.count)!
30+
return unsafe p.withUnsafeBufferPointer { _pPtr in
31+
return unsafe foo(len, _pPtr.baseAddress!)
32+
}
33+
}
34+
------------------------------
35+
36+
//--- test.swift
37+
func test(s: Span<CInt>) {
38+
foo(s)
39+
}
40+
41+
//--- bridging.h
42+
#include <ptrcheck.h>
43+
#include <lifetimebound.h>
44+
45+
void foo(int len, const int * __counted_by(len) p __noescape);

0 commit comments

Comments
 (0)