]> git.lizzy.rs Git - rust.git/blobdiff - src/librustdoc/config.rs
Auto merge of #91064 - matthiaskrgr:rollup-2ijidpt, r=matthiaskrgr
[rust.git] / src / librustdoc / config.rs
index 2f8bae5ded0adfc03bdce445f4b7101f4e515a5f..493aa56fce6efe061a2b12ba673628625ba88731 100644 (file)
@@ -2,7 +2,6 @@
 use std::convert::TryFrom;
 use std::ffi::OsStr;
 use std::fmt;
-use std::fs;
 use std::path::PathBuf;
 use std::str::FromStr;
 
@@ -26,7 +25,7 @@
 use crate::html::static_files;
 use crate::opts;
 use crate::passes::{self, Condition, DefaultPassOption};
-use crate::scrape_examples::AllCallLocations;
+use crate::scrape_examples::{AllCallLocations, ScrapeExamplesOptions};
 use crate::theme;
 
 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -161,9 +160,9 @@ fn try_from(value: &str) -> Result<Self, Self::Error> {
     /// Whether to skip capturing stdout and stderr of tests.
     crate nocapture: bool,
 
-    /// Path to output file to write JSON of call sites. If this option is Some(..) then
+    /// Configuration for scraping examples from the current crate. If this option is Some(..) then
     /// the compiler will scrape examples and not generate documentation.
-    crate scrape_examples: Option<PathBuf>,
+    crate scrape_examples_options: Option<ScrapeExamplesOptions>,
 }
 
 impl fmt::Debug for Options {
@@ -208,6 +207,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
             .field("run_check", &self.run_check)
             .field("no_run", &self.no_run)
             .field("nocapture", &self.nocapture)
+            .field("scrape_examples_options", &self.scrape_examples_options)
             .finish()
     }
 }
@@ -286,7 +286,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
     crate emit: Vec<EmitType>,
     /// If `true`, HTML source pages will generate links for items to their definition.
     crate generate_link_to_definition: bool,
-    crate call_locations: Option<AllCallLocations>,
+    crate call_locations: AllCallLocations,
 }
 
 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -321,13 +321,13 @@ impl Options {
     /// been printed, returns `Err` with the exit code.
     crate fn from_matches(matches: &getopts::Matches) -> Result<Options, i32> {
         // Check for unstable options.
-        nightly_options::check_nightly_options(&matches, &opts());
+        nightly_options::check_nightly_options(matches, &opts());
 
         if matches.opt_present("h") || matches.opt_present("help") {
             crate::usage("rustdoc");
             return Err(0);
         } else if matches.opt_present("version") {
-            rustc_driver::version("rustdoc", &matches);
+            rustc_driver::version("rustdoc", matches);
             return Err(0);
         }
 
@@ -363,10 +363,10 @@ fn println_condition(condition: Condition) {
             return Err(0);
         }
 
-        let color = config::parse_color(&matches);
+        let color = config::parse_color(matches);
         let config::JsonConfig { json_rendered, json_unused_externs, .. } =
-            config::parse_json(&matches);
-        let error_format = config::parse_error_format(&matches, color, json_rendered);
+            config::parse_json(matches);
+        let error_format = config::parse_error_format(matches, color, json_rendered);
 
         let codegen_options = CodegenOptions::build(matches, error_format);
         let debugging_opts = DebuggingOptions::build(matches, error_format);
@@ -374,7 +374,7 @@ fn println_condition(condition: Condition) {
         let diag = new_handler(error_format, None, &debugging_opts);
 
         // check for deprecated options
-        check_deprecated_options(&matches, &diag);
+        check_deprecated_options(matches, &diag);
 
         let mut emit = Vec::new();
         for list in matches.opt_strs("emit") {
@@ -440,8 +440,8 @@ fn println_condition(condition: Condition) {
             .iter()
             .map(|s| SearchPath::from_cli_opt(s, error_format))
             .collect();
-        let externs = parse_externs(&matches, &debugging_opts, error_format);
-        let extern_html_root_urls = match parse_extern_html_roots(&matches) {
+        let externs = parse_externs(matches, &debugging_opts, error_format);
+        let extern_html_root_urls = match parse_extern_html_roots(matches) {
             Ok(ex) => ex,
             Err(err) => {
                 diag.struct_err(err).emit();
@@ -560,7 +560,7 @@ fn println_condition(condition: Condition) {
             }
         }
 
-        let edition = config::parse_crate_edition(&matches);
+        let edition = config::parse_crate_edition(matches);
 
         let mut id_map = html::markdown::IdMap::new();
         let external_html = match ExternalHtml::load(
@@ -569,7 +569,7 @@ fn println_condition(condition: Condition) {
             &matches.opt_strs("html-after-content"),
             &matches.opt_strs("markdown-before-content"),
             &matches.opt_strs("markdown-after-content"),
-            nightly_options::match_is_nightly_build(&matches),
+            nightly_options::match_is_nightly_build(matches),
             &diag,
             &mut id_map,
             edition,
@@ -678,31 +678,9 @@ fn println_condition(condition: Condition) {
             return Err(1);
         }
 
-        let scrape_examples = matches.opt_str("scrape-examples").map(PathBuf::from);
+        let scrape_examples_options = ScrapeExamplesOptions::new(&matches, &diag)?;
         let with_examples = matches.opt_strs("with-examples");
-        let each_call_locations = with_examples
-            .into_iter()
-            .map(|path| {
-                let bytes = fs::read(&path).map_err(|e| format!("{} (for path {})", e, path))?;
-                let calls: AllCallLocations =
-                    serde_json::from_slice(&bytes).map_err(|e| format!("{}", e))?;
-                Ok(calls)
-            })
-            .collect::<Result<Vec<_>, _>>()
-            .map_err(|e: String| {
-                diag.err(&format!("failed to load examples with error: {}", e));
-                1
-            })?;
-        let call_locations = (each_call_locations.len() > 0).then(move || {
-            each_call_locations.into_iter().fold(FxHashMap::default(), |mut acc, map| {
-                for (function, calls) in map.into_iter() {
-                    acc.entry(function)
-                        .or_insert_with(FxHashMap::default)
-                        .extend(calls.into_iter());
-                }
-                acc
-            })
-        });
+        let call_locations = crate::scrape_examples::load_call_locations(with_examples, &diag)?;
 
         let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
 
@@ -775,7 +753,7 @@ fn println_condition(condition: Condition) {
             crate_name,
             output_format,
             json_unused_externs,
-            scrape_examples,
+            scrape_examples_options,
         })
     }