2 html_root_url = "https://doc.rust-lang.org/nightly/",
3 html_playground_url = "https://play.rust-lang.org/"
5 #![feature(rustc_private)]
6 #![feature(array_methods)]
7 #![feature(box_patterns)]
8 #![feature(box_syntax)]
9 #![feature(in_band_lifetimes)]
11 #![feature(or_patterns)]
13 #![feature(crate_visibility_modifier)]
14 #![feature(never_type)]
15 #![feature(once_cell)]
16 #![feature(type_ascription)]
17 #![feature(iter_intersperse)]
18 #![recursion_limit = "256"]
19 #![deny(rustc::internal)]
22 extern crate lazy_static;
26 // N.B. these need `extern crate` even in 2018 edition
27 // because they're loaded implicitly from the sysroot.
28 // The reason they're loaded from the sysroot is because
29 // the rustdoc artifacts aren't stored in rustc's cargo target directory.
30 // So if `rustc` was specified in Cargo.toml, this would spuriously rebuild crates.
32 // Dependencies listed in Cargo.toml do not need `extern crate`.
33 extern crate rustc_ast;
34 extern crate rustc_ast_pretty;
35 extern crate rustc_attr;
36 extern crate rustc_data_structures;
37 extern crate rustc_driver;
38 extern crate rustc_errors;
39 extern crate rustc_expand;
40 extern crate rustc_feature;
41 extern crate rustc_hir;
42 extern crate rustc_hir_pretty;
43 extern crate rustc_index;
44 extern crate rustc_infer;
45 extern crate rustc_interface;
46 extern crate rustc_lexer;
47 extern crate rustc_lint;
48 extern crate rustc_metadata;
49 extern crate rustc_middle;
50 extern crate rustc_mir;
51 extern crate rustc_parse;
52 extern crate rustc_resolve;
53 extern crate rustc_session;
54 extern crate rustc_span as rustc_span;
55 extern crate rustc_target;
56 extern crate rustc_trait_selection;
57 extern crate rustc_typeck;
58 extern crate test as testing;
60 use std::default::Default;
64 use rustc_driver::abort_on_err;
65 use rustc_errors::ErrorReported;
66 use rustc_interface::interface;
67 use rustc_middle::ty::TyCtxt;
68 use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
69 use rustc_session::getopts;
70 use rustc_session::{early_error, early_warn};
85 // used by the error-index generator, so it needs to be public
95 rustc_driver::set_sigpipe_handler();
96 rustc_driver::install_ice_hook();
97 rustc_driver::init_env_logger("RUSTDOC_LOG");
98 let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
99 Some(args) => main_args(&args),
100 _ => Err(ErrorReported),
102 process::exit(exit_code);
105 fn get_args() -> Option<Vec<String>> {
112 ErrorOutputType::default(),
113 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
121 fn opts() -> Vec<RustcOptGroup> {
122 let stable: fn(_, fn(&mut getopts::Options) -> &mut _) -> _ = RustcOptGroup::stable;
123 let unstable: fn(_, fn(&mut getopts::Options) -> &mut _) -> _ = RustcOptGroup::unstable;
125 stable("h", |o| o.optflag("h", "help", "show this help message")),
126 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
127 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
129 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
131 stable("w", |o| o.optopt("w", "output-format", "the output type to write", "[html]")),
132 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
133 stable("crate-name", |o| {
134 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
136 make_crate_type_option(),
138 o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
140 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
141 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
142 unstable("extern-html-root-url", |o| {
143 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
145 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
147 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
149 stable("passes", |o| {
153 "list of passes to also run, you might want to pass it multiple times; a value of \
154 `list` will print available passes",
158 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
159 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
160 stable("document-private-items", |o| {
161 o.optflag("", "document-private-items", "document private items")
163 unstable("document-hidden-items", |o| {
164 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
166 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
167 stable("test-args", |o| {
168 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
170 unstable("test-run-directory", |o| {
173 "test-run-directory",
174 "The working directory in which to run tests",
178 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
179 stable("markdown-css", |o| {
183 "CSS files to include via <link> in a rendered Markdown file",
187 stable("html-in-header", |o| {
191 "files to include inline in the <head> section of a rendered Markdown file \
192 or generated documentation",
196 stable("html-before-content", |o| {
199 "html-before-content",
200 "files to include inline between <body> and the content of a rendered \
201 Markdown file or generated documentation",
205 stable("html-after-content", |o| {
208 "html-after-content",
209 "files to include inline between the content and </body> of a rendered \
210 Markdown file or generated documentation",
214 unstable("markdown-before-content", |o| {
217 "markdown-before-content",
218 "files to include inline between <body> and the content of a rendered \
219 Markdown file or generated documentation",
223 unstable("markdown-after-content", |o| {
226 "markdown-after-content",
227 "files to include inline between the content and </body> of a rendered \
228 Markdown file or generated documentation",
232 stable("markdown-playground-url", |o| {
233 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
235 stable("markdown-no-toc", |o| {
236 o.optflag("", "markdown-no-toc", "don't include table of contents")
242 "To add some CSS rules with a given file to generate doc with your \
243 own theme. However, your theme might break if the rustdoc's generated HTML \
244 changes, so be careful!",
249 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
251 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
252 unstable("playground-url", |o| {
256 "URL to send code snippets to, may be reset by --markdown-playground-url \
257 or `#![doc(html_playground_url=...)]`",
261 unstable("display-warnings", |o| {
262 o.optflag("", "display-warnings", "to print code warnings when testing doc")
264 stable("crate-version", |o| {
265 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
267 unstable("sort-modules-by-appearance", |o| {
270 "sort-modules-by-appearance",
271 "sort modules by where they appear in the program, rather than alphabetically",
274 stable("default-theme", |o| {
278 "Set the default theme. THEME should be the theme name, generally lowercase. \
279 If an unknown default theme is specified, the builtin default is used. \
280 The set of themes, and the rustdoc built-in default, are not stable.",
284 unstable("default-setting", |o| {
288 "Default value for a rustdoc setting (used when \"rustdoc-SETTING\" is absent \
289 from web browser Local Storage). If VALUE is not supplied, \"true\" is used. \
290 Supported SETTINGs and VALUEs are not documented and not stable.",
294 stable("theme", |o| {
298 "additional themes which will be added to the generated docs",
302 stable("check-theme", |o| {
303 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
305 unstable("resource-suffix", |o| {
309 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
310 \"light-suffix.css\"",
314 stable("edition", |o| {
318 "edition to use when compiling rust code (default: 2015)",
322 stable("color", |o| {
326 "Configure coloring of output:
327 auto = colorize, if output goes to a tty (default);
328 always = always colorize output;
329 never = never colorize output",
333 stable("error-format", |o| {
337 "How errors and other messages are produced",
342 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
344 unstable("disable-minification", |o| {
345 o.optflag("", "disable-minification", "Disable minification applied on JS files")
347 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
348 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
349 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
350 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
351 stable("cap-lints", |o| {
355 "Set the most restrictive lint level. \
356 More restrictive lints are capped at this \
357 level. By default, it is at `forbid` level.",
361 unstable("index-page", |o| {
362 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
364 unstable("enable-index-page", |o| {
365 o.optflag("", "enable-index-page", "To enable generation of the index page")
367 unstable("static-root-path", |o| {
371 "Path string to force loading static files from in output pages. \
372 If not set, uses combinations of '../' to reach the documentation root.",
376 unstable("disable-per-crate-search", |o| {
379 "disable-per-crate-search",
380 "disables generating the crate selector on the search box",
383 unstable("persist-doctests", |o| {
387 "Directory to persist doctest executables into",
391 unstable("show-coverage", |o| {
395 "calculate percentage of public items with documentation",
398 unstable("enable-per-target-ignores", |o| {
401 "enable-per-target-ignores",
402 "parse ignore-foo for ignoring doctests on a per-target basis",
405 unstable("runtool", |o| {
410 "The tool to run tests with when building for a different target than host",
413 unstable("runtool-arg", |o| {
418 "One (of possibly many) arguments to pass to the runtool",
421 unstable("test-builder", |o| {
422 o.optopt("", "test-builder", "The rustc-like binary to use as the test builder", "PATH")
424 unstable("check", |o| o.optflag("", "check", "Run rustdoc checks")),
428 fn usage(argv0: &str) {
429 let mut options = getopts::Options::new();
430 for option in opts() {
431 (option.apply)(&mut options);
433 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
434 println!(" @path Read newline separated options from `path`\n");
435 println!("More information available at https://doc.rust-lang.org/rustdoc/what-is-rustdoc.html")
438 /// A result type used by several functions under `main()`.
439 type MainResult = Result<(), ErrorReported>;
441 fn main_args(at_args: &[String]) -> MainResult {
442 let args = rustc_driver::args::arg_expand_all(at_args);
444 let mut options = getopts::Options::new();
445 for option in opts() {
446 (option.apply)(&mut options);
448 let matches = match options.parse(&args[1..]) {
451 early_error(ErrorOutputType::default(), &err.to_string());
455 // Note that we discard any distinction between different non-zero exit
456 // codes from `from_matches` here.
457 let options = match config::Options::from_matches(&matches) {
459 Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
461 rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals(
463 1, // this runs single-threaded, even in a parallel compiler
465 move || main_options(options),
469 fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
473 diag.struct_err(&err).emit();
479 fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
481 renderopts: config::RenderOptions,
482 render_info: config::RenderInfo,
483 diag: &rustc_errors::Handler,
484 edition: rustc_span::edition::Edition,
487 match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition, tcx) {
490 let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
491 let file = e.file.display().to_string();
495 msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
502 fn main_options(options: config::Options) -> MainResult {
503 let diag = core::new_handler(options.error_format, None, &options.debugging_opts);
505 match (options.should_test, options.markdown_input()) {
506 (true, true) => return wrap_return(&diag, markdown::test(options)),
507 (true, false) => return doctest::run(options),
511 markdown::render(&options.input, options.render_options, options.edition),
517 // need to move these items separately because we lose them by the time the closure is called,
518 // but we can't create the Handler ahead of time because it's not Send
519 let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
520 let show_coverage = options.show_coverage;
521 let run_check = options.run_check;
523 // First, parse the crate and extract all relevant information.
524 info!("starting to run rustc");
526 // Interpret the input file as a rust source file, passing it through the
527 // compiler all the way through the analysis passes. The rustdoc output is
528 // then generated from the cleaned AST of the crate. This runs all the
529 // plug/cleaning passes.
530 let crate_version = options.crate_version.clone();
532 let default_passes = options.default_passes;
533 let output_format = options.output_format;
534 // FIXME: fix this clone (especially render_options)
535 let externs = options.externs.clone();
536 let manual_passes = options.manual_passes.clone();
537 let render_options = options.render_options.clone();
538 let config = core::create_config(options);
540 interface::create_compiler_and_run(config, |compiler| {
541 compiler.enter(|queries| {
542 let sess = compiler.session();
544 // We need to hold on to the complete resolver, so we cause everything to be
545 // cloned for the analysis passes to use. Suboptimal, but necessary in the
546 // current architecture.
547 let resolver = core::create_resolver(externs, queries, &sess);
549 if sess.has_errors() {
550 sess.fatal("Compilation failed, aborting rustdoc");
553 let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess).peek_mut();
555 global_ctxt.enter(|tcx| {
556 let (mut krate, render_info, render_opts) = sess.time("run_global_ctxt", || {
557 core::run_global_ctxt(
566 info!("finished with rustc");
568 krate.version = crate_version;
571 // if we ran coverage, bail early, we don't need to also generate docs at this point
572 // (also we didn't load in any of the useful passes)
574 } else if run_check {
575 // Since we're in "check" mode, no need to generate anything beyond this point.
579 info!("going to format");
580 let (error_format, edition, debugging_options) = diag_opts;
581 let diag = core::new_handler(error_format, None, &debugging_options);
582 match output_format {
583 config::OutputFormat::Html => sess.time("render_html", || {
584 run_renderer::<html::render::Context<'_>>(
593 config::OutputFormat::Json => sess.time("render_json", || {
594 run_renderer::<json::JsonRenderer<'_>>(