Skip to content

[SwiftCompiler/Regex] Use bridged DiagnosticEngine for error reporting #42583

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 2 commits into from
Apr 25, 2022
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
20 changes: 20 additions & 0 deletions SwiftCompilerSources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,3 +251,23 @@ else()

endif()

# Configure 'SwiftCompilerModules' SwiftPM package. The 'Package.swift' will
# be created at '${build_dir}/SwiftCompilerSources/Package.swift' and can be
# built with 'swift-build'.
# Note that this SwiftPM package itself is just for development purposes, and
# is not actually used for the compiler building.
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'.
# Create symlinks to the actual source directories.
execute_process(COMMAND
"${CMAKE_COMMAND}" -E create_symlink
"${CMAKE_CURRENT_SOURCE_DIR}/Sources"
"${CMAKE_CURRENT_BINARY_DIR}/${swiftcompiler_source_dir_name}")
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
execute_process(COMMAND
"${CMAKE_COMMAND}" -E create_symlink
"${EXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR}/Sources/_RegexParser"
"${CMAKE_CURRENT_BINARY_DIR}/_RegexParser_Sources")
endif()
51 changes: 0 additions & 51 deletions SwiftCompilerSources/Package.swift

This file was deleted.

92 changes: 92 additions & 0 deletions SwiftCompilerSources/Package.swift.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// 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) 2021 - 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

private extension Target {
static let defaultSwiftSettings: [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"
]),
]

static func compilerModuleTarget(
name: String,
dependencies: [Dependency],
path: String? = nil,
sources: [String]? = nil,
swiftSettings: [SwiftSetting] = []) -> Target {
.target(
name: name,
dependencies: dependencies,
path: path ?? "@swiftcompiler_source_dir_name@/\(name)",
exclude: ["CMakeLists.txt"],
sources: sources,
swiftSettings: defaultSwiftSettings + swiftSettings)
}
}

let package = Package(
name: "SwiftCompilerSources",
platforms: [
.macOS("10.9"),
],
products: [
.library(
name: "swiftCompilerModules",
type: .static,
targets: ["Basic", "AST", "Parse", "SIL", "Optimizer", "_CompilerRegexParser"]),
],
dependencies: [
],
// Note that targets and their dependencies must align with
// 'SwiftCompilerSources/Sources/CMakeLists.txt'
targets: [
.compilerModuleTarget(
name: "_CompilerRegexParser",
dependencies: [],
path: "_RegexParser_Sources",
swiftSettings: [
// Workaround until `_CompilerRegexParser` is imported as implementation-only
// by `_StringProcessing`.
.unsafeFlags([
"-Xfrontend",
"-disable-implicit-string-processing-module-import"
])]),
.compilerModuleTarget(
name: "Basic",
dependencies: []),
.compilerModuleTarget(
name: "AST",
dependencies: ["Basic"]),
.compilerModuleTarget(
name: "Parse",
dependencies: ["Basic", "AST", "_CompilerRegexParser"]),
.compilerModuleTarget(
name: "SIL",
dependencies: ["Basic"]),
.compilerModuleTarget(
name: "Optimizer",
dependencies: ["Basic", "SIL", "Parse"]),
]
)
6 changes: 6 additions & 0 deletions SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public struct DiagnosticEngine {
public init(bridged: BridgedDiagnosticEngine) {
self.bridged = bridged
}
public init?(bridged: BridgedOptionalDiagnosticEngine) {
guard let object = bridged.object else {
return nil
}
self.bridged = BridgedDiagnosticEngine(object: object)
}

public func diagnose(_ position: SourceLoc?,
_ id: DiagID,
Expand Down
6 changes: 6 additions & 0 deletions SwiftCompilerSources/Sources/Basic/SourceLoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public struct SourceLoc {
}
}

extension SourceLoc {
public func advanced(by n: Int) -> SourceLoc {
SourceLoc(locationInFile: locationInFile.advanced(by: n))!
}
}

extension Optional where Wrapped == SourceLoc {
public var bridged: BridgedSourceLoc {
self?.bridged ?? .init(pointer: nil)
Expand Down
5 changes: 3 additions & 2 deletions SwiftCompilerSources/Sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

# NOTE: Subdirectories must be added in dependency order.

add_subdirectory(Basic)
add_subdirectory(AST)
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
add_subdirectory(_RegexParser)
endif()
add_subdirectory(Basic)
add_subdirectory(AST)
add_subdirectory(Parse)
add_subdirectory(SIL)
add_subdirectory(Optimizer)
5 changes: 1 addition & 4 deletions SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors

set(dependencies)
list(APPEND dependencies Basic SIL)
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
list(APPEND dependencies _CompilerRegexParser)
endif()
list(APPEND dependencies Basic SIL Parse)

add_swift_compiler_module(Optimizer DEPENDS ${dependencies})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,13 @@

import SIL
import OptimizerBridging

#if canImport(_CompilerRegexParser)
import _CompilerRegexParser
#endif
import Parse

@_cdecl("initializeSwiftModules")
public func initializeSwiftModules() {
registerSILClasses()
registerSwiftPasses()

#if canImport(_CompilerRegexParser)
registerRegexParser()
#endif
}

private func registerPass(
Expand Down
18 changes: 18 additions & 0 deletions SwiftCompilerSources/Sources/Parse/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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

set(dependencies Basic AST)
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
list(APPEND dependencies _CompilerRegexParser)
endif()

add_swift_compiler_module(Parse
DEPENDS
${dependencies}
SOURCES
Regex.swift)
Loading