diff --git a/lib/ClangImporter/ClangImporter.cpp b/lib/ClangImporter/ClangImporter.cpp index c4d62b0580ef1..bf89d341f80ac 100644 --- a/lib/ClangImporter/ClangImporter.cpp +++ b/lib/ClangImporter/ClangImporter.cpp @@ -1565,9 +1565,20 @@ std::unique_ptr ClangImporter::create( importer->Impl.setObjectForKeyedSubscript = clangContext.Selectors.getSelector(2, setObjectForKeyedSubscriptIdents); + // Can't inherit implicit modules from main module, because it isn't loaded yet. + // Add the Swift module, because it is important for safe interop wrappers. + Identifier stdlibName = + importer->Impl.SwiftContext.getIdentifier(STDLIB_NAME); + ImportPath::Raw path = + importer->Impl.SwiftContext.AllocateCopy>( + Located(stdlibName, SourceLoc())); + ImplicitImportInfo implicitImportInfo; + implicitImportInfo.AdditionalUnloadedImports.emplace_back( + UnloadedImportedModule(ImportPath(path), ImportKind::Module)); + // Set up the imported header module. auto *importedHeaderModule = ModuleDecl::create( - ctx.getIdentifier(CLANG_HEADER_MODULE_NAME), ctx, + ctx.getIdentifier(CLANG_HEADER_MODULE_NAME), ctx, implicitImportInfo, [&](ModuleDecl *importedHeaderModule, auto addFile) { importer->Impl.ImportedHeaderUnit = new (ctx) ClangModuleUnit(*importedHeaderModule, importer->Impl, nullptr); diff --git a/test/Interop/C/swiftify-import/bridging-header.swift b/test/Interop/C/swiftify-import/bridging-header.swift new file mode 100644 index 0000000000000..4124fbb12b52c --- /dev/null +++ b/test/Interop/C/swiftify-import/bridging-header.swift @@ -0,0 +1,45 @@ +// REQUIRES: swift_feature_SafeInteropWrappers + +// RUN: %empty-directory(%t) +// RUN: split-file %s %t + +// 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 +// 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 +// RUN: %diff %t/macro-expansions.out %t/macro-expansions.expected +// 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 +// RUN: %diff %t/imports.out %t/imports.expected + +//--- imports.expected +imports for TMP_DIR/test.swift: + Swift + __ObjC + _StringProcessing + _SwiftConcurrencyShims + _Concurrency +imports for __ObjC.foo: +imports for @__swiftmacro_So3foo15_SwiftifyImportfMp_.swift: + __ObjC + Swift + +//--- macro-expansions.expected +@__swiftmacro_So3foo15_SwiftifyImportfMp_.swift +------------------------------ +/// This is an auto-generated wrapper for safer interop +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_disfavoredOverload public func foo(_ p: Span) { + let len = Int32(exactly: p.count)! + return unsafe p.withUnsafeBufferPointer { _pPtr in + return unsafe foo(len, _pPtr.baseAddress!) + } +} +------------------------------ + +//--- test.swift +func test(s: Span) { + foo(s) +} + +//--- bridging.h +#include +#include + +void foo(int len, const int * __counted_by(len) p __noescape);