use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{fmt, io};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::DiagCtxtHandle;
use rustc_session::config::{
self, get_cmd_lint_options, nightly_options, parse_crate_types_from_list, parse_externs,
parse_target_triple, CodegenOptions, CrateType, ErrorOutputType, Externs, Input,
JsonUnusedExterns, UnstableOptions,
};
use rustc_session::lint::Level;
use rustc_session::search_paths::SearchPath;
use rustc_session::{getopts, EarlyDiagCtxt};
use rustc_span::edition::Edition;
use rustc_span::FileName;
use rustc_target::spec::TargetTriple;
use crate::core::new_dcx;
use crate::externalfiles::ExternalHtml;
use crate::html::markdown::IdMap;
use crate::html::render::StylePath;
use crate::html::static_files;
use crate::passes::{self, Condition};
use crate::scrape_examples::{AllCallLocations, ScrapeExamplesOptions};
use crate::{html, opts, theme};
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub(crate) enum OutputFormat {
Json,
#[default]
Html,
}
impl OutputFormat {
pub(crate) fn is_json(&self) -> bool {
matches!(self, OutputFormat::Json)
}
}
impl TryFrom<&str> for OutputFormat {
type Error = String;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"json" => Ok(OutputFormat::Json),
"html" => Ok(OutputFormat::Html),
_ => Err(format!("unknown output format `{value}`")),
}
}
}
#[derive(Clone)]
pub(crate) struct Options {
pub(crate) input: Input,
pub(crate) crate_name: Option<String>,
pub(crate) bin_crate: bool,
pub(crate) proc_macro_crate: bool,
pub(crate) error_format: ErrorOutputType,
pub(crate) diagnostic_width: Option<usize>,
pub(crate) libs: Vec<SearchPath>,
pub(crate) lib_strs: Vec<String>,
pub(crate) externs: Externs,
pub(crate) extern_strs: Vec<String>,
pub(crate) cfgs: Vec<String>,
pub(crate) check_cfgs: Vec<String>,
pub(crate) codegen_options: CodegenOptions,
pub(crate) codegen_options_strs: Vec<String>,
pub(crate) unstable_opts: UnstableOptions,
pub(crate) unstable_opts_strs: Vec<String>,
pub(crate) target: TargetTriple,
pub(crate) edition: Edition,
pub(crate) maybe_sysroot: Option<PathBuf>,
pub(crate) lint_opts: Vec<(String, Level)>,
pub(crate) describe_lints: bool,
pub(crate) lint_cap: Option<Level>,
pub(crate) should_test: bool,
pub(crate) test_args: Vec<String>,
pub(crate) test_run_directory: Option<PathBuf>,
pub(crate) persist_doctests: Option<PathBuf>,
pub(crate) runtool: Option<String>,
pub(crate) runtool_args: Vec<String>,
pub(crate) enable_per_target_ignores: bool,
pub(crate) no_run: bool,
pub(crate) remap_path_prefix: Vec<(PathBuf, PathBuf)>,
pub(crate) test_builder: Option<PathBuf>,
pub(crate) test_builder_wrappers: Vec<PathBuf>,
pub(crate) show_coverage: bool,
pub(crate) crate_version: Option<String>,
pub(crate) output_format: OutputFormat,
pub(crate) run_check: bool,
pub(crate) json_unused_externs: JsonUnusedExterns,
pub(crate) nocapture: bool,
pub(crate) scrape_examples_options: Option<ScrapeExamplesOptions>,
pub(crate) unstable_features: rustc_feature::UnstableFeatures,
pub(crate) expanded_args: Vec<String>,
}
impl fmt::Debug for Options {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
struct FmtExterns<'a>(&'a Externs);
impl<'a> fmt::Debug for FmtExterns<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map().entries(self.0.iter()).finish()
}
}
f.debug_struct("Options")
.field("input", &self.input.source_name())
.field("crate_name", &self.crate_name)
.field("bin_crate", &self.bin_crate)
.field("proc_macro_crate", &self.proc_macro_crate)
.field("error_format", &self.error_format)
.field("libs", &self.libs)
.field("externs", &FmtExterns(&self.externs))
.field("cfgs", &self.cfgs)
.field("check-cfgs", &self.check_cfgs)
.field("codegen_options", &"...")
.field("unstable_options", &"...")
.field("target", &self.target)
.field("edition", &self.edition)
.field("maybe_sysroot", &self.maybe_sysroot)
.field("lint_opts", &self.lint_opts)
.field("describe_lints", &self.describe_lints)
.field("lint_cap", &self.lint_cap)
.field("should_test", &self.should_test)
.field("test_args", &self.test_args)
.field("test_run_directory", &self.test_run_directory)
.field("persist_doctests", &self.persist_doctests)
.field("show_coverage", &self.show_coverage)
.field("crate_version", &self.crate_version)
.field("runtool", &self.runtool)
.field("runtool_args", &self.runtool_args)
.field("enable-per-target-ignores", &self.enable_per_target_ignores)
.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)
.finish()
}
}
#[derive(Clone, Debug)]
pub(crate) struct RenderOptions {
pub(crate) output: PathBuf,
pub(crate) external_html: ExternalHtml,
pub(crate) id_map: IdMap,
pub(crate) playground_url: Option<String>,
pub(crate) module_sorting: ModuleSorting,
pub(crate) themes: Vec<StylePath>,
pub(crate) extension_css: Option<PathBuf>,
pub(crate) extern_html_root_urls: BTreeMap<String, String>,
pub(crate) extern_html_root_takes_precedence: bool,
pub(crate) default_settings: FxHashMap<String, String>,
pub(crate) resource_suffix: String,
pub(crate) enable_index_page: bool,
pub(crate) index_page: Option<PathBuf>,
pub(crate) static_root_path: Option<String>,
pub(crate) markdown_no_toc: bool,
pub(crate) markdown_css: Vec<String>,
pub(crate) markdown_playground_url: Option<String>,
pub(crate) document_private: bool,
pub(crate) document_hidden: bool,
pub(crate) generate_redirect_map: bool,
pub(crate) show_type_layout: bool,
pub(crate) unstable_features: rustc_feature::UnstableFeatures,
pub(crate) emit: Vec<EmitType>,
pub(crate) generate_link_to_definition: bool,
pub(crate) call_locations: AllCallLocations,
pub(crate) no_emit_shared: bool,
pub(crate) html_no_source: bool,
pub(crate) output_to_stdout: bool,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum ModuleSorting {
DeclarationOrder,
Alphabetical,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub(crate) enum EmitType {
Unversioned,
Toolchain,
InvocationSpecific,
}
impl FromStr for EmitType {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
use EmitType::*;
match s {
"unversioned-shared-resources" => Ok(Unversioned),
"toolchain-shared-resources" => Ok(Toolchain),
"invocation-specific" => Ok(InvocationSpecific),
_ => Err(()),
}
}
}
impl RenderOptions {
pub(crate) fn should_emit_crate(&self) -> bool {
self.emit.is_empty() || self.emit.contains(&EmitType::InvocationSpecific)
}
}
fn make_input(early_dcx: &EarlyDiagCtxt, input: &str) -> Input {
if input == "-" {
let mut src = String::new();
if io::stdin().read_to_string(&mut src).is_err() {
early_dcx.early_fatal("couldn't read from stdin, as it did not contain valid UTF-8");
}
Input::Str { name: FileName::anon_source_code(&src), input: src }
} else {
Input::File(PathBuf::from(input))
}
}
impl Options {
pub(crate) fn from_matches(
early_dcx: &mut EarlyDiagCtxt,
matches: &getopts::Matches,
args: Vec<String>,
) -> Option<(Options, RenderOptions)> {
nightly_options::check_nightly_options(early_dcx, matches, &opts());
if args.is_empty() || matches.opt_present("h") || matches.opt_present("help") {
crate::usage("rustdoc");
return None;
} else if matches.opt_present("version") {
rustc_driver::version!(&early_dcx, "rustdoc", matches);
return None;
}
if rustc_driver::describe_flag_categories(early_dcx, matches) {
return None;
}
let color = config::parse_color(early_dcx, matches);
let config::JsonConfig { json_rendered, json_unused_externs, json_color, .. } =
config::parse_json(early_dcx, matches);
let error_format =
config::parse_error_format(early_dcx, matches, color, json_color, json_rendered);
let diagnostic_width = matches.opt_get("diagnostic-width").unwrap_or_default();
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);
let dcx = dcx.handle();
check_deprecated_options(matches, dcx);
if matches.opt_strs("passes") == ["list"] {
println!("Available passes for running rustdoc:");
for pass in passes::PASSES {
println!("{:>20} - {}", pass.name, pass.description);
}
println!("\nDefault passes for rustdoc:");
for p in passes::DEFAULT_PASSES {
print!("{:>20}", p.pass.name);
println_condition(p.condition);
}
if nightly_options::match_is_nightly_build(matches) {
println!("\nPasses run with `--show-coverage`:");
for p in passes::COVERAGE_PASSES {
print!("{:>20}", p.pass.name);
println_condition(p.condition);
}
}
fn println_condition(condition: Condition) {
use Condition::*;
match condition {
Always => println!(),
WhenDocumentPrivate => println!(" (when --document-private-items)"),
WhenNotDocumentPrivate => println!(" (when not --document-private-items)"),
WhenNotDocumentHidden => println!(" (when not --document-hidden-items)"),
}
}
return None;
}
let mut emit = Vec::new();
for list in matches.opt_strs("emit") {
for kind in list.split(',') {
match kind.parse() {
Ok(kind) => emit.push(kind),
Err(()) => dcx.fatal(format!("unrecognized emission type: {kind}")),
}
}
}
if !matches!(matches.opt_str("output-format").as_deref(), None | Some("html"))
&& !matches.opt_present("show-coverage")
&& !nightly_options::is_unstable_enabled(matches)
{
dcx.fatal(
"the -Z unstable-options flag must be passed to enable --output-format for documentation generation (see https://github.com/rust-lang/rust/issues/76578)",
);
}
let to_check = matches.opt_strs("check-theme");
if !to_check.is_empty() {
let mut content =
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.bytes).unwrap();
if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
content = inside;
}
if let Some((inside, _)) = content.split_once("/* End theme: light */") {
content = inside;
}
let paths = match theme::load_css_paths(content) {
Ok(p) => p,
Err(e) => dcx.fatal(e),
};
let mut errors = 0;
println!("rustdoc: [check-theme] Starting tests! (Ignoring all other arguments)");
for theme_file in to_check.iter() {
print!(" - Checking \"{theme_file}\"...");
let (success, differences) = theme::test_theme_against(theme_file, &paths, dcx);
if !differences.is_empty() || !success {
println!(" FAILED");
errors += 1;
if !differences.is_empty() {
println!("{}", differences.join("\n"));
}
} else {
println!(" OK");
}
}
if errors != 0 {
dcx.fatal("[check-theme] one or more tests failed");
}
return None;
}
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(early_dcx, matches);
let input = if describe_lints {
"" } else {
match matches.free.as_slice() {
[] => dcx.fatal("missing file operand"),
[input] => input,
_ => dcx.fatal("too many file operands"),
}
};
let input = make_input(early_dcx, input);
let externs = parse_externs(early_dcx, matches, &unstable_opts);
let extern_html_root_urls = match parse_extern_html_roots(matches) {
Ok(ex) => ex,
Err(err) => dcx.fatal(err),
};
let default_settings: Vec<Vec<(String, String)>> = vec![
matches
.opt_str("default-theme")
.iter()
.flat_map(|theme| {
vec![
("use-system-theme".to_string(), "false".to_string()),
("theme".to_string(), theme.to_string()),
]
})
.collect(),
matches
.opt_strs("default-setting")
.iter()
.map(|s| match s.split_once('=') {
None => (s.clone(), "true".to_string()),
Some((k, v)) => (k.to_string(), v.to_string()),
})
.collect(),
];
let default_settings = default_settings
.into_iter()
.flatten()
.map(
|(k, v)| (k.replace('-', "_"), v),
)
.collect();
let test_args = matches.opt_strs("test-args");
let test_args: Vec<String> =
test_args.iter().flat_map(|s| s.split_whitespace()).map(|s| s.to_string()).collect();
let should_test = matches.opt_present("test");
let no_run = matches.opt_present("no-run");
if !should_test && no_run {
dcx.fatal("the `--test` flag must be passed to enable `--no-run`");
}
let mut output_to_stdout = false;
let test_builder_wrappers =
matches.opt_strs("test-builder-wrapper").iter().map(PathBuf::from).collect();
let output = match (matches.opt_str("out-dir"), matches.opt_str("output")) {
(Some(_), Some(_)) => {
dcx.fatal("cannot use both 'out-dir' and 'output' at once");
}
(Some(out_dir), None) | (None, Some(out_dir)) => {
output_to_stdout = out_dir == "-";
PathBuf::from(out_dir)
}
(None, None) => PathBuf::from("doc"),
};
let cfgs = matches.opt_strs("cfg");
let check_cfgs = matches.opt_strs("check-cfg");
let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
if let Some(ref p) = extension_css {
if !p.is_file() {
dcx.fatal("option --extend-css argument must be a file");
}
}
let mut themes = Vec::new();
if matches.opt_present("theme") {
let mut content =
std::str::from_utf8(static_files::STATIC_FILES.rustdoc_css.bytes).unwrap();
if let Some((_, inside)) = content.split_once("/* Begin theme: light */") {
content = inside;
}
if let Some((inside, _)) = content.split_once("/* End theme: light */") {
content = inside;
}
let paths = match theme::load_css_paths(content) {
Ok(p) => p,
Err(e) => dcx.fatal(e),
};
for (theme_file, theme_s) in
matches.opt_strs("theme").iter().map(|s| (PathBuf::from(&s), s.to_owned()))
{
if !theme_file.is_file() {
dcx.struct_fatal(format!("invalid argument: \"{theme_s}\""))
.with_help("arguments to --theme must be files")
.emit();
}
if theme_file.extension() != Some(OsStr::new("css")) {
dcx.struct_fatal(format!("invalid argument: \"{theme_s}\""))
.with_help("arguments to --theme must have a .css extension")
.emit();
}
let (success, ret) = theme::test_theme_against(&theme_file, &paths, dcx);
if !success {
dcx.fatal(format!("error loading theme file: \"{theme_s}\""));
} else if !ret.is_empty() {
dcx.struct_warn(format!(
"theme file \"{theme_s}\" is missing CSS rules from the default theme",
))
.with_warn("the theme may appear incorrect when loaded")
.with_help(format!(
"to see what rules are missing, call `rustdoc --check-theme \"{theme_s}\"`",
))
.emit();
}
themes.push(StylePath { path: theme_file });
}
}
let edition = config::parse_crate_edition(early_dcx, matches);
let mut id_map = html::markdown::IdMap::new();
let Some(external_html) = ExternalHtml::load(
&matches.opt_strs("html-in-header"),
&matches.opt_strs("html-before-content"),
&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),
dcx,
&mut id_map,
edition,
&None,
) else {
dcx.fatal("`ExternalHtml::load` failed");
};
match matches.opt_str("r").as_deref() {
Some("rust") | None => {}
Some(s) => dcx.fatal(format!("unknown input format: {s}")),
}
let index_page = matches.opt_str("index-page").map(|s| PathBuf::from(&s));
if let Some(ref index_page) = index_page {
if !index_page.is_file() {
dcx.fatal("option `--index-page` argument must be a file");
}
}
let target = parse_target_triple(early_dcx, matches);
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
let sysroot = match &maybe_sysroot {
Some(s) => s.clone(),
None => {
rustc_session::filesearch::get_or_default_sysroot().expect("Failed finding sysroot")
}
};
let libs = matches
.opt_strs("L")
.iter()
.map(|s| {
SearchPath::from_cli_opt(
&sysroot,
&target,
early_dcx,
s,
#[allow(rustc::bad_opt_access)] unstable_opts.unstable_options,
)
})
.collect();
let show_coverage = matches.opt_present("show-coverage");
let crate_types = match parse_crate_types_from_list(matches.opt_strs("crate-type")) {
Ok(types) => types,
Err(e) => {
dcx.fatal(format!("unknown crate type: {e}"));
}
};
let output_format = match matches.opt_str("output-format") {
Some(s) => match OutputFormat::try_from(s.as_str()) {
Ok(out_fmt) => {
if !out_fmt.is_json() && show_coverage {
dcx.fatal(
"html output format isn't supported for the --show-coverage option",
);
}
out_fmt
}
Err(e) => dcx.fatal(e),
},
None => OutputFormat::default(),
};
let crate_name = matches.opt_str("crate-name");
let bin_crate = crate_types.contains(&CrateType::Executable);
let proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
let playground_url = matches.opt_str("playground-url");
let module_sorting = if matches.opt_present("sort-modules-by-appearance") {
ModuleSorting::DeclarationOrder
} else {
ModuleSorting::Alphabetical
};
let resource_suffix = matches.opt_str("resource-suffix").unwrap_or_default();
let markdown_no_toc = matches.opt_present("markdown-no-toc");
let markdown_css = matches.opt_strs("markdown-css");
let markdown_playground_url = matches.opt_str("markdown-playground-url");
let crate_version = matches.opt_str("crate-version");
let enable_index_page = matches.opt_present("enable-index-page") || index_page.is_some();
let static_root_path = matches.opt_str("static-root-path");
let test_run_directory = matches.opt_str("test-run-directory").map(PathBuf::from);
let persist_doctests = matches.opt_str("persist-doctests").map(PathBuf::from);
let test_builder = matches.opt_str("test-builder").map(PathBuf::from);
let codegen_options_strs = matches.opt_strs("C");
let unstable_opts_strs = matches.opt_strs("Z");
let lib_strs = matches.opt_strs("L");
let extern_strs = matches.opt_strs("extern");
let runtool = matches.opt_str("runtool");
let runtool_args = matches.opt_strs("runtool-arg");
let enable_per_target_ignores = matches.opt_present("enable-per-target-ignores");
let document_private = matches.opt_present("document-private-items");
let document_hidden = matches.opt_present("document-hidden-items");
let run_check = matches.opt_present("check");
let generate_redirect_map = matches.opt_present("generate-redirect-map");
let show_type_layout = matches.opt_present("show-type-layout");
let nocapture = matches.opt_present("nocapture");
let generate_link_to_definition = matches.opt_present("generate-link-to-definition");
let extern_html_root_takes_precedence =
matches.opt_present("extern-html-root-takes-precedence");
let html_no_source = matches.opt_present("html-no-source");
if generate_link_to_definition && (show_coverage || output_format != OutputFormat::Html) {
dcx.struct_warn(
"`--generate-link-to-definition` option can only be used with HTML output format",
)
.with_note("`--generate-link-to-definition` option will be ignored")
.emit();
}
let scrape_examples_options = ScrapeExamplesOptions::new(matches, dcx);
let with_examples = matches.opt_strs("with-examples");
let call_locations = crate::scrape_examples::load_call_locations(with_examples, dcx);
let unstable_features =
rustc_feature::UnstableFeatures::from_environment(crate_name.as_deref());
let options = Options {
input,
bin_crate,
proc_macro_crate,
error_format,
diagnostic_width,
libs,
lib_strs,
externs,
extern_strs,
cfgs,
check_cfgs,
codegen_options,
codegen_options_strs,
unstable_opts,
unstable_opts_strs,
target,
edition,
maybe_sysroot,
lint_opts,
describe_lints,
lint_cap,
should_test,
test_args,
show_coverage,
crate_version,
test_run_directory,
persist_doctests,
runtool,
runtool_args,
enable_per_target_ignores,
test_builder,
run_check,
no_run,
test_builder_wrappers,
remap_path_prefix,
nocapture,
crate_name,
output_format,
json_unused_externs,
scrape_examples_options,
unstable_features,
expanded_args: args,
};
let render_options = RenderOptions {
output,
external_html,
id_map,
playground_url,
module_sorting,
themes,
extension_css,
extern_html_root_urls,
extern_html_root_takes_precedence,
default_settings,
resource_suffix,
enable_index_page,
index_page,
static_root_path,
markdown_no_toc,
markdown_css,
markdown_playground_url,
document_private,
document_hidden,
generate_redirect_map,
show_type_layout,
unstable_features,
emit,
generate_link_to_definition,
call_locations,
no_emit_shared: false,
html_no_source,
output_to_stdout,
};
Some((options, render_options))
}
pub(crate) fn markdown_input(&self) -> Option<&Path> {
self.input
.opt_path()
.filter(|p| matches!(p.extension(), Some(e) if e == "md" || e == "markdown"))
}
}
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()
}
fn check_deprecated_options(matches: &getopts::Matches, dcx: DiagCtxtHandle<'_>) {
let deprecated_flags = [];
for &flag in deprecated_flags.iter() {
if matches.opt_present(flag) {
dcx.struct_warn(format!("the `{flag}` flag is deprecated"))
.with_note(
"see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
for more information",
)
.emit();
}
}
let removed_flags = ["plugins", "plugin-path", "no-defaults", "passes", "input-format"];
for &flag in removed_flags.iter() {
if matches.opt_present(flag) {
let mut err = dcx.struct_warn(format!("the `{flag}` flag no longer functions"));
err.note(
"see issue #44136 <https://github.com/rust-lang/rust/issues/44136> \
for more information",
);
if flag == "no-defaults" || flag == "passes" {
err.help("you may want to use --document-private-items");
} else if flag == "plugins" || flag == "plugin-path" {
err.warn("see CVE-2018-1000622");
}
err.emit();
}
}
}
fn parse_extern_html_roots(
matches: &getopts::Matches,
) -> Result<BTreeMap<String, String>, &'static str> {
let mut externs = BTreeMap::new();
for arg in &matches.opt_strs("extern-html-root-url") {
let (name, url) =
arg.split_once('=').ok_or("--extern-html-root-url must be of the form name=url")?;
externs.insert(name.to_string(), url.to_string());
}
Ok(externs)
}