Files
swift-mirror/test/Macros/SwiftifyImport/SizedBy/Opaque.swift
Henrik G. Olsson f5fa481205 [Swiftify] Always remove count parameters when possible (#81585)
Previously we did not remove count parameters if any count parameters
were shared between count expressions, or if any count expression
contained operations. Buffer sizes were also just checked to be larger
than or equal than the given count.

We now extract the count from Spans/BufferPointers whenever possible,
and store that value in a variable at the start of the function. If
multiple parameters share the same count, a bounds check is emitted to
make sure that they have the same size. Subspans can be used if one span
is larger than necessary.

The message in the bounds check is changed so that it includes the
expected and actual value, to aid in debugging.

This patch also fixes some incorrect indentation, and adds the
Whitespace.swift test case to act as a regression test in case the
indentation changes, since the other test cases don't use significant
whitespace.

rdar://151488820
rdar://151511090
rdar://146333006
rdar://147715799
2025-05-24 22:08:51 -07:00

75 lines
3.4 KiB
Swift

// REQUIRES: swift_swift_parser
// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -strict-memory-safety -warnings-as-errors -dump-macro-expansions -verify 2>&1 | %FileCheck --match-full-lines %s
@_SwiftifyImport(.sizedBy(pointer: .param(1), size: "size"))
func nonnullUnsafeRawBufferPointer(_ ptr: OpaquePointer, _ size: CInt) {
}
@_SwiftifyImport(.sizedBy(pointer: .param(1), size: "size"))
func nullableUnsafeRawBufferPointer(_ ptr: OpaquePointer?, _ size: CInt) {
}
@_SwiftifyImport(.sizedBy(pointer: .param(1), size: "size"))
func impNullableUnsafeRawBufferPointer(_ ptr: OpaquePointer!, _ size: CInt) {
}
@_SwiftifyImport(.sizedBy(pointer: .param(1), size: "size"), .nonescaping(pointer: .param(1)))
func nonnullSpan(_ ptr: OpaquePointer, _ size: CInt) {
}
@_SwiftifyImport(.sizedBy(pointer: .param(1), size: "size"), .nonescaping(pointer: .param(1)))
func nullableSpan(_ ptr: OpaquePointer?, _ size: CInt) {
}
@_SwiftifyImport(.sizedBy(pointer: .param(1), size: "size"), .nonescaping(pointer: .param(1)))
func impNullableSpan(_ ptr: OpaquePointer!, _ size: CInt) {
}
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func nonnullUnsafeRawBufferPointer(_ ptr: UnsafeRawBufferPointer) {
// CHECK-NEXT: let size = CInt(exactly: unsafe ptr.count)!
// CHECK-NEXT: return unsafe nonnullUnsafeRawBufferPointer(OpaquePointer(ptr.baseAddress!), size)
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func nullableUnsafeRawBufferPointer(_ ptr: UnsafeRawBufferPointer?) {
// CHECK-NEXT: let size = CInt(exactly: unsafe ptr?.count ?? 0)!
// CHECK-NEXT: return unsafe nullableUnsafeRawBufferPointer(OpaquePointer(ptr?.baseAddress), size)
// CHECK-NEXT: }
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func impNullableUnsafeRawBufferPointer(_ ptr: UnsafeRawBufferPointer) {
// CHECK-NEXT: let size = CInt(exactly: unsafe ptr.count)!
// CHECK-NEXT: return unsafe impNullableUnsafeRawBufferPointer(OpaquePointer(ptr.baseAddress!), size)
// CHECK-NEXT: }
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func nonnullSpan(_ ptr: RawSpan) {
// CHECK-NEXT: let size = CInt(exactly: ptr.byteCount)!
// CHECK-NEXT: return unsafe ptr.withUnsafeBytes { _ptrPtr in
// CHECK-NEXT: return unsafe nonnullSpan(OpaquePointer(_ptrPtr.baseAddress!), size)
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func nullableSpan(_ ptr: RawSpan?) {
// CHECK-NEXT: let size = CInt(exactly: ptr?.byteCount ?? 0)!
// CHECK-NEXT: return { () in
// CHECK-NEXT: return if ptr == nil {
// CHECK-NEXT: unsafe nullableSpan(nil, size)
// CHECK-NEXT: } else {
// CHECK-NEXT: unsafe ptr!.withUnsafeBytes { _ptrPtr in
// CHECK-NEXT: return unsafe nullableSpan(OpaquePointer(_ptrPtr.baseAddress), size)
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: }()
// CHECK-NEXT: }
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func impNullableSpan(_ ptr: RawSpan) {
// CHECK-NEXT: let size = CInt(exactly: ptr.byteCount)!
// CHECK-NEXT: return unsafe ptr.withUnsafeBytes { _ptrPtr in
// CHECK-NEXT: return unsafe impNullableSpan(OpaquePointer(_ptrPtr.baseAddress!), size)
// CHECK-NEXT: }
// CHECK-NEXT: }