Questions with SourceKit framework


(Keqiu Hu) #1

Hey everyone,

I am poking around Swift compiler to see if I am able to get a full AST out
to do some preliminary code sanity check and dead code cleanup. So I was
looking at open source SourceKitten <https://github.com/jpsim/SourceKitten> to
see if I can retrieve a full AST from it, however, I was not able to enough
information from it. So I dug a bit deeper into its dependency SourceKit
<https://github.com/apple/swift/tree/master/tools/SourceKit> source code
and am poking around the sourcekitd-test scheme.

With the guidance of the Protocol.md
<https://github.com/apple/swift/blob/master/tools/SourceKit/docs/Protocol.md>
file, I am able to talk to SourceKit daemon with Indexing request and
Documentation request with a single file.

In order to get a full resolution of the code base, I thought passing in
all compiler args would help. So I pass in all the compiler args to the
sourcekit request based on the command spit out by *xcodebuild*, however,
the results became <<NULL>> :frowning:

*[MinimalSwiftApplication is a minimal iOS app with 3 Swift files]*

{
  key.request: source.request.docinfo,
  key.compilerargs: [
    "-module-name",
    "MinimalSwiftApplication",
    "-Xfrontend",
    "-c",
    "-sdk",

"/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.3.sdk",
    "-target",
    "x86_64-apple-ios10.3",

"/Users/khu/ios/MinimalSwiftApplication/MinimalSwiftApplication/ViewController.swift",

"/Users/khu/ios/MinimalSwiftApplication/MinimalSwiftApplication/AppDelegate.swift",

"/Users/khu/ios/MinimalSwiftApplication/MinimalSwiftApplication/Car.swift",
    "-emit-module",
    "-emit-module-path",

"/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/Objects-normal/x86_64/MinimalSwiftApplication.swiftmodule",
    "-Xcc",

"-I/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/swift-overrides.hmap",
    "-Xcc",
    "-iquote",
    "-Xcc",

"/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/MinimalSwiftApplication-generated-files.hmap",
    "-Xcc",

"-I/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/MinimalSwiftApplication-own-target-headers.hmap",
    "-Xcc",

"-I/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/MinimalSwiftApplication-all-target-headers.hmap",
    "-Xcc",
    "-iquote",
    "-Xcc",

"/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/MinimalSwiftApplication-project-headers.hmap",
    "-Xcc",

"-I/Users/khu/ios/MinimalSwiftApplication/build/Release-iphonesimulator/include",
    "-Xcc",

"-I/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/DerivedSources/x86_64",
    "-Xcc",

"-I/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/DerivedSources",
    "-emit-objc-header",
    "-emit-objc-header-path",

"/Users/khu/ios/MinimalSwiftApplication/build/MinimalSwiftApplication.build/Release-iphonesimulator/MinimalSwiftApplication.build/Objects-normal/x86_64/MinimalSwiftApplication-Swift.h",
    "-Xcc",
    "-working-directory/Users/khu/ios/MinimalSwiftApplication",
    "-D",
    "DEBUG"
  ],
  key.sourcefile:
"/Users/khu/ios/MinimalSwiftApplication/MinimalSwiftApplication/ViewController.swift"
}
[
]
<<NULL>>
Program ended with exit code: 0

*Besides this, I have another two questions.*
1. The information from doc and index request's response is quite
scattered. Is that *possible* to resolve a full AST of the code base from
SourceKit framework (like the Program Structure Interface
<http://www.jetbrains.org/intellij/sdk/docs/basics/architectural_overview/psi_files.html>
format in Intellij)?
2. Debug sourcekitd-test is easy, is there any guidance to debug the
sourcekitd? Basically how to spin off another sourcekitd and debug that.

Thanks!
Keqiu