-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Opaquepointer interop #78315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Opaquepointer interop #78315
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4005306
[SwiftifyImport] Add sizedBy support for OpaquePointer
hnrklssn 1a94e55
use swift-ide-test for checking interop signatures
hnrklssn 8801611
rename castPointerToTargetType
hnrklssn 0161391
enable Span in test
hnrklssn 8cee700
fix NullableSpan test
hnrklssn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,8 @@ module CountedByClang { | |
header "counted-by.h" | ||
export * | ||
} | ||
module SizedByClang { | ||
header "sized-by.h" | ||
export * | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#define __sized_by(x) __attribute__((__sized_by__(x))) | ||
|
||
void simple(int len, void * __sized_by(len) p); | ||
|
||
void swiftAttr(int len, void *p) __attribute__(( | ||
swift_attr("@_SwiftifyImport(.sizedBy(pointer: 2, size: \"len\"))"))); | ||
|
||
void shared(int len, void * __sized_by(len) p1, void * __sized_by(len) p2); | ||
|
||
void complexExpr(int len, int offset, void * __sized_by(len - offset) p); | ||
|
||
void nullUnspecified(int len, void * __sized_by(len) _Null_unspecified p); | ||
|
||
void nonnull(int len, void * __sized_by(len) _Nonnull p); | ||
|
||
void nullable(int len, void * __sized_by(len) _Nullable p); | ||
|
||
typedef struct foo opaque_t; | ||
void opaque(int len, opaque_t * __sized_by(len) p); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,19 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
|
||
// RUN: %target-swift-ide-test -print-module -module-to-print=CountedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s | ||
|
||
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -disable-availability-checking -plugin-path %swift-plugin-dir -o %t/CountedBy.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s | ||
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedBy.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s | ||
|
||
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __counted_by parameters. | ||
|
||
import CountedByClang | ||
|
||
@inlinable | ||
public func callSimple(_ p: UnsafeMutableBufferPointer<CInt>) { | ||
simple(p) | ||
} | ||
|
||
// Check that macros from swift_attr work the same as inferred macros. | ||
@inlinable | ||
public func callSwiftAttr(_ p: UnsafeMutableBufferPointer<CInt>) { | ||
swiftAttr(p) | ||
} | ||
|
||
@inlinable | ||
public func callShared(_ len: CInt, _ p1: UnsafeMutableBufferPointer<CInt>, _ p2: UnsafeMutableBufferPointer<CInt>) { | ||
shared(len, p1, p2) | ||
} | ||
|
||
@inlinable | ||
public func callComplexExpr(_ len: CInt, _ offset: CInt, _ p: UnsafeMutableBufferPointer<CInt>) { | ||
complexExpr(len, offset, p) | ||
} | ||
|
||
|
||
@inlinable | ||
public func callNullUnspecified(_ p: UnsafeMutableBufferPointer<CInt>) { | ||
nullUnspecified(p) | ||
} | ||
|
||
@inlinable | ||
public func callNonnull(_ p: UnsafeMutableBufferPointer<CInt>) { | ||
nonnull(p) | ||
} | ||
|
||
@inlinable | ||
public func callNullable(_ p: UnsafeMutableBufferPointer<CInt>?) { | ||
nullable(p) | ||
} | ||
|
||
// CHECK: @_alwaysEmitIntoClient public func complexExpr(_ len: Int{{.*}}, _ offset: Int{{.*}}, _ p: UnsafeMutableBufferPointer<Int{{.*}}>) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ p: UnsafeMutableBufferPointer<Int{{.*}}>) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ p: UnsafeMutableBufferPointer<Int{{.*}}>) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullable(_ p: UnsafeMutableBufferPointer<Int{{.*}}>?) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableBufferPointer<Int{{.*}}>, _ p2: UnsafeMutableBufferPointer<Int{{.*}}>) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ p: UnsafeMutableBufferPointer<Int{{.*}}>) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func swiftAttr(_ p: UnsafeMutableBufferPointer<Int{{.*}}>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// REQUIRES: swift_feature_SafeInteropWrappers | ||
|
||
// RUN: %target-swift-ide-test -print-module -module-to-print=SizedByClang -plugin-path %swift-plugin-dir -I %S/Inputs -source-filename=x -enable-experimental-feature SafeInteropWrappers | %FileCheck %s | ||
|
||
// swift-ide-test doesn't currently typecheck the macro expansions, so run the compiler as well | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/CountedBy.swiftmodule -I %S/Inputs -enable-experimental-feature SafeInteropWrappers %s | ||
|
||
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __sized_by parameters. | ||
import SizedByClang | ||
|
||
// CHECK: @_alwaysEmitIntoClient public func complexExpr(_ len: Int{{.*}}, _ offset: Int{{.*}}, _ p: UnsafeMutableRawBufferPointer) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ p: UnsafeMutableRawBufferPointer) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ p: UnsafeMutableRawBufferPointer) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullable(_ p: UnsafeMutableRawBufferPointer?) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func opaque(_ p: UnsafeRawBufferPointer) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ p: UnsafeMutableRawBufferPointer) | ||
// CHECK-NEXT: @_alwaysEmitIntoClient public func swiftAttr(_ p: UnsafeMutableRawBufferPointer) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// REQUIRES: swift_swift_parser | ||
// REQUIRES: swift_feature_Span | ||
|
||
// RUN: %target-typecheck-verify-swift -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -verify -enable-experimental-feature Span | ||
|
||
// XFAIL: * | ||
// expanded form errors with "type 'Span' does not conform to protocol 'Escapable'" because Optional doesn't support ~Escapable yet | ||
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1)) | ||
func nullableSpan(_ ptr: UnsafePointer<CInt>?, _ len: CInt) { | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// REQUIRES: swift_swift_parser | ||
|
||
// RUN: %target-typecheck-verify-swift -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -verify | ||
|
||
// expected-error@+2{{SizedBy only supported for raw pointers}} | ||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size")) | ||
func myFunc(_ ptr: UnsafePointer<Int>, _ size: CInt) { | ||
} | ||
|
||
// expected-error@+2{{raw pointers only supported for SizedBy}} | ||
@_SwiftifyImport(.countedBy(pointer: 1, count: "count")) | ||
func myFunc(_ ptr: UnsafeRawPointer, _ count: CInt) { | ||
} | ||
|
||
// expected-error@+2{{raw pointers only supported for SizedBy}} | ||
@_SwiftifyImport(.countedBy(pointer: 1, count: "count")) | ||
func myFunc(_ ptr: OpaquePointer, _ count: CInt) { | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// REQUIRES: swift_swift_parser | ||
// REQUIRES: swift_feature_Span | ||
|
||
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions -enable-experimental-feature Span -verify 2>&1 | %FileCheck --match-full-lines %s | ||
|
||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size")) | ||
func nonnullUnsafeRawBufferPointer(_ ptr: OpaquePointer, _ size: CInt) { | ||
} | ||
|
||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size")) | ||
func nullableUnsafeRawBufferPointer(_ ptr: OpaquePointer?, _ size: CInt) { | ||
} | ||
|
||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size")) | ||
func impNullableUnsafeRawBufferPointer(_ ptr: OpaquePointer!, _ size: CInt) { | ||
} | ||
|
||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size"), .nonescaping(pointer: 1)) | ||
func nonnullSpan(_ ptr: OpaquePointer, _ size: CInt) { | ||
} | ||
|
||
// expected-note@+2{{in expansion of macro '_SwiftifyImport' on global function 'nullableSpan' here}} | ||
// Cannot refer to source location for the error: "type 'RawSpan' does not conform to protocol 'Escapable'" (which is currently necessary for Optional) | ||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size"), .nonescaping(pointer: 1)) | ||
func nullableSpan(_ ptr: OpaquePointer?, _ size: CInt) { | ||
} | ||
|
||
@_SwiftifyImport(.sizedBy(pointer: 1, size: "size"), .nonescaping(pointer: 1)) | ||
func impNullableSpan(_ ptr: OpaquePointer!, _ size: CInt) { | ||
} | ||
|
||
// CHECK: @_alwaysEmitIntoClient | ||
// CHECK-NEXT: func nonnullUnsafeRawBufferPointer(_ ptr: UnsafeRawBufferPointer) { | ||
// CHECK-NEXT: return nonnullUnsafeRawBufferPointer(OpaquePointer(ptr.baseAddress!), CInt(exactly: ptr.count)!) | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: @_alwaysEmitIntoClient | ||
// CHECK-NEXT: func nullableUnsafeRawBufferPointer(_ ptr: UnsafeRawBufferPointer?) { | ||
// CHECK-NEXT: return nullableUnsafeRawBufferPointer(OpaquePointer(ptr?.baseAddress), CInt(exactly: ptr?.count ?? 0)!) | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: @_alwaysEmitIntoClient | ||
// CHECK-NEXT: func impNullableUnsafeRawBufferPointer(_ ptr: UnsafeRawBufferPointer) { | ||
// CHECK-NEXT: return impNullableUnsafeRawBufferPointer(OpaquePointer(ptr.baseAddress!), CInt(exactly: ptr.count)!) | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: @_alwaysEmitIntoClient | ||
// CHECK-NEXT: func nonnullSpan(_ ptr: RawSpan) { | ||
// CHECK-NEXT: return ptr.withUnsafeBytes { _ptrPtr in | ||
// CHECK-NEXT: return nonnullSpan(OpaquePointer(_ptrPtr.baseAddress!), CInt(exactly: ptr.byteCount)!) | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: @_alwaysEmitIntoClient | ||
// CHECK-NEXT: func nullableSpan(_ ptr: RawSpan?) { | ||
// CHECK-NEXT: return if ptr == nil { | ||
// CHECK-NEXT: nullableSpan(nil, CInt(exactly: ptr?.byteCount ?? 0)!) | ||
// CHECK-NEXT: } else { | ||
// CHECK-NEXT: ptr!.withUnsafeBytes { _ptrPtr in | ||
// CHECK-NEXT: return nullableSpan(OpaquePointer(_ptrPtr.baseAddress), CInt(exactly: ptr?.byteCount ?? 0)!) | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: } | ||
|
||
// CHECK: @_alwaysEmitIntoClient | ||
// CHECK-NEXT: func impNullableSpan(_ ptr: RawSpan) { | ||
// CHECK-NEXT: return ptr.withUnsafeBytes { _ptrPtr in | ||
// CHECK-NEXT: return impNullableSpan(OpaquePointer(_ptrPtr.baseAddress!), CInt(exactly: ptr.byteCount)!) | ||
// CHECK-NEXT: } | ||
// CHECK-NEXT: } | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.