From 9f4ce612a298190a1b3885ad96ffeac9f7e98b35 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Fri, 27 May 2022 15:51:57 -0700 Subject: [PATCH 1/2] [SwiftCompilerModules] Link lib_InternalSwiftSyntaxParser to libswift To use _RegexParser from SwiftSyntax. * Create 'libswiftCompilerModules_SwiftSyntax.a' which is a subset of 'libswiftCompilerModules.a' * Link 'lib_InternalSwiftSyntaxParser' to 'libswiftCompilerModules_SwiftSyntax.a' * Factor out swift runtime linking logic in CMake so that dynamic libraries can link to Swift runtime, in addition to executables * Link 'lib_InternalSwiftSyntaxParser' to swift runtime --- SwiftCompilerSources/CMakeLists.txt | 22 +- .../Sources/AST/CMakeLists.txt | 1 + .../Sources/Basic/CMakeLists.txt | 6 +- .../PassManager/PassRegistration.swift | 2 +- .../Sources/Parse/CMakeLists.txt | 2 + .../Sources/Parse/Parse.swift | 16 + .../Sources/Parse/Regex.swift | 4 +- .../Sources/_RegexParser/CMakeLists.txt | 2 + SwiftCompilerSources/stubs.cpp | 3 +- cmake/modules/AddSwift.cmake | 278 +++++++++--------- include/swift/Basic/InitializeSwiftModules.h | 1 + .../Parser/unterminated_multiline_regex.swift | 3 +- test/Syntax/Parser/unterminated_regex.swift | 5 +- test/Syntax/round_trip_regex.swift | 1 + .../round_trip_regex_escape_newline.swift | 4 +- tools/libSwiftSyntaxParser/CMakeLists.txt | 4 +- .../libSwiftSyntaxParser.cpp | 8 +- tools/swift-syntax-test/CMakeLists.txt | 4 +- tools/swift-syntax-test/swift-syntax-test.cpp | 5 +- 19 files changed, 218 insertions(+), 153 deletions(-) create mode 100644 SwiftCompilerSources/Sources/Parse/Parse.swift diff --git a/SwiftCompilerSources/CMakeLists.txt b/SwiftCompilerSources/CMakeLists.txt index 1a65a0efb1a37..370e621fd701d 100644 --- a/SwiftCompilerSources/CMakeLists.txt +++ b/SwiftCompilerSources/CMakeLists.txt @@ -16,7 +16,7 @@ # function(add_swift_compiler_module module) cmake_parse_arguments(ALSM - "" + "ADD_TO_SYNTAXPARSE" "" "DEPENDS;SOURCES" ${ARGN}) @@ -35,6 +35,7 @@ function(add_swift_compiler_module module) set_property(TARGET ${target_name} PROPERTY module_name ${module}) set_property(TARGET ${target_name} PROPERTY module_depends ${ALSM_DEPENDS}) + set_property(TARGET ${target_name} PROPERTY add_to_syntaxparse ${ALSM_ADD_TO_SYNTAXPARSE}) get_property(modules GLOBAL PROPERTY swift_compiler_modules) set_property(GLOBAL PROPERTY swift_compiler_modules ${modules} ${module}) @@ -120,6 +121,8 @@ function(add_swift_compiler_modules_library name) set(all_obj_files) set(all_module_targets) + set(syntaxparse_obj_files) + set(syntaxparse_module_targets) get_property(modules GLOBAL PROPERTY "swift_compiler_modules") foreach(module ${modules}) @@ -127,6 +130,7 @@ function(add_swift_compiler_modules_library name) get_target_property(module ${module_target} "module_name") get_target_property(sources ${module_target} SOURCES) get_target_property(dependencies ${module_target} "module_depends") + get_target_property(add_to_syntaxparse ${module_target} "add_to_syntaxparse") set(deps, "") if (dependencies) foreach(dep_module ${dependencies}) @@ -146,6 +150,9 @@ function(add_swift_compiler_modules_library name) set_property(TARGET ${module_target} PROPERTY "module_file" "${module_file}") set(all_obj_files ${all_obj_files} ${module_obj_file}) + if (add_to_syntaxparse) + set(syntaxparse_obj_files ${syntaxparse_obj_files} ${module_obj_file}) + endif() # Compile the module into an object file add_custom_command_target(dep_target OUTPUT ${module_obj_file} @@ -168,6 +175,9 @@ function(add_swift_compiler_modules_library name) set("${module}_dep_target" ${dep_target}) set(all_module_targets ${all_module_targets} ${dep_target}) + if (add_to_syntaxparse) + set(syntaxparse_module_targets ${syntaxparse_module_targets} ${dep_target}) + endif() endforeach() # Create a static library containing all module object files. @@ -181,6 +191,15 @@ function(add_swift_compiler_modules_library name) add_dependencies(${name} ${all_module_targets}) set_target_properties(${name} PROPERTIES LINKER_LANGUAGE CXX) set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name}) + + if (XCODE) + set(syntaxparse_obj_files force_lib.c ${syntaxparse_obj_files}) + endif() + add_library("${name}_SwiftSyntax" STATIC ${syntaxparse_obj_files}) + add_dependencies("${name}_SwiftSyntax" ${syntaxparse_module_targets}) + set_target_properties("${name}_SwiftSyntax" PROPERTIES LINKER_LANGUAGE CXX) + set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS "${name}_SwiftSyntax") + endfunction() @@ -190,6 +209,7 @@ add_swift_host_library(swiftCompilerStub OBJECT stubs.cpp) if (NOT BOOTSTRAPPING_MODE) add_library(swiftCompilerModules ALIAS swiftCompilerStub) + add_library(swiftCompilerModules_SwiftSyntax ALIAS swiftCompilerStub) else() # Note: "Swift" is not added intentionally here, because it would break diff --git a/SwiftCompilerSources/Sources/AST/CMakeLists.txt b/SwiftCompilerSources/Sources/AST/CMakeLists.txt index 3d814cad0a6c4..6218e7b0dc5cb 100644 --- a/SwiftCompilerSources/Sources/AST/CMakeLists.txt +++ b/SwiftCompilerSources/Sources/AST/CMakeLists.txt @@ -7,6 +7,7 @@ # See http://swift.org/CONTRIBUTORS.txt for Swift project authors add_swift_compiler_module(AST + ADD_TO_SYNTAXPARSE DEPENDS Basic SOURCES diff --git a/SwiftCompilerSources/Sources/Basic/CMakeLists.txt b/SwiftCompilerSources/Sources/Basic/CMakeLists.txt index 3bfca80810404..f3650c60fe011 100644 --- a/SwiftCompilerSources/Sources/Basic/CMakeLists.txt +++ b/SwiftCompilerSources/Sources/Basic/CMakeLists.txt @@ -7,5 +7,7 @@ # See http://swift.org/CONTRIBUTORS.txt for Swift project authors add_swift_compiler_module(Basic - SourceLoc.swift - Utils.swift) + ADD_TO_SYNTAXPARSE + SOURCES + SourceLoc.swift + Utils.swift) diff --git a/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift b/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift index 71a3452c11f82..5945ce788aff8 100644 --- a/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift +++ b/SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift @@ -18,7 +18,7 @@ import Parse public func initializeSwiftModules() { registerSILClasses() registerSwiftPasses() - registerRegexParser() + initializeSwiftParseModules() } private func registerPass( diff --git a/SwiftCompilerSources/Sources/Parse/CMakeLists.txt b/SwiftCompilerSources/Sources/Parse/CMakeLists.txt index e4c52316ff1df..94f29991b1c49 100644 --- a/SwiftCompilerSources/Sources/Parse/CMakeLists.txt +++ b/SwiftCompilerSources/Sources/Parse/CMakeLists.txt @@ -14,5 +14,7 @@ endif() add_swift_compiler_module(Parse DEPENDS ${dependencies} + ADD_TO_SYNTAXPARSE SOURCES + Parse.swift Regex.swift) diff --git a/SwiftCompilerSources/Sources/Parse/Parse.swift b/SwiftCompilerSources/Sources/Parse/Parse.swift new file mode 100644 index 0000000000000..a8a79489fc5a8 --- /dev/null +++ b/SwiftCompilerSources/Sources/Parse/Parse.swift @@ -0,0 +1,16 @@ +//===--- 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/Parse/Regex.swift b/SwiftCompilerSources/Sources/Parse/Regex.swift index 0553e755ac47a..f8ad6b0656a83 100644 --- a/SwiftCompilerSources/Sources/Parse/Regex.swift +++ b/SwiftCompilerSources/Sources/Parse/Regex.swift @@ -17,7 +17,7 @@ import Basic #if canImport(_CompilerRegexParser) @_spi(CompilerInterface) import _CompilerRegexParser -public func registerRegexParser() { +func registerRegexParser() { Parser_registerRegexLiteralParsingFn(_RegexLiteralParsingFn) Parser_registerRegexLiteralLexingFn(_RegexLiteralLexingFn) } @@ -122,6 +122,6 @@ public func _RegexLiteralParsingFn( #else // canImport(_CompilerRegexParser) #warning("Regex parsing is disabled") -public func registerRegexParser() {} +func registerRegexParser() {} #endif // canImport(_CompilerRegexParser) diff --git a/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt b/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt index b1d2ae37978f7..a5ed25aa8dbc5 100644 --- a/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt +++ b/SwiftCompilerSources/Sources/_RegexParser/CMakeLists.txt @@ -16,4 +16,6 @@ endforeach() message(STATUS "Using Experimental String Processing library for libswift _RegexParser (${EXPERIMENTAL_STRING_PROCESSING_SOURCE_DIR}).") add_swift_compiler_module(_CompilerRegexParser + ADD_TO_SYNTAXPARSE + SOURCES "${LIBSWIFT_REGEX_PARSER_SOURCES}") diff --git a/SwiftCompilerSources/stubs.cpp b/SwiftCompilerSources/stubs.cpp index 2400fe093fa5b..36df93a14d970 100644 --- a/SwiftCompilerSources/stubs.cpp +++ b/SwiftCompilerSources/stubs.cpp @@ -13,8 +13,9 @@ extern "C" { void initializeSwiftModules(); +void initializeSwiftParseModules(); } void initializeSwiftModules() {} - +void initializeSwiftParseModules() {} diff --git a/cmake/modules/AddSwift.cmake b/cmake/modules/AddSwift.cmake index 5cf79d8e3c1ca..a80a48867f336 100644 --- a/cmake/modules/AddSwift.cmake +++ b/cmake/modules/AddSwift.cmake @@ -428,6 +428,137 @@ function(_add_host_variant_link_flags target) endif() endfunction() +function(_add_swift_runtime_link_flags target relpath_to_lib_dir bootstrapping) + if(NOT BOOTSTRAPPING_MODE) + return() + endif() + + # RPATH where Swift runtime can be found. + set(swift_runtime_rpath) + + if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) + + set(sdk_dir "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift") + + # If we found a swift compiler and are going to use swift code in swift + # host side tools but link with clang, add the appropriate -L paths so we + # find all of the necessary swift libraries on Darwin. + if(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") + # Add in the toolchain directory so we can grab compatibility libraries + get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) + get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE) + target_link_directories(${target} PUBLIC ${TOOLCHAIN_LIB_DIR}) + + # Add the SDK directory for the host platform. + target_link_directories(${target} PRIVATE "${sdk_dir}") + + # Include the abi stable system stdlib in our rpath. + set(swift_runtime_rpath "/usr/lib/swift") + + elseif(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS") + + # Intentionally don't add the lib dir of the cross-compiled compiler, so that + # the stdlib is not picked up from there, but from the SDK. + # This requires to explicitly add all the needed compatibility libraries. We + # can take them from the current build. + set(vsuffix "-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}") + set(conctarget "swiftCompatibilityConcurrency${vsuffix}") + target_link_libraries(${target} PUBLIC ${conctarget}) + + # Add the SDK directory for the host platform. + target_link_directories(${target} PRIVATE "${sdk_dir}") + + # Include the abi stable system stdlib in our rpath. + set(swift_runtime_rpath "/usr/lib/swift") + + elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS") + # Add the SDK directory for the host platform. + target_link_directories(${target} PRIVATE "${sdk_dir}") + + # A backup in case the toolchain doesn't have one of the compatibility libraries. + target_link_directories(${target} PRIVATE + "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + + # Include the abi stable system stdlib in our rpath. + set(swift_runtime_rpath "/usr/lib/swift") + + elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING") + # At build time link against the built swift libraries from the + # previous bootstrapping stage. + get_bootstrapping_swift_lib_dir(bs_lib_dir "${bootstrapping}") + target_link_directories(${target} PRIVATE ${bs_lib_dir}) + + # Required to pick up the built libswiftCompatibility.a libraries + target_link_directories(${target} PRIVATE + "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + + # At runtime link against the built swift libraries from the current + # bootstrapping stage. + set(swift_runtime_rpath "@loader_path/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + else() + message(FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE}'") + endif() + + # Workaround to make lldb happy: we have to explicitly add all swift compiler modules + # to the linker command line. + set(swift_ast_path_flags "-Wl") + get_property(modules GLOBAL PROPERTY swift_compiler_modules) + foreach(module ${modules}) + get_target_property(module_file "SwiftModule${module}" "module_file") + string(APPEND swift_ast_path_flags ",-add_ast_path,${module_file}") + endforeach() + + set_property(TARGET ${target} APPEND_STRING PROPERTY + LINK_FLAGS ${swift_ast_path_flags}) + + # Workaround for a linker crash related to autolinking: rdar://77839981 + set_property(TARGET ${target} APPEND_STRING PROPERTY + LINK_FLAGS " -lobjc ") + + elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD") + set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}") + if(${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) + set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + + target_link_libraries(${target} PRIVATE ${swiftrt}) + target_link_libraries(${target} PRIVATE "swiftCore") + + target_link_directories(${target} PRIVATE ${host_lib_dir}) + if(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") + set(swift_runtime_rpath "${host_lib_dir}") + else() + set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + endif() + + elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING") + # At build time link against the built swift libraries from the + # previous bootstrapping stage. + if (NOT "${bootstrapping}" STREQUAL "0") + get_bootstrapping_swift_lib_dir(bs_lib_dir "${bootstrapping}") + target_link_directories(${target} PRIVATE ${bs_lib_dir}) + target_link_libraries(${target} PRIVATE ${swiftrt}) + target_link_libraries(${target} PRIVATE "swiftCore") + endif() + + # At runtime link against the built swift libraries from the current + # bootstrapping stage. + set(swift_runtime_rpath "$ORIGIN/${relpath_to_lib_dir}/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") + + elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS") + message(FATAL_ERROR "BOOTSTRAPPING_MODE 'BOOTSTRAPPING-WITH-HOSTLIBS' not supported on Linux") + else() + message(FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE}'") + endif() + endif() + + set_property(TARGET ${target} PROPERTY BUILD_WITH_INSTALL_RPATH YES) + set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${swift_runtime_rpath}") +endfunction() + # Add a new Swift host library. # # Usage: @@ -459,7 +590,8 @@ function(add_swift_host_library name) set(options SHARED STATIC - OBJECT) + OBJECT + HAS_SWIFT_MODULES) set(single_parameter_options) set(multiple_parameter_options LLVM_LINK_COMPONENTS) @@ -476,6 +608,9 @@ function(add_swift_host_library name) if(NOT ASHL_SHARED AND NOT ASHL_STATIC AND NOT ASHL_OBJECT) message(FATAL_ERROR "One of SHARED/STATIC/OBJECT must be specified") endif() + if(NOT ASHL_SHARED AND ASHL_HAS_SWIFT_MODULES) + message(WARNING "Ignoring HAS_SWIFT_MODULES; it's only for SHARED libraries") + endif() # Using `support` llvm component ends up adding `-Xlinker /path/to/lib/libLLVMDemangle.a` # to `LINK_FLAGS` but `libLLVMDemangle.a` is not added as an input to the linking ninja statement. @@ -551,6 +686,10 @@ function(add_swift_host_library name) _add_host_variant_c_compile_link_flags(${name}) _set_target_prefix_and_suffix(${name} "${libkind}" "${SWIFT_HOST_VARIANT_SDK}") + if (ASHL_SHARED AND ASHL_HAS_SWIFT_MODULES) + _add_swift_runtime_link_flags(${name} "." "") + endif() + # Set compilation and link flags. if(SWIFT_HOST_VARIANT_SDK STREQUAL WINDOWS) swift_windows_include_for_arch(${SWIFT_HOST_VARIANT_ARCH} @@ -691,142 +830,9 @@ function(add_swift_host_tool executable) set_target_properties(${executable} PROPERTIES JOB_POOL_LINK swift_link_job_pool) endif() - if(${SWIFT_HOST_VARIANT_SDK} IN_LIST SWIFT_DARWIN_PLATFORMS) - - # Lists of rpaths that we are going to add to our executables. - # - # Please add each rpath separately below to the list, explaining why you are - # adding it. - set(RPATH_LIST) - set(sdk_dir "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_ARCH_${SWIFT_HOST_VARIANT_ARCH}_PATH}/usr/lib/swift") - - # If we found a swift compiler and are going to use swift code in swift - # host side tools but link with clang, add the appropriate -L paths so we - # find all of the necessary swift libraries on Darwin. - if (ASHT_HAS_SWIFT_MODULES AND BOOTSTRAPPING_MODE) - - if(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") - # Add in the toolchain directory so we can grab compatibility libraries - get_filename_component(TOOLCHAIN_BIN_DIR ${SWIFT_EXEC_FOR_SWIFT_MODULES} DIRECTORY) - get_filename_component(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_BIN_DIR}/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}" ABSOLUTE) - target_link_directories(${executable} PUBLIC ${TOOLCHAIN_LIB_DIR}) - - # Add the SDK directory for the host platform. - target_link_directories(${executable} PRIVATE "${sdk_dir}") - - # Include the abi stable system stdlib in our rpath. - list(APPEND RPATH_LIST "/usr/lib/swift") - - elseif(BOOTSTRAPPING_MODE STREQUAL "CROSSCOMPILE-WITH-HOSTLIBS") - - # Intentionally don't add the lib dir of the cross-compiled compiler, so that - # the stdlib is not picked up from there, but from the SDK. - # This requires to explicitly add all the needed compatibility libraries. We - # can take them from the current build. - set(vsuffix "-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}") - set(conctarget "swiftCompatibilityConcurrency${vsuffix}") - target_link_libraries(${executable} PUBLIC ${conctarget}) - - # Add the SDK directory for the host platform. - target_link_directories(${executable} PRIVATE "${sdk_dir}") - - # Include the abi stable system stdlib in our rpath. - list(APPEND RPATH_LIST "/usr/lib/swift") - - elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS") - # Add the SDK directory for the host platform. - target_link_directories(${executable} PRIVATE "${sdk_dir}") - - # A backup in case the toolchain doesn't have one of the compatibility libraries. - target_link_directories(${executable} PRIVATE - "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - - # Include the abi stable system stdlib in our rpath. - list(APPEND RPATH_LIST "/usr/lib/swift") - - elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING") - # At build time link against the built swift libraries from the - # previous bootstrapping stage. - get_bootstrapping_swift_lib_dir(bs_lib_dir "${ASHT_BOOTSTRAPPING}") - target_link_directories(${executable} PRIVATE ${bs_lib_dir}) - - # Required to pick up the built libswiftCompatibility.a libraries - target_link_directories(${executable} PRIVATE - "${SWIFTLIB_DIR}/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - - # At runtime link against the built swift libraries from the current - # bootstrapping stage. - list(APPEND RPATH_LIST "@executable_path/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - else() - message(FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE}'") - endif() - - # Workaround to make lldb happy: we have to explicitly add all swift compiler modules - # to the linker command line. - set(swift_ast_path_flags "-Wl") - get_property(modules GLOBAL PROPERTY swift_compiler_modules) - foreach(module ${modules}) - get_target_property(module_file "SwiftModule${module}" "module_file") - string(APPEND swift_ast_path_flags ",-add_ast_path,${module_file}") - endforeach() - - set_property(TARGET ${executable} APPEND_STRING PROPERTY - LINK_FLAGS ${swift_ast_path_flags}) - # Workaround for a linker crash related to autolinking: rdar://77839981 - set_property(TARGET ${executable} APPEND_STRING PROPERTY - LINK_FLAGS " -lobjc ") - - endif() # ASHT_HAS_SWIFT_MODULES AND BOOTSTRAPPING_MODE - - set_target_properties(${executable} PROPERTIES - BUILD_WITH_INSTALL_RPATH YES - INSTALL_RPATH "${RPATH_LIST}") - - elseif(SWIFT_HOST_VARIANT_SDK MATCHES "LINUX|ANDROID|OPENBSD" AND ASHT_HAS_SWIFT_MODULES AND BOOTSTRAPPING_MODE) - set(swiftrt "swiftImageRegistrationObject${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_OBJECT_FORMAT}-${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}-${SWIFT_HOST_VARIANT_ARCH}") - if(${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) - set(host_lib_dir "${swift_dir}/lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - - target_link_libraries(${executable} PRIVATE ${swiftrt}) - target_link_libraries(${executable} PRIVATE "swiftCore") - - target_link_directories(${executable} PRIVATE ${host_lib_dir}) - if(BOOTSTRAPPING_MODE STREQUAL "HOSTTOOLS") - set_target_properties(${executable} PROPERTIES - BUILD_WITH_INSTALL_RPATH YES - INSTALL_RPATH "${host_lib_dir}") - else() - set_target_properties(${executable} PROPERTIES - BUILD_WITH_INSTALL_RPATH YES - INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - endif() - - elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING") - # At build time link against the built swift libraries from the - # previous bootstrapping stage. - if (NOT "${ASHT_BOOTSTRAPPING}" STREQUAL "0") - get_bootstrapping_swift_lib_dir(bs_lib_dir "${ASHT_BOOTSTRAPPING}") - target_link_directories(${executable} PRIVATE ${bs_lib_dir}) - target_link_libraries(${executable} PRIVATE ${swiftrt}) - target_link_libraries(${executable} PRIVATE "swiftCore") - endif() - - # At runtime link against the built swift libraries from the current - # bootstrapping stage. - set_target_properties(${executable} PROPERTIES - BUILD_WITH_INSTALL_RPATH YES - INSTALL_RPATH "$ORIGIN/../lib/swift/${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_LIB_SUBDIR}") - - elseif(BOOTSTRAPPING_MODE STREQUAL "BOOTSTRAPPING-WITH-HOSTLIBS") - message(FATAL_ERROR "BOOTSTRAPPING_MODE 'BOOTSTRAPPING-WITH-HOSTLIBS' not supported on Linux") - else() - message(FATAL_ERROR "Unknown BOOTSTRAPPING_MODE '${BOOTSTRAPPING_MODE}'") - endif() + if (ASHT_HAS_SWIFT_MODULES) + _add_swift_runtime_link_flags(${executable} "../lib" "${ASHT_BOOTSTRAPPING}") endif() llvm_update_compile_flags(${executable}) diff --git a/include/swift/Basic/InitializeSwiftModules.h b/include/swift/Basic/InitializeSwiftModules.h index d7984af4c4e0f..e627525be031a 100644 --- a/include/swift/Basic/InitializeSwiftModules.h +++ b/include/swift/Basic/InitializeSwiftModules.h @@ -18,6 +18,7 @@ extern "C" { #endif void initializeSwiftModules(); +void initializeSwiftParseModules(); #ifdef __cplusplus } diff --git a/test/Syntax/Parser/unterminated_multiline_regex.swift b/test/Syntax/Parser/unterminated_multiline_regex.swift index b083f28d64c55..1c52137892ea2 100644 --- a/test/Syntax/Parser/unterminated_multiline_regex.swift +++ b/test/Syntax/Parser/unterminated_multiline_regex.swift @@ -1,5 +1,6 @@ // RUN: %swift-syntax-parser-test -dump-diags %s | %FileCheck %s -// CHECK: 7:1 Error: unterminated regex literal +// REQUIRES: swift_in_compiler +// CHECK: 6:1 Error: unterminated regex literal // CHECK: 1 error(s) 0 warnings(s) 0 note(s) #/ diff --git a/test/Syntax/Parser/unterminated_regex.swift b/test/Syntax/Parser/unterminated_regex.swift index c205ce8a6906e..d8f13d78612b3 100644 --- a/test/Syntax/Parser/unterminated_regex.swift +++ b/test/Syntax/Parser/unterminated_regex.swift @@ -1,6 +1,7 @@ // RUN: %swift-syntax-parser-test -dump-diags --swift-version 5 --enable-bare-slash-regex %s | %FileCheck %s -// CHECK: 6:21 Error: unterminated regex literal +// REQUIRES: swift_in_compiler +// CHECK: 7:1 Error: unterminated regex literal // CHECK: 1 error(s) 0 warnings(s) 0 note(s) // IMPORTANT: This file must not contain a trailing newline -/unterminatedLiteral \ No newline at end of file +#/unterminatedLiteral \ No newline at end of file diff --git a/test/Syntax/round_trip_regex.swift b/test/Syntax/round_trip_regex.swift index 54503c9d2124a..41b836ec83384 100644 --- a/test/Syntax/round_trip_regex.swift +++ b/test/Syntax/round_trip_regex.swift @@ -1,6 +1,7 @@ // RUN: %empty-directory(%t) // RUN: %swift-syntax-test -input-source-filename %s -parse-gen -fail-on-parse-error > %t/afterRoundtrip.swift // RUN: diff -u %s %t/afterRoundtrip.swift +// REQUIRES: swift_in_compiler _ = /abc/ _ = #/abc/# diff --git a/test/Syntax/round_trip_regex_escape_newline.swift b/test/Syntax/round_trip_regex_escape_newline.swift index 8db0eb715721b..58561ec306236 100644 --- a/test/Syntax/round_trip_regex_escape_newline.swift +++ b/test/Syntax/round_trip_regex_escape_newline.swift @@ -3,8 +3,10 @@ // RUN: diff -u %s %t/afterRoundtrip.swift // RUN: cat %t/errors.swift | %FileCheck %s +// REQUIRES: swift_in_compiler + // Escaping newlines is not supported _ = /\ / -// CHECK: 7:7: error: unterminated regex literal +// CHECK: 10:1: error: expected expression path in Swift key path diff --git a/tools/libSwiftSyntaxParser/CMakeLists.txt b/tools/libSwiftSyntaxParser/CMakeLists.txt index 9de30f18059b0..38d103093862c 100644 --- a/tools/libSwiftSyntaxParser/CMakeLists.txt +++ b/tools/libSwiftSyntaxParser/CMakeLists.txt @@ -10,12 +10,14 @@ set(LLVM_EXPORTED_SYMBOL_FILE add_swift_host_library(libSwiftSyntaxParser SHARED c-include-check.c libSwiftSyntaxParser.cpp + HAS_SWIFT_MODULES LLVM_LINK_COMPONENTS support) if(NOT SWIFT_BUILT_STANDALONE AND NOT CMAKE_C_COMPILER_ID MATCHES Clang) add_dependencies(libSwiftSyntaxParser clang) endif() target_link_libraries(libSwiftSyntaxParser PRIVATE - swiftParse) + swiftParse + swiftCompilerModules_SwiftSyntax) set_target_properties(libSwiftSyntaxParser PROPERTIES OUTPUT_NAME ${SYNTAX_PARSER_LIB_NAME}) diff --git a/tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp b/tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp index d5f0025f52dad..a1d95a705c9e4 100644 --- a/tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp +++ b/tools/libSwiftSyntaxParser/libSwiftSyntaxParser.cpp @@ -18,11 +18,11 @@ #include "swift-c/SyntaxParser/SwiftSyntaxParser.h" #include "swift/AST/Module.h" #include "swift/Basic/LangOptions.h" +#include "swift/Basic/InitializeSwiftModules.h" #include "swift/Basic/SourceManager.h" #include "swift/Basic/Version.h" #include "swift/Parse/Parser.h" #include "swift/Parse/SyntaxParseActions.h" -#include "swift/Parse/SyntaxRegexFallbackLexing.h" #include "swift/Syntax/Serialization/SyntaxSerialization.h" #include "swift/Syntax/SyntaxNodes.h" #include "swift/Subsystems.h" @@ -498,7 +498,11 @@ struct SynParserDiagConsumer: public DiagnosticConsumer { }; swiftparse_client_node_t SynParser::parse(const char *source, size_t len) { - registerSyntaxFallbackRegexParser(); + + // Initialize libswift modules. + static std::once_flag flag; + std::call_once(flag, []() { initializeSwiftParseModules(); }); + SourceManager SM; unsigned bufID = SM.addNewSourceBuffer(llvm::MemoryBuffer::getMemBuffer( StringRef(source, len), "syntax_parse_source")); diff --git a/tools/swift-syntax-test/CMakeLists.txt b/tools/swift-syntax-test/CMakeLists.txt index 6393e83a6ea9f..99e242a5d6da0 100644 --- a/tools/swift-syntax-test/CMakeLists.txt +++ b/tools/swift-syntax-test/CMakeLists.txt @@ -1,5 +1,6 @@ add_swift_host_tool(swift-syntax-test swift-syntax-test.cpp + HAS_SWIFT_MODULES LLVM_LINK_COMPONENTS Support SWIFT_COMPONENT testsuite-tools @@ -10,4 +11,5 @@ target_link_libraries(swift-syntax-test swiftDriver swiftFrontend swiftSema - swiftSyntax) + swiftSyntax + swiftCompilerModules_SwiftSyntax) diff --git a/tools/swift-syntax-test/swift-syntax-test.cpp b/tools/swift-syntax-test/swift-syntax-test.cpp index e1d133f17aa70..0835f38996609 100644 --- a/tools/swift-syntax-test/swift-syntax-test.cpp +++ b/tools/swift-syntax-test/swift-syntax-test.cpp @@ -21,6 +21,7 @@ #include "swift/AST/DiagnosticEngine.h" #include "swift/AST/DiagnosticsFrontend.h" #include "swift/Basic/Defer.h" +#include "swift/Basic/InitializeSwiftModules.h" #include "swift/Basic/LLVMInitialize.h" #include "swift/Basic/LangOptions.h" #include "swift/Basic/SourceManager.h" @@ -28,7 +29,6 @@ #include "swift/Frontend/PrintingDiagnosticConsumer.h" #include "swift/Parse/Lexer.h" #include "swift/Parse/Parser.h" -#include "swift/Parse/SyntaxRegexFallbackLexing.h" #include "swift/Subsystems.h" #include "swift/Syntax/Serialization/SyntaxDeserialization.h" #include "swift/Syntax/Serialization/SyntaxSerialization.h" @@ -552,7 +552,7 @@ struct ParseInfo { int parseFile( const char *MainExecutablePath, const StringRef InputFileName, llvm::function_ref ActionSpecificCallback) { - registerSyntaxFallbackRegexParser(); + // The cache needs to be a heap allocated pointer since we construct it inside // an if block but need to keep it alive until the end of the function. SyntaxParsingCache *SyntaxCache = nullptr; @@ -899,6 +899,7 @@ static int invokeCommand(const char *MainExecutablePath, int main(int argc, char *argv[]) { PROGRAM_START(argc, argv); + initializeSwiftParseModules(); llvm::cl::ParseCommandLineOptions(argc, argv, "Swift Syntax Test\n"); int ExitCode = EXIT_SUCCESS; From 8826ef64d58347670ee03eb47147561ffaede515 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 2 Jun 2022 09:54:43 -0700 Subject: [PATCH 2/2] [Syntax] Remove fallback regex parsing logic Now that libSwiftSyntaxParser links to libswift. We don't need this anymore. --- .../swift/Parse/SyntaxRegexFallbackLexing.h | 21 --- lib/Parse/CMakeLists.txt | 3 +- lib/Parse/SyntaxRegexFallbackLexing.cpp | 153 ------------------ 3 files changed, 1 insertion(+), 176 deletions(-) delete mode 100644 include/swift/Parse/SyntaxRegexFallbackLexing.h delete mode 100644 lib/Parse/SyntaxRegexFallbackLexing.cpp diff --git a/include/swift/Parse/SyntaxRegexFallbackLexing.h b/include/swift/Parse/SyntaxRegexFallbackLexing.h deleted file mode 100644 index 539483622d6be..0000000000000 --- a/include/swift/Parse/SyntaxRegexFallbackLexing.h +++ /dev/null @@ -1,21 +0,0 @@ -//===--- SyntaxRegexFallbackLexing.h --------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 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 -// -//===----------------------------------------------------------------------===// - -namespace swift { -/// SwiftSyntax parsing currently doesn't link against -/// SwiftExperimentalStringProcessing and is thus missing the regex lexing -/// functions defined in it. This registers a fallback regex-lexing function -/// implemented in C++ that is sufficient to generate a valid SwiftSyntax tree. -/// The regex parser registered by this function will accept all regex literals -/// and is not suited for normal compilation. -void registerSyntaxFallbackRegexParser(); -} // end namespace swift diff --git a/lib/Parse/CMakeLists.txt b/lib/Parse/CMakeLists.txt index 514f591a3a21c..b7d694b6eeb12 100644 --- a/lib/Parse/CMakeLists.txt +++ b/lib/Parse/CMakeLists.txt @@ -24,8 +24,7 @@ add_swift_host_library(swiftParse STATIC ParseType.cpp PersistentParserState.cpp SyntaxParsingCache.cpp - SyntaxParsingContext.cpp - SyntaxRegexFallbackLexing.cpp) + SyntaxParsingContext.cpp) _swift_gyb_target_sources(swiftParse PRIVATE ParsedSyntaxBuilders.cpp.gyb ParsedSyntaxNodes.cpp.gyb diff --git a/lib/Parse/SyntaxRegexFallbackLexing.cpp b/lib/Parse/SyntaxRegexFallbackLexing.cpp deleted file mode 100644 index 7a0077665fddf..0000000000000 --- a/lib/Parse/SyntaxRegexFallbackLexing.cpp +++ /dev/null @@ -1,153 +0,0 @@ -//===--- SyntaxRegexFallbackLexing.cpp ------------------------------------===// -// -// This source file is part of the Swift.org open source project -// -// Copyright (c) 2014 - 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 -// -//===----------------------------------------------------------------------===// - -#include "swift/Parse/SyntaxRegexFallbackLexing.h" -#include "swift/AST/DiagnosticEngine.h" -#include "swift/AST/DiagnosticsParse.h" -#include "swift/Parse/Lexer.h" -#include "swift/Parse/RegexParserBridging.h" -#include - -using namespace swift; - -template -static void diagnose(BridgedOptionalDiagnosticEngine bridgedDiag, - const char *ptr, Diag DiagID, - ArgTypes &&...Args) { - if (auto *Diag = static_cast(bridgedDiag.object)) { - Diag->diagnose(SourceLoc(llvm::SMLoc::getFromPointer(ptr)), DiagID, - std::forward(Args)...); - } -} - -bool syntaxparse_lexRegexLiteral( - const char **InputPtr, const char *BufferEnd, bool MustBeRegex, - BridgedOptionalDiagnosticEngine BridgedDiagEngine) { - - const char *Ptr = *InputPtr; - - // Count leading '#'. - while (*Ptr == '#') { - ++Ptr; - } - if (*Ptr != '/') { - // This wasn't a regex literal. - return true; - } - - unsigned customDelimiterLen = Ptr - *InputPtr; - - ++Ptr; - - // If the delimiter allows multi-line, try skipping over any whitespace to a - // newline character. If we can do that, we enter multi-line mode. - bool allowsMultiline = customDelimiterLen != 0; - const char *firstNewline = nullptr; - if (allowsMultiline) { - while (Ptr != BufferEnd) { - switch (*Ptr) { - case ' ': - case '\t': - ++Ptr; - continue; - case '\r': - case '\n': - firstNewline = Ptr; - break; - default: - break; - } - break; - } - } - - bool isMultilineLiteral = (firstNewline != nullptr); - - while (true) { - switch (*Ptr++) { - case '\r': - case '\n': - if (!isMultilineLiteral) { - diagnose(BridgedDiagEngine, Ptr, diag::lex_regex_literal_unterminated); - *InputPtr = Ptr - 1; - return false; - } - break; - case '\\': - if (Ptr != BufferEnd) { - if (!isMultilineLiteral && (*Ptr == '\r' || *Ptr == '\n')) { - diagnose(BridgedDiagEngine, Ptr, diag::lex_regex_literal_unterminated); - *InputPtr = Ptr - 1; - return false; - } - if (validateUTF8CharacterAndAdvance(Ptr, BufferEnd) == ~0U) - diagnose(BridgedDiagEngine, Ptr, diag::lex_invalid_utf8); - } - break; - case '/': { - const char *AfterSlashPos = Ptr; - - // Eat '#' up to the open delimeter length. - while (*Ptr == '#' && (Ptr - AfterSlashPos) <= customDelimiterLen) { - ++Ptr; - } - - if ((Ptr - AfterSlashPos) != customDelimiterLen) { - // '#' count didn't match. Reset the cursor after the '/' and move on. - Ptr = AfterSlashPos; - break; - } - - // Found the closing delimiter. Finish. - *InputPtr = Ptr; - return false; - } - case '\0': { - if (Ptr - 1 == BufferEnd) { - // Reached to EOF. - diagnose(BridgedDiagEngine, Ptr - 1, diag::lex_regex_literal_unterminated); - // In multi-line mode, we don't want to skip over what is likely - // otherwise valid Swift code, so resume from the first newline. - *InputPtr = firstNewline ? firstNewline : (Ptr - 1); - return false; - } - - // TODO: Warn to match the behavior of String literal lexer? - // For now, just ignore them. - break; - } - default: { - --Ptr; - if (validateUTF8CharacterAndAdvance(Ptr, BufferEnd) == ~0U) - diagnose(BridgedDiagEngine, Ptr, diag::lex_invalid_utf8); - break; - } - } - } -} - -bool syntaxparse_parseRegexLiteral(const char *InputPtr, unsigned *VersionOut, - void *CaptureStructureOut, - unsigned CaptureStructureSize, - BridgedSourceLoc DiagnosticBaseLoc, - BridgedDiagnosticEngine BridgedDiagEngine) { - *VersionOut = ~0u; - return /*hasError*/ false; -} - -void swift::registerSyntaxFallbackRegexParser() { - static std::once_flag flag; - std::call_once(flag, []() { - Parser_registerRegexLiteralLexingFn(syntaxparse_lexRegexLiteral); - Parser_registerRegexLiteralParsingFn(syntaxparse_parseRegexLiteral); - }); -}