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 <dgregor@apple.com>
This commit is contained in:
Henrik G. Olsson
2024-11-11 14:54:25 -08:00
committed by GitHub
parent fc18223576
commit 0678829cf7
51 changed files with 1339 additions and 0 deletions

View File

@@ -326,6 +326,10 @@ function(_add_target_variant_swift_compile_flags
list(APPEND result "-D" "SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION")
endif()
if(SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS)
list(APPEND result "-D" "SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS")
endif()
if(SWIFT_ENABLE_SYNCHRONIZATION)
list(APPEND result "-D" "SWIFT_ENABLE_SYNCHRONIZATION")
endif()