Skip to content

Commit 60624f8

Browse files
committed
Port over missing debug info driver functionality from the C++ implementation.
1 parent d6d5608 commit 60624f8

File tree

5 files changed

+59
-5
lines changed

5 files changed

+59
-5
lines changed

Sources/SwiftDriver/Driver/Driver.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,10 @@ public struct Driver {
773773
diagnosticsEngine: diagnosticEngine)
774774

775775
// Compute debug information output.
776-
self.debugInfo = Self.computeDebugInfo(&parsedOptions, diagnosticsEngine: diagnosticEngine)
776+
let defaultDwarfVersion = self.toolchain.getDefaultDwarfVersion(targetTriple: self.frontendTargetInfo.target.triple)
777+
self.debugInfo = Self.computeDebugInfo(&parsedOptions,
778+
defaultDwarfVersion: defaultDwarfVersion,
779+
diagnosticsEngine: diagnosticEngine)
777780

778781
// Error if package-name is passed but the input is empty; if
779782
// package-name is not passed but `package` decls exist, error
@@ -2323,7 +2326,9 @@ extension Diagnostic.Message {
23232326
// Debug information
23242327
extension Driver {
23252328
/// Compute the level of debug information we are supposed to produce.
2326-
private static func computeDebugInfo(_ parsedOptions: inout ParsedOptions, diagnosticsEngine: DiagnosticsEngine) -> DebugInfo {
2329+
private static func computeDebugInfo(_ parsedOptions: inout ParsedOptions,
2330+
defaultDwarfVersion : UInt8,
2331+
diagnosticsEngine: DiagnosticsEngine) -> DebugInfo {
23272332
var shouldVerify = parsedOptions.hasArgument(.verifyDebugInfo)
23282333

23292334
for debugPrefixMap in parsedOptions.arguments(for: .debugPrefixMap) {
@@ -2392,7 +2397,7 @@ extension Driver {
23922397
}
23932398

23942399
// Determine the DWARF version.
2395-
var dwarfVersion: UInt8 = 4
2400+
var dwarfVersion: UInt8 = defaultDwarfVersion
23962401
if let versionArg = parsedOptions.getLastArgument(.dwarfVersion) {
23972402
if let parsedVersion = UInt8(versionArg.asSingle), parsedVersion >= 2 && parsedVersion <= 5 {
23982403
dwarfVersion = parsedVersion

Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,10 @@ extension Driver {
195195
try commandLine.appendLast(.enableTesting, from: &parsedOptions)
196196
try commandLine.appendLast(.enablePrivateImports, from: &parsedOptions)
197197
try commandLine.appendLast(in: .g, from: &parsedOptions)
198-
try commandLine.appendLast(.debugInfoFormat, from: &parsedOptions)
199-
try commandLine.appendLast(.dwarfVersion, from: &parsedOptions)
198+
if debugInfo.level != nil {
199+
commandLine.appendFlag("-debug-info-format=\(debugInfo.format)")
200+
commandLine.appendFlag("-dwarf-version=\(debugInfo.dwarfVersion)")
201+
}
200202
try commandLine.appendLast(.importUnderlyingModule, from: &parsedOptions)
201203
try commandLine.appendLast(.moduleCachePath, from: &parsedOptions)
202204
try commandLine.appendLast(.moduleLinkName, from: &parsedOptions)

Sources/SwiftDriver/Toolchains/DarwinToolchain.swift

+9
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,15 @@ public final class DarwinToolchain: Toolchain {
219219
}
220220
}
221221

222+
public func getDefaultDwarfVersion(targetTriple: Triple) -> UInt8 {
223+
if (targetTriple.isMacOSX && targetTriple.version(for: .macOS) < Triple.Version(10, 11, 0)) ||
224+
(targetTriple.isiOS && targetTriple.version(
225+
for: .iOS(targetTriple._isSimulatorEnvironment ? .simulator : .device)) < Triple.Version(9, 0, 0)) {
226+
return 2;
227+
}
228+
return 4
229+
}
230+
222231
func validateDeploymentTarget(_ parsedOptions: inout ParsedOptions,
223232
targetTriple: Triple, compilerOutputType: FileType?) throws {
224233
guard let os = targetTriple.os else {

Sources/SwiftDriver/Toolchains/Toolchain.swift

+5
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ public protocol Toolchain {
122122
compilerOutputType: FileType?,
123123
diagnosticsEngine: DiagnosticsEngine) throws
124124

125+
/// Return the DWARF version to emit, in the absence of arguments to the contrary.
126+
func getDefaultDwarfVersion(targetTriple: Triple) -> UInt8
127+
125128
/// Adds platform-specific linker flags to the provided command line
126129
func addPlatformSpecificLinkerArgs(
127130
to commandLine: inout [Job.ArgTemplate],
@@ -331,6 +334,8 @@ extension Toolchain {
331334
compilerOutputType: FileType?,
332335
diagnosticsEngine: DiagnosticsEngine) {}
333336

337+
public func getDefaultDwarfVersion(targetTriple: Triple) -> UInt8 { return 4 }
338+
334339
public func addPlatformSpecificCommonFrontendOptions(
335340
commandLine: inout [Job.ArgTemplate],
336341
inputs: inout [TypedVirtualPath],

Tests/SwiftDriverTests/SwiftDriverTests.swift

+33
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,39 @@ final class SwiftDriverTests: XCTestCase {
593593
XCTAssertTrue(jobs[0].commandLine.contains(.flag(".")))
594594
}
595595

596+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "x86_64-apple-macosx10.10") { driver in
597+
let jobs = try driver.planBuild()
598+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=2")))
599+
}
600+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "x86_64-apple-macosx10.11") { driver in
601+
let jobs = try driver.planBuild()
602+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4")))
603+
}
604+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "x86_64-apple-macos14.0") { driver in
605+
let jobs = try driver.planBuild()
606+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4")))
607+
}
608+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "arm64-apple-ios8.0") { driver in
609+
let jobs = try driver.planBuild()
610+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=2")))
611+
}
612+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "arm64-apple-ios9.0") { driver in
613+
let jobs = try driver.planBuild()
614+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4")))
615+
}
616+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "x86_64-apple-ios17-macabi") { driver in
617+
let jobs = try driver.planBuild()
618+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4")))
619+
}
620+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "arm64-apple-tvos17.0") { driver in
621+
let jobs = try driver.planBuild()
622+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4")))
623+
}
624+
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-target", "arm64_32-apple-watchos10.0") { driver in
625+
let jobs = try driver.planBuild()
626+
XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4")))
627+
}
628+
596629
try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-c", "-file-compilation-dir", ".") { driver in
597630
let jobs = try driver.planBuild()
598631
XCTAssertFalse(jobs[0].commandLine.contains(.flag("-file-compilation-dir")))

0 commit comments

Comments
 (0)