Skip to content

[SwiftCompiler] Use bridged DiagnosticEngine to emit regex literal parsing #41492

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

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 13 additions & 0 deletions SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ function(add_swift_compiler_modules_library name)
list(APPEND swift_compile_options "-O" "-cross-module-optimization")
endif()

if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
list(APPEND swift_compile_options "-D" "SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING")
endif()

get_bootstrapping_path(build_dir ${CMAKE_CURRENT_BINARY_DIR} "${ALS_BOOTSTRAPPING}")

set(sdk_option "")
Expand Down Expand Up @@ -251,3 +255,12 @@ else()

endif()

# Configure SwiftPM package.

set(swiftcompiler_source_dir_name "_Sources")
configure_file(Package.swift.in
"${CMAKE_CURRENT_BINARY_DIR}/Package.swift" @ONLY)
# SwiftPM requires all sources are inside the directory of 'Package.swift'.
execute_process(COMMAND
"${CMAKE_COMMAND}" -E create_symlink
"${CMAKE_CURRENT_SOURCE_DIR}/Sources" "${CMAKE_CURRENT_BINARY_DIR}/${swiftcompiler_source_dir_name}")
43 changes: 0 additions & 43 deletions SwiftCompilerSources/Package.swift

This file was deleted.

68 changes: 68 additions & 0 deletions SwiftCompilerSources/Package.swift.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// swift-tools-version:5.3
//===--- Package.swift.in - SwiftCompiler SwiftPM package -----------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

// NOTE: This 'Package.swift.in' file is for CMake configure_file().
// Generated 'Package.swift' can be found in
// '${swift_build_dir}/SwiftCompilerSources/Package.swift'.


import PackageDescription

var swiftSettings: [SwiftSetting] = [
.unsafeFlags([
"-Xfrontend", "-validate-tbd-against-ir=none",
"-Xfrontend", "-enable-cxx-interop",
// Bridging modules and headers
"-Xcc", "-I", "-Xcc", "@SWIFT_SOURCE_DIR@/include",
// Generated C headers
"-Xcc", "-I", "-Xcc", "@CMAKE_BINARY_DIR@/include",
"-cross-module-optimization"
]),
]

if "@SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING@" == "TRUE" {
swiftSettings.append(.define("SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING"))
}

func getTarget(_ name: String, dependencies: [Target.Dependency]) -> Target {
.target(
name: name,
dependencies: dependencies,
path: "@swiftcompiler_source_dir_name@/\(name)",
exclude: ["CMakeLists.txt"],
swiftSettings: swiftSettings)
}

let package = Package(
name: "SwiftCompilerSources",
platforms: [
.macOS("10.9"),
],
products: [
.library(
name: "SwiftCompiler",
type: .static,
targets: ["Basic", "AST", "Parse", "SIL", "Optimizer"]),
],
dependencies: [
],
// Note that all modules must be added to LIBSWIFT_MODULES in the top-level
// CMakeLists.txt file to get debugging working.
targets: [
getTarget("Basic", dependencies: []),
getTarget("AST", dependencies: ["Basic"]),
getTarget("Parse", dependencies: ["Basic", "AST"]),
getTarget("SIL", dependencies: ["Basic"]),
getTarget("Optimizer", dependencies: ["Basic", "SIL", "Parse"]),
]
)
14 changes: 14 additions & 0 deletions SwiftCompilerSources/Sources/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2022 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See http://swift.org/LICENSE.txt for license information
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_swift_compiler_module(AST
DEPENDS
Basic
SOURCES
DiagnosticEngine.swift)

117 changes: 117 additions & 0 deletions SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//===--- DiagnosticEngine.swift -------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

@_exported import ASTBridging
import Basic

public typealias DiagID = BridgedDiagID

extension BridgedDiagnosticArgument {
init(stringRef val: BridgedStringRef) {
self.init(kind: .stringRef, value: .init(stringRefValue: val))
}
init(int val: Int) {
self.init(kind: .int, value: .init(intValue: val))
}
init(uInt val: UInt) {
self.init(kind: .uInt, value: .init(uintValue: val))
}
}

public protocol DiagnosticArgument {
func withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void)
}
extension String: DiagnosticArgument {
public func withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
withBridgedStringRef { fn(BridgedDiagnosticArgument(stringRef: $0)) }
}
}
extension Int: DiagnosticArgument {
public func withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
fn(BridgedDiagnosticArgument(int: self))
}
}
extension UInt: DiagnosticArgument {
public func withBridgedDiagnosticArgument(_ fn: (BridgedDiagnosticArgument) -> Void) {
fn(BridgedDiagnosticArgument(uInt: self))
}
}

public struct DiagnosticFixIt {
public var start: SourceLoc
public var byteLength: Int
public var text: String

public init(start: SourceLoc, byteLength: Int, replacement text: String) {
self.start = start
self.byteLength = byteLength
self.text = text
}

func withBridgedDiagnosticFixIt(_ fn: (BridgedDiagnosticFixIt) -> Void) {
text.withBridgedStringRef { bridgedTextRef in
let bridgedDiagnosticFixIt = BridgedDiagnosticFixIt(
start: start.bridged,
byteLength: byteLength,
text: bridgedTextRef)
fn(bridgedDiagnosticFixIt)
}
}
}

public struct DiagnosticEngine {
private var bridged: BridgedDiagnosticEngine

public init(bridged: BridgedDiagnosticEngine) {
self.bridged = bridged
}

public func diagnose(_ position: SourceLoc?,
_ id: DiagID,
_ args: [DiagnosticArgument],
highlight: CharSourceRange? = nil,
fixIts: [DiagnosticFixIt] = []) {

let bridgedSourceLoc: BridgedSourceLoc = position.bridged
let bridgedHighlightRange: BridgedCharSourceRange = highlight.bridged
var bridgedArgs: [BridgedDiagnosticArgument] = []
var bridgedFixIts: [BridgedDiagnosticFixIt] = []

var closure: () -> Void = {
bridgedArgs.withBridgedArrayRef { bridgedArgsRef in
bridgedFixIts.withBridgedArrayRef { bridgedFixItsRef in
DiagnosticEngine_diagnose(bridged, bridgedSourceLoc,
id, bridgedArgsRef,
bridgedHighlightRange, bridgedFixItsRef)
}
}
}
for arg in args {
closure = { [closure, arg] in
arg.withBridgedDiagnosticArgument { bridgedArg in
bridgedArgs.append(bridgedArg)
closure()
}
}
}
for fixIt in fixIts {
closure = { [closure, fixIt] in
fixIt.withBridgedDiagnosticFixIt { bridgedFixIt in
bridgedFixIts.append(bridgedFixIt)
closure()
}
}
}

closure()
}
}
1 change: 1 addition & 0 deletions SwiftCompilerSources/Sources/Basic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

add_swift_compiler_module(Basic
SourceLoc.swift
Utils.swift)
73 changes: 73 additions & 0 deletions SwiftCompilerSources/Sources/Basic/SourceLoc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//===--- SourceLoc.swift - SourceLoc bridiging utilities ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

public struct SourceLoc {
public var pointer: UnsafePointer<UInt8>

public init?(pointer: UnsafePointer<UInt8>?) {
guard let pointer = pointer else {
return nil
}
self.pointer = pointer
}

public init?(pointer: UnsafePointer<CChar>?) {
guard let pointer = pointer else {
return nil
}
self.pointer = UnsafeRawPointer(pointer).assumingMemoryBound(to: UInt8.self)
}

public init?(bridged: BridgedSourceLoc) {
guard let pointer = bridged.pointer else {
return nil
}
self.init(pointer: pointer)
}

public var bridged: BridgedSourceLoc {
.init(pointer: pointer)
}
}

extension Optional where Wrapped == SourceLoc {
public var bridged: BridgedSourceLoc {
self?.bridged ?? .init(pointer: nil)
}
}

public struct CharSourceRange {
public var start: SourceLoc
public var byteLength: Int

public init(start: SourceLoc, byteLength: Int) {
self.start = start
self.byteLength = byteLength
}

public init?(bridged: BridgedCharSourceRange) {
guard let start = SourceLoc(bridged: bridged.start) else {
return nil
}
self.init(start: start, byteLength: bridged.byteLength)
}

public var bridged: BridgedCharSourceRange {
.init(start: start.bridged, byteLength: byteLength)
}
}

extension Optional where Wrapped == CharSourceRange {
public var bridged: BridgedCharSourceRange {
self?.bridged ?? .init(start: .init(pointer: nil), byteLength: 0)
}
}
5 changes: 2 additions & 3 deletions SwiftCompilerSources/Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
# NOTE: Subdirectories must be added in dependency order.

add_subdirectory(Basic)
if(SWIFT_ENABLE_EXPERIMENTAL_STRING_PROCESSING)
add_subdirectory(ExperimentalRegex)
endif()
add_subdirectory(AST)
add_subdirectory(Parse)
add_subdirectory(SIL)
add_subdirectory(Optimizer)
21 changes: 0 additions & 21 deletions SwiftCompilerSources/Sources/ExperimentalRegex/CMakeLists.txt

This file was deleted.

6 changes: 0 additions & 6 deletions SwiftCompilerSources/Sources/ExperimentalRegex/Regex.swift

This file was deleted.

Loading