From 90fce805a35abd58b779cf7cabef533d56f16788 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Thu, 5 May 2022 17:01:39 +0200 Subject: [PATCH] Add debug tracing to FilePathMapping::map_prefix --- compiler/rustc_span/src/source_map.rs | 55 +++++++++++++++++---------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_span/src/source_map.rs b/compiler/rustc_span/src/source_map.rs index 505c0af953af..03e82dde967a 100644 --- a/compiler/rustc_span/src/source_map.rs +++ b/compiler/rustc_span/src/source_map.rs @@ -1098,28 +1098,43 @@ impl FilePathMapping { /// The return value is the remapped path and a boolean indicating whether /// the path was affected by the mapping. pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) { - // NOTE: We are iterating over the mapping entries from last to first - // because entries specified later on the command line should - // take precedence. - for &(ref from, ref to) in self.mapping.iter().rev() { - if let Ok(rest) = path.strip_prefix(from) { - let remapped = if rest.as_os_str().is_empty() { - // This is subtle, joining an empty path onto e.g. `foo/bar` will - // result in `foo/bar/`, that is, there'll be an additional directory - // separator at the end. This can lead to duplicated directory separators - // in remapped paths down the line. - // So, if we have an exact match, we just return that without a call - // to `Path::join()`. - to.clone() - } else { - to.join(rest) - }; - - return (remapped, true); - } + if path.as_os_str().is_empty() { + return (path, false); } - (path, false) + return remap_path_prefix(&self.mapping, path); + + #[instrument(level = "debug", skip(mapping))] + fn remap_path_prefix(mapping: &[(PathBuf, PathBuf)], path: PathBuf) -> (PathBuf, bool) { + // NOTE: We are iterating over the mapping entries from last to first + // because entries specified later on the command line should + // take precedence. + for &(ref from, ref to) in mapping.iter().rev() { + debug!("Trying to apply {:?} => {:?}", from, to); + + if let Ok(rest) = path.strip_prefix(from) { + let remapped = if rest.as_os_str().is_empty() { + // This is subtle, joining an empty path onto e.g. `foo/bar` will + // result in `foo/bar/`, that is, there'll be an additional directory + // separator at the end. This can lead to duplicated directory separators + // in remapped paths down the line. + // So, if we have an exact match, we just return that without a call + // to `Path::join()`. + to.clone() + } else { + to.join(rest) + }; + debug!("Match - remapped {:?} => {:?}", path, remapped); + + return (remapped, true); + } else { + debug!("No match - prefix {:?} does not match {:?}", from, path); + } + } + + debug!("Path {:?} was not remapped", path); + (path, false) + } } fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {