From 13e34dd817d23c98b4fa5aa8d337a057d61489fd Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Mon, 13 Nov 2023 19:35:44 -0800 Subject: [PATCH 1/7] [ASTGen] Move regex literal parsing from SwiftCompilerSources to ASTGen ASTGen always builds with the host Swift compiler, without requiring bootstrapping, and is enabled in more places. Move the regex literal parsing logic there so it is enabled in more host environments, and makes use of CMake's Swift support. Enable all of the regex literal tests when ASTGen is built, to ensure everything is working. Remove the "AST" and "Parse" Swift modules from SwiftCompilerSources, because they are no longer needed. (cherry picked from commit 47f18d492e31f8f05b8be3a5630e5c54899ecbc5) Conflicts: SwiftCompilerSources/CMakeLists.txt SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift include/swift/Bridging/ASTGen.h lib/ASTGen/Package.swift lib/ASTGen/Sources/ASTGen/Bridge.swift lib/ASTGen/Sources/ASTGen/Regex.swift lib/Parse/Lexer.cpp lib/Parse/ParseRegex.cpp --- SwiftCompilerSources/CMakeLists.txt | 8 -- SwiftCompilerSources/Package.swift | 21 +-- .../Sources/AST/CMakeLists.txt | 14 -- .../Sources/AST/DiagnosticEngine.swift | 122 ------------------ .../Sources/Basic/SourceLoc.swift | 2 +- SwiftCompilerSources/Sources/CMakeLists.txt | 5 - .../Sources/Optimizer/CMakeLists.txt | 2 +- .../PassManager/PassRegistration.swift | 2 - .../Sources/Parse/CMakeLists.txt | 19 --- .../Sources/Parse/Parse.swift | 16 --- .../Sources/_RegexParser/CMakeLists.txt | 20 --- SwiftCompilerSources/_RegexParser_Sources | 1 - SwiftCompilerSources/stubs.cpp | 3 - include/swift/AST/DiagnosticsParse.def | 7 - include/swift/Basic/InitializeSwiftModules.h | 1 - lib/ASTGen/CMakeLists.txt | 22 ++++ lib/ASTGen/Package.swift | 9 +- .../ASTGen/Sources/ASTGen}/Regex.swift | 89 ++++++++----- lib/ASTGen/_RegexParser_Sources | 1 + lib/Parse/CMakeLists.txt | 7 + lib/Parse/Lexer.cpp | 22 ++-- lib/Parse/ParseRegex.cpp | 32 ++--- test/IDE/complete_regex.swift | 2 +- .../SourceKit/CodeComplete/rdar95772803.swift | 2 +- .../Frontend/enable-flag.swift | 2 +- .../Parse/forward-slash-regex-disabled.swift | 2 +- ...forward-slash-regex-skipping-allowed.swift | 2 +- ...forward-slash-regex-skipping-invalid.swift | 2 +- .../Parse/forward-slash-regex-skipping.swift | 2 +- .../Parse/forward-slash-regex.swift | 2 +- .../StringProcessing/Parse/prefix-slash.swift | 2 +- test/StringProcessing/Parse/regex.swift | 2 +- .../Parse/regex_parse_end_of_buffer.swift | 2 +- .../Parse/regex_parse_error.swift | 2 +- .../Runtime/regex_basic.swift | 2 +- .../SILGen/regex_literal_silgen.swift | 2 +- ...gex_builder_fix_import_after_imports.swift | 2 +- .../Sema/regex_builder_fix_import_decl.swift | 2 +- .../regex_builder_fix_import_top_level.swift | 2 +- .../Sema/regex_builder_unavailable.swift | 2 +- .../Sema/regex_literal_availability.swift | 2 +- .../Sema/regex_literal_diagnostics.swift | 2 +- .../Sema/regex_literal_type_inference.swift | 2 +- .../Sema/string_processing_import.swift | 2 +- test/stmt/then_stmt.swift | 2 +- tools/libSwiftScan/libSwiftScan.cpp | 4 - 46 files changed, 147 insertions(+), 328 deletions(-) delete mode 100644 SwiftCompilerSources/Sources/AST/CMakeLists.txt delete mode 100644 SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift delete mode 100644 SwiftCompilerSources/Sources/Parse/CMakeLists.txt delete mode 100644 SwiftCompilerSources/Sources/Parse/Parse.swift delete mode 100644 SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt delete mode 120000 SwiftCompilerSources/_RegexParser_Sources rename {SwiftCompilerSources/Sources/Parse => lib/ASTGen/Sources/ASTGen}/Regex.swift (66%) create mode 120000 lib/ASTGen/_RegexParser_Sources diff --git a/SwiftCompilerSources/CMakeLists.txt b/SwiftCompilerSources/CMakeLists.txt index 138d208d9a3a9..88e8bf32f5558 100644 --- a/SwiftCompilerSources/CMakeLists.txt +++ b/SwiftCompilerSources/CMakeLists.txt @@ -239,17 +239,9 @@ else() " #include \"Basic/BridgedSwiftObject.h\" #include \"Basic/BasicBridging.h\" -#include \"Basic/SourceLoc.h\" - -#include \"AST/ASTBridging.h\" -#include \"AST/DiagnosticEngine.h\" -#include \"AST/DiagnosticConsumer.h\" - #include \"SIL/SILBridging.h\" #include \"SILOptimizer/OptimizerBridging.h\" - -#include \"Parse/RegexParserBridging.h\" ") add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" diff --git a/SwiftCompilerSources/Package.swift b/SwiftCompilerSources/Package.swift index 3b1a3817cb686..5785c411c1907 100644 --- a/SwiftCompilerSources/Package.swift +++ b/SwiftCompilerSources/Package.swift @@ -48,39 +48,22 @@ let package = Package( .library( name: "swiftCompilerModules", type: .static, - targets: ["Basic", "AST", "Parse", "SIL", "Optimizer", "_CompilerRegexParser"]), + targets: ["Basic", "SIL", "Optimizer"]), ], 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"]), + dependencies: ["Basic", "SIL"]), ], cxxLanguageStandard: .cxx17 ) diff --git a/SwiftCompilerSources/Sources/AST/CMakeLists.txt b/SwiftCompilerSources/Sources/AST/CMakeLists.txt deleted file mode 100644 index 3d814cad0a6c4..0000000000000 --- a/SwiftCompilerSources/Sources/AST/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -# 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) - diff --git a/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift b/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift deleted file mode 100644 index 69d4c8b77f8a7..0000000000000 --- a/SwiftCompilerSources/Sources/AST/DiagnosticEngine.swift +++ /dev/null @@ -1,122 +0,0 @@ -//===--- 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 -// -//===----------------------------------------------------------------------===// - -import ASTBridging - -import Basic - -public typealias DiagID = BridgedDiagID - -public protocol DiagnosticArgument { - func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) -} -extension String: DiagnosticArgument { - public func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) { - _withStringRef { fn(swift.DiagnosticArgument($0)) } - } -} -extension Int: DiagnosticArgument { - public func _withBridgedDiagnosticArgument(_ fn: (swift.DiagnosticArgument) -> Void) { - fn(swift.DiagnosticArgument(Int32(self))) - } -} - -public struct DiagnosticFixIt { - public let start: SourceLoc - public let byteLength: Int - public let text: String - - public init(start: SourceLoc, byteLength: Int, replacement text: String) { - self.start = start - self.byteLength = byteLength - self.text = text - } - - func withBridgedDiagnosticFixIt(_ fn: (swift.DiagnosticInfo.FixIt) -> Void) { - text._withStringRef { bridgedTextRef in - let bridgedDiagnosticFixIt = swift.DiagnosticInfo.FixIt( - swift.CharSourceRange(start.bridged, UInt32(byteLength)), - bridgedTextRef, - ArrayRefOfDiagnosticArgument()) - fn(bridgedDiagnosticFixIt) - } - } -} - -public struct DiagnosticEngine { - private let bridged: BridgedDiagnosticEngine - - 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, - _ args: [DiagnosticArgument], - highlight: CharSourceRange? = nil, - fixIts: [DiagnosticFixIt] = []) { - - let bridgedSourceLoc: swift.SourceLoc = position.bridged - let bridgedHighlightRange: swift.CharSourceRange = highlight.bridged - var bridgedArgs: [swift.DiagnosticArgument] = [] - var bridgedFixIts: [swift.DiagnosticInfo.FixIt] = [] - - // Build a higher-order function to wrap every 'withBridgedXXX { ... }' - // calls, so we don't escape anything from the closure. 'bridgedArgs' and - // 'bridgedFixIts' are temporary storage to store bridged values. So they - // should not be used after the closure is executed. - - var closure: () -> Void = { - bridgedArgs.withBridgedArrayRef { bridgedArgsRef in - bridgedFixIts.withBridgedArrayRef { bridgedFixItsRef in - DiagnosticEngine_diagnose(bridged, bridgedSourceLoc, - id, bridgedArgsRef, - bridgedHighlightRange, bridgedFixItsRef) - } - } - } - // 'reversed()' because the closure should be wrapped in that order. - for arg in args.reversed() { - closure = { [closure, arg] in - arg._withBridgedDiagnosticArgument { bridgedArg in - bridgedArgs.append(bridgedArg) - closure() - } - } - } - // 'reversed()' because the closure should be wrapped in that order. - for fixIt in fixIts.reversed() { - closure = { [closure, fixIt] in - fixIt.withBridgedDiagnosticFixIt { bridgedFixIt in - bridgedFixIts.append(bridgedFixIt) - closure() - } - } - } - - closure() - } - - public func diagnose(_ position: SourceLoc?, - _ id: DiagID, - _ args: DiagnosticArgument..., - highlight: CharSourceRange? = nil, - fixIts: DiagnosticFixIt...) { - diagnose(position, id, args, highlight: highlight, fixIts: fixIts) - } -} diff --git a/SwiftCompilerSources/Sources/Basic/SourceLoc.swift b/SwiftCompilerSources/Sources/Basic/SourceLoc.swift index aa1094dd78679..0b7df7a03506b 100644 --- a/SwiftCompilerSources/Sources/Basic/SourceLoc.swift +++ b/SwiftCompilerSources/Sources/Basic/SourceLoc.swift @@ -10,7 +10,7 @@ // //===----------------------------------------------------------------------===// -import ASTBridging +import BasicBridging public struct SourceLoc { /// Points into a source file. diff --git a/SwiftCompilerSources/Sources/CMakeLists.txt b/SwiftCompilerSources/Sources/CMakeLists.txt index af6900ff8d0e9..622fa883889a9 100644 --- a/SwiftCompilerSources/Sources/CMakeLists.txt +++ b/SwiftCompilerSources/Sources/CMakeLists.txt @@ -8,11 +8,6 @@ # NOTE: Subdirectories must be added in dependency order. -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) diff --git a/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt b/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt index acce165e1ee79..48b74e373e115 100644 --- a/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt +++ b/SwiftCompilerSources/Sources/Optimizer/CMakeLists.txt @@ -7,7 +7,7 @@ # See http://swift.org/CONTRIBUTORS.txt for Swift project authors set(dependencies) -list(APPEND dependencies Basic SIL Parse) +list(APPEND dependencies Basic SIL) add_swift_compiler_module(Optimizer DEPENDS ${dependencies}) diff --git a/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift b/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift index 060adf01abf53..13f1b7fe7d106 100644 --- a/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift +++ b/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift @@ -12,14 +12,12 @@ import SIL import OptimizerBridging -import Parse @_cdecl("initializeSwiftModules") public func initializeSwiftModules() { registerSILClasses() registerSwiftAnalyses() registerSwiftPasses() - initializeSwiftParseModules() } private func registerPass( diff --git a/SwiftCompilerSources/Sources/Parse/CMakeLists.txt b/SwiftCompilerSources/Sources/Parse/CMakeLists.txt deleted file mode 100644 index 7e5c1c7d97717..0000000000000 --- a/SwiftCompilerSources/Sources/Parse/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -# 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 - Parse.swift - Regex.swift) diff --git a/SwiftCompilerSources/Sources/Parse/Parse.swift b/SwiftCompilerSources/Sources/Parse/Parse.swift deleted file mode 100644 index a8a79489fc5a8..0000000000000 --- a/SwiftCompilerSources/Sources/Parse/Parse.swift +++ /dev/null @@ -1,16 +0,0 @@ -//===--- Parse.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 -// -//===----------------------------------------------------------------------===// - -@_cdecl("initializeSwiftParseModules") -public func initializeSwiftParseModules() { - registerRegexParser() -} diff --git a/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt b/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt deleted file mode 100644 index d202c421e84d1..0000000000000 --- a/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt +++ /dev/null @@ -1,20 +0,0 @@ -# This source file is part of the Swift.org open source project -# -# Copyright (c) 2014 - 2021 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 - -file(GLOB_RECURSE _LIBSWIFT_REGEX_PARSER_SOURCES - "${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}/Sources/_RegexParser/*.swift") -set(LIBSWIFT_REGEX_PARSER_SOURCES) -foreach(source ${_LIBSWIFT_REGEX_PARSER_SOURCES}) - file(TO_CMAKE_PATH "${source}" source) - list(APPEND LIBSWIFT_REGEX_PARSER_SOURCES ${source}) -endforeach() -message(STATUS "Using Experimental String Processing library for libswift _RegexParser (${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}).") - -add_swift_compiler_module(_CompilerRegexParser - SOURCES - "${LIBSWIFT_REGEX_PARSER_SOURCES}") diff --git a/SwiftCompilerSources/_RegexParser_Sources b/SwiftCompilerSources/_RegexParser_Sources deleted file mode 120000 index 8c4799dab65a6..0000000000000 --- a/SwiftCompilerSources/_RegexParser_Sources +++ /dev/null @@ -1 +0,0 @@ -../../swift-experimental-string-processing/Sources/_RegexParser \ No newline at end of file diff --git a/SwiftCompilerSources/stubs.cpp b/SwiftCompilerSources/stubs.cpp index 36df93a14d970..075c963152dd7 100644 --- a/SwiftCompilerSources/stubs.cpp +++ b/SwiftCompilerSources/stubs.cpp @@ -13,9 +13,6 @@ extern "C" { void initializeSwiftModules(); -void initializeSwiftParseModules(); - } void initializeSwiftModules() {} -void initializeSwiftParseModules() {} diff --git a/include/swift/AST/DiagnosticsParse.def b/include/swift/AST/DiagnosticsParse.def index 4a404cc797611..7ce37a4bfd25f 100644 --- a/include/swift/AST/DiagnosticsParse.def +++ b/include/swift/AST/DiagnosticsParse.def @@ -2065,13 +2065,6 @@ ERROR(expected_closure_literal,none, ERROR(expected_multiple_closures_block_rbrace,none, "expected '}' at the end of a trailing closures block", ()) -//------------------------------------------------------------------------------ -// MARK: diagnostics emitted by Swift libraries -//------------------------------------------------------------------------------ - -ERROR(foreign_diagnostic,none, - "%0", (StringRef)) - //------------------------------------------------------------------------------ // MARK: macros //------------------------------------------------------------------------------ diff --git a/include/swift/Basic/InitializeSwiftModules.h b/include/swift/Basic/InitializeSwiftModules.h index e627525be031a..d7984af4c4e0f 100644 --- a/include/swift/Basic/InitializeSwiftModules.h +++ b/include/swift/Basic/InitializeSwiftModules.h @@ -18,7 +18,6 @@ extern "C" { #endif void initializeSwiftModules(); -void initializeSwiftParseModules(); #ifdef __cplusplus } diff --git a/lib/ASTGen/CMakeLists.txt b/lib/ASTGen/CMakeLists.txt index 2628124156dfd..cddbf0d083920 100644 --- a/lib/ASTGen/CMakeLists.txt +++ b/lib/ASTGen/CMakeLists.txt @@ -5,6 +5,26 @@ add_pure_swift_host_library(swiftLLVMJSON STATIC EMIT_MODULE swiftBasic ) +set(ASTGen_Swift_dependencies) + +# If requested, build the regular expression parser into the compiler itself. +if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER) + file(GLOB_RECURSE _COMPILER_REGEX_PARSER_SOURCES + "${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}/Sources/_RegexParser/*.swift") + set(COMPILER_REGEX_PARSER_SOURCES) + foreach(source ${_COMPILER_REGEX_PARSER_SOURCES}) + file(TO_CMAKE_PATH "${source}" source) + list(APPEND COMPILER_REGEX_PARSER_SOURCES ${source}) + endforeach() + message(STATUS "Using Experimental String Processing library for _CompilerRegexParser (${SWIFT_PATH_TO_STRING_PROCESSING_SOURCE}).") + + add_pure_swift_host_library(_CompilerRegexParser STATIC EMIT_MODULE + "${COMPILER_REGEX_PARSER_SOURCES}" + ) + + list(APPEND ASTGen_Swift_dependencies _CompilerRegexParser) +endif() + add_pure_swift_host_library(swiftASTGen STATIC Sources/ASTGen/ASTGen.swift Sources/ASTGen/Decls.swift @@ -15,6 +35,7 @@ add_pure_swift_host_library(swiftASTGen STATIC Sources/ASTGen/Macros.swift Sources/ASTGen/Misc.swift Sources/ASTGen/PluginHost.swift + Sources/ASTGen/Regex.swift Sources/ASTGen/SourceFile.swift Sources/ASTGen/SourceManager.swift Sources/ASTGen/SourceManager+MacroExpansionContext.swift @@ -35,4 +56,5 @@ add_pure_swift_host_library(swiftASTGen STATIC SwiftSyntaxMacros SwiftSyntaxMacroExpansion swiftLLVMJSON + ${ASTGen_Swift_dependencies} ) diff --git a/lib/ASTGen/Package.swift b/lib/ASTGen/Package.swift index 151ea07f960ba..455e841cda11b 100644 --- a/lib/ASTGen/Package.swift +++ b/lib/ASTGen/Package.swift @@ -42,7 +42,8 @@ let package = Package( .product(name: "SwiftSyntaxBuilder", package: "swift-syntax"), .product(name: "SwiftSyntaxMacros", package: "swift-syntax"), .product(name: "SwiftSyntaxMacroExpansion", package: "swift-syntax"), - "swiftLLVMJSON" + "swiftLLVMJSON", + "_CompilerRegexParser", ], path: "Sources/ASTGen", swiftSettings: swiftSetttings @@ -53,5 +54,11 @@ let package = Package( path: "Sources/LLVMJSON", swiftSettings: swiftSetttings ), + .target( + name: "_CompilerRegexParser", + dependencies: [], + path: "_RegexParser_Sources", + swiftSettings: swiftSetttings + ), ] ) diff --git a/SwiftCompilerSources/Sources/Parse/Regex.swift b/lib/ASTGen/Sources/ASTGen/Regex.swift similarity index 66% rename from SwiftCompilerSources/Sources/Parse/Regex.swift rename to lib/ASTGen/Sources/ASTGen/Regex.swift index a2229bc6fbc24..e4d3187b96000 100644 --- a/SwiftCompilerSources/Sources/Parse/Regex.swift +++ b/lib/ASTGen/Sources/ASTGen/Regex.swift @@ -10,28 +10,22 @@ // //===----------------------------------------------------------------------===// -import _RegexParserBridging -import AST -import Basic +import CASTBridging +import CBasicBridging #if canImport(_CompilerRegexParser) @_spi(CompilerInterface) import _CompilerRegexParser -func registerRegexParser() { - Parser_registerRegexLiteralParsingFn(_RegexLiteralParsingFn) - Parser_registerRegexLiteralLexingFn(_RegexLiteralLexingFn) -} - /// Bridging between C++ lexer and swiftCompilerLexRegexLiteral. /// /// Attempt to lex a regex literal string. /// /// - Parameters: -/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be +/// - curPtrPtr: A pointer to the current pointer of lexer, which should be /// the start of the literal. This will be advanced to the point /// at which the lexer should resume, or will remain the same if /// this is not a regex literal. -/// - BufferEndPtr: A pointer to the end of the buffer, which should not be +/// - bufferEndPtr: A pointer to the end of the buffer, which should not be /// lexed past. /// - mustBeRegex: A bool value whether an error during lexing should be /// considered a regex literal, or some thing else. If true @@ -42,12 +36,13 @@ func registerRegexParser() { /// - Returns: A bool indicating whether lexing was completely erroneous, and /// cannot be recovered from, or false if there either was no error, /// or there was a recoverable error. -private func _RegexLiteralLexingFn( +@_cdecl("swift_ASTGen_lexRegexLiteral") +public func _RegexLiteralLexingFn( _ curPtrPtr: UnsafeMutablePointer>, _ bufferEndPtr: UnsafePointer, - _ mustBeRegex: CBool, - _ bridgedDiagnosticEngine: BridgedOptionalDiagnosticEngine -) -> /*CompletelyErroneous*/ CBool { + _ mustBeRegex: Bool, + _ bridgedDiagnosticEnginePtr: UnsafeMutableRawPointer? +) -> /*CompletelyErroneous*/ Bool { let inputPtr = curPtrPtr.pointee guard let (resumePtr, error) = swiftCompilerLexRegexLiteral( @@ -62,10 +57,18 @@ private func _RegexLiteralLexingFn( if let error = error { // Emit diagnostic if diagnostics are enabled. - if let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) { - let startLoc = SourceLoc( - locationInFile: error.location.assumingMemoryBound(to: UInt8.self))! - diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message) + + if let diagEnginePtr = bridgedDiagnosticEnginePtr { + var message = error.message + message.withBridgedString { message in + let diag = Diagnostic_create( + BridgedDiagnosticEngine(raw: diagEnginePtr), + .error, + BridgedSourceLoc(raw: error.location), + message + ) + Diagnostic_finish(diag) + } } return error.completelyErroneous } @@ -75,27 +78,26 @@ private func _RegexLiteralLexingFn( /// Bridging between C++ parser and swiftCompilerParseRegexLiteral. /// /// - Parameters: -/// - inputPtr: A null-terminated C string. -/// - errOut: A buffer accepting an error string upon error. -/// - versionOut: A buffer accepting a regex literal format -/// version. +/// - input: Regex literal text. +/// - versionOut: A buffer accepting a regex literal format version. /// - captureStructureOut: A buffer accepting a byte sequence representing the /// capture structure. /// - captureStructureSize: The size of the capture structure buffer. Must be -/// greater than or equal to `strlen(inputPtr)`. +/// greater than or equal to `input.size()`. /// - bridgedDiagnosticBaseLoc: Source location of the start of the literal /// - bridgedDiagnosticEngine: Diagnostic engine to emit diagnostics. /// /// - Returns: `true` if there was a parse error, `false` otherwise. +@_cdecl("swift_ASTGen_parseRegexLiteral") public func _RegexLiteralParsingFn( - _ inputPtr: UnsafePointer, - _ versionOut: UnsafeMutablePointer, + _ input: BridgedString, + _ versionOut: UnsafeMutablePointer, _ captureStructureOut: UnsafeMutableRawPointer, - _ captureStructureSize: CUnsignedInt, - _ bridgedDiagnosticBaseLoc: swift.SourceLoc, + _ captureStructureSize: UInt, + _ bridgedDiagnosticBaseLoc: BridgedSourceLoc, _ bridgedDiagnosticEngine: BridgedDiagnosticEngine ) -> Bool { - let str = String(cString: inputPtr) + let str = String(bridged: input) let captureBuffer = UnsafeMutableRawBufferPointer( start: captureStructureOut, count: Int(captureStructureSize)) do { @@ -103,25 +105,42 @@ public func _RegexLiteralParsingFn( // For now, it is the same as the input. let (_, version) = try swiftCompilerParseRegexLiteral( str, captureBufferOut: captureBuffer) - versionOut.pointee = CUnsignedInt(version) + versionOut.pointee = UInt(version) return false } catch let error as CompilerParseError { - var diagLoc = SourceLoc(bridged: bridgedDiagnosticBaseLoc) - let diagEngine = DiagnosticEngine(bridged: bridgedDiagnosticEngine) - if let _diagLoc = diagLoc, let errorLoc = error.location { + var diagLoc = bridgedDiagnosticBaseLoc + + if let diagLocPtr = diagLoc.raw, let errorLoc = error.location { let offset = str.utf8.distance(from: str.startIndex, to: errorLoc) - diagLoc = _diagLoc.advanced(by: offset) + diagLoc = BridgedSourceLoc(raw: diagLocPtr.advanced(by: offset)) + } + var message = error.message + message.withBridgedString { message in + let diag = Diagnostic_create( + bridgedDiagnosticEngine, + .error, + diagLoc, + message + ) + Diagnostic_finish(diag) } - diagEngine.diagnose(diagLoc, .foreign_diagnostic, error.message) return true } catch { fatalError("Expected CompilerParseError") } } +extension String { + init(bridged: BridgedString) { + self.init( + decoding: UnsafeBufferPointer(start: bridged.data, count: Int(bridged.length)), + as: UTF8.self + ) + } +} + #else // canImport(_CompilerRegexParser) #warning("Regex parsing is disabled") -func registerRegexParser() {} #endif // canImport(_CompilerRegexParser) diff --git a/lib/ASTGen/_RegexParser_Sources b/lib/ASTGen/_RegexParser_Sources new file mode 120000 index 0000000000000..b52a98d0897ed --- /dev/null +++ b/lib/ASTGen/_RegexParser_Sources @@ -0,0 +1 @@ +../../../swift-experimental-string-processing/Sources/_RegexParser \ No newline at end of file diff --git a/lib/Parse/CMakeLists.txt b/lib/Parse/CMakeLists.txt index 8a01e55f893b0..a1bc9542f0e11 100644 --- a/lib/Parse/CMakeLists.txt +++ b/lib/Parse/CMakeLists.txt @@ -57,6 +57,13 @@ if (SWIFT_BUILD_SWIFT_SYNTAX) ) endif() +if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER) + target_compile_definitions(swiftParse + PRIVATE + SWIFT_BUILD_REGEX_PARSER_IN_COMPILER + ) +endif() + if(SWIFT_COMPILER_VERSION) set_property(SOURCE ParseVersion.cpp APPEND_STRING PROPERTY COMPILE_FLAGS " -DSWIFT_COMPILER_VERSION=\"\\\"${SWIFT_COMPILER_VERSION}\\\"\"") diff --git a/lib/Parse/Lexer.cpp b/lib/Parse/Lexer.cpp index 80e0a7182b711..5142ff3e3efe6 100644 --- a/lib/Parse/Lexer.cpp +++ b/lib/Parse/Lexer.cpp @@ -15,7 +15,6 @@ //===----------------------------------------------------------------------===// #include "swift/Parse/Lexer.h" -#include "swift/AST/BridgingUtils.h" #include "swift/AST/DiagnosticsParse.h" #include "swift/AST/Identifier.h" #include "swift/Basic/LangOptions.h" @@ -33,11 +32,10 @@ #include -// Regex lexing delivered via libSwift. -static RegexLiteralLexingFn regexLiteralLexingFn = nullptr; -void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn fn) { - regexLiteralLexingFn = fn; -} +extern "C" bool +swift_ASTGen_lexRegexLiteral(const char *_Nonnull *_Nonnull curPtrPtr, + const char *_Nonnull bufferEndPtr, + bool mustBeRegex, void *_Nullable diagEngine); using namespace swift; @@ -2040,9 +2038,10 @@ bool Lexer::isPotentialUnskippableBareSlashRegexLiteral(const Token &Tok) const const char *Lexer::tryScanRegexLiteral(const char *TokStart, bool MustBeRegex, DiagnosticEngine *Diags, bool &CompletelyErroneous) const { +#if SWIFT_BUILD_REGEX_PARSER_IN_COMPILER // We need to have experimental string processing enabled, and have the // parsing logic for regex literals available. - if (!LangOpts.EnableExperimentalStringProcessing || !regexLiteralLexingFn) + if (!LangOpts.EnableExperimentalStringProcessing) return nullptr; bool IsForwardSlash = (*TokStart == '/'); @@ -2088,9 +2087,9 @@ const char *Lexer::tryScanRegexLiteral(const char *TokStart, bool MustBeRegex, // - Ptr will not be advanced if this is not for a regex literal. // - CompletelyErroneous will be set if there was an error that cannot be // recovered from. - auto *Ptr = TokStart; - CompletelyErroneous = regexLiteralLexingFn( - &Ptr, BufferEnd, MustBeRegex, getBridgedOptionalDiagnosticEngine(Diags)); + const char *Ptr = TokStart; + CompletelyErroneous = + swift_ASTGen_lexRegexLiteral(&Ptr, BufferEnd, MustBeRegex, Diags); // If we didn't make any lexing progress, this isn't a regex literal and we // should fallback to lexing as something else. @@ -2178,6 +2177,9 @@ const char *Lexer::tryScanRegexLiteral(const char *TokStart, bool MustBeRegex, } assert(Ptr > TokStart && Ptr <= BufferEnd); return Ptr; +#else + return nullptr; +#endif } bool Lexer::tryLexRegexLiteral(const char *TokStart) { diff --git a/lib/Parse/ParseRegex.cpp b/lib/Parse/ParseRegex.cpp index 5ed804653d3e0..8231ba6408762 100644 --- a/lib/Parse/ParseRegex.cpp +++ b/lib/Parse/ParseRegex.cpp @@ -14,38 +14,35 @@ // //===----------------------------------------------------------------------===// -#include "swift/AST/BridgingUtils.h" +#include "swift/AST/CASTBridging.h" #include "swift/AST/DiagnosticsParse.h" -#include "swift/Basic/BridgingUtils.h" #include "swift/Parse/Parser.h" -// Regex parser delivered via Swift modules. -#include "swift/Parse/RegexParserBridging.h" -static RegexLiteralParsingFn regexLiteralParsingFn = nullptr; -void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn fn) { - regexLiteralParsingFn = fn; -} +extern "C" bool swift_ASTGen_parseRegexLiteral( + BridgedString input, size_t *_Nonnull versionOut, + void *_Nonnull UnsafeMutableRawPointer, size_t captureStructureSize, + BridgedSourceLoc diagLoc, void *_Nonnull diagEngine); using namespace swift; ParserResult Parser::parseExprRegexLiteral() { assert(Tok.is(tok::regex_literal)); - assert(regexLiteralParsingFn); +#if SWIFT_BUILD_REGEX_PARSER_IN_COMPILER auto regexText = Tok.getText(); // Let the Swift library parse the contents, returning an error, or null if // successful. - unsigned version = 0; + size_t version = 0; auto capturesBuf = Context.AllocateUninitialized( RegexLiteralExpr::getCaptureStructureSerializationAllocationSize( regexText.size())); - bool hadError = - regexLiteralParsingFn(regexText.str().c_str(), &version, - /*captureStructureOut*/ capturesBuf.data(), - /*captureStructureSize*/ capturesBuf.size(), - /*diagBaseLoc*/ Tok.getLoc(), - getBridgedDiagnosticEngine(&Diags)); + bool hadError = swift_ASTGen_parseRegexLiteral( + {(const unsigned char *)regexText.data(), SwiftInt(regexText.size())}, + /*versionOut=*/&version, + /*captureStructureOut=*/capturesBuf.data(), + /*captureStructureSize=*/capturesBuf.size(), + /*diagBaseLoc=*/{Tok.getLoc().getOpaquePointerValue()}, &Diags); auto loc = consumeToken(); SourceMgr.recordRegexLiteralStartLoc(loc); @@ -55,4 +52,7 @@ ParserResult Parser::parseExprRegexLiteral() { assert(version >= 1); return makeParserResult(RegexLiteralExpr::createParsed( Context, loc, regexText, version, capturesBuf)); +#else + llvm_unreachable("Lexer should not emit tok::regex_literal"); +#endif } diff --git a/test/IDE/complete_regex.swift b/test/IDE/complete_regex.swift index 587c3b24324ca..c5da4e05e02f9 100644 --- a/test/IDE/complete_regex.swift +++ b/test/IDE/complete_regex.swift @@ -1,4 +1,4 @@ -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // RUN: %empty-directory(%t) // RUN: %target-swift-ide-test -enable-bare-slash-regex -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t diff --git a/test/SourceKit/CodeComplete/rdar95772803.swift b/test/SourceKit/CodeComplete/rdar95772803.swift index b68bca2571ad9..eec4f67e38ad2 100644 --- a/test/SourceKit/CodeComplete/rdar95772803.swift +++ b/test/SourceKit/CodeComplete/rdar95772803.swift @@ -11,4 +11,4 @@ var qux: Regex { / x}/ } // RUN: -req=complete -pos=4:28 %s -- -enable-bare-slash-regex %s == \ // RUN: -req=complete -pos=5:28 %s -- -enable-bare-slash-regex %s -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser diff --git a/test/StringProcessing/Frontend/enable-flag.swift b/test/StringProcessing/Frontend/enable-flag.swift index 601cdb71e1a59..b1768a913dcc6 100644 --- a/test/StringProcessing/Frontend/enable-flag.swift +++ b/test/StringProcessing/Frontend/enable-flag.swift @@ -2,7 +2,7 @@ // RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-string-processing -enable-bare-slash-regex // RUN: %target-typecheck-verify-swift -disable-availability-checking -disable-experimental-string-processing -enable-experimental-string-processing -enable-bare-slash-regex -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser prefix operator / diff --git a/test/StringProcessing/Parse/forward-slash-regex-disabled.swift b/test/StringProcessing/Parse/forward-slash-regex-disabled.swift index 4c491a4a863a0..db403c97dfea8 100644 --- a/test/StringProcessing/Parse/forward-slash-regex-disabled.swift +++ b/test/StringProcessing/Parse/forward-slash-regex-disabled.swift @@ -1,6 +1,6 @@ // RUN: %target-typecheck-verify-swift -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser prefix operator / prefix operator ^/ diff --git a/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift b/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift index e9b9c2b122c47..1974bd55c6044 100644 --- a/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift +++ b/test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift @@ -4,7 +4,7 @@ // RUN: %{python} %utils/process-stats-dir.py --set-csv-baseline %t/stats.csv %t // RUN: %FileCheck -input-file %t/stats.csv %s -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // Make sure we can skip in all of the below cases. diff --git a/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift b/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift index ccebc28bee855..23e3e8c16f9f0 100644 --- a/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift +++ b/test/StringProcessing/Parse/forward-slash-regex-skipping-invalid.swift @@ -2,7 +2,7 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies-without-types // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // We don't consider this a regex literal when skipping as it has an initial // space. diff --git a/test/StringProcessing/Parse/forward-slash-regex-skipping.swift b/test/StringProcessing/Parse/forward-slash-regex-skipping.swift index e3e9c9d07048b..031bef3a1ef20 100644 --- a/test/StringProcessing/Parse/forward-slash-regex-skipping.swift +++ b/test/StringProcessing/Parse/forward-slash-regex-skipping.swift @@ -2,7 +2,7 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies-without-types // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -experimental-skip-non-inlinable-function-bodies -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // Make sure we properly handle `/.../` regex literals in skipped function // bodies. Currently we detect them and avoid skipping, but in the future we diff --git a/test/StringProcessing/Parse/forward-slash-regex.swift b/test/StringProcessing/Parse/forward-slash-regex.swift index 22c6a654e6da0..f4990d4fcb4d4 100644 --- a/test/StringProcessing/Parse/forward-slash-regex.swift +++ b/test/StringProcessing/Parse/forward-slash-regex.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -typo-correction-limit 0 -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // REQUIRES: concurrency prefix operator / diff --git a/test/StringProcessing/Parse/prefix-slash.swift b/test/StringProcessing/Parse/prefix-slash.swift index 4be97f55397e2..dbe33cac8d91c 100644 --- a/test/StringProcessing/Parse/prefix-slash.swift +++ b/test/StringProcessing/Parse/prefix-slash.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // Test the behavior of prefix '/' with regex literals enabled. diff --git a/test/StringProcessing/Parse/regex.swift b/test/StringProcessing/Parse/regex.swift index 797fc2af85eec..a25db9de0019c 100644 --- a/test/StringProcessing/Parse/regex.swift +++ b/test/StringProcessing/Parse/regex.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser _ = /abc/ _ = #/abc/# diff --git a/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift b/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift index 5c62181c0c8c2..a31ddb71b3543 100644 --- a/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift +++ b/test/StringProcessing/Parse/regex_parse_end_of_buffer.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // Note there is purposefully no trailing newline here. // expected-error@+2:20 {{unterminated regex literal}} diff --git a/test/StringProcessing/Parse/regex_parse_error.swift b/test/StringProcessing/Parse/regex_parse_error.swift index c719e33ad9b80..ffa05d1ac9a8c 100644 --- a/test/StringProcessing/Parse/regex_parse_error.swift +++ b/test/StringProcessing/Parse/regex_parse_error.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser _ = /(/ // expected-error@:7 {{expected ')'}} _ = #/(/# // expected-error@:8 {{expected ')'}} diff --git a/test/StringProcessing/Runtime/regex_basic.swift b/test/StringProcessing/Runtime/regex_basic.swift index 9ad6c31d30662..498894cd1dff6 100644 --- a/test/StringProcessing/Runtime/regex_basic.swift +++ b/test/StringProcessing/Runtime/regex_basic.swift @@ -1,6 +1,6 @@ // RUN: %target-run-simple-swift(-Xfrontend -enable-bare-slash-regex) -// REQUIRES: swift_in_compiler,string_processing,executable_test +// REQUIRES: swift_swift_parser,string_processing,executable_test import StdlibUnittest diff --git a/test/StringProcessing/SILGen/regex_literal_silgen.swift b/test/StringProcessing/SILGen/regex_literal_silgen.swift index 7d16c82d81040..3cfa2bba161af 100644 --- a/test/StringProcessing/SILGen/regex_literal_silgen.swift +++ b/test/StringProcessing/SILGen/regex_literal_silgen.swift @@ -1,5 +1,5 @@ // RUN: %target-swift-frontend -emit-silgen -enable-bare-slash-regex -disable-availability-checking %s | %FileCheck %s -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser var s = #/abc/# // CHECK: [[REGEX_STR_LITERAL:%[0-9]+]] = string_literal utf8 "#/abc/#" diff --git a/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift b/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift index 29e7881822ac1..acaf229511b91 100644 --- a/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift +++ b/test/StringProcessing/Sema/regex_builder_fix_import_after_imports.swift @@ -7,7 +7,7 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -I %t -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser import A diff --git a/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift b/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift index 31feb6744497d..a0df60f6cdfd3 100644 --- a/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift +++ b/test/StringProcessing/Sema/regex_builder_fix_import_decl.swift @@ -1,6 +1,6 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser struct S { func foo() { diff --git a/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift b/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift index eaae05546891c..155b43f50d71a 100644 --- a/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift +++ b/test/StringProcessing/Sema/regex_builder_fix_import_top_level.swift @@ -1,6 +1,6 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser Regex {} // expected-error {{regex builder requires the 'RegexBuilder' module be imported'}} {{5:1-1=import RegexBuilder\n\n}} diff --git a/test/StringProcessing/Sema/regex_builder_unavailable.swift b/test/StringProcessing/Sema/regex_builder_unavailable.swift index 38bf6049b31d3..7167315c87a34 100644 --- a/test/StringProcessing/Sema/regex_builder_unavailable.swift +++ b/test/StringProcessing/Sema/regex_builder_unavailable.swift @@ -1,6 +1,6 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -target %target-cpu-apple-macosx12.0 -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // REQUIRES: OS=macosx import RegexBuilder diff --git a/test/StringProcessing/Sema/regex_literal_availability.swift b/test/StringProcessing/Sema/regex_literal_availability.swift index ec68855876783..bcf8cdce2d0fb 100644 --- a/test/StringProcessing/Sema/regex_literal_availability.swift +++ b/test/StringProcessing/Sema/regex_literal_availability.swift @@ -1,6 +1,6 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -target %target-cpu-apple-macosx12.0 -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // REQUIRES: OS=macosx _ = /x/ // expected-error {{'Regex' is only available in}} diff --git a/test/StringProcessing/Sema/regex_literal_diagnostics.swift b/test/StringProcessing/Sema/regex_literal_diagnostics.swift index 0a0f0629669ba..e3ec4b77c70dd 100644 --- a/test/StringProcessing/Sema/regex_literal_diagnostics.swift +++ b/test/StringProcessing/Sema/regex_literal_diagnostics.swift @@ -1,6 +1,6 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser postfix operator ^^ postfix func ^^ (_ x: T) -> T { x } diff --git a/test/StringProcessing/Sema/regex_literal_type_inference.swift b/test/StringProcessing/Sema/regex_literal_type_inference.swift index 9cce7a1b0e76e..5ea94df139e08 100644 --- a/test/StringProcessing/Sema/regex_literal_type_inference.swift +++ b/test/StringProcessing/Sema/regex_literal_type_inference.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -enable-bare-slash-regex -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser let r0 = #/./# let _: Regex = r0 diff --git a/test/StringProcessing/Sema/string_processing_import.swift b/test/StringProcessing/Sema/string_processing_import.swift index 5a83931364d6b..200e0729f8b78 100644 --- a/test/StringProcessing/Sema/string_processing_import.swift +++ b/test/StringProcessing/Sema/string_processing_import.swift @@ -1,5 +1,5 @@ // RUN: %target-typecheck-verify-swift -disable-implicit-string-processing-module-import -disable-availability-checking -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser // expected-error @+1 {{missing 'Regex' declaration, probably because the '_StringProcessing' module was not imported properly}} let r0 = #/./# diff --git a/test/stmt/then_stmt.swift b/test/stmt/then_stmt.swift index ab81db4116b93..213aee7987a20 100644 --- a/test/stmt/then_stmt.swift +++ b/test/stmt/then_stmt.swift @@ -4,7 +4,7 @@ // REQUIRES: asserts // Required for regex -// REQUIRES: swift_in_compiler +// REQUIRES: swift_swift_parser func then(_: Int = 0, x: Int = 0, fn: () -> Void = {}) {} diff --git a/tools/libSwiftScan/libSwiftScan.cpp b/tools/libSwiftScan/libSwiftScan.cpp index 406efddb7e6bd..b94e8c32356cd 100644 --- a/tools/libSwiftScan/libSwiftScan.cpp +++ b/tools/libSwiftScan/libSwiftScan.cpp @@ -14,7 +14,6 @@ // //===----------------------------------------------------------------------===// -#include "swift/Basic/InitializeSwiftModules.h" #include "swift/Basic/LLVMInitialize.h" #include "swift/DependencyScan/DependencyScanImpl.h" #include "swift/DependencyScan/DependencyScanningTool.h" @@ -169,9 +168,6 @@ void swiftscan_scanner_cache_reset(swiftscan_scanner_t scanner) { swiftscan_scanner_t swiftscan_scanner_create(void) { INITIALIZE_LLVM(); - // We must initialize the swift modules responsible for parsing functionality, - // such as parsing regex. - initializeSwiftParseModules(); return wrap(new DependencyScanningTool()); } From 65d8f072c0e3f3e9a995aeb4bb8c3b6e61034df4 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 16 Nov 2023 11:01:32 -0800 Subject: [PATCH 2/7] [Windows] Link host tools with 'swiftrt.obj' swiftrt.obj is required for making things work (cherry picked from commit abd416480fce5870a00d0862b32f36d07c9e6351) --- cmake/modules/AddSwift.cmake | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 561e1ab92d8cc..f4a2cd3017faf 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -537,20 +537,26 @@ function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) set_property(TARGET ${target} APPEND_STRING PROPERTY LINK_FLAGS " -lobjc ") - elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD") + elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD|FREEBSD|WINDOWS") set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}") if(ASRLF_BOOTSTRAPPING_MODE MATCHES "HOSTTOOLS|CROSSCOMPILE") # At build time and run time, link against the swift libraries in the # installed host toolchain. - get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) - get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY) + if(SWIFT_PATH_TO_SWIFT_SDK) + set(swift_dir "${SWIFT_PATH_TO_SWIFT_SDK}/usr") + else() + get_filename_component(swift_bin_dir ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) + get_filename_component(swift_dir ${swift_bin_dir} DIRECTORY) + endif() set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - set(swiftrt "${host_lib_dir}/${SWIFT_HOST_VARIANT_ARCH}/swiftrt.o") + set(host_lib_arch_dir "${host_lib_dir}/${SWIFT_HOST_VARIANT_ARCH}") + set(swiftrt "${host_lib_arch_dir}/swiftrt${CMAKE_C_OUTPUT_EXTENSION}") target_link_libraries(${target} PRIVATE ${swiftrt}) target_link_libraries(${target} PRIVATE "swiftCore") target_link_directories(${target} PRIVATE ${host_lib_dir}) + target_link_directories(${target} PRIVATE ${host_lib_arch_dir}) # At runtime, use swiftCore in the current toolchain. # For building stdlib, LD_LIBRARY_PATH will be set to builder's stdlib From da903aee8f7f9837f326570e50bcc1a2327cb245 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 16 Nov 2023 11:02:52 -0800 Subject: [PATCH 3/7] Revert "ASTGen: add a workaround for CSC conformance visibility" This reverts commit 5d0c5a643fdf3ac9d9c721ec107c27d3b0aeb3a7. (cherry picked from commit 24b44e371c7c18c28a8527b4cdaf324e9b6ab8eb) --- lib/ASTGen/Sources/ASTGen/Macros.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/ASTGen/Sources/ASTGen/Macros.swift b/lib/ASTGen/Sources/ASTGen/Macros.swift index 399b1ef29cee6..72ec2681d6ae1 100644 --- a/lib/ASTGen/Sources/ASTGen/Macros.swift +++ b/lib/ASTGen/Sources/ASTGen/Macros.swift @@ -186,11 +186,7 @@ enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage { var message: String { switch self { case .thrownError(let error): - if let err = error as? PluginError { - return err.description - } else { - return String(describing: error) - } + return String(describing: error) case .oldStyleExternalMacro: return "external macro definitions are now written using #externalMacro" From e922100a8c560af524c1aee76f9f3150ef2a38d7 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 16 Nov 2023 13:54:14 -0800 Subject: [PATCH 4/7] [CMake] Correct dependencies HeaderDependencies.cpp and the stdlib filelist didn't have dependencies. That causes issues when the content is changed. (cherry picked from commit f327bf7616d0c9ad196c505a2fca9cb84e874561) --- SwiftCompilerSources/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/SwiftCompilerSources/CMakeLists.txt b/SwiftCompilerSources/CMakeLists.txt index 88e8bf32f5558..5e0eda5c0a652 100644 --- a/SwiftCompilerSources/CMakeLists.txt +++ b/SwiftCompilerSources/CMakeLists.txt @@ -245,6 +245,7 @@ else() ") add_custom_command( OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" + DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp" COMMAND ${CMAKE_COMMAND} -E copy_if_different "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp.tmp" "${CMAKE_CURRENT_BINARY_DIR}/HeaderDependencies.cpp" From 5be944c3bb2fb9820fbbc746d7f6565a3e3331da Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 16 Nov 2023 14:01:41 -0800 Subject: [PATCH 5/7] [Parse] Remove RegexParserBridging.h This is not used anymore (cherry picked from commit 986415a4af31b8a539c76fbea7c2134c0a5d0e2a) Conflicts: include/swift/Parse/RegexParserBridging.h lib/ASTGen/Sources/ASTGen/Regex.swift --- include/module.modulemap | 5 -- include/swift/Parse/RegexParserBridging.h | 64 ----------------------- lib/Parse/Lexer.cpp | 1 - 3 files changed, 70 deletions(-) delete mode 100644 include/swift/Parse/RegexParserBridging.h diff --git a/include/module.modulemap b/include/module.modulemap index 2e421469dd1dd..fd7691596ac4e 100644 --- a/include/module.modulemap +++ b/include/module.modulemap @@ -41,8 +41,3 @@ module OptimizerBridging { header "swift/SILOptimizer/OptimizerBridging.h" export * } - -module _RegexParserBridging { - header "swift/Parse/RegexParserBridging.h" - export * -} diff --git a/include/swift/Parse/RegexParserBridging.h b/include/swift/Parse/RegexParserBridging.h deleted file mode 100644 index 35ea10eeadbfc..0000000000000 --- a/include/swift/Parse/RegexParserBridging.h +++ /dev/null @@ -1,64 +0,0 @@ -//===-- RegexParserBridging.h --- Regex parser interface -*- C++ -*-===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 2017 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 -// -//===----------------------------------------------------------------------===// - - -#ifndef REGEX_PARSER_BRIDGING -#define REGEX_PARSER_BRIDGING - -#include "swift/AST/ASTBridging.h" -#include "swift/Basic/Nullability.h" -#include - -/// Attempt to lex a regex literal string. Takes the following arguments: -/// -/// - CurPtrPtr: A pointer to the current pointer of lexer, which should be the -/// start of the literal. This will be advanced to the point at -/// which the lexer should resume, or will remain the same if this -/// is not a regex literal. -/// - BufferEnd: A pointer to the end of the buffer, which should not be lexed -/// past. -/// - MustBeRegex: whether an error during lexing should be considered a regex -/// literal, or some thing else. -/// - BridgedOptionalDiagnosticEngine: RegexLiteralLexingFn should diagnose the -/// token using this engine. -/// -/// Returns: A bool indicating whether lexing was completely erroneous, and -/// cannot be recovered from, or false if there either was no error, -/// or there was a recoverable error. -typedef bool (*RegexLiteralLexingFn)( - /*CurPtrPtr*/ const char *_Nonnull *_Nonnull, - /*BufferEnd*/ const char *_Nonnull, - /*MustBeRegex*/ bool, BridgedOptionalDiagnosticEngine); -void Parser_registerRegexLiteralLexingFn(RegexLiteralLexingFn _Nullable fn); - -/// Parse a regex literal string. Takes the following arguments: -/// -/// - InputPtr: A null-terminated C string of the regex literal. -/// - VersionOut: A buffer accepting a regex literal format version. -/// - CaptureStructureOut: A buffer accepting a byte sequence representing the -/// capture structure of the literal. -/// - CaptureStructureSize: The size of the capture structure buffer. Must be -/// greater than or equal to `strlen(InputPtr) + 3`. -/// - DiagnosticBaseLoc: Start location of the regex literal. -/// - BridgedDiagnosticEngine: RegexLiteralParsingFn should diagnose the -/// parsing errors using this engine. -/// -/// Returns: A bool value indicating if there was an error while parsing. -typedef bool (*RegexLiteralParsingFn)(/*InputPtr*/ const char *_Nonnull, - /*VersionOut*/ unsigned *_Nonnull, - /*CaptureStructureOut*/ void *_Nonnull, - /*CaptureStructureSize*/ unsigned, - /*DiagnosticBaseLoc*/ swift::SourceLoc, - BridgedDiagnosticEngine); -void Parser_registerRegexLiteralParsingFn(RegexLiteralParsingFn _Nullable fn); - -#endif // REGEX_PARSER_BRIDGING diff --git a/lib/Parse/Lexer.cpp b/lib/Parse/Lexer.cpp index 5142ff3e3efe6..12f93db9ac2ea 100644 --- a/lib/Parse/Lexer.cpp +++ b/lib/Parse/Lexer.cpp @@ -20,7 +20,6 @@ #include "swift/Basic/LangOptions.h" #include "swift/Basic/SourceManager.h" #include "swift/Parse/Confusables.h" -#include "swift/Parse/RegexParserBridging.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/Twine.h" From 1d95d69d85aae5086d08cb566325dcf2e3d1ed98 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 16 Nov 2023 16:43:43 -0800 Subject: [PATCH 6/7] [Parse] Remove _Nonnull _Nonnull is not a thing in Windows `cl.exe`. Since it's not needed in C++, just remove it. --- lib/Parse/Lexer.cpp | 5 ++--- lib/Parse/ParseRegex.cpp | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/Parse/Lexer.cpp b/lib/Parse/Lexer.cpp index 12f93db9ac2ea..d4a96302dd838 100644 --- a/lib/Parse/Lexer.cpp +++ b/lib/Parse/Lexer.cpp @@ -32,9 +32,8 @@ #include extern "C" bool -swift_ASTGen_lexRegexLiteral(const char *_Nonnull *_Nonnull curPtrPtr, - const char *_Nonnull bufferEndPtr, - bool mustBeRegex, void *_Nullable diagEngine); +swift_ASTGen_lexRegexLiteral(const char ** curPtrPtr, const char * bufferEndPtr, + bool mustBeRegex, void * diagEngine); using namespace swift; diff --git a/lib/Parse/ParseRegex.cpp b/lib/Parse/ParseRegex.cpp index 8231ba6408762..8b557a8040591 100644 --- a/lib/Parse/ParseRegex.cpp +++ b/lib/Parse/ParseRegex.cpp @@ -19,9 +19,9 @@ #include "swift/Parse/Parser.h" extern "C" bool swift_ASTGen_parseRegexLiteral( - BridgedString input, size_t *_Nonnull versionOut, - void *_Nonnull UnsafeMutableRawPointer, size_t captureStructureSize, - BridgedSourceLoc diagLoc, void *_Nonnull diagEngine); + BridgedString input, size_t * versionOut, + void * UnsafeMutableRawPointer, size_t captureStructureSize, + BridgedSourceLoc diagLoc, void * diagEngine); using namespace swift; From 8ef2fa299f963b3309f53980f4cd2aa050ff61dc Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Fri, 17 Nov 2023 15:32:16 -0800 Subject: [PATCH 7/7] [CMake] Disable Regex literal when Swift parser integration is disabled Regex literal parsing is now built as a part of ASTGen, that requires host toolchain and swift-syntax integration. (cherry picked from commit ee982fd6d5df9926dd9dcd3965b93fb83be3bf79) --- CMakeLists.txt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 294649a5ac034..c758792bf4a4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -270,10 +270,6 @@ option(SWIFT_BUILD_PERF_TESTSUITE "Create in-tree targets for building swift performance benchmarks." FALSE) -option(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER - "Build the Swift regex parser as part of the compiler." - TRUE) - option(SWIFT_INCLUDE_TESTS "Create targets for building/running tests." TRUE) option(SWIFT_INCLUDE_TEST_BINARIES @@ -696,6 +692,14 @@ option(SWIFT_BUILD_SWIFT_SYNTAX "Enable building swift syntax" FALSE) +option(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER + "Build the Swift regex parser as part of the compiler." + TRUE) +if(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER AND NOT SWIFT_BUILD_SWIFT_SYNTAX) + message(WARNING "Force setting SWIFT_BUILD_REGEX_PARSER_IN_COMPILER=OFF because Swift parser integration is disabled") + set(SWIFT_BUILD_REGEX_PARSER_IN_COMPILER OFF) +endif() + set(SWIFT_BUILD_HOST_DISPATCH FALSE) if(SWIFT_ENABLE_DISPATCH AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") # Only build libdispatch for the host if the host tools are being built and