Skip to content

Commit f8c77e1

Browse files
authored
Re-apply "SwiftSyntax: Teach SwiftSyntax to use SourceKitd to serialize syntax trees. (#14424)" (#14465)
1 parent 21de54a commit f8c77e1

File tree

15 files changed

+573
-26
lines changed

15 files changed

+573
-26
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ set_property(GLOBAL PROPERTY JOB_POOLS local_jobs=${localhost_logical_cores})
1111
# Put linking in that category
1212
set_property(GLOBAL PROPERTY JOB_POOL_LINK local_jobs)
1313

14+
ENABLE_LANGUAGE(C)
15+
1416
# First include general CMake utilities.
1517
include(SwiftUtils)
1618
include(CheckSymbolExists)
@@ -748,10 +750,10 @@ if(swift_build_android AND NOT "${SWIFT_ANDROID_NDK_PATH}" STREQUAL "")
748750

749751
set(SWIFT_ANDROID_PREBUILT_PATH
750752
"${SWIFT_ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_suffix}")
751-
753+
752754
# Resolve the correct linker based on the file name of CMAKE_LINKER (being 'ld' or 'ld.gold' the options)
753755
get_filename_component(SWIFT_ANDROID_LINKER_NAME "${CMAKE_LINKER}" NAME)
754-
set(SWIFT_SDK_ANDROID_ARCH_armv7_LINKER
756+
set(SWIFT_SDK_ANDROID_ARCH_armv7_LINKER
755757
"${SWIFT_ANDROID_NDK_PATH}/toolchains/arm-linux-androideabi-${SWIFT_ANDROID_NDK_GCC_VERSION}/prebuilt/${_swift_android_prebuilt_suffix}/bin/arm-linux-androideabi-${SWIFT_ANDROID_LINKER_NAME}")
756758

757759
configure_sdk_unix(ANDROID "Android" "android" "android" "armv7" "armv7-none-linux-androideabi" "${SWIFT_ANDROID_SDK_PATH}")
@@ -879,7 +881,7 @@ endif()
879881
###############
880882
#
881883
# We have to include stdlib/ before tools/.
882-
# Do not move add_subdirectory(stdlib) after add_subdirectory(tools)!
884+
# Do not move add_subdirectory(stdlib) after add_subdirectory(tools)!
883885
#
884886
# We must include stdlib/ before tools/ because stdlib/CMakeLists.txt
885887
# declares the swift-stdlib-* set of targets. These targets will then
@@ -898,7 +900,7 @@ add_subdirectory(stdlib)
898900
if(SWIFT_INCLUDE_TOOLS)
899901
add_subdirectory(include)
900902
add_subdirectory(lib)
901-
903+
902904
# Always include this after including stdlib/!
903905
# Refer to the large comment above the add_subdirectory(stdlib) call.
904906
# https://bugs.swift.org/browse/SR-5975

cmake/modules/AddSwift.cmake

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ function(_add_variant_c_compile_flags)
255255
else()
256256
list(APPEND result "-DNDEBUG")
257257
endif()
258-
258+
259259
if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
260260
list(APPEND result "-DSWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
261261
endif()
@@ -320,7 +320,7 @@ function(_add_variant_swift_compile_flags
320320
if (SWIFT_ENABLE_GUARANTEED_NORMAL_ARGUMENTS)
321321
list(APPEND result "-Xfrontend" "-enable-guaranteed-normal-arguments")
322322
endif()
323-
323+
324324
if(SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS)
325325
list(APPEND result "-D" "SWIFT_ENABLE_RUNTIME_FUNCTION_COUNTERS")
326326
endif()
@@ -1605,7 +1605,7 @@ function(add_swift_library name)
16051605
if(SWIFTLIB_IS_SDK_OVERLAY)
16061606
list(APPEND swiftlib_swift_compile_flags_all "-Fsystem" "${SWIFT_SDK_${sdk}_PATH}/System/Library/PrivateFrameworks/")
16071607
endif()
1608-
1608+
16091609
if("${sdk}" STREQUAL "IOS_SIMULATOR")
16101610
if("${name}" STREQUAL "swiftMediaPlayer")
16111611
message("DISABLING AUTOLINK FOR swiftMediaPlayer")
@@ -1854,7 +1854,7 @@ function(add_swift_library name)
18541854
LIBRARY DESTINATION lib${LLVM_LIBDIR_SUFFIX}
18551855
RUNTIME DESTINATION bin)
18561856
swift_is_installing_component(dev is_installing)
1857-
1857+
18581858
if(NOT is_installing)
18591859
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${name})
18601860
else()
@@ -2177,7 +2177,7 @@ function(add_swift_host_tool executable)
21772177

21782178
swift_is_installing_component(${ADDSWIFTHOSTTOOL_SWIFT_COMPONENT}
21792179
is_installing)
2180-
2180+
21812181
if(NOT is_installing)
21822182
set_property(GLOBAL APPEND PROPERTY SWIFT_BUILDTREE_EXPORTS ${executable})
21832183
else()

test/SwiftSyntax/DeserializeFile.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import StdlibUnittest
77
import Foundation
88
import SwiftSyntax
9+
import SwiftSourceKit
910

1011
func getInput(_ file: String) -> URL {
1112
var result = URL(fileURLWithPath: #file)

test/SwiftSyntax/ParseFile.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// REQUIRES: executable_test
33
// REQUIRES: OS=macosx
44
// REQUIRES: objc_interop
5-
// REQUIRES: rdar36740859
65

76
import Foundation
87
import StdlibUnittest
98
import SwiftSyntax
9+
import SwiftSourceKit
1010

1111
var ParseFile = TestSuite("ParseFile")
1212

@@ -31,7 +31,8 @@ ParseFile.test("ParseSingleFile") {
3131
let currentFile = URL(fileURLWithPath: #file)
3232
expectDoesNotThrow({
3333
let currentFileContents = try String(contentsOf: currentFile)
34-
let parsed = try SourceFileSyntax.parse(currentFile)
34+
let parsed = try SourceFileSyntax.decodeSourceFileSyntax(try
35+
SourceKitdService.encodeSourceFileSyntax(currentFile))
3536
expectEqual("\(parsed)", currentFileContents)
3637
})
3738
}

test/SwiftSyntax/VisitorTest.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
// REQUIRES: OS=macosx
44
// REQUIRES: objc_interop
55

6-
// FIXME: This test fails occassionally in CI with invalid json.
7-
// REQUIRES: disabled
8-
96
import StdlibUnittest
107
import Foundation
118
import SwiftSyntax
9+
import SwiftSourceKit
1210

1311
func getInput(_ file: String) -> URL {
1412
var result = URL(fileURLWithPath: #file)
@@ -29,7 +27,8 @@ VisitorTests.test("Basic") {
2927
}
3028
}
3129
expectDoesNotThrow({
32-
let parsed = try SourceFileSyntax.parse(getInput("visitor.swift"))
30+
let parsed = try SourceFileSyntax.decodeSourceFileSyntax(try
31+
SourceKitdService.encodeSourceFileSyntax(getInput("visitor.swift")))
3332
let counter = FuncCounter()
3433
let hashBefore = parsed.hashValue
3534
counter.visit(parsed)

test/lit.cfg

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ else:
286286
test_resource_dir = os.path.join(config.swift_lib_dir, 'swift')
287287
resource_dir_opt = ""
288288
stdlib_resource_dir_opt = resource_dir_opt
289+
sourcekitd_framework_dir = config.swift_lib_dir
289290
lit_config.note('Using resource dir: ' + test_resource_dir)
290291

291292
# Parse the variant triple.
@@ -670,12 +671,13 @@ if run_vendor == 'apple':
670671
(run_cpu, run_os, run_vers, clang_mcp_opt))
671672

672673
config.target_build_swift = (
673-
"%s %s %s -F %s -Xlinker -rpath -Xlinker %s %s %s %s %s"
674+
"%s %s %s -F %s -Xlinker -rpath -Xlinker %s %s %s %s %s -F %s -Xlinker -rpath -Xlinker %s"
674675
% (xcrun_prefix, config.swiftc, target_options,
675676
extra_frameworks_dir, extra_frameworks_dir,
676677
sdk_overlay_linker_opt, config.swift_test_options,
677678
config.swift_driver_test_options,
678-
swift_execution_tests_extra_flags))
679+
swift_execution_tests_extra_flags, sourcekitd_framework_dir,
680+
sourcekitd_framework_dir))
679681
config.target_run = ""
680682

681683
if 'interpret' in lit_config.params:

tools/CMakeLists.txt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
include(CheckIncludeFiles)
2+
check_include_files("xpc/xpc.h" HAVE_XPC_H)
3+
4+
swift_is_installing_component(sourcekit-inproc SOURCEKIT_INSTALLING_INPROC)
5+
6+
if(HAVE_XPC_H AND SWIFT_BUILD_SOURCEKIT AND NOT SOURCEKIT_INSTALLING_INPROC)
7+
set(BUILD_SOURCEKIT_XPC_SERVICE TRUE)
8+
else()
9+
set(BUILD_SOURCEKIT_XPC_SERVICE FALSE)
10+
endif()
11+
112
# Add generated libSyntax headers to global dependencies.
213
list(APPEND LLVM_COMMON_DEPENDS swift-syntax-generated-headers)
314

@@ -16,19 +27,27 @@ add_swift_tool_subdirectory(swift-api-digester)
1627
add_swift_tool_subdirectory(swift-syntax-test)
1728
add_swift_tool_subdirectory(swift-refactor)
1829

30+
if(SWIFT_BUILD_STDLIB AND SWIFT_BUILD_SDK_OVERLAY)
31+
set(BUILD_FOUNDATION TRUE)
32+
else()
33+
set(BUILD_FOUNDATION FALSE)
34+
endif()
35+
1936
if(SWIFT_BUILD_SOURCEKIT)
2037
add_swift_tool_subdirectory(SourceKit)
38+
if(BUILD_SOURCEKIT_XPC_SERVICE AND BUILD_FOUNDATION)
39+
add_subdirectory(SwiftSourceKitClient)
40+
endif()
2141
endif()
2242

23-
2443
if(SWIFT_HOST_VARIANT STREQUAL "macosx")
2544
# Only build Darwin-specific tools when deploying to OS X.
2645
add_swift_tool_subdirectory(swift-stdlib-tool)
2746

2847
# SwiftSyntax depends on both the standard library (because it's a
2948
# Swift module), and the SDK overlays (because it depends on Foundation).
3049
# Ensure we only build SwiftSyntax when we're building both.
31-
if(SWIFT_BUILD_STDLIB AND SWIFT_BUILD_SDK_OVERLAY)
50+
if(BUILD_FOUNDATION)
3251
add_subdirectory(SwiftSyntax)
3352
endif()
3453
endif()

tools/SourceKit/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ configure_file(
2424
set(SOURCEKIT_DEPLOYMENT_OS "${SWIFT_HOST_VARIANT}")
2525
set(SOURCEKIT_DEPLOYMENT_TARGET "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_DEPLOYMENT_VERSION}")
2626

27-
swift_is_installing_component(sourcekit-inproc SOURCEKIT_INSTALLING_INPROC)
28-
2927
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin" AND NOT CMAKE_CROSSCOMPILING)
3028
set(CMAKE_OSX_SYSROOT "${SWIFT_SDK_${SWIFT_HOST_VARIANT_SDK}_PATH}")
3129
set(CMAKE_OSX_ARCHITECTURES "${SWIFT_HOST_VARIANT_ARCH}")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
add_subdirectory(InProc)
2-
if (HAVE_XPC_H)
2+
if(HAVE_XPC_H)
33
add_subdirectory(XPC)
44
endif()

tools/SourceKit/tools/sourcekitd/lib/API/CMakeLists.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
include(CheckIncludeFiles)
2-
3-
check_include_files("xpc/xpc.h" HAVE_XPC_H)
4-
51
# If we were going to build for APPLE but don't have XPC, just build inproc.
62
if(APPLE AND NOT HAVE_XPC_H)
73
set(SWIFT_SOURCEKIT_USE_INPROC_LIBRARY TRUE)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
set(EXTRA_COMPILE_FLAGS "-F" "${SWIFT_LIBRARY_OUTPUT_INTDIR}")
2+
set(EXTRA_LINKER_FLAGS "-Xlinker" "-rpath" "-Xlinker" "${SWIFT_LIBRARY_OUTPUT_INTDIR}"
3+
"-Xlinker" "-F" "-Xlinker" "${SWIFT_LIBRARY_OUTPUT_INTDIR}")
4+
5+
add_swift_library(swiftSwiftSourceKit SHARED
6+
SourceKitdClient.swift
7+
SourceKitdRequest.swift
8+
SourceKitdResponse.swift
9+
SourceKitdUID.swift
10+
11+
DEPENDS SourceKitService
12+
SWIFT_COMPILE_FLAGS ${EXTRA_COMPILE_FLAGS}
13+
LINK_FLAGS ${EXTRA_LINKER_FLAGS}
14+
SWIFT_MODULE_DEPENDS Foundation
15+
INSTALL_IN_COMPONENT swift-syntax
16+
TARGET_SDKS OSX
17+
IS_STDLIB)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===--------------------- SourceKitdClient.swift -------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
// This file provides a wrapper of SourceKitd service.
13+
//===----------------------------------------------------------------------===//
14+
15+
import sourcekitd
16+
import Foundation
17+
18+
public class SourceKitdService {
19+
20+
public init() {
21+
sourcekitd_initialize()
22+
}
23+
deinit {
24+
sourcekitd_shutdown()
25+
}
26+
public func sendSyn(request: SourceKitdRequest) -> SourceKitdResponse {
27+
return SourceKitdResponse(resp: sourcekitd_send_request_sync(request.rawRequest))
28+
}
29+
}
30+
31+
extension SourceKitdService {
32+
/// Parses the Swift file at the provided URL into a `Syntax` tree in Json
33+
/// serialization format by querying SourceKitd service.
34+
/// - Parameter url: The URL you wish to parse.
35+
/// - Returns: The syntax tree in Json format string.
36+
public static func encodeSourceFileSyntax(_ url: URL) throws -> String {
37+
let Service = SourceKitdService()
38+
let Request = SourceKitdRequest(uid: .source_request_editor_open)
39+
let Path = url.path
40+
Request.addParameter(.key_sourcefile, value: Path)
41+
Request.addParameter(.key_name, value: Path)
42+
Request.addParameter(.key_enable_syntax_tree, value: 1)
43+
44+
// FIXME: SourceKitd error handling.
45+
let Resp = Service.sendSyn(request: Request)
46+
return Resp.value.getString(.key_serialized_syntax_tree)
47+
}
48+
}

0 commit comments

Comments
 (0)