Skip to content

Commit 20ad631

Browse files
committed
Add @PointerBounds macro
@PointerBounds is a macro intended to be applied by ClangImporter when importing functions with pointer parameters from C headers. By leveraging C attributes we can get insight into bounds, esapability, and (eventually) lifetimes of pointers, allowing us to map them to safe(r) and more ergonomic types than UnsafePointer. This initial macro implementation supports CountedBy and Sizedby, but not yet EndedBy. It can generate function overloads with and without an explicit count parameter, as well as with UnsafeBufferPointer or Span (if marked nonescaping), and any of their combinations. It supports nullable/optional pointers, and both mutable and immutable pointers. It supports arbitrary count expressions. These are passed to the macro as a string literal since any parameters referred to in the count expression will not have been declared yet when parsing the macro. It does not support indirect pointers or inout parameters. It supports functions with return values, but returned pointers can not be bounds checked yet. Bounds checked pointers must be of type Unsafe[Mutable]Pointer[?]<T> or Unsafe[Mutable]RawPointer[?]. Count expressions must conform to the BinaryInteger protocol, and have an initializer with signature "init(exactly: Int) -> T?" (or accept a supertype of Int). rdar://137628612
1 parent 4e3f665 commit 20ad631

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1179
-0
lines changed

CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,10 @@ option(SWIFT_ENABLE_EXPERIMENTAL_PARSER_VALIDATION
748748
"Enable experimental SwiftParser validation by default"
749749
FALSE)
750750

751+
option(SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS
752+
"Enable experimental safe wrappers around external functions"
753+
FALSE)
754+
751755
cmake_dependent_option(SWIFT_BUILD_SOURCEKIT
752756
"Build SourceKit" TRUE
753757
"SWIFT_ENABLE_DISPATCH" FALSE)
@@ -1400,6 +1404,7 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
14001404
message(STATUS "Observation Support: ${SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION}")
14011405
message(STATUS "Synchronization Support: ${SWIFT_ENABLE_SYNCHRONIZATION}")
14021406
message(STATUS "Volatile Support: ${SWIFT_ENABLE_VOLATILE}")
1407+
message(STATUS "Pointer Bounds Support: ${SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS}")
14031408
message(STATUS "")
14041409
else()
14051410
message(STATUS "Not building Swift standard library, SDK overlays, and runtime")

lib/Macros/Sources/SwiftMacros/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ add_swift_macro_library(SwiftMacros
1616
DistributedResolvableMacro.swift
1717
SyntaxExtensions.swift
1818
TaskLocalMacro.swift
19+
PointerBoundsMacro.swift
1920
SWIFT_DEPENDENCIES
2021
SwiftDiagnostics
2122
SwiftSyntax

0 commit comments

Comments
 (0)