Skip to content

Commit 267325f

Browse files
authored
Merge pull request #475 from cltnschlosser/cs_test_DriverCompile
Fix Driver/driver-compile.swift
2 parents c86e00d + bb0a0bf commit 267325f

File tree

5 files changed

+74
-20
lines changed

5 files changed

+74
-20
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 40 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,17 @@ 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+
throw Diagnostics.fatalError
1358+
} else {
1359+
swiftFiles[basename] = input
1360+
}
1361+
}
13331362

13341363
return TypedVirtualPath(file: file, type: fileType)
13351364
}
@@ -1453,6 +1482,14 @@ extension Diagnostic.Message {
14531482
static var warn_ignore_embed_bitcode_marker: Diagnostic.Message {
14541483
.warning("ignoring -embed-bitcode-marker since no object file is being generated")
14551484
}
1485+
1486+
static func error_two_files_same_name(basename: String, firstPath: String, secondPath: String) -> Diagnostic.Message {
1487+
.error("filename \"\(basename)\" used twice: '\(firstPath)' and '\(secondPath)'")
1488+
}
1489+
1490+
static var note_explain_two_files_same_name: Diagnostic.Message {
1491+
.note("filenames are used to distinguish private declarations with the same name")
1492+
}
14561493
}
14571494

14581495
// Multithreading
@@ -2288,8 +2325,7 @@ extension Driver {
22882325
compilerOutputType: FileType?,
22892326
compilerMode: CompilerMode,
22902327
outputFileMap: OutputFileMap?,
2291-
moduleName: String,
2292-
patternOutputFile: VirtualPath? = nil
2328+
moduleName: String
22932329
) throws -> VirtualPath? {
22942330
// If there is an explicit argument for the output path, use that
22952331
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
@@ -311,15 +311,11 @@ extension Driver {
311311
input: input,
312312
flag: "-emit-dependencies-path")
313313

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

324320
addOutputOfType(
325321
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.

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,27 @@ final class SwiftDriverTests: XCTestCase {
627627
XCTAssertFalse(plannedJobs[2].commandLine.contains(.flag("-emit-dependencies-path")))
628628
XCTAssertFalse(plannedJobs[2].commandLine.contains(.flag("-serialize-diagnostics-path")))
629629
}
630+
631+
func testReferenceDependencies() throws {
632+
var driver = try Driver(args: ["swiftc", "foo.swift", "-incremental"])
633+
let plannedJobs = try driver.planBuild()
634+
XCTAssertTrue(plannedJobs[0].kind == .compile)
635+
XCTAssertTrue(plannedJobs[0].commandLine.contains(.flag("-emit-reference-dependencies-path")))
636+
}
637+
638+
func testDuplicateName() throws {
639+
assertDiagnostics { diagnosticsEngine, verify in
640+
_ = try? Driver(args: ["swiftc", "-c", "foo.swift", "foo.swift"], diagnosticsEngine: diagnosticsEngine)
641+
verify.expect(.error("filename \"foo.swift\" used twice: 'foo.swift' and 'foo.swift'"))
642+
verify.expect(.note("filenames are used to distinguish private declarations with the same name"))
643+
}
644+
645+
assertDiagnostics { diagnosticsEngine, verify in
646+
_ = try? Driver(args: ["swiftc", "-c", "foo.swift", "foo/foo.swift"], diagnosticsEngine: diagnosticsEngine)
647+
verify.expect(.error("filename \"foo.swift\" used twice: 'foo.swift' and 'foo/foo.swift'"))
648+
verify.expect(.note("filenames are used to distinguish private declarations with the same name"))
649+
}
650+
}
630651

631652
func testOutputFileMapStoring() throws {
632653
// Create sample OutputFileMap:

0 commit comments

Comments
 (0)