diff --git a/Sources/SwiftDriver/Driver/DebugInfo.swift b/Sources/SwiftDriver/Driver/DebugInfo.swift index 92c131501..2f8bdd859 100644 --- a/Sources/SwiftDriver/Driver/DebugInfo.swift +++ b/Sources/SwiftDriver/Driver/DebugInfo.swift @@ -41,9 +41,12 @@ } } - // The format of debug information. + /// The format of debug information. public let format: Format + /// The DWARF standard version to be produced. + public let dwarfVersion: UInt8 + /// The level of debug information. public let level: Level? diff --git a/Sources/SwiftDriver/Driver/Driver.swift b/Sources/SwiftDriver/Driver/Driver.swift index 0cfc0e212..a4c98b8d1 100644 --- a/Sources/SwiftDriver/Driver/Driver.swift +++ b/Sources/SwiftDriver/Driver/Driver.swift @@ -2308,7 +2308,17 @@ extension Driver { diagnosticsEngine.emit(.error_argument_not_allowed_with(arg: fullNotAllowedOption, other: levelOption.spelling)) } - return DebugInfo(format: format, level: level, shouldVerify: shouldVerify) + // Determine the DWARF version. + var dwarfVersion: UInt8 = 4 + if let versionArg = parsedOptions.getLastArgument(.dwarfVersion) { + if let parsedVersion = UInt8(versionArg.asSingle), parsedVersion >= 2 && parsedVersion <= 5 { + dwarfVersion = parsedVersion + } else { + diagnosticsEngine.emit(.error_invalid_arg_value(arg: .dwarfVersion, value: versionArg.asSingle)) + } + } + + return DebugInfo(format: format, dwarfVersion: dwarfVersion, level: level, shouldVerify: shouldVerify) } /// Parses the set of `-sanitize={sanitizer}` arguments and returns all the diff --git a/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift b/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift index 9050e3f49..25d8f5112 100644 --- a/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift +++ b/Sources/SwiftDriver/Jobs/FrontendJobHelpers.swift @@ -193,6 +193,7 @@ extension Driver { try commandLine.appendLast(.enablePrivateImports, from: &parsedOptions) try commandLine.appendLast(in: .g, from: &parsedOptions) try commandLine.appendLast(.debugInfoFormat, from: &parsedOptions) + try commandLine.appendLast(.dwarfVersion, from: &parsedOptions) try commandLine.appendLast(.importUnderlyingModule, from: &parsedOptions) try commandLine.appendLast(.moduleCachePath, from: &parsedOptions) try commandLine.appendLast(.moduleLinkName, from: &parsedOptions) diff --git a/Sources/SwiftOptions/Options.swift b/Sources/SwiftOptions/Options.swift index b7809a6ee..23bb0c3b0 100644 --- a/Sources/SwiftOptions/Options.swift +++ b/Sources/SwiftOptions/Options.swift @@ -109,6 +109,7 @@ extension Option { public static let debugForbidTypecheckPrefix: Option = Option("-debug-forbid-typecheck-prefix", .separate, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Triggers llvm fatal_error if typechecker tries to typecheck a decl with the provided prefix name") public static let debugGenericSignatures: Option = Option("-debug-generic-signatures", .flag, attributes: [.helpHidden, .frontend, .noDriver], helpText: "Debug generic signatures") public static let debugInfoFormat: Option = Option("-debug-info-format=", .joined, attributes: [.frontend], helpText: "Specify the debug info format type to either 'dwarf' or 'codeview'") + public static let dwarfVersion: Option = Option("-dwarf-version=", .joined, attributes: [.frontend], helpText: "DWARF debug info version to produce if requested") public static let debugInfoStoreInvocation: Option = Option("-debug-info-store-invocation", .flag, attributes: [.frontend], helpText: "Emit the compiler invocation in the debug info.") public static let debugMapping: Option = Option("-debug-mapping", .flag, attributes: [.noDriver], helpText: "Dumping information for debug purposes") public static let debugMapping_: Option = Option("--debug-mapping", .flag, alias: Option.debugMapping, attributes: [.noDriver], helpText: "Dumping information for debug purposes") @@ -907,6 +908,7 @@ extension Option { Option.debugForbidTypecheckPrefix, Option.debugGenericSignatures, Option.debugInfoFormat, + Option.dwarfVersion, Option.debugInfoStoreInvocation, Option.debugMapping, Option.debugMapping_, diff --git a/Tests/SwiftDriverTests/SwiftDriverTests.swift b/Tests/SwiftDriverTests/SwiftDriverTests.swift index 01732b6d2..5acd6b547 100644 --- a/Tests/SwiftDriverTests/SwiftDriverTests.swift +++ b/Tests/SwiftDriverTests/SwiftDriverTests.swift @@ -574,6 +574,19 @@ final class SwiftDriverTests: XCTestCase { $1.expect(.error("argument '-debug-info-format=codeview' is not allowed with '-gdwarf-types'")) } + try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-dwarf-version=0") { + $1.expect(.error("invalid value '0' in '-dwarf-version=")) + } + + try assertDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-dwarf-version=6") { + $1.expect(.error("invalid value '6' in '-dwarf-version=")) + } + + try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-emit-module", "-g", "-debug-info-format=dwarf", "-dwarf-version=4") { driver in + let jobs = try driver.planBuild() + XCTAssertTrue(jobs[0].commandLine.contains(.flag("-dwarf-version=4"))) + } + try assertNoDriverDiagnostics(args: "swiftc", "foo.swift", "-g", "-c", "-file-compilation-dir", ".") { driver in let jobs = try driver.planBuild() XCTAssertTrue(jobs[0].commandLine.contains(.flag("-file-compilation-dir")))