Replace Session should_remap_filepaths with filename_display_preference

This commit is contained in:
Urgau 2024-03-22 15:27:17 +01:00
parent 4f4fa42b0e
commit fefb8f1f9c
6 changed files with 48 additions and 50 deletions

View File

@ -89,11 +89,7 @@ impl DebugContext {
match &source_file.name {
FileName::Real(path) => {
let (dir_path, file_name) =
split_path_dir_and_file(if self.should_remap_filepaths {
path.remapped_path_if_available()
} else {
path.local_path_if_available()
});
split_path_dir_and_file(path.to_path(self.filename_display_preference));
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
let file_name = osstr_as_utf8_bytes(file_name);
@ -115,14 +111,7 @@ impl DebugContext {
filename => {
let dir_id = line_program.default_directory();
let dummy_file_name = LineString::new(
filename
.display(if self.should_remap_filepaths {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
})
.to_string()
.into_bytes(),
filename.display(self.filename_display_preference).to_string().into_bytes(),
line_program.encoding(),
line_strings,
);

View File

@ -42,7 +42,7 @@ pub(crate) struct DebugContext {
namespace_map: DefIdMap<UnitEntryId>,
array_size_type: UnitEntryId,
should_remap_filepaths: bool,
filename_display_preference: FileNameDisplayPreference,
}
pub(crate) struct FunctionDebugContext {
@ -85,26 +85,17 @@ impl DebugContext {
let mut dwarf = DwarfUnit::new(encoding);
use rustc_session::config::RemapPathScopeComponents;
use rustc_session::RemapFileNameExt;
let should_remap_filepaths =
tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);
let filename_display_preference =
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
let producer = producer(tcx.sess);
let comp_dir = tcx
.sess
.opts
.working_dir
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
.to_string_lossy()
.to_string();
let comp_dir =
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();
let (name, file_info) = match tcx.sess.local_crate_source_file() {
Some(path) => {
let name = path
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
.to_string_lossy()
.to_string();
let name = path.to_string_lossy(filename_display_preference).to_string();
(name, None)
}
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
@ -161,7 +152,7 @@ impl DebugContext {
stack_pointer_register,
namespace_map: DefIdMap::default(),
array_size_type,
should_remap_filepaths,
filename_display_preference,
}
}

View File

@ -258,19 +258,17 @@ pub fn target_machine_factory(
};
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
let should_prefer_remapped_paths =
sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);
let file_name_display_preference =
sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
Arc::new(move |config: TargetMachineFactoryConfig| {
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
let path = path.unwrap_or_default();
let path = path_mapping.to_real_filename(path);
let path = if should_prefer_remapped_paths {
path.remapped_path_if_available()
} else {
path.local_path_if_available()
};
CString::new(path.to_str().unwrap()).unwrap()
let path = path_mapping
.to_real_filename(path)
.to_string_lossy(file_name_display_preference)
.into_owned();
CString::new(path).unwrap()
};
let split_dwarf_file = path_to_cstring_helper(config.split_dwarf_file);

View File

@ -554,13 +554,16 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
) -> &'ll DIFile {
debug!(?source_file.name);
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
let filename_display_preference =
cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
use rustc_session::config::RemapPathScopeComponents;
let (directory, file_name) = match &source_file.name {
FileName::Real(filename) => {
let working_directory = &cx.sess().opts.working_dir;
debug!(?working_directory);
if cx.sess().should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
if filename_display_preference == FileNameDisplayPreference::Remapped {
let filename = cx
.sess()
.source_map()
@ -623,13 +626,7 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
}
other => {
debug!(?other);
(
"".into(),
other
.for_scope(cx.sess(), RemapPathScopeComponents::DEBUGINFO)
.to_string_lossy()
.into_owned(),
)
("".into(), other.display(filename_display_preference).to_string())
}
};

View File

@ -29,7 +29,7 @@ use rustc_macros::HashStable_Generic;
pub use rustc_span::def_id::StableCrateId;
use rustc_span::edition::Edition;
use rustc_span::source_map::{FileLoader, FilePathMapping, RealFileLoader, SourceMap};
use rustc_span::RealFileName;
use rustc_span::{FileNameDisplayPreference, RealFileName};
use rustc_span::{SourceFileHashAlgorithm, Span, Symbol};
use rustc_target::asm::InlineAsmArch;
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@ -882,8 +882,19 @@ impl Session {
self.opts.cg.link_dead_code.unwrap_or(false)
}
pub fn should_prefer_remapped(&self, scope: RemapPathScopeComponents) -> bool {
self.opts.unstable_opts.remap_path_scope.contains(scope)
pub fn filename_display_preference(
&self,
scope: RemapPathScopeComponents,
) -> FileNameDisplayPreference {
assert!(
scope.bits().count_ones() == 1,
"one and only one scope should be passed to `Session::filename_display_preference`"
);
if self.opts.unstable_opts.remap_path_scope.contains(scope) {
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
}
}
}

View File

@ -271,6 +271,18 @@ impl RealFileName {
}
}
/// Return the path remmapped or not depending on the [`FileNameDisplayPreference`].
///
/// For the purpose of this function, local and short preference are equal.
pub fn to_path(&self, display_pref: FileNameDisplayPreference) -> &Path {
match display_pref {
FileNameDisplayPreference::Local | FileNameDisplayPreference::Short => {
self.local_path_if_available()
}
FileNameDisplayPreference::Remapped => self.remapped_path_if_available(),
}
}
pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> {
match display_pref {
FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(),