Skip to content

Commit d9e1c2b

Browse files
committed
Fix Driver/driver-compile.swift: include swiftDeps without output file map, and diagnose multiple files with same name
1 parent 3ab998c commit d9e1c2b

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ public struct Driver {
219219
.appending(component: filename + ".priors")
220220
}
221221

222+
/// Whether to consider incremental compilation.
223+
let shouldAttemptIncrementalCompilation: Bool
224+
222225
/// Code & data for incremental compilation. Nil if not running in incremental mode.
223226
/// Set during planning because needs the jobs to look at outputs.
224227
@_spi(Testing) public private(set) var incrementalCompilationState: IncrementalCompilationState? = nil
@@ -246,6 +249,9 @@ public struct Driver {
246249

247250
/// Path to the dependencies file.
248251
let dependenciesFilePath: VirtualPath?
252+
253+
/// Path to the references dependencies file.
254+
let referenceDependenciesPath: VirtualPath?
249255

250256
/// Path to the serialized diagnostics file.
251257
let serializedDiagnosticsFilePath: VirtualPath?
@@ -376,6 +382,10 @@ public struct Driver {
376382

377383
// Determine the compilation mode.
378384
self.compilerMode = try Self.computeCompilerMode(&parsedOptions, driverKind: driverKind, diagnosticsEngine: diagnosticEngine)
385+
386+
self.shouldAttemptIncrementalCompilation = Self.shouldAttemptIncrementalCompilation(&parsedOptions,
387+
diagnosticEngine: diagnosticsEngine,
388+
compilerMode: compilerMode)
379389

380390
// Compute the working directory.
381391
workingDirectory = try parsedOptions.getLastArgument(.workingDirectory).map { workingDirectoryArg in
@@ -411,7 +421,7 @@ public struct Driver {
411421
swiftCompilerPrefixArgs: self.swiftCompilerPrefixArgs)
412422

413423
// Classify and collect all of the input files.
414-
let inputFiles = try Self.collectInputFiles(&self.parsedOptions)
424+
let inputFiles = try Self.collectInputFiles(&self.parsedOptions, diagnosticsEngine: diagnosticsEngine)
415425
self.inputFiles = inputFiles
416426
self.recordedInputModificationDates = .init(uniqueKeysWithValues:
417427
Set(inputFiles).compactMap {
@@ -533,6 +543,13 @@ public struct Driver {
533543
compilerMode: compilerMode,
534544
outputFileMap: self.outputFileMap,
535545
moduleName: moduleOutputInfo.name)
546+
self.referenceDependenciesPath = try Self.computeSupplementaryOutputPath(
547+
&parsedOptions, type: .swiftDeps, isOutputOptions: shouldAttemptIncrementalCompilation ? [.incremental] : [],
548+
outputPath: .emitReferenceDependenciesPath,
549+
compilerOutputType: compilerOutputType,
550+
compilerMode: compilerMode,
551+
outputFileMap: self.outputFileMap,
552+
moduleName: moduleOutputInfo.name)
536553
self.serializedDiagnosticsFilePath = try Self.computeSupplementaryOutputPath(
537554
&parsedOptions, type: .diagnostics, isOutputOptions: [.serializeDiagnostics],
538555
outputPath: .serializeDiagnosticsPath,
@@ -1314,7 +1331,8 @@ extension Driver {
13141331
}
13151332

13161333
/// Collect all of the input files from the parsed options, translating them into input files.
1317-
private static func collectInputFiles(_ parsedOptions: inout ParsedOptions) throws -> [TypedVirtualPath] {
1334+
private static func collectInputFiles(_ parsedOptions: inout ParsedOptions, diagnosticsEngine: DiagnosticsEngine) throws -> [TypedVirtualPath] {
1335+
var swiftFiles = [String: String]() // [Basename: Path]
13181336
return try parsedOptions.allInputs.map { input in
13191337
// Standard input is assumed to be Swift code.
13201338
if input == "-" {
@@ -1330,6 +1348,16 @@ extension Driver {
13301348
// FIXME: The object-file default is carried over from the existing
13311349
// driver, but seems odd.
13321350
let fileType = FileType(rawValue: fileExtension) ?? FileType.object
1351+
1352+
if fileType == .swift {
1353+
let basename = file.basename
1354+
if let originalPath = swiftFiles[basename] {
1355+
diagnosticsEngine.emit(.error_two_files_same_name(basename: basename, firstPath: originalPath, secondPath: input))
1356+
diagnosticsEngine.emit(.note_explain_two_files_same_name)
1357+
} else {
1358+
swiftFiles[basename] = input
1359+
}
1360+
}
13331361

13341362
return TypedVirtualPath(file: file, type: fileType)
13351363
}
@@ -1453,6 +1481,14 @@ extension Diagnostic.Message {
14531481
static var warn_ignore_embed_bitcode_marker: Diagnostic.Message {
14541482
.warning("ignoring -embed-bitcode-marker since no object file is being generated")
14551483
}
1484+
1485+
static func error_two_files_same_name(basename: String, firstPath: String, secondPath: String) -> Diagnostic.Message {
1486+
.error("filename \"\(basename)\" used twice: '\(firstPath)' and '\(secondPath)'")
1487+
}
1488+
1489+
static var note_explain_two_files_same_name: Diagnostic.Message {
1490+
.note("filenames are used to distinguish private declarations with the same name")
1491+
}
14561492
}
14571493

14581494
// Multithreading
@@ -2288,8 +2324,7 @@ extension Driver {
22882324
compilerOutputType: FileType?,
22892325
compilerMode: CompilerMode,
22902326
outputFileMap: OutputFileMap?,
2291-
moduleName: String,
2292-
patternOutputFile: VirtualPath? = nil
2327+
moduleName: String
22932328
) throws -> VirtualPath? {
22942329
// If there is an explicit argument for the output path, use that
22952330
if let outputPathArg = parsedOptions.getLastArgument(outputPath) {

Sources/SwiftDriver/IncrementalCompilation/IncrementalCompilationState.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ public class IncrementalCompilationState {
4747
options: Options,
4848
jobsInPhases: JobsInPhases
4949
) throws {
50-
guard driver.shouldAttemptIncrementalCompilation()
51-
else {
52-
return nil
53-
}
50+
guard driver.shouldAttemptIncrementalCompilation else { return nil }
5451

5552
if options.contains(.showIncremental) {
5653
self.reporter = Reporter(diagnosticEngine: driver.diagnosticEngine,
@@ -359,9 +356,13 @@ extension IncrementalCompilationState {
359356
}
360357
}
361358

362-
fileprivate extension Driver {
359+
extension Driver {
363360
/// Check various arguments to rule out incremental compilation if need be.
364-
mutating func shouldAttemptIncrementalCompilation() -> Bool {
361+
static func shouldAttemptIncrementalCompilation(
362+
_ parsedOptions: inout ParsedOptions,
363+
diagnosticEngine: DiagnosticsEngine,
364+
compilerMode: CompilerMode
365+
) -> Bool {
365366
guard parsedOptions.hasArgument(.incremental) else {
366367
return false
367368
}

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,11 @@ extension Driver {
310310
input: input,
311311
flag: "-emit-dependencies-path")
312312

313-
if let input = input, let outputFileMap = outputFileMap {
314-
let referenceDependenciesPath =
315-
outputFileMap.existingOutput(inputFile: input.file, outputType: .swiftDeps)
316-
addOutputOfType(
317-
outputType: .swiftDeps,
318-
finalOutputPath: referenceDependenciesPath,
319-
input: input,
320-
flag: "-emit-reference-dependencies-path")
321-
}
313+
addOutputOfType(
314+
outputType: .swiftDeps,
315+
finalOutputPath: referenceDependenciesPath,
316+
input: input,
317+
flag: "-emit-reference-dependencies-path")
322318

323319
addOutputOfType(
324320
outputType: .yamlOptimizationRecord,

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct CompileJobGroup {
6969
}
7070
}
7171

72-
/// // MARK: Standard build planning
72+
// MARK: Standard build planning
7373
extension Driver {
7474
/// Plan a standard compilation, which produces jobs for compiling separate
7575
/// primary files.

0 commit comments

Comments
 (0)