From f2a2c4c9ef8443329cc0d0a0a7dd5fdbf364b001 Mon Sep 17 00:00:00 2001 From: Urgau Date: Fri, 22 Mar 2024 15:27:17 +0100 Subject: [PATCH] Replace Session should_remap_filepaths with filename_display_preference --- .../src/debuginfo/line_info.rs | 15 ++---------- .../src/debuginfo/mod.rs | 23 ++++++------------- compiler/rustc_codegen_llvm/src/back/write.rs | 16 ++++++------- .../src/debuginfo/metadata.rs | 15 +++++------- compiler/rustc_session/src/session.rs | 17 +++++++++++--- compiler/rustc_span/src/lib.rs | 12 ++++++++++ 6 files changed, 48 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs index d1b21d0a0b6c5..a5f0b34309fb8 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs @@ -87,11 +87,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); @@ -112,14 +108,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, ); diff --git a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs index 9b40f0e6020ab..c334f8a0a26bb 100644 --- a/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs @@ -29,7 +29,7 @@ pub(crate) struct DebugContext { dwarf: DwarfUnit, unit_range_list: RangeList, - should_remap_filepaths: bool, + filename_display_preference: FileNameDisplayPreference, } pub(crate) struct FunctionDebugContext { @@ -63,26 +63,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), @@ -116,7 +107,7 @@ impl DebugContext { endian, dwarf, unit_range_list: RangeList(Vec::new()), - should_remap_filepaths, + filename_display_preference, } } diff --git a/compiler/rustc_codegen_llvm/src/back/write.rs b/compiler/rustc_codegen_llvm/src/back/write.rs index d27df052d4309..4efea66a7f1e1 100644 --- a/compiler/rustc_codegen_llvm/src/back/write.rs +++ b/compiler/rustc_codegen_llvm/src/back/write.rs @@ -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| -> 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); diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index 4dd51adc67b7a..e5fecddec528d 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -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()) } }; diff --git a/compiler/rustc_session/src/session.rs b/compiler/rustc_session/src/session.rs index 0e15646841e85..85428303382a2 100644 --- a/compiler/rustc_session/src/session.rs +++ b/compiler/rustc_session/src/session.rs @@ -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 + } } } diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 616a7ccc7c64f..0c49dea2c7ec4 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -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(),