|
| 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 -o %t/test.swiftmodule %t/test.swift -I %t -enable-experimental-feature SafeInteropWrappers -strict-memory-safety \ |
| 7 | +// RUN: -verify -verify-additional-file %t%{fs-sep}foo.h -verify-additional-file %t%{fs-sep}bar.h -verify-additional-file %t%{fs-sep}baz.h -verify-additional-file %t%{fs-sep}qux.h -Rmacro-expansions |
| 8 | + |
| 9 | +// Macro expansions in foo.h do not have access to the definition of `struct qux`, |
| 10 | +// so don't attach macro on functions that use contain that type in foo.h. |
| 11 | +// bar.h imports the type, so attach the macro. |
| 12 | +// baz.h imports bar.h which re-exports the type, so attach the macro there too. |
| 13 | + |
| 14 | +//--- foo.h |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#define __counted_by(x) __attribute__((__counted_by__(x))) |
| 18 | + |
| 19 | +struct qux; |
| 20 | +// expected-note@+1{{'foo' declared here}} |
| 21 | +void foo(struct qux *x, int * __counted_by(len) p, int len); |
| 22 | +// expected-note@+1{{'fooReturn' declared here}} |
| 23 | +struct qux * fooReturn(int * __counted_by(len) p, int len); |
| 24 | + |
| 25 | + |
| 26 | +//--- bar.h |
| 27 | +#pragma once |
| 28 | +#include "qux.h" |
| 29 | + |
| 30 | +#define __counted_by(x) __attribute__((__counted_by__(x))) |
| 31 | + |
| 32 | +// expected-expansion@+10:59{{ |
| 33 | +// expected-remark@1{{macro content: |/// This is an auto-generated wrapper for safer interop|}} |
| 34 | +// expected-remark@2{{macro content: |@_alwaysEmitIntoClient @_disfavoredOverload public func bar(_ x: UnsafeMutablePointer<qux>!, _ p: UnsafeMutableBufferPointer<Int32>) {|}} |
| 35 | +// expected-remark@3{{macro content: | let len = Int32(exactly: p.count)!|}} |
| 36 | +// expected-remark@4{{macro content: | return unsafe bar(x, p.baseAddress!, len)|}} |
| 37 | +// expected-remark@5{{macro content: |}|}} |
| 38 | +// }} |
| 39 | +// expected-expansion@+3:6{{ |
| 40 | +// expected-note@1 5{{in expansion of macro '_SwiftifyImport' on global function 'bar' here}} |
| 41 | +// }} |
| 42 | +void bar(struct qux *x, int * __counted_by(len) p, int len); |
| 43 | +// expected-expansion@+10:58{{ |
| 44 | +// expected-remark@1{{macro content: |/// This is an auto-generated wrapper for safer interop|}} |
| 45 | +// expected-remark@2{{macro content: |@_alwaysEmitIntoClient @_disfavoredOverload public func barReturn(_ p: UnsafeMutableBufferPointer<Int32>) -> UnsafeMutablePointer<qux>! {|}} |
| 46 | +// expected-remark@3{{macro content: | let len = Int32(exactly: p.count)!|}} |
| 47 | +// expected-remark@4{{macro content: | return unsafe barReturn(p.baseAddress!, len)|}} |
| 48 | +// expected-remark@5{{macro content: |}|}} |
| 49 | +// }} |
| 50 | +// expected-expansion@+3:14{{ |
| 51 | +// expected-note@1 5{{in expansion of macro '_SwiftifyImport' on global function 'barReturn' here}} |
| 52 | +// }} |
| 53 | +struct qux * barReturn(int * __counted_by(len) p, int len); |
| 54 | + |
| 55 | + |
| 56 | +//--- baz.h |
| 57 | +#pragma once |
| 58 | + |
| 59 | +#include "bar.h" |
| 60 | + |
| 61 | +struct qux; |
| 62 | +// expected-expansion@+10:59{{ |
| 63 | +// expected-remark@1{{macro content: |/// This is an auto-generated wrapper for safer interop|}} |
| 64 | +// expected-remark@2{{macro content: |@_alwaysEmitIntoClient @_disfavoredOverload public func baz(_ x: UnsafeMutablePointer<qux>!, _ p: UnsafeMutableBufferPointer<Int32>) {|}} |
| 65 | +// expected-remark@3{{macro content: | let len = Int32(exactly: p.count)!|}} |
| 66 | +// expected-remark@4{{macro content: | return unsafe baz(x, p.baseAddress!, len)|}} |
| 67 | +// expected-remark@5{{macro content: |}|}} |
| 68 | +// }} |
| 69 | +// expected-expansion@+3:6{{ |
| 70 | +// expected-note@1 5{{in expansion of macro '_SwiftifyImport' on global function 'baz' here}} |
| 71 | +// }} |
| 72 | +void baz(struct qux *x, int * __counted_by(len) p, int len); |
| 73 | +// expected-expansion@+10:58{{ |
| 74 | +// expected-remark@1{{macro content: |/// This is an auto-generated wrapper for safer interop|}} |
| 75 | +// expected-remark@2{{macro content: |@_alwaysEmitIntoClient @_disfavoredOverload public func bazReturn(_ p: UnsafeMutableBufferPointer<Int32>) -> UnsafeMutablePointer<qux>! {|}} |
| 76 | +// expected-remark@3{{macro content: | let len = Int32(exactly: p.count)!|}} |
| 77 | +// expected-remark@4{{macro content: | return unsafe bazReturn(p.baseAddress!, len)|}} |
| 78 | +// expected-remark@5{{macro content: |}|}} |
| 79 | +// }} |
| 80 | +// expected-expansion@+3:14{{ |
| 81 | +// expected-note@1 5{{in expansion of macro '_SwiftifyImport' on global function 'bazReturn' here}} |
| 82 | +// }} |
| 83 | +struct qux * bazReturn(int * __counted_by(len) p, int len); |
| 84 | + |
| 85 | + |
| 86 | +//--- qux.h |
| 87 | +#pragma once |
| 88 | + |
| 89 | +struct qux { int placeholder; }; |
| 90 | + |
| 91 | + |
| 92 | +//--- test.swift |
| 93 | +import Foo |
| 94 | +import Bar |
| 95 | +import Baz |
| 96 | + |
| 97 | +func callFoo(_ x: UnsafeMutablePointer<qux>, _ p: UnsafeMutablePointer<CInt>, _ len: CInt) { |
| 98 | + unsafe foo(x, p, len) |
| 99 | + let _: UnsafeMutablePointer<qux> = unsafe fooReturn(p, len) |
| 100 | +} |
| 101 | + |
| 102 | +func callFoo2(_ x: UnsafeMutablePointer<qux>, _ p: UnsafeMutableBufferPointer<CInt>) { |
| 103 | + // expected-error@+2{{missing argument for parameter #3 in call}} |
| 104 | + // expected-error@+1{{cannot convert value of type 'UnsafeMutableBufferPointer<CInt>' (aka 'UnsafeMutableBufferPointer<Int32>') to expected argument type 'UnsafeMutablePointer<Int32>'}} |
| 105 | + unsafe foo(x, p) |
| 106 | + // expected-error@+2{{missing argument for parameter #2 in call}} |
| 107 | + // expected-error@+1{{cannot convert value of type 'UnsafeMutableBufferPointer<CInt>' (aka 'UnsafeMutableBufferPointer<Int32>') to expected argument type 'UnsafeMutablePointer<Int32>'}} |
| 108 | + let _: UnsafeMutablePointer<qux> = unsafe fooReturn(p) |
| 109 | +} |
| 110 | + |
| 111 | + |
| 112 | +func callBar(_ x: UnsafeMutablePointer<qux>, _ p: UnsafeMutablePointer<CInt>, _ len: CInt) { |
| 113 | + unsafe bar(x, p, len) |
| 114 | + let _: UnsafeMutablePointer<qux> = unsafe barReturn(p, len) |
| 115 | +} |
| 116 | + |
| 117 | +func callBar2(_ x: UnsafeMutablePointer<qux>, _ p: UnsafeMutableBufferPointer<CInt>) { |
| 118 | + unsafe bar(x, p) |
| 119 | + let _: UnsafeMutablePointer<qux> = unsafe barReturn(p) |
| 120 | +} |
| 121 | + |
| 122 | + |
| 123 | +func callBaz(_ x: UnsafeMutablePointer<qux>, _ p: UnsafeMutablePointer<CInt>, _ len: CInt) { |
| 124 | + unsafe baz(x, p, len) |
| 125 | + let _: UnsafeMutablePointer<qux> = unsafe bazReturn(p, len) |
| 126 | +} |
| 127 | + |
| 128 | +func callBaz2(_ x: UnsafeMutablePointer<qux>, _ p: UnsafeMutableBufferPointer<CInt>) { |
| 129 | + unsafe baz(x, p) |
| 130 | + let _: UnsafeMutablePointer<qux> = unsafe bazReturn(p) |
| 131 | +} |
| 132 | + |
| 133 | + |
| 134 | +//--- module.modulemap |
| 135 | +module Foo { |
| 136 | + header "foo.h" |
| 137 | +} |
| 138 | +module Bar { |
| 139 | + header "bar.h" |
| 140 | + export qux |
| 141 | +} |
| 142 | +module Baz { |
| 143 | + header "baz.h" |
| 144 | + export Bar |
| 145 | +} |
| 146 | +module qux { |
| 147 | + header "qux.h" |
| 148 | +} |
0 commit comments