Skip to content

Commit 35369ed

Browse files
committed
Make the DWARF version emitted by the Swift compiler configurable.
Previously it was hardcoded to version 4 on all platforms. This patch introduces new driver and frontend flags -dwarf-version=<N>.
1 parent 8fa3d30 commit 35369ed

File tree

9 files changed

+75
-9
lines changed

9 files changed

+75
-9
lines changed

include/swift/AST/IRGenOptions.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class IRGenOptions {
237237
std::string DebugCompilationDir;
238238

239239
/// The DWARF version of debug info.
240-
unsigned DWARFVersion;
240+
uint8_t DWARFVersion = 4;
241241

242242
/// The command line string that is to be stored in the debug info.
243243
std::string DebugFlags;
@@ -503,8 +503,7 @@ class IRGenOptions {
503503
llvm::CallingConv::ID PlatformCCallingConvention;
504504

505505
IRGenOptions()
506-
: DWARFVersion(2),
507-
OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
506+
: OutputKind(IRGenOutputKind::LLVMAssemblyAfterOptimization),
508507
Verify(true), OptMode(OptimizationMode::NotSet),
509508
Sanitizers(OptionSet<SanitizerKind>()),
510509
SanitizersWithRecoveryInstrumentation(OptionSet<SanitizerKind>()),

include/swift/Driver/Driver.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ class OutputInfo {
125125
/// What kind of debug info to generate.
126126
IRGenDebugInfoFormat DebugInfoFormat = IRGenDebugInfoFormat::None;
127127

128+
/// DWARF output format version number.
129+
std::optional<uint8_t> DWARFVersion;
130+
128131
/// Whether or not the driver should generate a module.
129132
bool ShouldGenerateModule = false;
130133

include/swift/Option/Options.td

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ def gdwarf_types : Flag<["-"], "gdwarf-types">,
991991
Group<g_Group>, Flags<[FrontendOption]>,
992992
HelpText<"Emit full DWARF type info.">;
993993
def debug_prefix_map : Separate<["-"], "debug-prefix-map">,
994-
Flags<[FrontendOption]>,
995-
HelpText<"Remap source paths in debug info">, MetaVarName<"<prefix=replacement>">;
994+
Flags<[FrontendOption]>,
995+
HelpText<"Remap source paths in debug info">, MetaVarName<"<prefix=replacement>">;
996996
def coverage_prefix_map : Separate<["-"], "coverage-prefix-map">,
997997
Flags<[FrontendOption]>,
998998
HelpText<"Remap source paths in coverage info">, MetaVarName<"<prefix=replacement>">;
@@ -1007,6 +1007,10 @@ def file_compilation_dir : Separate<["-"], "file-compilation-dir">,
10071007
def debug_info_format : Joined<["-"], "debug-info-format=">,
10081008
Flags<[FrontendOption]>,
10091009
HelpText<"Specify the debug info format type to either 'dwarf' or 'codeview'">;
1010+
def dwarf_version : Joined<["-"], "dwarf-version=">,
1011+
Flags<[FrontendOption]>,
1012+
HelpText<"DWARF debug info version to produce if requested">,
1013+
MetaVarName<"<version>">;
10101014

10111015
def prefix_serialized_debugging_options : Flag<["-"], "prefix-serialized-debugging-options">,
10121016
Flags<[FrontendOption]>,

lib/Driver/DarwinToolChains.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,28 @@ toolchains::Darwin::addDeploymentTargetArgs(ArgStringList &Arguments,
626626
}
627627
}
628628

629+
static unsigned getDWARFVersionForTriple(const llvm::Triple &triple) {
630+
llvm::VersionTuple osVersion;
631+
const DarwinPlatformKind kind = getDarwinPlatformKind(triple);
632+
switch (kind) {
633+
case DarwinPlatformKind::MacOS:
634+
triple.getMacOSXVersion(osVersion);
635+
if (osVersion < llvm::VersionTuple(10, 11))
636+
return 2;
637+
return 4;
638+
case DarwinPlatformKind::IPhoneOSSimulator:
639+
case DarwinPlatformKind::IPhoneOS:
640+
case DarwinPlatformKind::TvOS:
641+
case DarwinPlatformKind::TvOSSimulator:
642+
osVersion = triple.getiOSVersion();
643+
if (osVersion < llvm::VersionTuple(9))
644+
return 2;
645+
return 4;
646+
default:
647+
return 4;
648+
}
649+
}
650+
629651
void toolchains::Darwin::addCommonFrontendArgs(
630652
const OutputInfo &OI, const CommandOutput &output,
631653
const llvm::opt::ArgList &inputArgs,
@@ -644,6 +666,16 @@ void toolchains::Darwin::addCommonFrontendArgs(
644666
inputArgs.MakeArgString(variantSDKVersion->getAsString()));
645667
}
646668
}
669+
std::string dwarfVersion;
670+
{
671+
llvm::raw_string_ostream os(dwarfVersion);
672+
os << "-dwarf-version=";
673+
if (OI.DWARFVersion)
674+
os << *OI.DWARFVersion;
675+
else
676+
os << getDWARFVersionForTriple(getTriple());
677+
}
678+
arguments.push_back(inputArgs.MakeArgString(dwarfVersion));
647679
}
648680

649681
/// Add the frontend arguments needed to find external plugins in standard

lib/Driver/Driver.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,16 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
16851685
: "-gdwarf_types");
16861686
}
16871687

1688+
if (const Arg *A = Args.getLastArg(options::OPT_dwarf_version)) {
1689+
unsigned vers;
1690+
if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 &&
1691+
vers <= 5)
1692+
OI.DWARFVersion = vers;
1693+
else
1694+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
1695+
A->getAsString(Args), A->getValue());
1696+
}
1697+
16881698
if (Args.hasArg(options::OPT_emit_module, options::OPT_emit_module_path)) {
16891699
// The user has requested a module, so generate one and treat it as
16901700
// top-level output.

lib/Driver/ToolChains.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ void ToolChain::addCommonFrontendArgs(const OutputInfo &OI,
262262
inputArgs.AddLastArg(arguments, options::OPT_enable_private_imports);
263263
inputArgs.AddLastArg(arguments, options::OPT_g_Group);
264264
inputArgs.AddLastArg(arguments, options::OPT_debug_info_format);
265+
inputArgs.AddLastArg(arguments, options::OPT_dwarf_version);
265266
inputArgs.AddLastArg(arguments, options::OPT_import_underlying_module);
266267
inputArgs.AddLastArg(arguments, options::OPT_module_cache_path);
267268
inputArgs.AddLastArg(arguments, options::OPT_module_link_name);

lib/Frontend/CompilerInvocation.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2469,7 +2469,6 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
24692469
Opts.DebugCompilationDir = std::string(cwd.str());
24702470
}
24712471
}
2472-
24732472
if (const Arg *A = Args.getLastArg(options::OPT_debug_info_format)) {
24742473
if (A->containsValue("dwarf"))
24752474
Opts.DebugInfoFormat = IRGenDebugInfoFormat::DWARF;
@@ -2499,6 +2498,16 @@ static bool ParseIRGenArgs(IRGenOptions &Opts, ArgList &Args,
24992498
: "-gdwarf_types");
25002499
}
25012500

2501+
if (auto A = Args.getLastArg(OPT_dwarf_version)) {
2502+
unsigned vers;
2503+
if (!StringRef(A->getValue()).getAsInteger(10, vers) && vers >= 2 &&
2504+
vers <= 5)
2505+
Opts.DWARFVersion = vers;
2506+
else
2507+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
2508+
A->getAsString(Args), A->getValue());
2509+
}
2510+
25022511
for (auto A : Args.getAllArgValues(options::OPT_file_prefix_map)) {
25032512
auto SplitMap = StringRef(A).split('=');
25042513
Opts.FilePrefixMap.addMapping(SplitMap.first, SplitMap.second);

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,10 +2240,10 @@ int swift::performFrontend(ArrayRef<const char *> Args,
22402240
trace.emplace(*buffer);
22412241
});
22422242

2243-
// Setting DWARF Version depend on platform
2243+
// Setting DWARF Version depending on platform.
22442244
IRGenOptions &IRGenOpts = Invocation.getIRGenOptions();
2245-
IRGenOpts.DWARFVersion = swift::DWARFVersion;
2246-
2245+
IRGenOpts.DWARFVersion = Invocation.getFrontendOptions().DWARFVersion;
2246+
22472247
// The compiler invocation is now fully configured; notify our observer.
22482248
if (observer) {
22492249
observer->parsedArgs(Invocation);

test/Driver/options.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,14 @@
116116
// RUN: not %swiftc_driver -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix MISSING_OPTION_G_ERROR %s
117117
// MISSING_OPTION_G_ERROR: error: option '-debug-info-format={{.*}}' is missing a required argument (-g)
118118

119+
// RUN: %swift_driver -### -g -dwarf-version=3 %s 2>&1 | %FileCheck -check-prefix DWARF_VERSION_3 %s
120+
// DWARF_VERSION_3: -dwarf-version=3
121+
// RUN: not %swift_driver -dwarf-version=1 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
122+
// RUN: not %swift_driver -dwarf-version=6 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
123+
// RUN: not %swiftc_driver -dwarf-version=1 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
124+
// RUN: not %swiftc_driver -dwarf-version=6 %s 2>&1 | %FileCheck -check-prefix INVALID_DWARF_VERSION %s
125+
// INVALID_DWARF_VERSION: invalid value '{{1|6}}' in '-dwarf-version={{1|6}}'
126+
119127
// RUN: not %swift_driver -gline-tables-only -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s
120128
// RUN: not %swift_driver -gdwarf-types -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s
121129
// RUN: not %swiftc_driver -gline-tables-only -debug-info-format=codeview %s 2>&1 | %FileCheck -check-prefix BAD_DEBUG_LEVEL_ERROR %s

0 commit comments

Comments
 (0)