Skip to content

Commit 3133e8b

Browse files
committed
[Dependency Scanning] Add support for dependency-scan-specific serialized diagnostic output
1 parent e4a9efa commit 3133e8b

File tree

6 files changed

+82
-7
lines changed

6 files changed

+82
-7
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,9 @@ public struct Driver {
374374
/// Path to the serialized diagnostics file of the emit-module task.
375375
let emitModuleSerializedDiagnosticsFilePath: VirtualPath.Handle?
376376

377+
/// Path to the serialized diagnostics file of the dependency scanning task.
378+
let dependencyScanSerializedDiagnosticsPath: VirtualPath.Handle?
379+
377380
/// Path to the discovered dependencies file of the emit-module task.
378381
let emitModuleDependenciesFilePath: VirtualPath.Handle?
379382

@@ -1241,6 +1244,14 @@ public struct Driver {
12411244
emitModuleSeparately: emitModuleSeparately,
12421245
outputFileMap: self.outputFileMap,
12431246
moduleName: moduleOutputInfo.name)
1247+
self.dependencyScanSerializedDiagnosticsPath = try Self.computeSupplementaryOutputPath(
1248+
&parsedOptions, type: .dependencyScanDiagnostics, isOutputOptions: [.serializeDiagnostics],
1249+
outputPath: .dependencyScanSerializeDiagnosticsPath,
1250+
compilerOutputType: compilerOutputType,
1251+
compilerMode: compilerMode,
1252+
emitModuleSeparately: emitModuleSeparately,
1253+
outputFileMap: self.outputFileMap,
1254+
moduleName: moduleOutputInfo.name)
12441255
self.emitModuleDependenciesFilePath = try Self.computeSupplementaryOutputPath(
12451256
&parsedOptions, type: .emitModuleDependencies, isOutputOptions: [.emitDependencies],
12461257
outputPath: .emitModuleDependenciesPath,

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public extension Driver {
193193
commandLine.appendFlag(.resolvedPluginVerification)
194194
}
195195

196+
if let depScanSerializedDiagnosticsPath = dependencyScanSerializedDiagnosticsPath {
197+
commandLine.appendFlag("-serialize-diagnostics-path")
198+
commandLine.appendPath(VirtualPath.lookup(depScanSerializedDiagnosticsPath))
199+
}
200+
196201
// Pass on the input files
197202
commandLine.append(contentsOf: inputFiles.filter { $0.type == .swift }.map { .path($0.file) })
198203
return (inputs, commandLine)

Sources/SwiftDriver/Jobs/CompileJob.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ extension Driver {
9797
.moduleTrace, .yamlOptimizationRecord, .bitstreamOptimizationRecord, .pcm, .pch,
9898
.clangModuleMap, .jsonCompilerFeatures, .jsonTargetInfo, .jsonSwiftArtifacts,
9999
.indexUnitOutputPath, .modDepCache, .jsonAPIBaseline, .jsonABIBaseline,
100-
.swiftConstValues, .jsonAPIDescriptor, .moduleSummary, .moduleSemanticInfo,
100+
.swiftConstValues, .jsonAPIDescriptor, .moduleSummary, .moduleSemanticInfo, .dependencyScanDiagnostics,
101101
.cachedDiagnostics, .jsonSupportedFeatures, nil:
102102
return false
103103
}
@@ -500,7 +500,7 @@ extension FileType {
500500
.swiftSourceInfoFile, .clangModuleMap, .jsonSwiftArtifacts,
501501
.indexUnitOutputPath, .modDepCache, .jsonAPIBaseline, .jsonABIBaseline,
502502
.swiftConstValues, .jsonAPIDescriptor, .moduleSummary, .moduleSemanticInfo,
503-
.cachedDiagnostics:
503+
.cachedDiagnostics, .dependencyScanDiagnostics:
504504
fatalError("Output type can never be a primary output")
505505
}
506506
}

Sources/SwiftDriver/Utilities/FileType.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ public enum FileType: String, Hashable, CaseIterable, Codable {
8787
/// Serialized diagnostics produced by module-generation
8888
case emitModuleDiagnostics = "emit-module.dia"
8989

90+
/// Serialized diagnostics produced by dependency scanning
91+
case dependencyScanDiagnostics = "dependency-scan.dia"
92+
9093
/// Serialized diagnostics produced by module-generation
9194
case emitModuleDependencies = "emit-module.d"
9295

@@ -254,6 +257,9 @@ extension FileType: CustomStringConvertible {
254257
case .emitModuleDiagnostics:
255258
return "emit-module-diagnostics"
256259

260+
case .dependencyScanDiagnostics:
261+
return "dependency-scan-diagnostics"
262+
257263
case .jsonAPIBaseline:
258264
return "api-baseline-json"
259265

@@ -291,7 +297,7 @@ extension FileType {
291297
case .object, .pch, .ast, .llvmIR, .llvmBitcode, .assembly, .swiftModule,
292298
.importedModules, .indexData, .remap, .dSYM, .autolink, .dependencies,
293299
.emitModuleDependencies, .swiftDocumentation, .pcm, .diagnostics,
294-
.emitModuleDiagnostics, .objcHeader, .image, .swiftDeps, .moduleTrace,
300+
.emitModuleDiagnostics, .dependencyScanDiagnostics, .objcHeader, .image, .swiftDeps, .moduleTrace,
295301
.tbd, .yamlOptimizationRecord, .bitstreamOptimizationRecord,
296302
.swiftInterface, .privateSwiftInterface, .packageSwiftInterface, .swiftSourceInfoFile,
297303
.jsonDependencies, .clangModuleMap, .jsonTargetInfo, .jsonCompilerFeatures,
@@ -399,6 +405,8 @@ extension FileType {
399405
return "diagnostics"
400406
case .emitModuleDiagnostics:
401407
return "emit-module-diagnostics"
408+
case .dependencyScanDiagnostics:
409+
return "dependency-scan-diagnostics"
402410
case .indexUnitOutputPath:
403411
return "index-unit-output-path"
404412
case .jsonAPIBaseline:
@@ -435,7 +443,7 @@ extension FileType {
435443
case .image, .object, .dSYM, .pch, .sib, .raw_sib, .swiftModule,
436444
.swiftDocumentation, .swiftSourceInfoFile, .llvmBitcode, .diagnostics,
437445
.pcm, .swiftDeps, .remap, .indexData, .bitstreamOptimizationRecord,
438-
.indexUnitOutputPath, .modDepCache, .emitModuleDiagnostics:
446+
.indexUnitOutputPath, .modDepCache, .emitModuleDiagnostics, .dependencyScanDiagnostics:
439447
return false
440448
}
441449
}
@@ -455,7 +463,7 @@ extension FileType {
455463
.clangModuleMap, .jsonCompilerFeatures, .jsonTargetInfo, .jsonSwiftArtifacts,
456464
.indexUnitOutputPath, .jsonAPIBaseline, .jsonABIBaseline, .swiftConstValues,
457465
.jsonAPIDescriptor, .moduleSummary, .moduleSemanticInfo, .cachedDiagnostics,
458-
.raw_llvmIr, .jsonSupportedFeatures:
466+
.raw_llvmIr, .jsonSupportedFeatures, .dependencyScanDiagnostics:
459467
return false
460468
}
461469
}
@@ -465,7 +473,7 @@ extension FileType {
465473
switch self {
466474
case .swift, .ast, .indexData, .indexUnitOutputPath, .jsonCompilerFeatures, .jsonTargetInfo, .jsonSupportedFeatures:
467475
return false
468-
case .sil, .sib, .image, .object, .dSYM, .dependencies, .autolink, .swiftModule, .swiftDocumentation, .swiftInterface, .privateSwiftInterface, .packageSwiftInterface, .swiftSourceInfoFile, .swiftConstValues, .assembly, .raw_sil, .raw_sib, .llvmIR, .llvmBitcode, .diagnostics, .emitModuleDiagnostics, .emitModuleDependencies, .objcHeader, .swiftDeps, .modDepCache, .remap, .importedModules, .tbd, .jsonDependencies, .jsonSwiftArtifacts, .moduleTrace, .yamlOptimizationRecord, .bitstreamOptimizationRecord, .pcm, .pch, .clangModuleMap, .jsonAPIBaseline, .jsonABIBaseline, .jsonAPIDescriptor, .moduleSummary, .moduleSemanticInfo, .cachedDiagnostics, .raw_llvmIr:
476+
case .sil, .sib, .image, .object, .dSYM, .dependencies, .autolink, .swiftModule, .swiftDocumentation, .swiftInterface, .privateSwiftInterface, .packageSwiftInterface, .swiftSourceInfoFile, .swiftConstValues, .assembly, .raw_sil, .raw_sib, .llvmIR, .llvmBitcode, .diagnostics, .emitModuleDiagnostics, .emitModuleDependencies, .objcHeader, .swiftDeps, .modDepCache, .remap, .importedModules, .tbd, .jsonDependencies, .jsonSwiftArtifacts, .moduleTrace, .yamlOptimizationRecord, .bitstreamOptimizationRecord, .pcm, .pch, .clangModuleMap, .jsonAPIBaseline, .jsonABIBaseline, .jsonAPIDescriptor, .moduleSummary, .moduleSemanticInfo, .cachedDiagnostics, .raw_llvmIr, .dependencyScanDiagnostics:
469477
return true
470478
}
471479
}
@@ -481,7 +489,7 @@ extension FileType {
481489
.jsonCompilerFeatures, .jsonTargetInfo, .autolink, .jsonSupportedFeatures:
482490
return false
483491
case .assembly, .llvmIR, .llvmBitcode, .object, .sil, .sib, .ast,
484-
.dependencies, .emitModuleDependencies, .swiftModule,
492+
.dependencies, .emitModuleDependencies, .swiftModule, .dependencyScanDiagnostics,
485493
.swiftDocumentation, .swiftInterface, .privateSwiftInterface, .packageSwiftInterface,
486494
.swiftSourceInfoFile, .raw_sil, .raw_sib, .objcHeader, .swiftDeps, .tbd,
487495
.moduleTrace, .indexData, .yamlOptimizationRecord,

Sources/SwiftOptions/Options.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ extension Option {
372372
public static let emitModuleSemanticInfoPath: Option = Option("-emit-module-semantic-info-path", .separate, attributes: [.frontend, .noDriver, .cacheInvariant], metaVar: "<path>", helpText: "Output semantic info of current module to <path>")
373373
public static let emitModuleSeparatelyWMO: Option = Option("-emit-module-separately-wmo", .flag, attributes: [.helpHidden], helpText: "Emit module files as a distinct job in wmo builds")
374374
public static let emitModuleSerializeDiagnosticsPath: Option = Option("-emit-module-serialize-diagnostics-path", .separate, attributes: [.argumentIsPath, .supplementaryOutput], metaVar: "<path>", helpText: "Emit a serialized diagnostics file for the emit-module task to <path>")
375+
public static let dependencyScanSerializeDiagnosticsPath: Option = Option("-dependency-scan-serialize-diagnostics-path", .separate, attributes: [.argumentIsPath, .supplementaryOutput], metaVar: "<path>", helpText: "Emit a serialized diagnostics file for the dependency scanning task to <path>")
375376
public static let emitModuleSourceInfoPath: Option = Option("-emit-module-source-info-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Output module source info file to <path>")
376377
public static let emitModuleSourceInfo: Option = Option("-emit-module-source-info", .flag, attributes: [.frontend, .noDriver], helpText: "Output module source info file")
377378
public static let emitModuleSummaryPath: Option = Option("-emit-module-summary-path", .separate, attributes: [.frontend, .noInteractive, .argumentIsPath, .supplementaryOutput, .cacheInvariant], metaVar: "<path>", helpText: "Output module summary file to <path>")
@@ -1342,6 +1343,7 @@ extension Option {
13421343
Option.emitModuleSemanticInfoPath,
13431344
Option.emitModuleSeparatelyWMO,
13441345
Option.emitModuleSerializeDiagnosticsPath,
1346+
Option.dependencyScanSerializeDiagnosticsPath,
13451347
Option.emitModuleSourceInfoPath,
13461348
Option.emitModuleSourceInfo,
13471349
Option.emitModuleSummaryPath,

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,6 +1652,55 @@ final class ExplicitModuleBuildTests: XCTestCase {
16521652
}
16531653
}
16541654

1655+
func testInMemoryScanWithSerializedDiagnostics() throws {
1656+
try withTemporaryDirectory { path in
1657+
let (stdLibPath, shimsPath, _, hostTriple) = try getDriverArtifactsForScanning()
1658+
let scannerCachePath: AbsolutePath = path.appending(component: "ClangScannerCache")
1659+
let moduleCachePath = path.appending(component: "ModuleCache")
1660+
let serializedDiagnosticsOutputPath = path.appending(component: "ScanDiags.dia")
1661+
1662+
// Setup our main test module
1663+
let mainSourcePath = path.appending(component: "Foo.swift")
1664+
try localFileSystem.writeFileContents(mainSourcePath, bytes: "import Swift")
1665+
1666+
// Setup the build plan
1667+
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
1668+
var driver = try Driver(args: ["swiftc",
1669+
"-I", stdLibPath.nativePathString(escaped: true),
1670+
"-I", shimsPath.nativePathString(escaped: true),
1671+
"-explicit-module-build",
1672+
"-dependency-scan-serialize-diagnostics-path",
1673+
serializedDiagnosticsOutputPath.nativePathString(escaped: false),
1674+
"-module-name", "main",
1675+
"-target", hostTriple.triple,
1676+
"-working-directory", path.nativePathString(escaped: true),
1677+
"-clang-scanner-module-cache-path",
1678+
scannerCachePath.nativePathString(escaped: false),
1679+
"-module-cache-path",
1680+
moduleCachePath.nativePathString(escaped: true),
1681+
mainSourcePath.nativePathString(escaped: true)] + sdkArgumentsForTesting)
1682+
1683+
// Set up the in-memory dependency scan using the dependency oracle
1684+
let dependencyOracle = driver.interModuleDependencyOracle
1685+
let scanLibPath = try XCTUnwrap(driver.toolchain.lookupSwiftScanLib())
1686+
try dependencyOracle.verifyOrCreateScannerInstance(swiftScanLibPath: scanLibPath)
1687+
let resolver = try ArgsResolver(fileSystem: localFileSystem)
1688+
let scannerCommand = try driver.dependencyScannerInvocationCommand().1.map { try resolver.resolve($0) }
1689+
XCTAssertTrue(scannerCommand.contains(subsequence: ["-serialize-diagnostics-path", serializedDiagnosticsOutputPath.pathString]))
1690+
1691+
// Perform the scan
1692+
var scanDiagnostics: [ScannerDiagnosticPayload] = []
1693+
let _ = try dependencyOracle.getDependencies(workingDirectory: path,
1694+
commandLine: scannerCommand,
1695+
diagnostics: &scanDiagnostics)
1696+
1697+
// TODO: Ensure the serialized diagnostics output got written out
1698+
// This requires an ability to confirm first whether the compiler we're using
1699+
// has this capability.
1700+
// XCTAssertTrue(localFileSystem.exists(serializedDiagnosticsOutputPath))
1701+
}
1702+
}
1703+
16551704
func testBinaryFrameworkDependencyScan() throws {
16561705
try withTemporaryDirectory { path in
16571706
let (stdLibPath, shimsPath, toolchain, hostTriple) = try getDriverArtifactsForScanning()

0 commit comments

Comments
 (0)