Skip to content

Commit 83e3e37

Browse files
committed
Clean up uses of the unstable dwarf_version option
- Consolidate calculation of the effective value. - Check the target `DebuginfoKind` instead of using `is_like_msvc`.
1 parent 76a030a commit 83e3e37

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
919919
.unwrap_or_default();
920920
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
921921

922-
let dwarf_version =
923-
tcx.sess.opts.unstable_opts.dwarf_version.unwrap_or(tcx.sess.target.default_dwarf_version);
922+
let dwarf_version = tcx.sess.dwarf_version();
924923
let is_dwarf_kind =
925924
matches!(tcx.sess.target.debuginfo_kind, DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym);
926925
// Don't emit `.debug_pubnames` and `.debug_pubtypes` on DWARFv4 or lower.

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+26-23
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use rustc_session::config::{self, DebugInfo};
2222
use rustc_span::{
2323
BytePos, Pos, SourceFile, SourceFileAndLine, SourceFileHash, Span, StableSourceFileId, Symbol,
2424
};
25+
use rustc_target::spec::DebuginfoKind;
2526
use smallvec::SmallVec;
2627
use tracing::debug;
2728

@@ -93,29 +94,31 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
9394

9495
pub(crate) fn finalize(&self, sess: &Session) {
9596
unsafe { llvm::LLVMRustDIBuilderFinalize(self.builder) };
96-
if !sess.target.is_like_msvc {
97-
// Debuginfo generation in LLVM by default uses a higher
98-
// version of dwarf than macOS currently understands. We can
99-
// instruct LLVM to emit an older version of dwarf, however,
100-
// for macOS to understand. For more info see #11352
101-
// This can be overridden using --llvm-opts -dwarf-version,N.
102-
// Android has the same issue (#22398)
103-
let dwarf_version =
104-
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
105-
llvm::add_module_flag_u32(
106-
self.llmod,
107-
llvm::ModuleFlagMergeBehavior::Warning,
108-
"Dwarf Version",
109-
dwarf_version,
110-
);
111-
} else {
112-
// Indicate that we want CodeView debug information on MSVC
113-
llvm::add_module_flag_u32(
114-
self.llmod,
115-
llvm::ModuleFlagMergeBehavior::Warning,
116-
"CodeView",
117-
1,
118-
);
97+
98+
match sess.target.debuginfo_kind {
99+
DebuginfoKind::Dwarf | DebuginfoKind::DwarfDsym => {
100+
// Debuginfo generation in LLVM by default uses a higher
101+
// version of dwarf than macOS currently understands. We can
102+
// instruct LLVM to emit an older version of dwarf, however,
103+
// for macOS to understand. For more info see #11352
104+
// This can be overridden using --llvm-opts -dwarf-version,N.
105+
// Android has the same issue (#22398)
106+
llvm::add_module_flag_u32(
107+
self.llmod,
108+
llvm::ModuleFlagMergeBehavior::Warning,
109+
"Dwarf Version",
110+
sess.dwarf_version(),
111+
);
112+
}
113+
DebuginfoKind::Pdb => {
114+
// Indicate that we want CodeView debug information
115+
llvm::add_module_flag_u32(
116+
self.llmod,
117+
llvm::ModuleFlagMergeBehavior::Warning,
118+
"CodeView",
119+
1,
120+
);
121+
}
119122
}
120123

121124
// Prevent bitcode readers from deleting the debug info.

compiler/rustc_session/src/options.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1803,6 +1803,7 @@ options! {
18031803
"output statistics about monomorphization collection"),
18041804
dump_mono_stats_format: DumpMonoStatsFormat = (DumpMonoStatsFormat::Markdown, parse_dump_mono_stats, [UNTRACKED],
18051805
"the format to use for -Z dump-mono-stats (`markdown` (default) or `json`)"),
1806+
#[rustc_lint_opt_deny_field_access("use `Session::dwarf_version` instead of this field")]
18061807
dwarf_version: Option<u32> = (None, parse_opt_number, [TRACKED],
18071808
"version of DWARF debug information to emit (default: 2 or 4, depending on platform)"),
18081809
dylib_lto: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_session/src/session.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,10 @@ impl Session {
732732
self.opts.cg.split_debuginfo.unwrap_or(self.target.split_debuginfo)
733733
}
734734

735+
pub fn dwarf_version(&self) -> u32 {
736+
self.opts.unstable_opts.dwarf_version.unwrap_or(self.target.default_dwarf_version)
737+
}
738+
735739
pub fn stack_protector(&self) -> StackProtector {
736740
if self.target.options.supports_stack_protector {
737741
self.opts.unstable_opts.stack_protector
@@ -1260,8 +1264,7 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
12601264
}
12611265

12621266
if sess.opts.unstable_opts.embed_source {
1263-
let dwarf_version =
1264-
sess.opts.unstable_opts.dwarf_version.unwrap_or(sess.target.default_dwarf_version);
1267+
let dwarf_version = sess.dwarf_version();
12651268

12661269
if dwarf_version < 5 {
12671270
sess.dcx().emit_warn(errors::EmbedSourceInsufficientDwarfVersion { dwarf_version });

0 commit comments

Comments
 (0)