Skip to content

Commit

Permalink
Replace Session should_remap_filepaths with filename_display_preference
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Mar 27, 2024
1 parent 8bf1ccb commit f2a2c4c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 50 deletions.
15 changes: 2 additions & 13 deletions compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs
Expand Up @@ -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);

Expand All @@ -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,
);
Expand Down
23 changes: 7 additions & 16 deletions compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -116,7 +107,7 @@ impl DebugContext {
endian,
dwarf,
unit_range_list: RangeList(Vec::new()),
should_remap_filepaths,
filename_display_preference,
}
}

Expand Down
16 changes: 7 additions & 9 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Expand Up @@ -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);
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs
Expand Up @@ -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()
Expand Down Expand Up @@ -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())
}
};

Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_session/src/session.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions compiler/rustc_span/src/lib.rs
Expand Up @@ -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(),
Expand Down

0 comments on commit f2a2c4c

Please sign in to comment.