Skip to content

Commit

Permalink
Consolidate checking for msvc when generating debuginfo
Browse files Browse the repository at this point in the history
If the target we're generating code for is msvc, then we do two main
things differently: we generate type names in a C++ style instead of a
Rust style and we generate debuginfo for enums differently.

I've refactored the code so that there is one function
(`cpp_like_debuginfo`) which determines if we should use the C++ style
of naming types and other debuginfo generation or the regular Rust one.
  • Loading branch information
wesleywiser committed Jan 7, 2022
1 parent f8abed9 commit 836addc
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 64 deletions.
28 changes: 14 additions & 14 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::llvm::debuginfo::{
use crate::value::Value;

use cstr::cstr;
use rustc_codegen_ssa::debuginfo::type_names::cpp_like_debuginfo;
use rustc_codegen_ssa::traits::*;
use rustc_data_structures::fingerprint::Fingerprint;
use rustc_data_structures::fx::FxHashMap;
Expand Down Expand Up @@ -933,16 +934,16 @@ fn basic_type_metadata<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'l

// When targeting MSVC, emit MSVC style type names for compatibility with
// .natvis visualizers (and perhaps other existing native debuggers?)
let msvc_like_names = cx.tcx.sess.target.is_like_msvc;
let cpp_like_debuginfo = cpp_like_debuginfo(cx.tcx);

let (name, encoding) = match t.kind() {
ty::Never => ("!", DW_ATE_unsigned),
ty::Tuple(elements) if elements.is_empty() => ("()", DW_ATE_unsigned),
ty::Bool => ("bool", DW_ATE_boolean),
ty::Char => ("char", DW_ATE_unsigned_char),
ty::Int(int_ty) if msvc_like_names => (int_ty.msvc_basic_name(), DW_ATE_signed),
ty::Uint(uint_ty) if msvc_like_names => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
ty::Float(float_ty) if msvc_like_names => (float_ty.msvc_basic_name(), DW_ATE_float),
ty::Int(int_ty) if cpp_like_debuginfo => (int_ty.msvc_basic_name(), DW_ATE_signed),
ty::Uint(uint_ty) if cpp_like_debuginfo => (uint_ty.msvc_basic_name(), DW_ATE_unsigned),
ty::Float(float_ty) if cpp_like_debuginfo => (float_ty.msvc_basic_name(), DW_ATE_float),
ty::Int(int_ty) => (int_ty.name_str(), DW_ATE_signed),
ty::Uint(uint_ty) => (uint_ty.name_str(), DW_ATE_unsigned),
ty::Float(float_ty) => (float_ty.name_str(), DW_ATE_float),
Expand All @@ -959,7 +960,7 @@ fn basic_type_metadata<'ll, 'tcx>(cx: &CodegenCx<'ll, 'tcx>, t: Ty<'tcx>) -> &'l
)
};

if !msvc_like_names {
if !cpp_like_debuginfo {
return ty_metadata;
}

Expand Down Expand Up @@ -1521,13 +1522,6 @@ fn prepare_union_metadata<'ll, 'tcx>(
// Enums
//=-----------------------------------------------------------------------------

/// DWARF variant support is only available starting in LLVM 8, but
/// on MSVC we have to use the fallback mode, because LLVM doesn't
/// lower variant parts to PDB.
fn use_enum_fallback(cx: &CodegenCx<'_, '_>) -> bool {
cx.sess().target.is_like_msvc
}

// FIXME(eddyb) maybe precompute this? Right now it's computed once
// per generator monomorphization, but it doesn't depend on substs.
fn generator_layout_and_saved_local_names<'tcx>(
Expand Down Expand Up @@ -1602,7 +1596,10 @@ impl<'ll, 'tcx> EnumMemberDescriptionFactory<'ll, 'tcx> {
_ => bug!(),
};

let fallback = use_enum_fallback(cx);
// While LLVM supports generating debuginfo for variant types (enums), it doesn't support
// lowering that debuginfo to CodeView records for msvc targets. So if we are targeting
// msvc, then we need to use a different, fallback encoding of the debuginfo.
let fallback = cpp_like_debuginfo(cx.tcx);
// This will always find the metadata in the type map.
let self_metadata = type_metadata(cx, self.enum_type, self.span);

Expand Down Expand Up @@ -2155,7 +2152,10 @@ fn prepare_enum_metadata<'ll, 'tcx>(
return FinalMetadata(discriminant_type_metadata(tag.value));
}

if use_enum_fallback(cx) {
// While LLVM supports generating debuginfo for variant types (enums), it doesn't support
// lowering that debuginfo to CodeView records for msvc targets. So if we are targeting
// msvc, then we need to use a different encoding of the debuginfo.
if cpp_like_debuginfo(tcx) {
let discriminant_type_metadata = match layout.variants {
Variants::Single { .. } => None,
Variants::Multiple { tag_encoding: TagEncoding::Niche { .. }, tag, .. }
Expand Down
Loading

0 comments on commit 836addc

Please sign in to comment.