Skip to content

Commit fefb8f1

Browse files
committed
Replace Session should_remap_filepaths with filename_display_preference
1 parent 4f4fa42 commit fefb8f1

File tree

6 files changed

+48
-50
lines changed

6 files changed

+48
-50
lines changed

Diff for: compiler/rustc_codegen_cranelift/src/debuginfo/line_info.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ impl DebugContext {
8989
match &source_file.name {
9090
FileName::Real(path) => {
9191
let (dir_path, file_name) =
92-
split_path_dir_and_file(if self.should_remap_filepaths {
93-
path.remapped_path_if_available()
94-
} else {
95-
path.local_path_if_available()
96-
});
92+
split_path_dir_and_file(path.to_path(self.filename_display_preference));
9793
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
9894
let file_name = osstr_as_utf8_bytes(file_name);
9995

@@ -115,14 +111,7 @@ impl DebugContext {
115111
filename => {
116112
let dir_id = line_program.default_directory();
117113
let dummy_file_name = LineString::new(
118-
filename
119-
.display(if self.should_remap_filepaths {
120-
FileNameDisplayPreference::Remapped
121-
} else {
122-
FileNameDisplayPreference::Local
123-
})
124-
.to_string()
125-
.into_bytes(),
114+
filename.display(self.filename_display_preference).to_string().into_bytes(),
126115
line_program.encoding(),
127116
line_strings,
128117
);

Diff for: compiler/rustc_codegen_cranelift/src/debuginfo/mod.rs

+7-16
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) struct DebugContext {
4242
namespace_map: DefIdMap<UnitEntryId>,
4343
array_size_type: UnitEntryId,
4444

45-
should_remap_filepaths: bool,
45+
filename_display_preference: FileNameDisplayPreference,
4646
}
4747

4848
pub(crate) struct FunctionDebugContext {
@@ -85,26 +85,17 @@ impl DebugContext {
8585
let mut dwarf = DwarfUnit::new(encoding);
8686

8787
use rustc_session::config::RemapPathScopeComponents;
88-
use rustc_session::RemapFileNameExt;
8988

90-
let should_remap_filepaths =
91-
tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);
89+
let filename_display_preference =
90+
tcx.sess.filename_display_preference(RemapPathScopeComponents::DEBUGINFO);
9291

9392
let producer = producer(tcx.sess);
94-
let comp_dir = tcx
95-
.sess
96-
.opts
97-
.working_dir
98-
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
99-
.to_string_lossy()
100-
.to_string();
93+
let comp_dir =
94+
tcx.sess.opts.working_dir.to_string_lossy(filename_display_preference).to_string();
10195

10296
let (name, file_info) = match tcx.sess.local_crate_source_file() {
10397
Some(path) => {
104-
let name = path
105-
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
106-
.to_string_lossy()
107-
.to_string();
98+
let name = path.to_string_lossy(filename_display_preference).to_string();
10899
(name, None)
109100
}
110101
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),
@@ -161,7 +152,7 @@ impl DebugContext {
161152
stack_pointer_register,
162153
namespace_map: DefIdMap::default(),
163154
array_size_type,
164-
should_remap_filepaths,
155+
filename_display_preference,
165156
}
166157
}
167158

Diff for: 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);

Diff for: 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

Diff for: 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

Diff for: 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)