Skip to content

Commit 34bd4c0

Browse files
Merge pull request #372 from cltnschlosser/cs_driverPrintActions
Rework printActions to be more like how the actions are created
2 parents de4c66b + 1f81adb commit 34bd4c0

File tree

3 files changed

+68
-17
lines changed

3 files changed

+68
-17
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,7 @@ extension Driver {
825825
// Print actions using the same style as the old C++ driver
826826
// This is mostly for testing purposes. We should print semantically
827827
// equivalent actions as the old driver.
828-
Driver.printActions(jobs)
828+
printActions(jobs)
829829
return
830830
}
831831

@@ -915,41 +915,76 @@ extension Driver {
915915
stdoutStream.flush()
916916
}
917917

918-
private static func printActions(_ jobs: [Job]) {
918+
/// This handles -driver-print-actions flag. The C++ driver has a concept of actions
919+
/// which it builds up a list of actions before then creating them into jobs.
920+
/// The swift-driver doesn't have actions, so the logic here takes the jobs and tries
921+
/// to mimic the actions that would be created by the C++ driver and
922+
/// prints them in *hopefully* the same order.
923+
private func printActions(_ jobs: [Job]) {
919924
defer {
920925
stdoutStream.flush()
921926
}
927+
928+
// Put bridging header as first input if we have it
929+
let allInputs: [TypedVirtualPath]
930+
if let objcHeader = importedObjCHeader, bridgingPrecompiledHeader != nil {
931+
allInputs = [TypedVirtualPath(file: objcHeader, type: .objcHeader)] + inputFiles
932+
} else {
933+
allInputs = inputFiles
934+
}
935+
922936
var jobIdMap = Dictionary<Job, UInt>()
923937
// The C++ driver treats each input as an action, we should print them as
924938
// an action too for testing purposes.
925939
var inputIdMap = Dictionary<TypedVirtualPath, UInt>()
926940
var nextId: UInt = 0
941+
var allInputsIterator = allInputs.makeIterator()
927942
for job in jobs {
943+
// After "module input" jobs, print any left over inputs
944+
switch job.kind {
945+
case .generatePCH, .compile, .backend:
946+
break
947+
default:
948+
while let input = allInputsIterator.next() {
949+
Self.printInputIfNew(input, inputIdMap: &inputIdMap, nextId: &nextId)
950+
}
951+
}
928952
// All input action IDs for this action.
929-
var inputIds = Set<UInt>()
953+
var inputIds = [UInt]()
930954
// Collect input job IDs.
931955
for input in job.displayInputs.isEmpty ? job.inputs : job.displayInputs {
956+
if let id = inputIdMap[input] {
957+
inputIds.append(id)
958+
continue
959+
}
932960
var foundInput = false
933961
for (prevJob, id) in jobIdMap {
934962
if prevJob.outputs.contains(input) {
935963
foundInput = true
936-
inputIds.insert(id)
964+
inputIds.append(id)
965+
break
937966
}
938967
}
939-
if (!foundInput) {
940-
if inputIdMap[input] == nil {
941-
stdoutStream <<< nextId <<< ": " <<< "input, "
942-
stdoutStream <<< "\"" <<< input.file <<< "\", " <<< input.type <<< "\n"
943-
inputIdMap[input] = nextId
944-
nextId += 1
968+
if !foundInput {
969+
while let nextInputAction = allInputsIterator.next() {
970+
Self.printInputIfNew(nextInputAction, inputIdMap: &inputIdMap, nextId: &nextId)
971+
if let id = inputIdMap[input] {
972+
inputIds.append(id)
973+
break
974+
}
945975
}
946-
inputIds.insert(inputIdMap[input]!)
947976
}
948977
}
978+
949979
// Print current Job
950980
stdoutStream <<< nextId <<< ": " <<< job.kind.rawValue <<< ", {"
951-
stdoutStream <<< inputIds.sorted().map({ $0.description })
952-
.joined(separator: ", ")
981+
switch job.kind {
982+
// Don't sort for compile jobs. Puts pch last
983+
case .compile:
984+
stdoutStream <<< inputIds.map(\.description).joined(separator: ", ")
985+
default:
986+
stdoutStream <<< inputIds.sorted().map(\.description).joined(separator: ", ")
987+
}
953988
var typeName = job.outputs.first?.type.name
954989
if typeName == nil {
955990
typeName = "none"
@@ -960,6 +995,15 @@ extension Driver {
960995
}
961996
}
962997

998+
private static func printInputIfNew(_ input: TypedVirtualPath, inputIdMap: inout [TypedVirtualPath: UInt], nextId: inout UInt) {
999+
if inputIdMap[input] == nil {
1000+
stdoutStream <<< nextId <<< ": " <<< "input, "
1001+
stdoutStream <<< "\"" <<< input.file <<< "\", " <<< input.type <<< "\n"
1002+
inputIdMap[input] = nextId
1003+
nextId += 1
1004+
}
1005+
}
1006+
9631007
private func printVersion<S: OutputByteStream>(outputStream: inout S) throws {
9641008
outputStream <<< frontendTargetInfo.compilerVersion <<< "\n"
9651009
outputStream <<< "Target: \(frontendTargetInfo.target.triple.triple)\n"

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,20 @@ extension Driver {
291291
inputs.append(TypedVirtualPath(file: moduleOutputInfo.output!.outputPath, type: .swiftModule))
292292
}
293293

294+
var displayInputs = primaryInputs
295+
294296
// Bridging header is needed for compiling these .swift sources.
295297
if let pchPath = bridgingPrecompiledHeader {
296-
inputs.append(TypedVirtualPath(file: pchPath, type: .pch))
298+
let pchInput = TypedVirtualPath(file: pchPath, type: .pch)
299+
inputs.append(pchInput)
300+
displayInputs.append(pchInput)
297301
}
298302
return Job(
299303
moduleName: moduleOutputInfo.name,
300304
kind: .compile,
301305
tool: .absolute(try toolchain.getToolPath(.swiftCompiler)),
302306
commandLine: commandLine,
303-
displayInputs: primaryInputs,
307+
displayInputs: displayInputs,
304308
inputs: inputs,
305309
primaryInputs: primaryInputs,
306310
outputs: outputs,

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ extension Driver {
259259
diagnosticEngine.emit(.error_unexpected_input_file(input.file))
260260
}
261261

262-
case .swiftModule, .swiftDocumentation:
262+
case .swiftModule:
263263
if moduleOutputInfo.output != nil && linkerOutputType == nil {
264264
// When generating a .swiftmodule as a top-level output (as opposed
265265
// to, for example, linking an image), treat .swiftmodule files as
@@ -347,7 +347,10 @@ extension Driver {
347347
addJob(wrapJob)
348348
wrapJob.outputs.forEach(addLinkerInput)
349349
} else {
350-
mergeJob.outputs.forEach(addLinkerInput)
350+
let mergeModuleOutputs = mergeJob.outputs.filter { $0.type == .swiftModule }
351+
assert(mergeModuleOutputs.count == 1,
352+
"Merge module job should only have one swiftmodule output")
353+
addLinkerInput(mergeModuleOutputs[0])
351354
}
352355
}
353356

0 commit comments

Comments
 (0)