about summary refs log tree commit diff
path: root/src/librustdoc
diff options
context:
space:
mode:
authorEdward Shen <code@eddie.sh>2024-06-09 10:17:21 -0700
committerEdward Shen <code@eddie.sh>2024-06-09 10:34:54 -0700
commitd9f78cb7939b340f72389fbaedfa4d872d0bbe43 (patch)
treeecec62b566c18a2c9694b329dfe3fa62d39689a7 /src/librustdoc
parent212841e17cce13d1e29b00ad144300b687bfafdd (diff)
downloadrust-d9f78cb7939b340f72389fbaedfa4d872d0bbe43.tar.gz
rust-d9f78cb7939b340f72389fbaedfa4d872d0bbe43.zip
rustdoc: Add support for --remap-path-prefix
Adds --remap-path-prefix as an unstable option. This is implemented to
mimic the behavior of rustc's --remap-path-prefix but with minor
adjustments.

This flag similarly takes in two paths, a prefix to replace and a
replacement string.
Diffstat (limited to 'src/librustdoc')
-rw-r--r--src/librustdoc/config.rs26
-rw-r--r--src/librustdoc/doctest.rs4
-rw-r--r--src/librustdoc/doctest/rust.rs12
-rw-r--r--src/librustdoc/lib.rs8
4 files changed, 43 insertions, 7 deletions
diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs
index 012afada1e5..78d4129f642 100644
--- a/src/librustdoc/config.rs
+++ b/src/librustdoc/config.rs
@@ -128,6 +128,8 @@ pub(crate) struct Options {
     pub(crate) enable_per_target_ignores: bool,
     /// Do not run doctests, compile them if should_test is active.
     pub(crate) no_run: bool,
+    /// What sources are being mapped.
+    pub(crate) remap_path_prefix: Vec<(PathBuf, PathBuf)>,
 
     /// The path to a rustc-like binary to build tests with. If not set, we
     /// default to loading from `$sysroot/bin/rustc`.
@@ -211,6 +213,7 @@ impl fmt::Debug for Options {
             .field("run_check", &self.run_check)
             .field("no_run", &self.no_run)
             .field("test_builder_wrappers", &self.test_builder_wrappers)
+            .field("remap-file-prefix", &self.remap_path_prefix)
             .field("nocapture", &self.nocapture)
             .field("scrape_examples_options", &self.scrape_examples_options)
             .field("unstable_features", &self.unstable_features)
@@ -372,6 +375,13 @@ impl Options {
         let codegen_options = CodegenOptions::build(early_dcx, matches);
         let unstable_opts = UnstableOptions::build(early_dcx, matches);
 
+        let remap_path_prefix = match parse_remap_path_prefix(&matches) {
+            Ok(prefix_mappings) => prefix_mappings,
+            Err(err) => {
+                early_dcx.early_fatal(err);
+            }
+        };
+
         let dcx = new_dcx(error_format, None, diagnostic_width, &unstable_opts);
 
         // check for deprecated options
@@ -772,6 +782,7 @@ impl Options {
             run_check,
             no_run,
             test_builder_wrappers,
+            remap_path_prefix,
             nocapture,
             crate_name,
             output_format,
@@ -820,6 +831,21 @@ impl Options {
     }
 }
 
+fn parse_remap_path_prefix(
+    matches: &getopts::Matches,
+) -> Result<Vec<(PathBuf, PathBuf)>, &'static str> {
+    matches
+        .opt_strs("remap-path-prefix")
+        .into_iter()
+        .map(|remap| {
+            remap
+                .rsplit_once('=')
+                .ok_or("--remap-path-prefix must contain '=' between FROM and TO")
+                .map(|(from, to)| (PathBuf::from(from), PathBuf::from(to)))
+        })
+        .collect()
+}
+
 /// Prints deprecation warnings for deprecated options
 fn check_deprecated_options(matches: &getopts::Matches, dcx: &rustc_errors::DiagCtxt) {
     let deprecated_flags = [];
diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs
index a732e645b6b..81a7463deca 100644
--- a/src/librustdoc/doctest.rs
+++ b/src/librustdoc/doctest.rs
@@ -129,6 +129,7 @@ pub(crate) fn run(
         edition: options.edition,
         target_triple: options.target.clone(),
         crate_name: options.crate_name.clone(),
+        remap_path_prefix: options.remap_path_prefix.clone(),
         ..config::Options::default()
     };
 
@@ -572,7 +573,6 @@ fn make_maybe_absolute_path(path: PathBuf) -> PathBuf {
         std::env::current_dir().map(|c| c.join(&path)).unwrap_or_else(|_| path)
     }
 }
-
 struct IndividualTestOptions {
     outdir: DirState,
     test_id: String,
@@ -651,7 +651,7 @@ impl CreateRunnableDoctests {
         if !item_path.is_empty() {
             item_path.push(' ');
         }
-        format!("{} - {item_path}(line {line})", filename.prefer_local())
+        format!("{} - {item_path}(line {line})", filename.prefer_remapped_unconditionaly())
     }
 
     fn add_test(&mut self, test: ScrapedDoctest) {
diff --git a/src/librustdoc/doctest/rust.rs b/src/librustdoc/doctest/rust.rs
index e6bef395fa9..fc8e119ccc2 100644
--- a/src/librustdoc/doctest/rust.rs
+++ b/src/librustdoc/doctest/rust.rs
@@ -27,11 +27,13 @@ struct RustCollector {
 impl RustCollector {
     fn get_filename(&self) -> FileName {
         let filename = self.source_map.span_to_filename(self.position);
-        if let FileName::Real(ref filename) = filename
-            && let Ok(cur_dir) = env::current_dir()
-            && let Some(local_path) = filename.local_path()
-            && let Ok(path) = local_path.strip_prefix(&cur_dir)
-        {
+        if let FileName::Real(ref filename) = filename {
+            let path = filename.remapped_path_if_available();
+            // Strip the cwd prefix from the path. This will likely exist if
+            // the path was not remapped.
+            let path = env::current_dir()
+                .map(|cur_dir| path.strip_prefix(&cur_dir).unwrap_or(path))
+                .unwrap_or(path);
             return path.to_owned().into();
         }
         filename
diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs
index 3b6bddf263a..d6e715d48ea 100644
--- a/src/librustdoc/lib.rs
+++ b/src/librustdoc/lib.rs
@@ -555,6 +555,14 @@ fn opts() -> Vec<RustcOptGroup> {
         unstable("no-run", |o| {
             o.optflagmulti("", "no-run", "Compile doctests without running them")
         }),
+        unstable("remap-path-prefix", |o| {
+            o.optmulti(
+                "",
+                "remap-path-prefix",
+                "Remap source names in compiler messages",
+                "FROM=TO",
+            )
+        }),
         unstable("show-type-layout", |o| {
             o.optflagmulti("", "show-type-layout", "Include the memory layout of types in the docs")
         }),