Skip to content

Commit 83fe0ee

Browse files
authored
Merge pull request #58390 from rintaro/5.7-regex_diagnostics
[5.7][SwiftCompiler/Regex] Use bridged DiagnosticEngine for error reporting
2 parents eb493f8 + 2ab7d83 commit 83fe0ee

File tree

23 files changed

+377
-145
lines changed

23 files changed

+377
-145
lines changed

SwiftCompilerSources/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,23 @@ else()
251251

252252
endif()
253253

254+
# Configure 'SwiftCompilerModules' SwiftPM package. The 'Package.swift' will
255+
# be created at '${build_dir}/SwiftCompilerSources/Package.swift' and can be
256+
# built with 'swift-build'.
257+
# Note that this SwiftPM package itself is just for development purposes, and
258+
# is not actually used for the compiler building.
259+
set(swiftcompiler_source_dir_name "_Sources")
260+
configure_file(Package.swift.in
261+
"${CMAKE_CURRENT_BINARY_DIR}/Package.swift" @ONLY)
262+
# SwiftPM requires all sources are inside the directory of 'Package.swift'.
263+
# Create symlinks to the actual source directories.
264+
execute_process(COMMAND
265+
"${CMAKE_COMMAND}" -E create_symlink
266+
"${CMAKE_CURRENT_SOURCE_DIR}/Sources"
267+
"${CMAKE_CURRENT_BINARY_DIR}/${swiftcompiler_source_dir_name}")
268+
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
269+
execute_process(COMMAND
270+
"${CMAKE_COMMAND}" -E create_symlink
271+
"${EXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR}/Sources/_RegexParser"
272+
"${CMAKE_CURRENT_BINARY_DIR}/_RegexParser_Sources")
273+
endif()

SwiftCompilerSources/Package.swift

Lines changed: 0 additions & 51 deletions
This file was deleted.

SwiftCompilerSources/Package.swift.in

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// swift-tools-version:5.3
2+
//===--- Package.swift.in - SwiftCompiler SwiftPM package -----------------===//
3+
//
4+
// This source file is part of the Swift.org open source project
5+
//
6+
// Copyright (c) 2021 - 2022 Apple Inc. and the Swift project authors
7+
// Licensed under Apache License v2.0 with Runtime Library Exception
8+
//
9+
// See https://swift.org/LICENSE.txt for license information
10+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
// NOTE: This 'Package.swift.in' file is for CMake configure_file().
15+
// Generated 'Package.swift' can be found in
16+
// '${swift_build_dir}/SwiftCompilerSources/Package.swift'.
17+
18+
import PackageDescription
19+
20+
private extension Target {
21+
static let defaultSwiftSettings: [SwiftSetting] = [
22+
.unsafeFlags([
23+
"-Xfrontend", "-validate-tbd-against-ir=none",
24+
"-Xfrontend", "-enable-cxx-interop",
25+
// Bridging modules and headers
26+
"-Xcc", "-I", "-Xcc", "@SWIFT_SOURCE_DIR@/include",
27+
// Generated C headers
28+
"-Xcc", "-I", "-Xcc", "@CMAKE_BINARY_DIR@/include",
29+
"-cross-module-optimization"
30+
]),
31+
]
32+
33+
static func compilerModuleTarget(
34+
name: String,
35+
dependencies: [Dependency],
36+
path: String? = nil,
37+
sources: [String]? = nil,
38+
swiftSettings: [SwiftSetting] = []) -> Target {
39+
.target(
40+
name: name,
41+
dependencies: dependencies,
42+
path: path ?? "@swiftcompiler_source_dir_name@/\(name)",
43+
exclude: ["CMakeLists.txt"],
44+
sources: sources,
45+
swiftSettings: defaultSwiftSettings + swiftSettings)
46+
}
47+
}
48+
49+
let package = Package(
50+
name: "SwiftCompilerSources",
51+
platforms: [
52+
.macOS("10.9"),
53+
],
54+
products: [
55+
.library(
56+
name: "swiftCompilerModules",
57+
type: .static,
58+
targets: ["Basic", "AST", "Parse", "SIL", "Optimizer", "_CompilerRegexParser"]),
59+
],
60+
dependencies: [
61+
],
62+
// Note that targets and their dependencies must align with
63+
// 'SwiftCompilerSources/Sources/CMakeLists.txt'
64+
targets: [
65+
.compilerModuleTarget(
66+
name: "_CompilerRegexParser",
67+
dependencies: [],
68+
path: "_RegexParser_Sources",
69+
swiftSettings: [
70+
// Workaround until `_CompilerRegexParser` is imported as implementation-only
71+
// by `_StringProcessing`.
72+
.unsafeFlags([
73+
"-Xfrontend",
74+
"-disable-implicit-string-processing-module-import"
75+
])]),
76+
.compilerModuleTarget(
77+
name: "Basic",
78+
dependencies: []),
79+
.compilerModuleTarget(
80+
name: "AST",
81+
dependencies: ["Basic"]),
82+
.compilerModuleTarget(
83+
name: "Parse",
84+
dependencies: ["Basic", "AST", "_CompilerRegexParser"]),
85+
.compilerModuleTarget(
86+
name: "SIL",
87+
dependencies: ["Basic"]),
88+
.compilerModuleTarget(
89+
name: "Optimizer",
90+
dependencies: ["Basic", "SIL", "Parse"]),
91+
]
92+
)

SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public struct DiagnosticEngine {
6767
public init(bridged: BridgedDiagnosticEngine) {
6868
self.bridged = bridged
6969
}
70+
public init?(bridged: BridgedOptionalDiagnosticEngine) {
71+
guard let object = bridged.object else {
72+
return nil
73+
}
74+
self.bridged = BridgedDiagnosticEngine(object: object)
75+
}
7076

7177
public func diagnose(_ position: SourceLoc?,
7278
_ id: DiagID,

SwiftCompilerSources/Sources/Basic/SourceLoc.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ public struct SourceLoc {
3333
}
3434
}
3535

36+
extension SourceLoc {
37+
public func advanced(by n: Int) -> SourceLoc {
38+
SourceLoc(locationInFile: locationInFile.advanced(by: n))!
39+
}
40+
}
41+
3642
extension Optional where Wrapped == SourceLoc {
3743
public var bridged: BridgedSourceLoc {
3844
self?.bridged ?? .init(pointer: nil)

SwiftCompilerSources/Sources/CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
# NOTE: Subdirectories must be added in dependency order.
1010

11-
add_subdirectory(Basic)
12-
add_subdirectory(AST)
1311
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
1412
add_subdirectory(_RegexParser)
1513
endif()
14+
add_subdirectory(Basic)
15+
add_subdirectory(AST)
16+
add_subdirectory(Parse)
1617
add_subdirectory(SIL)
1718
add_subdirectory(Optimizer)

SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
set(dependencies)
10-
list(APPEND dependencies Basic SIL)
11-
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
12-
list(APPEND dependencies _CompilerRegexParser)
13-
endif()
10+
list(APPEND dependencies Basic SIL Parse)
1411

1512
add_swift_compiler_module(Optimizer DEPENDS ${dependencies})
1613

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,13 @@
1212

1313
import SIL
1414
import OptimizerBridging
15-
16-
#if canImport(_CompilerRegexParser)
17-
import _CompilerRegexParser
18-
#endif
15+
import Parse
1916

2017
@_cdecl("initializeSwiftModules")
2118
public func initializeSwiftModules() {
2219
registerSILClasses()
2320
registerSwiftPasses()
24-
25-
#if canImport(_CompilerRegexParser)
2621
registerRegexParser()
27-
#endif
2822
}
2923

3024
private func registerPass(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2022 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
set(dependencies Basic AST)
10+
if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER)
11+
list(APPEND dependencies _CompilerRegexParser)
12+
endif()
13+
14+
add_swift_compiler_module(Parse
15+
DEPENDS
16+
${dependencies}
17+
SOURCES
18+
Regex.swift)

0 commit comments

Comments
 (0)