Skip to content

Add support for reading fine-grained source file dependency graphs #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Sources/SwiftDriver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ add_library(SwiftDriver
"Incremental Compilation/IncrementalCompilation.swift"
"Incremental Compilation/InputIInfoMap.swift"
"Incremental Compilation/InputInfo.swift"
"Incremental Compilation/SourceFileDependencyGraph.swift"

Jobs/AutolinkExtractJob.swift
Jobs/CommandLineArguments.swift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//===------- SourceFileDependencyGraph.swift - Read swiftdeps files -------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
@_implementationOnly import Yams

public struct SourceFileDependencyGraph: Codable {
public static let sourceFileProvidesInterfaceSequenceNumber: Int = 0
public static let sourceFileProvidesImplementationSequenceNumber: Int = 1

private var allNodes: [Node]

public var sourceFileNodePair: (interface: Node, implementation: Node) {
(interface: allNodes[SourceFileDependencyGraph.sourceFileProvidesInterfaceSequenceNumber],
implementation: allNodes[SourceFileDependencyGraph.sourceFileProvidesImplementationSequenceNumber])
}

public func forEachNode(_ doIt: (Node)->Void) {
allNodes.forEach(doIt)
}

public func forEachDefDependedUpon(by node: Node, _ doIt: (Node)->Void) {
for sequenceNumber in node.defsIDependUpon {
doIt(allNodes[sequenceNumber])
}
}

public func forEachArc(_ doIt: (Node, Node)->Void) {
forEachNode { useNode in
forEachDefDependedUpon(by: useNode) { defNode in
doIt(defNode, useNode)
}
}
}

@discardableResult public func verify() -> Bool {
assert(Array(allNodes.indices) == allNodes.map { $0.sequenceNumber })
forEachNode {
$0.verify()
}
return true
}

public init(contents: String) throws {
let decoder = YAMLDecoder()
self = try decoder.decode(Self.self, from: contents)
assert(verify())
}
}

public enum DeclAspect: String, Codable {
case interface, implementation
}

public struct DependencyKey: Codable {
public enum Kind: String, Codable {
case topLevel
case nominal
case potentialMember
case member
case dynamicLookup
case externalDepend
case sourceFileProvide
}

public var kind: Kind
public var aspect: DeclAspect
public var context: String
public var name: String

public func verify() {
assert(kind != .externalDepend || aspect == .interface, "All external dependencies must be interfaces.")
switch kind {
case .topLevel, .dynamicLookup, .externalDepend, .sourceFileProvide:
assert(context.isEmpty && !name.isEmpty, "Must only have a name")
case .nominal, .potentialMember:
assert(!context.isEmpty && name.isEmpty, "Must only have a context")
case .member:
assert(!context.isEmpty && !name.isEmpty, "Must have both")
}
}
}


extension SourceFileDependencyGraph {
public struct Node: Codable {
public var key: DependencyKey
public var fingerprint: String?
public var sequenceNumber: Int
public var defsIDependUpon: [Int]
public var isProvides: Bool

public func verify() {
key.verify()

if key.kind == .sourceFileProvide {
switch key.aspect {
case .interface:
assert(sequenceNumber == SourceFileDependencyGraph.sourceFileProvidesInterfaceSequenceNumber)
case .implementation:
assert(sequenceNumber == SourceFileDependencyGraph.sourceFileProvidesImplementationSequenceNumber)
}
}
}
}
}
45 changes: 30 additions & 15 deletions Tests/SwiftDriverTests/IncrementalCompilationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,7 @@ import SwiftDriver

final class IncrementalCompilationTests: XCTestCase {
func testInputInfoMapReading() throws {
let contents = """
version: "Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)"
options: "abbbfbcaf36b93e58efaadd8271ff142"
build_time: [1570318779, 32358000]
inputs:
"/Volumes/AS/repos/swift-driver/sandbox/sandbox/sandbox/file2.swift": !dirty [1570318778, 0]
"/Volumes/AS/repos/swift-driver/sandbox/sandbox/sandbox/main.swift": [1570083660, 0]
"/Volumes/gazorp.swift": !private [0,0]
"""

let inputInfoMap = try! InputInfoMap(contents: contents)
// print(inputInfoMap.buildTime)
// inputInfoMap.inputs.forEach {
// print($0.key, $0.value)
// }
let inputInfoMap = try! InputInfoMap(contents: Inputs.inputInfoMap)
XCTAssertEqual(inputInfoMap.swiftVersion,
"Apple Swift version 5.1 (swiftlang-1100.0.270.13 clang-1100.0.33.7)")
XCTAssertEqual(inputInfoMap.argsHash, "abbbfbcaf36b93e58efaadd8271ff142")
Expand All @@ -49,5 +35,34 @@ final class IncrementalCompilationTests: XCTestCase {
previousModTime: Date(legacyDriverSecsAndNanos: [0, 0]))
])
}

func testReadSourceFileDependencyGraph() throws {
let graph = try SourceFileDependencyGraph(contents: Inputs.fineGrainedSourceFileDependencyGraph)

graph.verify()

var found = false
graph.forEachNode { node in
guard node.sequenceNumber == 10 else { return }
found = true
XCTAssertEqual(node.key.kind, .nominal)
XCTAssertEqual(node.key.aspect, .interface)
XCTAssertEqual(node.key.context, "5hello3FooV")
XCTAssertTrue(node.key.name.isEmpty)
XCTAssertEqual(node.fingerprint, "8daabb8cdf69d8e8702b4788be12efd6")
XCTAssertTrue(node.isProvides)

graph.forEachDefDependedUpon(by: node) { def in
XCTAssertTrue(def.sequenceNumber == SourceFileDependencyGraph.sourceFileProvidesInterfaceSequenceNumber)
XCTAssertEqual(def.key.kind, .sourceFileProvide)
XCTAssertEqual(def.key.aspect, .interface)
XCTAssertTrue(def.key.context.isEmpty)
XCTAssertTrue(def.key.name.hasSuffix("/hello.swiftdeps"))
XCTAssertEqual(def.fingerprint, "85188db3503106210367dbcb7f5d1524")
XCTAssertTrue(def.isProvides)
}
}
XCTAssertTrue(found)
}
}

Loading