Skip to content

Commit 456b558

Browse files
committed
Update to new path APIs
Follow up to #1204 that adopts to the new API.
1 parent a84c81e commit 456b558

24 files changed

+147
-133
lines changed

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,10 @@ public struct Driver {
395395
guard let moduleOutput = moduleOutputInfo.output else {
396396
return nil
397397
}
398-
return TypedVirtualPath(file: VirtualPath.lookup(moduleOutput.outputPath)
399-
.replacingExtension(with: .jsonABIBaseline).intern(), type: .jsonABIBaseline)
398+
guard let path = try? VirtualPath.lookup(moduleOutput.outputPath).replacingExtension(with: .jsonABIBaseline) else {
399+
return nil
400+
}
401+
return TypedVirtualPath(file: path.intern(), type: .jsonABIBaseline)
400402
}()
401403

402404
public static func isOptionFound(_ opt: String, allOpts: Set<String>) -> Bool {
@@ -513,7 +515,7 @@ public struct Driver {
513515
// Compute the working directory.
514516
workingDirectory = try parsedOptions.getLastArgument(.workingDirectory).map { workingDirectoryArg in
515517
let cwd = fileSystem.currentWorkingDirectory
516-
return try cwd.map{ AbsolutePath(workingDirectoryArg.asSingle, relativeTo: $0) } ?? AbsolutePath(validating: workingDirectoryArg.asSingle)
518+
return try cwd.map{ try AbsolutePath(validating: workingDirectoryArg.asSingle, relativeTo: $0) } ?? AbsolutePath(validating: workingDirectoryArg.asSingle)
517519
}
518520

519521
// Apply the working directory to the parsed options.
@@ -823,7 +825,7 @@ public struct Driver {
823825
// with private Clang modules.
824826
if let swiftInterfacePath = self.swiftInterfacePath,
825827
givenPrivateInterfacePath == nil {
826-
self.swiftPrivateInterfacePath = VirtualPath.lookup(swiftInterfacePath)
828+
self.swiftPrivateInterfacePath = try VirtualPath.lookup(swiftInterfacePath)
827829
.replacingExtension(with: .privateSwiftInterface).intern()
828830
} else {
829831
self.swiftPrivateInterfacePath = givenPrivateInterfacePath
@@ -1167,7 +1169,10 @@ extension Driver {
11671169
) -> AbsolutePath? {
11681170
let responseFile: AbsolutePath
11691171
if let basePath = basePath {
1170-
responseFile = AbsolutePath(path, relativeTo: basePath)
1172+
guard let absolutePath = try? AbsolutePath(validating: path, relativeTo: basePath) else {
1173+
return nil
1174+
}
1175+
responseFile = absolutePath
11711176
} else {
11721177
guard let absolutePath = try? AbsolutePath(validating: path) else {
11731178
return nil
@@ -1741,7 +1746,7 @@ extension Driver {
17411746
/// Apply the given working directory to all paths in the parsed options.
17421747
private static func applyWorkingDirectory(_ workingDirectory: AbsolutePath,
17431748
to parsedOptions: inout ParsedOptions) throws {
1744-
parsedOptions.forEachModifying { parsedOption in
1749+
try parsedOptions.forEachModifying { parsedOption in
17451750
// Only translate options whose arguments are paths.
17461751
if !parsedOption.option.attributes.contains(.argumentIsPath) { return }
17471752

@@ -1754,12 +1759,12 @@ extension Driver {
17541759
if arg == "-" {
17551760
translatedArgument = parsedOption.argument
17561761
} else {
1757-
translatedArgument = .single(AbsolutePath(arg, relativeTo: workingDirectory).pathString)
1762+
translatedArgument = .single(try AbsolutePath(validating: arg, relativeTo: workingDirectory).pathString)
17581763
}
17591764

17601765
case .multiple(let args):
1761-
translatedArgument = .multiple(args.map { arg in
1762-
AbsolutePath(arg, relativeTo: workingDirectory).pathString
1766+
translatedArgument = .multiple(try args.map { arg in
1767+
try AbsolutePath(validating: arg, relativeTo: workingDirectory).pathString
17631768
})
17641769
}
17651770

@@ -2400,7 +2405,7 @@ extension Driver {
24002405

24012406
// Use working directory if specified
24022407
if let moduleRelative = moduleOutputPath.relativePath {
2403-
moduleOutputPath = Driver.useWorkingDirectory(moduleRelative, workingDirectory)
2408+
moduleOutputPath = try Driver.useWorkingDirectory(moduleRelative, workingDirectory)
24042409
}
24052410

24062411
switch moduleOutputKind! {
@@ -2500,8 +2505,8 @@ extension Driver {
25002505
// FIXME: TSC should provide a better utility for this.
25012506
if let absPath = try? AbsolutePath(validating: sdkPath) {
25022507
path = absPath
2503-
} else if let cwd = fileSystem.currentWorkingDirectory {
2504-
path = AbsolutePath(sdkPath, relativeTo: cwd)
2508+
} else if let cwd = fileSystem.currentWorkingDirectory, let absPath = try? AbsolutePath(validating: sdkPath, relativeTo: cwd) {
2509+
path = absPath
25052510
} else {
25062511
diagnosticsEngine.emit(.warning_no_such_sdk(sdkPath))
25072512
return nil
@@ -2585,7 +2590,7 @@ extension Driver {
25852590
return nil
25862591
}
25872592

2588-
if let outputPath = outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
2593+
if let outputPath = try outputFileMap?.existingOutput(inputFile: input, outputType: .pch) {
25892594
return outputPath
25902595
}
25912596

@@ -2689,9 +2694,11 @@ extension Driver {
26892694
if let profileArgs = parsedOptions.getLastArgument(.profileUse)?.asMultiple,
26902695
let workingDirectory = workingDirectory ?? fileSystem.currentWorkingDirectory {
26912696
for profilingData in profileArgs {
2692-
if !fileSystem.exists(AbsolutePath(profilingData,
2693-
relativeTo: workingDirectory)) {
2694-
diagnosticEngine.emit(Error.missingProfilingData(profilingData))
2697+
if let path = try? AbsolutePath(validating: profilingData,
2698+
relativeTo: workingDirectory) {
2699+
if !fileSystem.exists(path) {
2700+
diagnosticEngine.emit(Error.missingProfilingData(profilingData))
2701+
}
26952702
}
26962703
}
26972704
}
@@ -3065,7 +3072,7 @@ extension Driver {
30653072
// If this is a single-file compile and there is an entry in the
30663073
// output file map, use that.
30673074
if compilerMode.isSingleCompilation,
3068-
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
3075+
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
30693076
outputType: type) {
30703077
return singleOutputPath
30713078
}
@@ -3074,23 +3081,23 @@ extension Driver {
30743081
// primary output type is a .swiftmodule and we are using the emit-module-separately
30753082
// flow, then also consider single output paths specified in the output file-map.
30763083
if compilerOutputType == .swiftModule && emitModuleSeparately,
3077-
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
3084+
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
30783085
outputType: type) {
30793086
return singleOutputPath
30803087
}
30813088

30823089
// Emit-module serialized diagnostics are always specified as a single-output
30833090
// file
30843091
if type == .emitModuleDiagnostics,
3085-
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
3092+
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
30863093
outputType: type) {
30873094
return singleOutputPath
30883095
}
30893096

30903097
// Emit-module discovered dependencies are always specified as a single-output
30913098
// file
30923099
if type == .emitModuleDependencies,
3093-
let path = outputFileMap?.existingOutputForSingleInput(outputType: type) {
3100+
let path = try outputFileMap?.existingOutputForSingleInput(outputType: type) {
30943101
return path
30953102
}
30963103

@@ -3113,7 +3120,7 @@ extension Driver {
31133120
// synthesize a path from the master swift dependency path. This is
31143121
// important as we may otherwise emit this file at the location where the
31153122
// driver was invoked, which is normally the root of the package.
3116-
if let path = outputFileMap?.existingOutputForSingleInput(outputType: .swiftDeps) {
3123+
if let path = try outputFileMap?.existingOutputForSingleInput(outputType: .swiftDeps) {
31173124
return VirtualPath.lookup(path)
31183125
.parentDirectory
31193126
.appending(component: "\(moduleName).\(type.rawValue)")
@@ -3230,7 +3237,7 @@ extension Driver {
32303237
// If this is a single-file compile and there is an entry in the
32313238
// output file map, use that.
32323239
if compilerMode.isSingleCompilation,
3233-
let singleOutputPath = outputFileMap?.existingOutputForSingleInput(
3240+
let singleOutputPath = try outputFileMap?.existingOutputForSingleInput(
32343241
outputType: type) {
32353242
return singleOutputPath
32363243
}
@@ -3249,7 +3256,7 @@ extension Driver {
32493256
parentPath = VirtualPath.lookup(moduleOutputPath).parentDirectory
32503257
}
32513258

3252-
return parentPath
3259+
return try parentPath
32533260
.appending(component: VirtualPath.lookup(moduleOutputPath).basename)
32543261
.replacingExtension(with: type)
32553262
.intern()

Sources/SwiftDriver/Driver/OutputFileMap.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public struct OutputFileMap: Hashable, Codable {
3535

3636
/// For the given input file, retrieve or create an output file for the given
3737
/// file type.
38-
public func getOutput(inputFile: VirtualPath.Handle, outputType: FileType) -> VirtualPath.Handle {
38+
public func getOutput(inputFile: VirtualPath.Handle, outputType: FileType) throws -> VirtualPath.Handle {
3939
// If we already have an output file, retrieve it.
40-
if let output = existingOutput(inputFile: inputFile, outputType: outputType) {
40+
if let output = try existingOutput(inputFile: inputFile, outputType: outputType) {
4141
return output
4242
}
4343

@@ -50,7 +50,7 @@ public struct OutputFileMap: Hashable, Codable {
5050
return VirtualPath.createUniqueTemporaryFile(RelativePath(inputFile.basenameWithoutExt.appendingFileTypeExtension(outputType))).intern()
5151
}
5252

53-
public func existingOutput(inputFile: VirtualPath.Handle, outputType: FileType) -> VirtualPath.Handle? {
53+
public func existingOutput(inputFile: VirtualPath.Handle, outputType: FileType) throws -> VirtualPath.Handle? {
5454
if let path = entries[inputFile]?[outputType] {
5555
return path
5656
}
@@ -60,14 +60,14 @@ public struct OutputFileMap: Hashable, Codable {
6060
guard let path = entries[inputFile]?[.swiftModule] else {
6161
return nil
6262
}
63-
return VirtualPath.lookup(path).replacingExtension(with: outputType).intern()
63+
return try VirtualPath.lookup(path).replacingExtension(with: outputType).intern()
6464

6565
case .jsonAPIBaseline, .jsonABIBaseline:
6666
// Infer paths for these entities using .swiftsourceinfo path.
6767
guard let path = entries[inputFile]?[.swiftSourceInfoFile] else {
6868
return nil
6969
}
70-
return VirtualPath.lookup(path).replacingExtension(with: outputType).intern()
70+
return try VirtualPath.lookup(path).replacingExtension(with: outputType).intern()
7171

7272
case .object:
7373
// We may generate .o files from bitcode .bc files, but the output file map
@@ -86,8 +86,8 @@ public struct OutputFileMap: Hashable, Codable {
8686
}
8787
}
8888

89-
public func existingOutputForSingleInput(outputType: FileType) -> VirtualPath.Handle? {
90-
existingOutput(inputFile: Self.singleInputKey, outputType: outputType)
89+
public func existingOutputForSingleInput(outputType: FileType) throws -> VirtualPath.Handle? {
90+
try existingOutput(inputFile: Self.singleInputKey, outputType: outputType)
9191
}
9292

9393
public func resolveRelativePaths(relativeTo absPath: AbsolutePath) -> OutputFileMap {

Sources/SwiftDriver/IncrementalCompilation/BuildRecordInfo.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import class Dispatch.DispatchQueue
8585
recordedInputModificationDates: [TypedVirtualPath: TimePoint]
8686
) {
8787
// Cannot write a buildRecord without a path.
88-
guard let buildRecordPath = Self.computeBuildRecordPath(
88+
guard let buildRecordPath = try? Self.computeBuildRecordPath(
8989
outputFileMap: outputFileMap,
9090
incremental: incremental,
9191
compilerOutputType: compilerOutputType,
@@ -127,14 +127,14 @@ import class Dispatch.DispatchQueue
127127
compilerOutputType: FileType?,
128128
workingDirectory: AbsolutePath?,
129129
diagnosticEngine: DiagnosticsEngine
130-
) -> VirtualPath? {
130+
) throws -> VirtualPath? {
131131
// FIXME: This should work without an output file map. We should have
132132
// another way to specify a build record and where to put intermediates.
133133
guard let ofm = outputFileMap else {
134134
return nil
135135
}
136136
guard let partialBuildRecordPath =
137-
ofm.existingOutputForSingleInput(outputType: .swiftDeps)
137+
try ofm.existingOutputForSingleInput(outputType: .swiftDeps)
138138
else {
139139
if incremental {
140140
diagnosticEngine.emit(.warning_incremental_requires_build_record_entry)

Sources/SwiftDriver/IncrementalCompilation/IncrementalCompilationState+Extensions.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,10 @@ extension IncrementalCompilationState {
253253
report(message, pathIfGiven?.file)
254254
return
255255
}
256-
let output = outputFileMap.getOutput(inputFile: path.fileHandle, outputType: .object)
256+
guard let output = try? outputFileMap.getOutput(inputFile: path.fileHandle, outputType: .object) else {
257+
report(message, pathIfGiven?.file)
258+
return
259+
}
257260
let compiling = " {compile: \(VirtualPath.lookup(output).basename) <= \(input.basename)}"
258261
diagnosticEngine.emit(.remark_incremental_compilation(because: "\(message) \(compiling)"))
259262
}

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraph.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ extension ModuleDependencyGraph {
323323
// to preserve legacy behavior cancel whole thing
324324
info.diagnosticEngine.emit(
325325
.remark_incremental_compilation_has_been_disabled(
326-
because: "malformed dependencies file '\(dependencySource.fileToRead(info: info)?.file.name ?? "none?!")'"))
326+
because: "malformed dependencies file '\((try? dependencySource.fileToRead(info: info))?.file.name ?? "none?!")'"))
327327
return nil
328328
}
329329
let invalidatedNodes = Integrator.integrate(

Sources/SwiftDriver/IncrementalCompilation/ModuleDependencyGraphParts/DependencySource.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension DependencySource {
7070
info: IncrementalCompilationState.IncrementalDependencyAndInputSetup,
7171
internedStringTable: InternedStringTable
7272
) -> SourceFileDependencyGraph? {
73-
guard let fileToRead = fileToRead(info: info) else {return nil}
73+
guard let fileToRead = try? fileToRead(info: info) else {return nil}
7474
do {
7575
info.reporter?.report("Reading dependencies from \(description)")
7676
return try SourceFileDependencyGraph.read(from: fileToRead,
@@ -89,10 +89,10 @@ extension DependencySource {
8989
/// - Returns: The corresponding swiftdeps file for a swift file, or the swiftmodule file for an incremental imports source.
9090
public func fileToRead(
9191
info: IncrementalCompilationState.IncrementalDependencyAndInputSetup
92-
) -> TypedVirtualPath? {
92+
) throws -> TypedVirtualPath? {
9393
typedFile.type != .swift
9494
? typedFile
95-
: info.outputFileMap.getSwiftDeps(for: typedFile, diagnosticEngine: info.diagnosticEngine)
95+
: try info.outputFileMap.getSwiftDeps(for: typedFile, diagnosticEngine: info.diagnosticEngine)
9696
}
9797
}
9898

@@ -101,9 +101,9 @@ extension OutputFileMap {
101101
fileprivate func getSwiftDeps(
102102
for sourceFile: TypedVirtualPath,
103103
diagnosticEngine: DiagnosticsEngine
104-
) -> TypedVirtualPath? {
104+
) throws -> TypedVirtualPath? {
105105
assert(sourceFile.type == FileType.swift)
106-
guard let swiftDepsHandle = existingOutput(inputFile: sourceFile.fileHandle,
106+
guard let swiftDepsHandle = try existingOutput(inputFile: sourceFile.fileHandle,
107107
outputType: .swiftDeps)
108108
else {
109109
// The legacy driver fails silently here.

Sources/SwiftDriver/Jobs/BackendJob.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ extension Driver {
5757
if let compilerOutputType = compilerOutputType {
5858
// If there is no baseInput (singleCompileMode), primary output computation
5959
// is not input-specific, therefore it does not matter which input is passed.
60-
let output = computePrimaryOutput(for: baseInput ?? input,
61-
outputType: compilerOutputType,
62-
isTopLevel: isTopLevelOutput(type: compilerOutputType))
60+
let output = try computePrimaryOutput(for: baseInput ?? input,
61+
outputType: compilerOutputType,
62+
isTopLevel: isTopLevelOutput(type: compilerOutputType))
6363
commandLine.appendFlag(.o)
6464
commandLine.appendPath(output.file)
6565
outputs.append(output)

0 commit comments

Comments
 (0)