diff --git a/include/swift/AST/IRGenOptions.h b/include/swift/AST/IRGenOptions.h index 5718e2567cb..fae1dc6cd27 100644 --- a/include/swift/AST/IRGenOptions.h +++ b/include/swift/AST/IRGenOptions.h @@ -237,7 +237,7 @@ public: std::string DebugCompilationDir; /// The DWARF version of debug info. - unsigned DWARFVersion; + uint8_t DWARFVersion = 4; /// The command line string that is to be stored in the debug info. std::string DebugFlags; @@ -512,8 +512,7 @@ public: bool EmitCASIDFile; IRGenOptions() - : DWARFVersion(2), - OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization), + : OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization), Verify(true), OptMode(OptimizationMode::NotSet), Sanitizers(OptionSet()), SanitizersWithRecoveryInstrumentation(OptionSet()), diff --git a/include/swift/Driver/Driver.h b/include/swift/Driver/Driver.h index 89e9b3fd1af..ed02a35f650 100644 --- a/include/swift/Driver/Driver.h +++ b/include/swift/Driver/Driver.h @@ -125,6 +125,9 @@ public: /// What kind of debug info to generate. IRGenDebugInfoFormat DebugInfoFormat = IRGenDebugInfoFormat::None; + /// DWARF output format version number. + std::optional DWARFVersion; + /// Whether or not the driver should generate a module. bool ShouldGenerateModule = false; diff --git a/include/swift/Option/Options.td b/include/swift/Option/Options.td index b38fe2ac9c4..431a5405053 100644 --- a/include/swift/Option/Options.td +++ b/include/swift/Option/Options.td @@ -991,8 +991,8 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">, Group, Flags<[FrontendOption]>, HelpText<"Emit full DWARF type info.">; def debug_prefix_map : Separate<["-"], "debug-prefix-map">, - Flags<[FrontendOption]>, - HelpText<"Remap source paths in debug info">, MetaVarName<"">; + Flags<[FrontendOption]>, + HelpText<"Remap source paths in debug info">, MetaVarName<"">; def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">, Flags<[FrontendOption]>, HelpText<"Remap source paths in coverage info">, MetaVarName<"">; @@ -1007,6 +1007,10 @@ def file_compilation_dir : Separate<["-"], "file-compilation-dir">, def debug_info_format : Joined<["-"], "debug-info-format=">, Flags<[FrontendOption]>, HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">; +def dwarf_version : Joined<["-"], "dwarf-version=">, + Flags<[FrontendOption]>, + HelpText<"DWARF debug info version to produce if requested">, + MetaVarName<"">; def prefix_serialized_debugging_options : Flag<["-"], "prefix-serialized-debugging-options">, Flags<[FrontendOption]>, diff --git a/lib/Driver/DarwinToolChains.cpp b/lib/Driver/DarwinToolChains.cpp index a54a9cd4157..a4973310a11 100644 --- a/lib/Driver/DarwinToolChains.cpp +++ b/lib/Driver/DarwinToolChains.cpp @@ -625,6 +625,28 @@ toolchains::Darwin::addDeploymentTargetArgs(ArgStringList &Arguments, } } +static unsigned getDWARFVersionForTriple(const llvm::Triple &triple) { + llvm::VersionTuple osVersion; + const DarwinPlatformKind kind = getDarwinPlatformKind(triple); + switch (kind) { + case DarwinPlatformKind::MacOS: + triple.getMacOSXVersion(osVersion); + if (osVersion < llvm::VersionTuple(10, 11)) + return 2; + return 4; + case DarwinPlatformKind::IPhoneOSSimulator: + case DarwinPlatformKind::IPhoneOS: + case DarwinPlatformKind::TvOS: + case DarwinPlatformKind::TvOSSimulator: + osVersion = triple.getiOSVersion(); + if (osVersion < llvm::VersionTuple(9)) + return 2; + return 4; + default: + return 4; + } +} + void toolchains::Darwin::addCommonFrontendArgs( const OutputInfo &OI, const CommandOutput &output, const llvm::opt::ArgList &inputArgs, @@ -643,6 +665,16 @@ void toolchains::Darwin::addCommonFrontendArgs( inputArgs.MakeArgString(variantSDKVersion->getAsString())); } } + std::string dwarfVersion; + { + llvm::raw_string_ostream os(dwarfVersion); + os << "-dwarf-version="; + if (OI.DWARFVersion) + os << *OI.DWARFVersion; + else + os << getDWARFVersionForTriple(getTriple()); + } + arguments.push_back(inputArgs.MakeArgString(dwarfVersion)); } /// Add the frontend arguments needed to find external plugins in standard diff --git a/lib/Driver/Driver.cpp b/lib/Driver/Driver.cpp index a15a49956a5..8b7bcdd8d5e 100644 --- a/lib/Driver/Driver.cpp +++ b/lib/Driver/Driver.cpp @@ -1685,6 +1685,16 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args, : "-gdwarf_types"); } + if (const Arg *A = Args.getLastArg(options::OPT_dwarf_version)) { + unsigned vers; + if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 && + vers <= 5) + OI.DWARFVersion = vers; + else + Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value, + A->getAsString(Args), A->getValue()); + } + if (Args.hasArg(options::OPT_emit_module, options::OPT_emit_module_path)) { // The user has requested a module, so generate one and treat it as // top-level output. diff --git a/lib/Driver/ToolChains.cpp b/lib/Driver/ToolChains.cpp index b413917371a..77923ab21f1 100644 --- a/lib/Driver/ToolChains.cpp +++ b/lib/Driver/ToolChains.cpp @@ -261,6 +261,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI, inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports); inputArgs.AddLastArg(arguments, options::OPT_g_Group); inputArgs.AddLastArg(arguments, options::OPT_debug_info_format); + inputArgs.AddLastArg(arguments, options::OPT_dwarf_version); inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module); inputArgs.AddLastArg(arguments, options::OPT_module_cache_path); inputArgs.AddLastArg(arguments, options::OPT_module_link_name); diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index e7f82765b14..057173d09a8 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -2472,7 +2472,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args, Opts.DebugCompilationDir = std::string(cwd.str()); } } - if (const Arg *A = Args.getLastArg(options::OPT_debug_info_format)) { if (A->containsValue("dwarf")) Opts.DebugInfoFormat = IRGenDebugInfoFormat::DWARF; @@ -2502,6 +2501,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args, : "-gdwarf_types"); } + if (auto A = Args.getLastArg(OPT_dwarf_version)) { + unsigned vers; + if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 && + vers <= 5) + Opts.DWARFVersion = vers; + else + Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value, + A->getAsString(Args), A->getValue()); + } + for (auto A : Args.getAllArgValues(options::OPT_file_prefix_map)) { auto SplitMap = StringRef(A).split('='); Opts.FilePrefixMap.addMapping(SplitMap.first, SplitMap.second); diff --git a/lib/FrontendTool/FrontendTool.cpp b/lib/FrontendTool/FrontendTool.cpp index d2f24376d42..efe3920cc02 100644 --- a/lib/FrontendTool/FrontendTool.cpp +++ b/lib/FrontendTool/FrontendTool.cpp @@ -2240,6 +2240,10 @@ int swift::performFrontend(ArrayRef Args, trace.emplace(*buffer); }); + // Setting DWARF Version based on frontend options. + IRGenOptions &IRGenOpts = Invocation.getIRGenOptions(); + IRGenOpts.DWARFVersion = IRGenOpts.DWARFVersion; + // The compiler invocation is now fully configured; notify our observer. if (observer) { observer->parsedArgs(Invocation); diff --git a/test/Driver/options.swift b/test/Driver/options.swift index 69de3189ad6..996499f6447 100644 --- a/test/Driver/options.swift +++ b/test/Driver/options.swift @@ -116,6 +116,14 @@ // RUN: not %swiftc_driver -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix MISSING_OPTION_G_ERROR %s // MISSING_OPTION_G_ERROR: error: option '-debug-info-format={{.*}}' is missing a required argument (-g) +// RUN: %swift_driver -### -g -dwarf-version=3 %s 2>&1 | %FileCheck -check-prefix DWARF_VERSION_3 %s +// DWARF_VERSION_3: -dwarf-version=3 +// RUN: not %swift_driver -dwarf-version=1 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s +// RUN: not %swift_driver -dwarf-version=6 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s +// RUN: not %swiftc_driver -dwarf-version=1 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s +// RUN: not %swiftc_driver -dwarf-version=6 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s +// INVALID_DWARF_VERSION: invalid value '{{1|6}}' in '-dwarf-version={{1|6}}' + // RUN: not %swift_driver -gline-tables-only -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s // RUN: not %swift_driver -gdwarf-types -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s // RUN: not %swiftc_driver -gline-tables-only -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s