Skip to content

Commit 4f4fa42

Browse files
committed
Introduce FileNameMapping::to_real_filename and use it everywhere
1 parent ee2898d commit 4f4fa42

File tree

6 files changed

+42
-41
lines changed

6 files changed

+42
-41
lines changed

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

+8-12
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ impl DebugContext {
8585
let mut dwarf = DwarfUnit::new(encoding);
8686

8787
use rustc_session::config::RemapPathScopeComponents;
88+
use rustc_session::RemapFileNameExt;
89+
8890
let should_remap_filepaths =
8991
tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO);
9092

@@ -93,22 +95,16 @@ impl DebugContext {
9395
.sess
9496
.opts
9597
.working_dir
96-
.to_string_lossy(if should_remap_filepaths {
97-
FileNameDisplayPreference::Remapped
98-
} else {
99-
FileNameDisplayPreference::Local
100-
})
101-
.into_owned();
98+
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
99+
.to_string_lossy()
100+
.to_string();
102101

103102
let (name, file_info) = match tcx.sess.local_crate_source_file() {
104103
Some(path) => {
105104
let name = path
106-
.to_string_lossy(if should_remap_filepaths {
107-
FileNameDisplayPreference::Remapped
108-
} else {
109-
FileNameDisplayPreference::Local
110-
})
111-
.into_owned();
105+
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
106+
.to_string_lossy()
107+
.to_string();
112108
(name, None)
113109
}
114110
None => (tcx.crate_name(LOCAL_CRATE).to_string(), None),

Diff for: compiler/rustc_codegen_llvm/src/back/write.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,11 @@ pub fn target_machine_factory(
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);
267268
let path = if should_prefer_remapped_paths {
268-
path_mapping.map_prefix(path).0
269+
path.remapped_path_if_available()
269270
} else {
270-
path.into()
271+
path.local_path_if_available()
271272
};
272273
CString::new(path.to_str().unwrap()).unwrap()
273274
};

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -878,26 +878,21 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
878878
.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO)
879879
.to_string_lossy();
880880
let output_filenames = tcx.output_filenames(());
881-
let split_name = if tcx.sess.target_can_use_split_dwarf() {
882-
output_filenames
883-
.split_dwarf_path(
884-
tcx.sess.split_debuginfo(),
885-
tcx.sess.opts.unstable_opts.split_dwarf_kind,
886-
Some(codegen_unit_name),
887-
)
888-
// We get a path relative to the working directory from split_dwarf_path
889-
.map(|f| {
890-
if tcx.sess.should_prefer_remapped(RemapPathScopeComponents::DEBUGINFO) {
891-
tcx.sess.source_map().path_mapping().map_prefix(f).0
892-
} else {
893-
f.into()
894-
}
895-
})
881+
let split_name = if tcx.sess.target_can_use_split_dwarf()
882+
&& let Some(f) = output_filenames.split_dwarf_path(
883+
tcx.sess.split_debuginfo(),
884+
tcx.sess.opts.unstable_opts.split_dwarf_kind,
885+
Some(codegen_unit_name),
886+
) {
887+
// We get a path relative to the working directory from split_dwarf_path
888+
Some(tcx.sess.source_map().path_mapping().to_real_filename(f))
896889
} else {
897890
None
898-
}
899-
.unwrap_or_default();
900-
let split_name = split_name.to_str().unwrap();
891+
};
892+
let split_name = split_name
893+
.as_ref()
894+
.map(|f| f.for_scope(tcx.sess, RemapPathScopeComponents::DEBUGINFO).to_string_lossy())
895+
.unwrap_or_default();
901896
let kind = DebugEmissionKind::from_generic(tcx.sess.opts.debuginfo);
902897

903898
let dwarf_version =

Diff for: compiler/rustc_session/src/config.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -2842,13 +2842,8 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
28422842
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
28432843
});
28442844

2845-
let remap = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
2846-
let (path, remapped) = remap.map_prefix(&working_dir);
2847-
let working_dir = if remapped {
2848-
RealFileName::Remapped { virtual_name: path.into_owned(), local_path: Some(working_dir) }
2849-
} else {
2850-
RealFileName::LocalPath(path.into_owned())
2851-
};
2845+
let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
2846+
let working_dir = file_mapping.to_real_filename(&working_dir);
28522847

28532848
let verbose = matches.opt_present("verbose") || unstable_opts.verbose_internals;
28542849

Diff for: compiler/rustc_session/src/session.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,7 @@ impl Session {
252252
}
253253

254254
pub fn local_crate_source_file(&self) -> Option<RealFileName> {
255-
let path = self.io.input.opt_path()?;
256-
Some(RealFileName::LocalPath(path.to_path_buf()))
255+
Some(self.source_map().path_mapping().to_real_filename(self.io.input.opt_path()?))
257256
}
258257

259258
fn check_miri_unleashed_features(&self) -> Option<ErrorGuaranteed> {

Diff for: compiler/rustc_span/src/source_map.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,21 @@ impl FilePathMapping {
11291129
}
11301130
}
11311131

1132+
/// Applies any path prefix substitution as defined by the mapping.
1133+
/// The return value is the local path with a "virtual path" representing the remapped
1134+
/// part if any remapping was performed.
1135+
pub fn to_real_filename<'a>(&self, local_path: impl Into<Cow<'a, Path>>) -> RealFileName {
1136+
let local_path = local_path.into();
1137+
if let (remapped_path, true) = self.map_prefix(&*local_path) {
1138+
RealFileName::Remapped {
1139+
virtual_name: remapped_path.into_owned(),
1140+
local_path: Some(local_path.into_owned()),
1141+
}
1142+
} else {
1143+
RealFileName::LocalPath(local_path.into_owned())
1144+
}
1145+
}
1146+
11321147
/// Expand a relative path to an absolute path with remapping taken into account.
11331148
/// Use this when absolute paths are required (e.g. debuginfo or crate metadata).
11341149
///

0 commit comments

Comments
 (0)