Skip to content

Commit f2a2c4c

Browse files
committed
Replace Session should_remap_filepaths with filename_display_preference
1 parent 8bf1ccb commit f2a2c4c

File tree

6 files changed

+48
-50
lines changed

6 files changed

+48
-50
lines changed

compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,7 @@ impl DebugContext {
8787
match &source_file.name {
8888
FileName::Real(path) => {
8989
let (dir_path, file_name) =
90-
split_path_dir_and_file(if self.should_remap_filepaths {
91-
path.remapped_path_if_available()
92-
} else {
93-
path.local_path_if_available()
94-
});
90+
split_path_dir_and_file(path.to_path(self.filename_display_preference));
9591
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
9692
let file_name = osstr_as_utf8_bytes(file_name);
9793

@@ -112,14 +108,7 @@ impl DebugContext {
112108
filename => {
113109
let dir_id = line_program.default_directory();
114110
let dummy_file_name = LineString::new(
115-
filename
116-
.display(if self.should_remap_filepaths {
117-
FileNameDisplayPreference::Remapped
118-
} else {
119-
FileNameDisplayPreference::Local
120-
})
121-
.to_string()
122-
.into_bytes(),
111+
filename.display(self.filename_display_preference).to_string().into_bytes(),
123112
line_program.encoding(),
124113
line_strings,
125114
);

compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub(crate) struct DebugContext {
2929
dwarf: DwarfUnit,
3030
unit_range_list: RangeList,
3131

32-
should_remap_filepaths: bool,
32+
filename_display_preference: FileNameDisplayPreference,
3333
}
3434

3535
pub(crate) struct FunctionDebugContext {
@@ -63,26 +63,17 @@ impl DebugContext {
6363
let mut dwarf = DwarfUnit::new(encoding);
6464

6565
use rustc_session::config::RemapPathScopeComponents;
66-
use rustc_session::RemapFileNameExt;
6766

68-
let should_remap_filepaths =
69-
tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);
67+
let filename_display_preference =
68+
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
7069

7170
let producer = producer(tcx.sess);
72-
let comp_dir = tcx
73-
.sess
74-
.opts
75-
.working_dir
76-
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
77-
.to_string_lossy()
78-
.to_string();
71+
let comp_dir =
72+
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();
7973

8074
let (name, file_info) = match tcx.sess.local_crate_source_file() {
8175
Some(path) => {
82-
let name = path
83-
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
84-
.to_string_lossy()
85-
.to_string();
76+
let name = path.to_string_lossy(filename_display_preference).to_string();
8677
(name, None)
8778
}
8879
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
@@ -116,7 +107,7 @@ impl DebugContext {
116107
endian,
117108
dwarf,
118109
unit_range_list: RangeList(Vec::new()),
119-
should_remap_filepaths,
110+
filename_display_preference,
120111
}
121112
}
122113

compiler/rustc_codegen_llvm/src/back/write.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,17 @@ pub fn target_machine_factory(
258258
};
259259
let debuginfo_compression = SmallCStr::new(&debuginfo_compression);
260260

261-
let should_prefer_remapped_paths =
262-
sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);
261+
let file_name_display_preference =
262+
sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
263263

264264
Arc::new(move |config: TargetMachineFactoryConfig| {
265265
let path_to_cstring_helper = |path: Option<PathBuf>| -> CString {
266266
let path = path.unwrap_or_default();
267-
let path = path_mapping.to_real_filename(path);
268-
let path = if should_prefer_remapped_paths {
269-
path.remapped_path_if_available()
270-
} else {
271-
path.local_path_if_available()
272-
};
273-
CString::new(path.to_str().unwrap()).unwrap()
267+
let path = path_mapping
268+
.to_real_filename(path)
269+
.to_string_lossy(file_name_display_preference)
270+
.into_owned();
271+
CString::new(path).unwrap()
274272
};
275273

276274
let split_dwarf_file = path_to_cstring_helper(config.split_dwarf_file);

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,16 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
554554
) -> &'ll DIFile {
555555
debug!(?source_file.name);
556556

557-
use rustc_session::{config::RemapPathScopeComponents, RemapFileNameExt};
557+
let filename_display_preference =
558+
cx.sess().filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
559+
560+
use rustc_session::config::RemapPathScopeComponents;
558561
let (directory, file_name) = match &source_file.name {
559562
FileName::Real(filename) => {
560563
let working_directory = &cx.sess().opts.working_dir;
561564
debug!(?working_directory);
562565

563-
if cx.sess().should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
566+
if filename_display_preference == FileNameDisplayPreference::Remapped {
564567
let filename = cx
565568
.sess()
566569
.source_map()
@@ -623,13 +626,7 @@ pub fn file_metadata<'ll>(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) ->
623626
}
624627
other => {
625628
debug!(?other);
626-
(
627-
"".into(),
628-
other
629-
.for_scope(cx.sess(), RemapPathScopeComponents::DEBUGINFO)
630-
.to_string_lossy()
631-
.into_owned(),
632-
)
629+
("".into(), other.display(filename_display_preference).to_string())
633630
}
634631
};
635632

compiler/rustc_session/src/session.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use rustc_macros::HashStable_Generic;
2929
pub use rustc_span::def_id::StableCrateId;
3030
use rustc_span::edition::Edition;
3131
use rustc_span::source_map::{FileLoader, FilePathMapping, RealFileLoader, SourceMap};
32-
use rustc_span::RealFileName;
32+
use rustc_span::{FileNameDisplayPreference, RealFileName};
3333
use rustc_span::{SourceFileHashAlgorithm, Span, Symbol};
3434
use rustc_target::asm::InlineAsmArch;
3535
use rustc_target::spec::{CodeModel, PanicStrategy, RelocModel, RelroLevel};
@@ -882,8 +882,19 @@ impl Session {
882882
self.opts.cg.link_dead_code.unwrap_or(false)
883883
}
884884

885-
pub fn should_prefer_remapped(&self, scope: RemapPathScopeComponents) -> bool {
886-
self.opts.unstable_opts.remap_path_scope.contains(scope)
885+
pub fn filename_display_preference(
886+
&self,
887+
scope: RemapPathScopeComponents,
888+
) -> FileNameDisplayPreference {
889+
assert!(
890+
scope.bits().count_ones() == 1,
891+
"one and only one scope should be passed to `Session::filename_display_preference`"
892+
);
893+
if self.opts.unstable_opts.remap_path_scope.contains(scope) {
894+
FileNameDisplayPreference::Remapped
895+
} else {
896+
FileNameDisplayPreference::Local
897+
}
887898
}
888899
}
889900

compiler/rustc_span/src/lib.rs

+12
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,18 @@ impl RealFileName {
271271
}
272272
}
273273

274+
/// Return the path remmapped or not depending on the [`FileNameDisplayPreference`].
275+
///
276+
/// For the purpose of this function, local and short preference are equal.
277+
pub fn to_path(&self, display_pref: FileNameDisplayPreference) -> &Path {
278+
match display_pref {
279+
FileNameDisplayPreference::Local | FileNameDisplayPreference::Short => {
280+
self.local_path_if_available()
281+
}
282+
FileNameDisplayPreference::Remapped => self.remapped_path_if_available(),
283+
}
284+
}
285+
274286
pub fn to_string_lossy(&self, display_pref: FileNameDisplayPreference) -> Cow<'_, str> {
275287
match display_pref {
276288
FileNameDisplayPreference::Local => self.local_path_if_available().to_string_lossy(),

0 commit comments

Comments
 (0)