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)]
12 #![feature(peekable_next_if)]
14 #![feature(crate_visibility_modifier)]
15 #![feature(never_type)]
16 #![feature(once_cell)]
17 #![feature(type_ascription)]
18 #![feature(split_inclusive)]
19 #![feature(str_split_once)]
20 #![feature(iter_intersperse)]
21 #![recursion_limit = "256"]
24 extern crate lazy_static;
28 // N.B. these need `extern crate` even in 2018 edition
29 // because they're loaded implicitly from the sysroot.
30 // The reason they're loaded from the sysroot is because
31 // the rustdoc artifacts aren't stored in rustc's cargo target directory.
32 // So if `rustc` was specified in Cargo.toml, this would spuriously rebuild crates.
34 // Dependencies listed in Cargo.toml do not need `extern crate`.
35 extern crate rustc_ast;
36 extern crate rustc_ast_pretty;
37 extern crate rustc_attr;
38 extern crate rustc_data_structures;
39 extern crate rustc_driver;
40 extern crate rustc_errors;
41 extern crate rustc_expand;
42 extern crate rustc_feature;
43 extern crate rustc_hir;
44 extern crate rustc_hir_pretty;
45 extern crate rustc_index;
46 extern crate rustc_infer;
47 extern crate rustc_interface;
48 extern crate rustc_lexer;
49 extern crate rustc_lint;
50 extern crate rustc_metadata;
51 extern crate rustc_middle;
52 extern crate rustc_mir;
53 extern crate rustc_parse;
54 extern crate rustc_resolve;
55 extern crate rustc_session;
56 extern crate rustc_span as rustc_span;
57 extern crate rustc_target;
58 extern crate rustc_trait_selection;
59 extern crate rustc_typeck;
60 extern crate test as testing;
62 use std::default::Default;
66 use rustc_driver::abort_on_err;
67 use rustc_errors::ErrorReported;
68 use rustc_interface::interface;
70 use rustc_session::config::{make_crate_type_option, ErrorOutputType, RustcOptGroup};
71 use rustc_session::getopts;
72 use rustc_session::{early_error, early_warn};
96 rustc_driver::set_sigpipe_handler();
97 rustc_driver::install_ice_hook();
98 rustc_driver::init_env_logger("RUSTDOC_LOG");
99 let exit_code = rustc_driver::catch_with_exit_code(|| match get_args() {
100 Some(args) => main_args(&args),
101 _ => Err(ErrorReported),
103 process::exit(exit_code);
106 fn get_args() -> Option<Vec<String>> {
113 ErrorOutputType::default(),
114 &format!("Argument {} is not valid Unicode: {:?}", i, arg),
122 fn opts() -> Vec<RustcOptGroup> {
123 let stable: fn(_, fn(&mut getopts::Options) -> &mut _) -> _ = RustcOptGroup::stable;
124 let unstable: fn(_, fn(&mut getopts::Options) -> &mut _) -> _ = RustcOptGroup::unstable;
126 stable("h", |o| o.optflag("h", "help", "show this help message")),
127 stable("V", |o| o.optflag("V", "version", "print rustdoc's version")),
128 stable("v", |o| o.optflag("v", "verbose", "use verbose output")),
130 o.optopt("r", "input-format", "the input type of the specified file", "[rust]")
132 stable("w", |o| o.optopt("w", "output-format", "the output type to write", "[html]")),
133 stable("o", |o| o.optopt("o", "output", "where to place the output", "PATH")),
134 stable("crate-name", |o| {
135 o.optopt("", "crate-name", "specify the name of this crate", "NAME")
137 make_crate_type_option(),
139 o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
141 stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
142 stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
143 unstable("extern-html-root-url", |o| {
144 o.optmulti("", "extern-html-root-url", "base URL to use for dependencies", "NAME=URL")
146 stable("plugin-path", |o| o.optmulti("", "plugin-path", "removed", "DIR")),
148 o.optmulti("C", "codegen", "pass a codegen option to rustc", "OPT[=VALUE]")
150 stable("passes", |o| {
154 "list of passes to also run, you might want to pass it multiple times; a value of \
155 `list` will print available passes",
159 stable("plugins", |o| o.optmulti("", "plugins", "removed", "PLUGINS")),
160 stable("no-default", |o| o.optflag("", "no-defaults", "don't run the default passes")),
161 stable("document-private-items", |o| {
162 o.optflag("", "document-private-items", "document private items")
164 unstable("document-hidden-items", |o| {
165 o.optflag("", "document-hidden-items", "document items that have doc(hidden)")
167 stable("test", |o| o.optflag("", "test", "run code examples as tests")),
168 stable("test-args", |o| {
169 o.optmulti("", "test-args", "arguments to pass to the test runner", "ARGS")
171 stable("target", |o| o.optopt("", "target", "target triple to document", "TRIPLE")),
172 stable("markdown-css", |o| {
176 "CSS files to include via <link> in a rendered Markdown file",
180 stable("html-in-header", |o| {
184 "files to include inline in the <head> section of a rendered Markdown file \
185 or generated documentation",
189 stable("html-before-content", |o| {
192 "html-before-content",
193 "files to include inline between <body> and the content of a rendered \
194 Markdown file or generated documentation",
198 stable("html-after-content", |o| {
201 "html-after-content",
202 "files to include inline between the content and </body> of a rendered \
203 Markdown file or generated documentation",
207 unstable("markdown-before-content", |o| {
210 "markdown-before-content",
211 "files to include inline between <body> and the content of a rendered \
212 Markdown file or generated documentation",
216 unstable("markdown-after-content", |o| {
219 "markdown-after-content",
220 "files to include inline between the content and </body> of a rendered \
221 Markdown file or generated documentation",
225 stable("markdown-playground-url", |o| {
226 o.optopt("", "markdown-playground-url", "URL to send code snippets to", "URL")
228 stable("markdown-no-toc", |o| {
229 o.optflag("", "markdown-no-toc", "don't include table of contents")
235 "To add some CSS rules with a given file to generate doc with your \
236 own theme. However, your theme might break if the rustdoc's generated HTML \
237 changes, so be careful!",
242 o.optmulti("Z", "", "internal and debugging options (only on nightly build)", "FLAG")
244 stable("sysroot", |o| o.optopt("", "sysroot", "Override the system root", "PATH")),
245 unstable("playground-url", |o| {
249 "URL to send code snippets to, may be reset by --markdown-playground-url \
250 or `#![doc(html_playground_url=...)]`",
254 unstable("display-warnings", |o| {
255 o.optflag("", "display-warnings", "to print code warnings when testing doc")
257 stable("crate-version", |o| {
258 o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
260 unstable("sort-modules-by-appearance", |o| {
263 "sort-modules-by-appearance",
264 "sort modules by where they appear in the program, rather than alphabetically",
267 stable("default-theme", |o| {
271 "Set the default theme. THEME should be the theme name, generally lowercase. \
272 If an unknown default theme is specified, the builtin default is used. \
273 The set of themes, and the rustdoc built-in default, are not stable.",
277 unstable("default-setting", |o| {
281 "Default value for a rustdoc setting (used when \"rustdoc-SETTING\" is absent \
282 from web browser Local Storage). If VALUE is not supplied, \"true\" is used. \
283 Supported SETTINGs and VALUEs are not documented and not stable.",
287 stable("theme", |o| {
291 "additional themes which will be added to the generated docs",
295 stable("check-theme", |o| {
296 o.optmulti("", "check-theme", "check if given theme is valid", "FILES")
298 unstable("resource-suffix", |o| {
302 "suffix to add to CSS and JavaScript files, e.g., \"light.css\" will become \
303 \"light-suffix.css\"",
307 stable("edition", |o| {
311 "edition to use when compiling rust code (default: 2015)",
315 stable("color", |o| {
319 "Configure coloring of output:
320 auto = colorize, if output goes to a tty (default);
321 always = always colorize output;
322 never = never colorize output",
326 stable("error-format", |o| {
330 "How errors and other messages are produced",
335 o.optopt("", "json", "Configure the structure of JSON diagnostics", "CONFIG")
337 unstable("disable-minification", |o| {
338 o.optflag("", "disable-minification", "Disable minification applied on JS files")
340 stable("warn", |o| o.optmulti("W", "warn", "Set lint warnings", "OPT")),
341 stable("allow", |o| o.optmulti("A", "allow", "Set lint allowed", "OPT")),
342 stable("deny", |o| o.optmulti("D", "deny", "Set lint denied", "OPT")),
343 stable("forbid", |o| o.optmulti("F", "forbid", "Set lint forbidden", "OPT")),
344 stable("cap-lints", |o| {
348 "Set the most restrictive lint level. \
349 More restrictive lints are capped at this \
350 level. By default, it is at `forbid` level.",
354 unstable("index-page", |o| {
355 o.optopt("", "index-page", "Markdown file to be used as index page", "PATH")
357 unstable("enable-index-page", |o| {
358 o.optflag("", "enable-index-page", "To enable generation of the index page")
360 unstable("static-root-path", |o| {
364 "Path string to force loading static files from in output pages. \
365 If not set, uses combinations of '../' to reach the documentation root.",
369 unstable("disable-per-crate-search", |o| {
372 "disable-per-crate-search",
373 "disables generating the crate selector on the search box",
376 unstable("persist-doctests", |o| {
380 "Directory to persist doctest executables into",
384 unstable("show-coverage", |o| {
388 "calculate percentage of public items with documentation",
391 unstable("enable-per-target-ignores", |o| {
394 "enable-per-target-ignores",
395 "parse ignore-foo for ignoring doctests on a per-target basis",
398 unstable("runtool", |o| {
403 "The tool to run tests with when building for a different target than host",
406 unstable("runtool-arg", |o| {
411 "One (of possibly many) arguments to pass to the runtool",
414 unstable("test-builder", |o| {
418 "specified the rustc-like binary to use as the test builder",
421 unstable("check", |o| o.optflag("", "check", "Run rustdoc checks")),
425 fn usage(argv0: &str) {
426 let mut options = getopts::Options::new();
427 for option in opts() {
428 (option.apply)(&mut options);
430 println!("{}", options.usage(&format!("{} [options] <input>", argv0)));
431 println!("More information available at https://doc.rust-lang.org/rustdoc/what-is-rustdoc.html")
434 /// A result type used by several functions under `main()`.
435 type MainResult = Result<(), ErrorReported>;
437 fn main_args(args: &[String]) -> MainResult {
438 let mut options = getopts::Options::new();
439 for option in opts() {
440 (option.apply)(&mut options);
442 let matches = match options.parse(&args[1..]) {
445 early_error(ErrorOutputType::default(), &err.to_string());
449 // Note that we discard any distinction between different non-zero exit
450 // codes from `from_matches` here.
451 let options = match config::Options::from_matches(&matches) {
453 Err(code) => return if code == 0 { Ok(()) } else { Err(ErrorReported) },
455 rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals(
457 1, // this runs single-threaded, even in a parallel compiler
459 move || main_options(options),
463 fn wrap_return(diag: &rustc_errors::Handler, res: Result<(), String>) -> MainResult {
467 diag.struct_err(&err).emit();
473 fn run_renderer<'tcx, T: formats::FormatRenderer<'tcx>>(
475 renderopts: config::RenderOptions,
476 render_info: config::RenderInfo,
477 diag: &rustc_errors::Handler,
478 edition: rustc_span::edition::Edition,
479 tcx: ty::TyCtxt<'tcx>,
481 match formats::run_format::<T>(krate, renderopts, render_info, &diag, edition, tcx) {
484 let mut msg = diag.struct_err(&format!("couldn't generate documentation: {}", e.error));
485 let file = e.file.display().to_string();
489 msg.note(&format!("failed to create or modify \"{}\"", file)).emit()
496 fn main_options(options: config::Options) -> MainResult {
497 let diag = core::new_handler(options.error_format, None, &options.debugging_opts);
499 match (options.should_test, options.markdown_input()) {
500 (true, true) => return wrap_return(&diag, markdown::test(options)),
501 (true, false) => return doctest::run(options),
505 markdown::render(&options.input, options.render_options, options.edition),
511 // need to move these items separately because we lose them by the time the closure is called,
512 // but we can't create the Handler ahead of time because it's not Send
513 let diag_opts = (options.error_format, options.edition, options.debugging_opts.clone());
514 let show_coverage = options.show_coverage;
515 let run_check = options.run_check;
517 // First, parse the crate and extract all relevant information.
518 info!("starting to run rustc");
520 // Interpret the input file as a rust source file, passing it through the
521 // compiler all the way through the analysis passes. The rustdoc output is
522 // then generated from the cleaned AST of the crate. This runs all the
523 // plug/cleaning passes.
524 let crate_version = options.crate_version.clone();
526 let default_passes = options.default_passes;
527 let output_format = options.output_format;
528 // FIXME: fix this clone (especially render_options)
529 let externs = options.externs.clone();
530 let manual_passes = options.manual_passes.clone();
531 let render_options = options.render_options.clone();
532 let config = core::create_config(options);
534 interface::create_compiler_and_run(config, |compiler| {
535 compiler.enter(|queries| {
536 let sess = compiler.session();
538 // We need to hold on to the complete resolver, so we cause everything to be
539 // cloned for the analysis passes to use. Suboptimal, but necessary in the
540 // current architecture.
541 let resolver = core::create_resolver(externs, queries, &sess);
543 if sess.has_errors() {
544 sess.fatal("Compilation failed, aborting rustdoc");
547 let mut global_ctxt = abort_on_err(queries.global_ctxt(), sess).take();
549 global_ctxt.enter(|tcx| {
550 let (mut krate, render_info, render_opts) = sess.time("run_global_ctxt", || {
551 core::run_global_ctxt(
560 info!("finished with rustc");
562 krate.version = crate_version;
565 // if we ran coverage, bail early, we don't need to also generate docs at this point
566 // (also we didn't load in any of the useful passes)
568 } else if run_check {
569 // Since we're in "check" mode, no need to generate anything beyond this point.
573 info!("going to format");
574 let (error_format, edition, debugging_options) = diag_opts;
575 let diag = core::new_handler(error_format, None, &debugging_options);
576 match output_format {
577 None | Some(config::OutputFormat::Html) => sess.time("render_html", || {
578 run_renderer::<html::render::Context<'_>>(
587 Some(config::OutputFormat::Json) => sess.time("render_json", || {
588 run_renderer::<json::JsonRenderer<'_>>(