Revamp RealFileName public methods

This commit is contained in:
Andy Wang 2021-04-14 14:12:39 +01:00 committed by Andy Wang
parent f8e55da6de
commit fb4f6439f6
No known key found for this signature in database
GPG Key ID: 181B49F9F38F3374
7 changed files with 53 additions and 41 deletions

View File

@ -66,7 +66,7 @@ fn line_program_add_file(
) -> FileId {
match &file.name {
FileName::Real(path) => {
let (dir_path, file_name) = split_path_dir_and_file(path.stable_name());
let (dir_path, file_name) = split_path_dir_and_file(path.remapped_path_if_available());
let dir_name = osstr_as_utf8_bytes(dir_path.as_os_str());
let file_name = osstr_as_utf8_bytes(file_name);

View File

@ -64,7 +64,7 @@ pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa) -> Self {
// FIXME: how to get version when building out of tree?
// Normally this would use option_env!("CFG_VERSION").
let producer = format!("cg_clif (rustc {})", "unknown version");
let comp_dir = tcx.sess.working_dir.stable_name().to_string_lossy().into_owned();
let comp_dir = tcx.sess.working_dir.to_string_lossy(false).into_owned();
let (name, file_info) = match tcx.sess.local_crate_source_file.clone() {
Some(path) => {
let name = path.to_string_lossy().into_owned();

View File

@ -764,7 +764,7 @@ pub fn file_metadata(cx: &CodegenCx<'ll, '_>, source_file: &SourceFile) -> &'ll
let hash = Some(&source_file.src_hash);
let file_name = Some(source_file.name.to_string());
let directory = if source_file.is_real_file() && !source_file.is_imported() {
Some(cx.sess().working_dir.stable_name().to_string_lossy().to_string())
Some(cx.sess().working_dir.to_string_lossy(false).to_string())
} else {
// If the path comes from an upstream crate we assume it has been made
// independent of the compiler's working directory one way or another.
@ -992,7 +992,7 @@ pub fn compile_unit_metadata(
let producer = format!("clang LLVM ({})", rustc_producer);
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
let work_dir = tcx.sess.working_dir.stable_name().to_string_lossy();
let work_dir = tcx.sess.working_dir.to_string_lossy(false);
let flags = "\0";
let out_dir = &tcx.output_filenames(LOCAL_CRATE).out_directory;
let split_name = if tcx.sess.target_can_use_split_dwarf() {

View File

@ -490,22 +490,27 @@ fn encode_source_map(&mut self) -> Lazy<[rustc_span::SourceFile]> {
FileName::Real(ref realname) => {
let mut adapted = (**source_file).clone();
adapted.name = FileName::Real(match realname {
RealFileName::LocalPath(local_path) => {
// Prepend path of working directory onto local path.
// because relative paths are potentially relative to a
// wrong directory.
RealFileName::LocalPath(path_to_file) => {
// Prepend path of working directory onto potentially
// relative paths, because they could become relative
// to a wrong directory.
let working_dir = &self.tcx.sess.working_dir;
if let RealFileName::LocalPath(absolute) = working_dir {
// If working_dir has not been remapped, then we emit a
// LocalPath variant as it's likely to be a valid path
RealFileName::LocalPath(Path::new(absolute).join(local_path))
} else {
// If working_dir has been remapped, then we emit
// Remapped variant as the expanded path won't be valid
RealFileName::Remapped {
local_path: None,
virtual_name: Path::new(working_dir.stable_name())
.join(local_path),
match working_dir {
RealFileName::LocalPath(absolute) => {
// If working_dir has not been remapped, then we emit a
// LocalPath variant as it's likely to be a valid path
RealFileName::LocalPath(
Path::new(absolute).join(path_to_file),
)
}
RealFileName::Remapped { local_path: _, virtual_name } => {
// If working_dir has been remapped, then we emit
// Remapped variant as the expanded path won't be valid
RealFileName::Remapped {
local_path: None,
virtual_name: Path::new(virtual_name)
.join(path_to_file),
}
}
}
}

View File

@ -190,7 +190,7 @@ pub fn dump_compilation_options(&mut self, input: &Input, crate_name: &str) {
};
let data = CompilationOptions {
directory: self.tcx.sess.working_dir.stable_name().into(),
directory: self.tcx.sess.working_dir.remapped_path_if_available().into(),
program,
arguments,
output: self.save_ctxt.compilation_output(crate_name),

View File

@ -26,7 +26,12 @@ pub fn make_filename_string(&self, file: &SourceFile) -> String {
.display()
.to_string()
} else {
self.sess.working_dir.stable_name().join(&path).display().to_string()
self.sess
.working_dir
.remapped_path_if_available()
.join(&path)
.display()
.to_string()
}
}
// If the file name was remapped, we assume the user

View File

@ -172,7 +172,7 @@ fn encode(&self, encoder: &mut S) -> Result<(), S::Error> {
impl RealFileName {
/// Returns the path suitable for reading from the file system on the local host,
/// if this information exists.
/// Avoid embedding this in build artifacts; see `stable_name()` for that.
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
pub fn local_path(&self) -> Option<&Path> {
match self {
RealFileName::LocalPath(p) => Some(p),
@ -184,7 +184,7 @@ pub fn local_path(&self) -> Option<&Path> {
/// Returns the path suitable for reading from the file system on the local host,
/// if this information exists.
/// Avoid embedding this in build artifacts; see `stable_name()` for that.
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
pub fn into_local_path(self) -> Option<PathBuf> {
match self {
RealFileName::LocalPath(p) => Some(p),
@ -192,31 +192,33 @@ pub fn into_local_path(self) -> Option<PathBuf> {
}
}
/// Returns the path suitable for embedding into build artifacts. Note that
/// a remapped path will not correspond to a valid file system path; see
/// `local_path()` for something that is more likely to return paths into the
/// local host file system.
pub fn stable_name(&self) -> &Path {
/// Returns the path suitable for embedding into build artifacts. This would still
/// be a local path if it has not been remapped. A remapped path will not correspond
/// to a valid file system path: see `local_path_if_available()` for something that
/// is more likely to return paths into the local host file system.
pub fn remapped_path_if_available(&self) -> &Path {
match self {
RealFileName::LocalPath(p)
| RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
}
}
fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
use RealFileName::*;
/// Returns the path suitable for reading from the file system on the local host,
/// if this information exists. Otherwise returns the remapped name.
/// Avoid embedding this in build artifacts; see `remapped_path_if_available()` for that.
pub fn local_path_if_available(&self) -> &Path {
match self {
RealFileName::LocalPath(path)
| RealFileName::Remapped { local_path: None, virtual_name: path }
| RealFileName::Remapped { local_path: Some(path), virtual_name: _ } => path,
}
}
pub fn to_string_lossy(&self, prefer_local: bool) -> Cow<'_, str> {
if prefer_local {
match self {
LocalPath(path)
| Remapped { local_path: None, virtual_name: path }
| Remapped { local_path: Some(path), virtual_name: _ } => path.to_string_lossy(),
}
self.local_path_if_available().to_string_lossy()
} else {
match self {
LocalPath(path) | Remapped { local_path: _, virtual_name: path } => {
path.to_string_lossy()
}
}
self.remapped_path_if_available().to_string_lossy()
}
}
}
@ -1358,7 +1360,7 @@ fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
impl fmt::Debug for SourceFile {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "SourceFile({})", self.name)
write!(fmt, "SourceFile({:?})", self.name)
}
}