Skip to content

Commit 0678829

Browse files
hnrklssnDougGregor
andauthored
Add @PointerBounds macro (#76969)
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 be of type Int). rdar://137628612 --------- Co-authored-by: Doug Gregor <[email protected]>
1 parent fc18223 commit 0678829

Some content is hidden

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

51 files changed

+1339
-0
lines changed

CMakeLists.txt

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

750+
option(SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS
751+
"Enable experimental safe wrappers around external functions"
752+
FALSE)
753+
750754
cmake_dependent_option(SWIFT_BUILD_SOURCEKIT
751755
"Build SourceKit" TRUE
752756
"SWIFT_ENABLE_DISPATCH" FALSE)
@@ -1399,6 +1403,7 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
13991403
message(STATUS "Observation Support: ${SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION}")
14001404
message(STATUS "Synchronization Support: ${SWIFT_ENABLE_SYNCHRONIZATION}")
14011405
message(STATUS "Volatile Support: ${SWIFT_ENABLE_VOLATILE}")
1406+
message(STATUS "Pointer Bounds Support: ${SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS}")
14021407
message(STATUS "")
14031408
else()
14041409
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)