Skip to content

Rename pointer bounds #78210

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 6 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -747,10 +747,6 @@ option(SWIFT_ENABLE_EXPERIMENTAL_PARSER_VALIDATION
"Enable experimental SwiftParser validation by default"
FALSE)

option(SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS
"Enable experimental safe wrappers around external functions"
FALSE)

cmake_dependent_option(SWIFT_BUILD_SOURCEKIT
"Build SourceKit" TRUE
"SWIFT_ENABLE_DISPATCH" FALSE)
Expand Down Expand Up @@ -1403,7 +1399,6 @@ if(SWIFT_BUILD_STDLIB OR SWIFT_BUILD_SDK_OVERLAY)
message(STATUS "Observation Support: ${SWIFT_ENABLE_EXPERIMENTAL_OBSERVATION}")
message(STATUS "Synchronization Support: ${SWIFT_ENABLE_SYNCHRONIZATION}")
message(STATUS "Volatile Support: ${SWIFT_ENABLE_VOLATILE}")
message(STATUS "Pointer Bounds Support: ${SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS}")
message(STATUS "")
else()
message(STATUS "Not building Swift standard library, SDK overlays, and runtime")
Expand Down
1 change: 0 additions & 1 deletion Runtimes/Core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
# Catalyst Support
# -- Will need shadow invocations to generate swiftmodules for Swift parts
# Install *.abi.json, swiftdoc, and swiftsourceinfo
# Pointer bounds (SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS)

cmake_minimum_required(VERSION 3.26...3.29)

Expand Down
5 changes: 0 additions & 5 deletions include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -792,11 +792,6 @@ def enable_experimental_concise_pound_file : Flag<["-"],
Flags<[FrontendOption, ModuleInterfaceOption]>,
HelpText<"Enable experimental concise '#file' identifier">;

def enable_experimental_bounds_safety_interop :
Flag<["-"], "enable-experimental-bounds-safety-interop">,
Flags<[NoDriverOption, FrontendOption, HelpHidden, ModuleInterfaceOption]>,
HelpText<"Enable experimental C bounds safety interop code generation and config directives">;

def enable_experimental_cxx_interop :
Flag<["-"], "enable-experimental-cxx-interop">,
Flags<[NoDriverOption, FrontendOption, HelpHidden]>,
Expand Down
10 changes: 5 additions & 5 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8624,16 +8624,16 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
}

namespace {
class PointerParamInfoPrinter {
class SwiftifyInfoPrinter {
public:
clang::ASTContext &ctx;
llvm::raw_ostream &out;
bool firstParam = true;
PointerParamInfoPrinter(clang::ASTContext &ctx, llvm::raw_ostream &out)
SwiftifyInfoPrinter(clang::ASTContext &ctx, llvm::raw_ostream &out)
: ctx(ctx), out(out) {
out << "@PointerBounds(";
out << "@_SwiftifyImport(";
}
~PointerParamInfoPrinter() { out << ")"; }
~SwiftifyInfoPrinter() { out << ")"; }

void printCountedBy(const clang::CountAttributedType *CAT,
size_t pointerIndex) {
Expand Down Expand Up @@ -8676,7 +8676,7 @@ void ClangImporter::Implementation::importBoundsAttributes(
{
llvm::raw_svector_ostream out(MacroString);

PointerParamInfoPrinter printer(getClangASTContext(), out);
SwiftifyInfoPrinter printer(getClangASTContext(), out);
for (auto [index, param] : llvm::enumerate(ClangDecl->parameters())) {
if (auto CAT = param->getType()->getAs<clang::CountAttributedType>()) {
printer.printCountedBy(CAT, index);
Expand Down
2 changes: 1 addition & 1 deletion lib/Macros/Sources/SwiftMacros/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_swift_macro_library(SwiftMacros
DistributedResolvableMacro.swift
SyntaxExtensions.swift
TaskLocalMacro.swift
PointerBoundsMacro.swift
SwiftifyImportMacro.swift
SWIFT_DEPENDENCIES
SwiftDiagnostics
SwiftSyntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,12 +461,12 @@ func getParameterIndexForDeclRef(
/// Intended to map to C attributes like __counted_by, __ended_by and __no_escape,
/// for automatic application by ClangImporter when the C declaration is annotated
/// appropriately.
public struct PointerBoundsMacro: PeerMacro {
public struct SwiftifyImportMacro: PeerMacro {
static func parseEnumName(_ enumConstructorExpr: FunctionCallExprSyntax) throws -> String {
guard let calledExpr = enumConstructorExpr.calledExpression.as(MemberAccessExprSyntax.self)
else {
throw DiagnosticError(
"expected PointerParam enum literal as argument, got '\(enumConstructorExpr)'",
"expected _SwiftifyInfo enum literal as argument, got '\(enumConstructorExpr)'",
node: enumConstructorExpr)
}
return calledExpr.declName.baseName.text
Expand Down Expand Up @@ -564,7 +564,7 @@ public struct PointerBoundsMacro: PeerMacro {
let paramExpr = paramAST.expression
guard let enumConstructorExpr = paramExpr.as(FunctionCallExprSyntax.self) else {
throw DiagnosticError(
"expected PointerParam enum literal as argument, got '\(paramExpr)'", node: paramExpr)
"expected _SwiftifyInfo enum literal as argument, got '\(paramExpr)'", node: paramExpr)
}
let enumName = try parseEnumName(enumConstructorExpr)
switch enumName {
Expand Down Expand Up @@ -627,7 +627,7 @@ public struct PointerBoundsMacro: PeerMacro {
}
if argByIndex[i] != nil {
throw DiagnosticError(
"multiple PointerParams referring to parameter with index "
"multiple _SwiftifyInfos referring to parameter with index "
+ "\(i): \(pointerArg) and \(argByIndex[i]!)", node: pointerArg.original)
}
argByIndex[i] = pointerArg
Expand All @@ -647,7 +647,7 @@ public struct PointerBoundsMacro: PeerMacro {
) throws -> [DeclSyntax] {
do {
guard let funcDecl = declaration.as(FunctionDeclSyntax.self) else {
throw DiagnosticError("@PointerBounds only works on functions", node: declaration)
throw DiagnosticError("@_SwiftifyImport only works on functions", node: declaration)
}

let argumentList = node.arguments!.as(LabeledExprListSyntax.self)!
Expand Down Expand Up @@ -692,7 +692,7 @@ public struct PointerBoundsMacro: PeerMacro {
case .attribute(let attr):
// don't apply this macro recursively, and avoid dupe _alwaysEmitIntoClient
let name = attr.attributeName.as(IdentifierTypeSyntax.self)?.name.text
return name == nil || (name != "PointerBounds" && name != "_alwaysEmitIntoClient")
return name == nil || (name != "_SwiftifyImport" && name != "_alwaysEmitIntoClient")
default: return true
}
} + [
Expand Down
4 changes: 0 additions & 4 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ 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()
Expand Down
6 changes: 1 addition & 5 deletions stdlib/public/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ split_embedded_sources(
EMBEDDED Int128.swift
EMBEDDED Mirror.swift
NORMAL PlaygroundDisplay.swift
EMBEDDED SwiftifyImport.swift
NORMAL CommandLine.swift
EMBEDDED SliceBuffer.swift
EMBEDDED StaticBigInt.swift
Expand Down Expand Up @@ -263,11 +264,6 @@ if(SWIFT_STDLIB_ENABLE_VECTOR_TYPES)
list(APPEND SWIFTLIB_EMBEDDED_GYB_SOURCES SIMDConcreteOperations.swift.gyb SIMDVectorTypes.swift.gyb)
endif()

if (SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS)
list(APPEND SWIFTLIB_SOURCES PointerBounds.swift)
list(APPEND SWIFTLIB_EMBEDDED_SOURCES PointerBounds.swift)
endif()

# Freestanding and Linux/Android builds both have failures to resolve.
if(NOT BOOTSTRAPPING_MODE STREQUAL "OFF" AND NOT SWIFT_FREESTANDING_FLAVOR AND NOT SWIFT_HOST_VARIANT_SDK STREQUAL "LINUX" AND NOT SWIFT_HOST_VARIANT_SDK STREQUAL "ANDROID")
list(APPEND SWIFTLIB_SOURCES ObjectIdentifier+DebugDescription.swift)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/GroupInfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
],
"Pointer": [
"Pointer.swift",
"PointerBounds.swift",
"SwiftifyImport.swift",
"TemporaryAllocation.swift",
"UnsafePointer.swift",
"UnsafeRawPointer.swift",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/// Different ways to annotate pointer parameters using the `@PointerBounds` macro.
/// Different ways to annotate pointer parameters using the `@_SwiftifyImport` macro.
/// All indices into parameter lists start at 1. Indices __must__ be integer literals, and strings
/// __must__ be string literals, because their contents are parsed by the `@PointerBounds` macro.
/// __must__ be string literals, because their contents are parsed by the `@_SwiftifyImport` macro.
/// Only 1 instance of `countedBy`, `sizedBy` or `endedBy` can refer to each pointer index, however
/// `nonescaping` is orthogonal to the rest and can (and should) overlap with other annotations.
public enum PointerParam {
///
/// This is not marked @available, because _SwiftifyImport is available for any target. Instances
/// of _SwiftifyInfo should ONLY be passed as arguments directly to _SwiftifyImport, so they should
/// not affect linkage since there are never any instances at runtime.
public enum _SwiftifyInfo {
/// Corresponds to the C `__counted_by(count)` attribute.
/// Parameter pointer: index of pointer in function parameter list. Must be of type
/// `Unsafe[Mutable]Pointer<T>[?]`, i.e. not an `UnsafeRawPointer`.
Expand Down Expand Up @@ -40,5 +44,5 @@ public enum PointerParam {
/// Parameter paramInfo: information about how the function uses the pointer passed to it. The
/// safety of the generated wrapper function depends on this info being extensive and accurate.
@attached(peer, names: overloaded)
public macro PointerBounds(_ paramInfo: PointerParam...) =
#externalMacro(module: "SwiftMacros", type: "PointerBoundsMacro")
public macro _SwiftifyImport(_ paramInfo: _SwiftifyInfo...) =
#externalMacro(module: "SwiftMacros", type: "SwiftifyImportMacro")
4 changes: 0 additions & 4 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -457,10 +457,6 @@ foreach(SDK ${SWIFT_SDKS})
list(APPEND LIT_ARGS "--param" "string_processing")
endif()

if(SWIFT_ENABLE_EXPERIMENTAL_POINTER_BOUNDS)
list(APPEND LIT_ARGS "--param" "pointer_bounds")
endif()

if(SWIFT_ENABLE_BACKTRACING)
list(APPEND LIT_ARGS "--param" "backtracing")
endif()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

void simple(int len, int * __counted_by(len) p);

void swiftAttr(int len, int * p) __attribute__((swift_attr("@PointerBounds(.countedBy(pointer: 2, count: \"len\"))")));
void swiftAttr(int len, int *p) __attribute__((
swift_attr("@_SwiftifyImport(.countedBy(pointer: 2, count: \"len\"))")));

void shared(int len, int * __counted_by(len) p1, int * __counted_by(len) p2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// REQUIRES: swift_feature_SafeInteropWrappers
// REQUIRES: pointer_bounds

// 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

// Check that ClangImporter correctly infers and expands @PointerBounds macros for functions with __counted_by parameters.
// Check that ClangImporter correctly infers and expands @_SwiftifyImport macros for functions with __counted_by parameters.

import CountedByClang

Expand Down
9 changes: 0 additions & 9 deletions test/Macros/PointerBounds/MacroErrors/SamePointer.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "size * count"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "size * count"))
func myFunc(_ ptr: UnsafePointer<CInt>, _ size: CInt, _ count: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"), .countedBy(pointer: 3, count: "len2"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"), .countedBy(pointer: 3, count: "len2"))
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt, _ ptr2: UnsafePointer<CInt>, _ len2: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func myFunc(_ ptr: UnsafeMutablePointer<CInt>, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds
// REQUIRES: swift_feature_Span

// RUN: not %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 > %t.log 2>&1
// RUN: %FileCheck --match-full-lines %s < %t.log

@PointerBounds(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
func myFunc(_ ptr: UnsafeMutablePointer<CInt>, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func myFunc(_ ptr: UnsafePointer<CInt>?, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func foo(_ ptr: Swift.UnsafePointer<Swift.Int>, _ len: Swift.Int) -> Swift.Void {
}

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func bar(_ ptr: Swift.UnsafePointer<Swift.CInt>, _ len: Swift.Int) -> () {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt) -> CInt {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"), .countedBy(pointer: 2, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"), .countedBy(pointer: 2, count: "len"))
func myFunc(_ ptr: UnsafePointer<CInt>, _ ptr2: UnsafePointer<CInt>, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds
// 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 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds
// 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 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"), .nonescaping(pointer: 1))
func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt) -> CInt {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-swift-frontend %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -dump-macro-expansions 2>&1 | %FileCheck --match-full-lines %s

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
func myFunc(_ ptr: UnsafePointer<CInt>!, _ len: CInt) {
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// REQUIRES: swift_swift_parser
// REQUIRES: pointer_bounds

// RUN: %target-typecheck-verify-swift %s -swift-version 5 -module-name main -disable-availability-checking -typecheck -plugin-path %swift-plugin-dir -verify

@PointerBounds(.countedBy(pointer: 1, count: "len"))
@_SwiftifyImport(.countedBy(pointer: 1, count: "len"))
// expected-error@+1{{expected pointer type, got [CInt] with kind arrayType}}
func myFunc(_ ptr: [CInt], _ len: String) {
}
Loading