|
| 1 | +// REQUIRES: swift_feature_SafeInteropWrappers |
| 2 | + |
| 3 | +// RUN: %empty-directory(%t) |
| 4 | +// RUN: split-file %s %t |
| 5 | + |
| 6 | +// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -I %t -enable-experimental-feature SafeInteropWrappers %t/test.swift -verify -Xcc -Wno-nullability-completeness |
| 7 | +// RUN: env SWIFT_BACKTRACE="" %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -I %t -enable-experimental-feature SafeInteropWrappers %t/test.swift -dump-macro-expansions 2> %t/expansions.out |
| 8 | +// RUN: %diff %t/expansions.out %t/expansions.expected |
| 9 | + |
| 10 | +//--- test.h |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#define __counted_by(x) __attribute__((__counted_by__(x))) |
| 14 | +#define __noescape __attribute__((noescape)) |
| 15 | +#define __lifetimebound __attribute__((lifetimebound)) |
| 16 | + |
| 17 | +void foo(int len, int * __counted_by(len) p __noescape); |
| 18 | +@protocol TestProtocol |
| 19 | + - (void) bar:(int * __noescape)p _Nullable:(int)asdf; |
| 20 | + - (void) simple:(int)len :(int * __counted_by(len) __noescape)p; |
| 21 | + - (void) shared:(int)len :(int * __counted_by(len) __noescape)p1 :(int * __counted_by(len) __noescape)p2; |
| 22 | + - (void) complexExpr:(int)len :(int) offset :(int * __counted_by(len - offset) __noescape)p; |
| 23 | + - (void) nullUnspecified:(int)len :(int * __counted_by(len) _Null_unspecified __noescape)p; |
| 24 | + - (void) nonnull:(int)len :(int * __counted_by(len) _Nonnull __noescape)p; |
| 25 | + - (void) nullable:(int)len :(int * __counted_by(len) _Nullable __noescape)p; |
| 26 | + - (void) mixedEscapability:(int)len :(int * __counted_by(len) __noescape)p1 :(int * __counted_by(len))p2; |
| 27 | + |
| 28 | + + (void) staticMethod:(int)len :(int * __counted_by(len) __noescape)p; |
| 29 | +@end |
| 30 | +#pragma clang debug dump(TestProtocol) |
| 31 | + |
| 32 | +//--- expansions.expected |
| 33 | +@__swiftmacro_So12TestProtocol015_SwiftifyImportB0fMe_.swift |
| 34 | +------------------------------ |
| 35 | +extension TestProtocol { |
| 36 | + /// This is an auto-generated wrapper for safer interop |
| 37 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload public |
| 38 | + func simple(_ p: inout MutableSpan<Int32>) { |
| 39 | + let len = Int32(exactly: p.count)! |
| 40 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 41 | + return unsafe simple(len, _pPtr.baseAddress!) |
| 42 | + } |
| 43 | + } |
| 44 | + /// This is an auto-generated wrapper for safer interop |
| 45 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p1: copy p1) @_lifetime(p2: copy p2) @_disfavoredOverload public |
| 46 | + func shared(_ p1: inout MutableSpan<Int32>, _ p2: inout MutableSpan<Int32>) { |
| 47 | + let len = Int32(exactly: p1.count)! |
| 48 | + if p2.count != len { |
| 49 | + fatalError("bounds check failure in shared: expected \(len) but got \(p2.count)") |
| 50 | + } |
| 51 | + return unsafe p2.withUnsafeMutableBufferPointer { _p2Ptr in |
| 52 | + return unsafe p1.withUnsafeMutableBufferPointer { _p1Ptr in |
| 53 | + return unsafe shared(len, _p1Ptr.baseAddress!, _p2Ptr.baseAddress!) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + /// This is an auto-generated wrapper for safer interop |
| 58 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload public |
| 59 | + func complexExpr(_ len: Int32, _ offset: Int32, _ p: inout MutableSpan<Int32>) { |
| 60 | + let _pCount = p.count |
| 61 | + if _pCount != len - offset { |
| 62 | + fatalError("bounds check failure in complexExpr: expected \(len - offset) but got \(_pCount)") |
| 63 | + } |
| 64 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 65 | + return unsafe complexExpr(len, offset, _pPtr.baseAddress!) |
| 66 | + } |
| 67 | + } |
| 68 | + /// This is an auto-generated wrapper for safer interop |
| 69 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload public |
| 70 | + func nullUnspecified(_ p: inout MutableSpan<Int32>) { |
| 71 | + let len = Int32(exactly: p.count)! |
| 72 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 73 | + return unsafe nullUnspecified(len, _pPtr.baseAddress!) |
| 74 | + } |
| 75 | + } |
| 76 | + /// This is an auto-generated wrapper for safer interop |
| 77 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload public |
| 78 | + func nonnull(_ p: inout MutableSpan<Int32>) { |
| 79 | + let len = Int32(exactly: p.count)! |
| 80 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 81 | + return unsafe nonnull(len, _pPtr.baseAddress!) |
| 82 | + } |
| 83 | + } |
| 84 | + /// This is an auto-generated wrapper for safer interop |
| 85 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload public |
| 86 | + func nullable(_ p: inout MutableSpan<Int32>?) { |
| 87 | + let len = Int32(exactly: p?.count ?? 0)! |
| 88 | + return { () in |
| 89 | + return if p == nil { |
| 90 | + unsafe nullable(len, nil) |
| 91 | + } else { |
| 92 | + unsafe p!.withUnsafeMutableBufferPointer { _pPtr in |
| 93 | + return unsafe nullable(len, _pPtr.baseAddress) |
| 94 | + } |
| 95 | + } |
| 96 | + }() |
| 97 | + } |
| 98 | + /// This is an auto-generated wrapper for safer interop |
| 99 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p1: copy p1) @_disfavoredOverload public |
| 100 | + func mixedEscapability(_ p1: inout MutableSpan<Int32>, _ p2: UnsafeMutableBufferPointer<Int32>) { |
| 101 | + let len = Int32(exactly: p1.count)! |
| 102 | + if p2.count != len { |
| 103 | + fatalError("bounds check failure in mixedEscapability: expected \(len) but got \(p2.count)") |
| 104 | + } |
| 105 | + return unsafe p1.withUnsafeMutableBufferPointer { _p1Ptr in |
| 106 | + return unsafe mixedEscapability(len, _p1Ptr.baseAddress!, p2.baseAddress!) |
| 107 | + } |
| 108 | + } |
| 109 | + /// This is an auto-generated wrapper for safer interop |
| 110 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 111 | + static public func staticMethod(_ p: inout MutableSpan<Int32>) { |
| 112 | + let len = Int32(exactly: p.count)! |
| 113 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 114 | + return unsafe staticMethod(len, _pPtr.baseAddress!) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +------------------------------ |
| 119 | +@__swiftmacro_So3foo15_SwiftifyImportfMp_.swift |
| 120 | +------------------------------ |
| 121 | +/// This is an auto-generated wrapper for safer interop |
| 122 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload public func foo(_ p: inout MutableSpan<Int32>) { |
| 123 | + let len = Int32(exactly: p.count)! |
| 124 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 125 | + return unsafe foo(len, _pPtr.baseAddress!) |
| 126 | + } |
| 127 | +} |
| 128 | +------------------------------ |
| 129 | +//--- test.swift |
| 130 | +import TestClang |
| 131 | + |
| 132 | +@inlinable |
| 133 | +public func call(p: inout MutableSpan<CInt>, p2: inout MutableSpan<CInt>, p3: inout MutableSpan<CInt>?, a: TestProtocol) { |
| 134 | + a.simple(&p) |
| 135 | + a.shared(&p, &p2) |
| 136 | + a.complexExpr(2, 3, &p) |
| 137 | + a.nullUnspecified(&p) |
| 138 | + a.nonnull(&p) |
| 139 | + a.nullable(&p3) |
| 140 | + foo(&p) |
| 141 | +} |
| 142 | + |
| 143 | +//--- module.modulemap |
| 144 | +module TestClang { |
| 145 | + header "test.h" |
| 146 | + export * |
| 147 | +} |
0 commit comments