Skip to content

Commit 157833c

Browse files
swap uses of Matches with pre-parsed args
1 parent f5f496e commit 157833c

File tree

5 files changed

+68
-111
lines changed

5 files changed

+68
-111
lines changed

src/librustdoc/config.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use opts;
3232
use passes::{self, DefaultPassOption};
3333
use theme;
3434

35+
#[derive(Clone)]
3536
pub struct Options {
3637
// Basic options / Options passed directly to rustc
3738

@@ -330,6 +331,14 @@ impl Options {
330331
}
331332
};
332333

334+
match matches.opt_str("r").as_ref().map(|s| &**s) {
335+
Some("rust") | None => {}
336+
Some(s) => {
337+
diag.struct_err(&format!("unknown input format: {}", s)).emit();
338+
return Err(1);
339+
}
340+
}
341+
333342
match matches.opt_str("w").as_ref().map(|s| &**s) {
334343
Some("html") | None => {}
335344
Some(s) => {
@@ -448,6 +457,19 @@ fn check_deprecated_options(matches: &getopts::Matches, diag: &errors::Handler)
448457
err.emit();
449458
}
450459
}
460+
461+
let removed_flags = [
462+
"plugins",
463+
"plugin-path",
464+
];
465+
466+
for &flag in removed_flags.iter() {
467+
if matches.opt_present(flag) {
468+
diag.struct_warn(&format!("the '{}' flag no longer functions", flag))
469+
.warn("see CVE-2018-1000622")
470+
.emit();
471+
}
472+
}
451473
}
452474

453475
/// Extracts `--extern-html-root-url` arguments from `matches` and returns a map of crate names to

src/librustdoc/html/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ pub fn markdown_links(md: &str) -> Vec<(String, Option<Range<usize>>)> {
905905
links
906906
}
907907

908-
#[derive(Default)]
908+
#[derive(Clone, Default)]
909909
pub struct IdMap {
910910
map: FxHashMap<String, usize>,
911911
}

src/librustdoc/html/render.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ use std::rc::Rc;
5555
use externalfiles::ExternalHtml;
5656

5757
use errors;
58-
use getopts;
5958

6059
use serialize::json::{ToJson, Json, as_json};
6160
use syntax::ast;
@@ -70,6 +69,7 @@ use rustc::util::nodemap::{FxHashMap, FxHashSet};
7069
use rustc_data_structures::flock;
7170

7271
use clean::{self, AttributesExt, GetDefId, SelfTy, Mutability};
72+
use config;
7373
use doctree;
7474
use fold::DocFolder;
7575
use html::escape::Escape;
@@ -509,7 +509,7 @@ pub fn run(mut krate: clean::Crate,
509509
id_map: IdMap,
510510
enable_index_page: bool,
511511
index_page: Option<PathBuf>,
512-
matches: &getopts::Matches,
512+
options: config::Options,
513513
diag: &errors::Handler,
514514
) -> Result<(), Error> {
515515
let src_root = match krate.src {
@@ -678,7 +678,7 @@ pub fn run(mut krate: clean::Crate,
678678
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
679679
CURRENT_LOCATION_KEY.with(|s| s.borrow_mut().clear());
680680

681-
write_shared(&cx, &krate, &*cache, index, enable_minification, matches, diag)?;
681+
write_shared(&cx, &krate, &*cache, index, enable_minification, &options, diag)?;
682682

683683
// And finally render the whole crate's documentation
684684
cx.krate(krate)
@@ -760,7 +760,7 @@ fn write_shared(
760760
cache: &Cache,
761761
search_index: String,
762762
enable_minification: bool,
763-
matches: &getopts::Matches,
763+
options: &config::Options,
764764
diag: &errors::Handler,
765765
) -> Result<(), Error> {
766766
// Write out the shared files. Note that these are shared among all rustdoc
@@ -994,9 +994,11 @@ themePicker.onblur = handleThemeButtonsBlur;
994994
if let Some(ref index_page) = cx.index_page {
995995
::markdown::render(index_page,
996996
cx.dst.clone(),
997-
&matches, &(*cx.shared).layout.external_html,
998-
!matches.opt_present("markdown-no-toc"),
999-
diag);
997+
&options.markdown_css.clone(),
998+
options.markdown_playground_url.clone()
999+
.or_else(|| options.playground_url.clone()),
1000+
&(*cx.shared).layout.external_html,
1001+
!options.markdown_no_toc, diag);
10001002
} else {
10011003
let dst = cx.dst.join("index.html");
10021004
let mut w = BufWriter::new(try_err!(File::create(&dst), &dst));

src/librustdoc/lib.rs

Lines changed: 31 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,11 @@ extern crate serialize as rustc_serialize; // used by deriving
5656
use std::default::Default;
5757
use std::env;
5858
use std::panic;
59-
use std::path::PathBuf;
6059
use std::process;
6160
use std::sync::mpsc::channel;
6261

63-
use syntax::edition::Edition;
6462
use rustc::session::{early_warn, early_error};
65-
use rustc::session::search_paths::SearchPaths;
66-
use rustc::session::config::{ErrorOutputType, RustcOptGroup, Externs, CodegenOptions};
67-
use rustc_target::spec::TargetTriple;
68-
use rustc::session::config::get_cmd_lint_options;
63+
use rustc::session::config::{ErrorOutputType, RustcOptGroup};
6964

7065
#[macro_use]
7166
mod externalfiles;
@@ -387,17 +382,23 @@ fn main_args(args: &[String]) -> isize {
387382
options.codegen_options)
388383
}
389384
(false, true) => return markdown::render(&options.input, options.output,
390-
&matches,
385+
&options.markdown_css,
386+
options.markdown_playground_url
387+
.or(options.playground_url),
391388
&options.external_html,
392389
!options.markdown_no_toc, &diag),
393390
(false, false) => {}
394391
}
395392

396-
let res = acquire_input(options.input.clone(), options.externs.clone(), options.edition,
397-
options.codegen_options.clone(), matches, options.error_format,
398-
move |out, matches| {
393+
//TODO: split render-time options into their own struct so i don't have to clone here
394+
rust_input(options.clone(), move |out| {
399395
let Output { krate, passes, renderinfo } = out;
400396
info!("going to format");
397+
let diag = core::new_handler(options.error_format,
398+
None,
399+
options.debugging_options.treat_err_as_bug,
400+
options.debugging_options.ui_testing);
401+
let html_opts = options.clone();
401402
html::render::run(krate, options.extern_html_root_urls, &options.external_html, options.playground_url,
402403
options.output,
403404
options.resource_suffix,
@@ -408,106 +409,48 @@ fn main_args(args: &[String]) -> isize {
408409
options.themes,
409410
options.enable_minification, options.id_map,
410411
options.enable_index_page, options.index_page,
411-
&matches,
412+
html_opts,
412413
&diag)
413414
.expect("failed to generate documentation");
414415
0
415-
});
416-
res.unwrap_or_else(|s| {
417-
diag.struct_err(&format!("input error: {}", s)).emit();
418-
1
419416
})
420417
}
421418

422-
/// Looks inside the command line arguments to extract the relevant input format
423-
/// and files and then generates the necessary rustdoc output for formatting.
424-
fn acquire_input<R, F>(input: PathBuf,
425-
externs: Externs,
426-
edition: Edition,
427-
cg: CodegenOptions,
428-
matches: getopts::Matches,
429-
error_format: ErrorOutputType,
430-
f: F)
431-
-> Result<R, String>
432-
where R: 'static + Send, F: 'static + Send + FnOnce(Output, &getopts::Matches) -> R {
433-
match matches.opt_str("r").as_ref().map(|s| &**s) {
434-
Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, error_format, f)),
435-
Some(s) => Err(format!("unknown input format: {}", s)),
436-
None => Ok(rust_input(input, externs, edition, cg, matches, error_format, f))
437-
}
438-
}
439-
440419
/// Interprets the input file as a rust source file, passing it through the
441420
/// compiler all the way through the analysis passes. The rustdoc output is then
442421
/// generated from the cleaned AST of the crate.
443422
///
444423
/// This form of input will run all of the plug/cleaning passes
445-
fn rust_input<R, F>(cratefile: PathBuf,
446-
externs: Externs,
447-
edition: Edition,
448-
cg: CodegenOptions,
449-
matches: getopts::Matches,
450-
error_format: ErrorOutputType,
424+
fn rust_input<R, F>(options: config::Options,
451425
f: F) -> R
452426
where R: 'static + Send,
453-
F: 'static + Send + FnOnce(Output, &getopts::Matches) -> R
427+
F: 'static + Send + FnOnce(Output) -> R
454428
{
455-
let default_passes = if matches.opt_present("no-defaults") {
456-
passes::DefaultPassOption::None
457-
} else if matches.opt_present("document-private-items") {
458-
passes::DefaultPassOption::Private
459-
} else {
460-
passes::DefaultPassOption::Default
461-
};
462-
463-
let manual_passes = matches.opt_strs("passes");
464-
let plugins = matches.opt_strs("plugins");
465-
466429
// First, parse the crate and extract all relevant information.
467-
let mut paths = SearchPaths::new();
468-
for s in &matches.opt_strs("L") {
469-
paths.add_path(s, ErrorOutputType::default());
470-
}
471-
let mut cfgs = matches.opt_strs("cfg");
472-
cfgs.push("rustdoc".to_string());
473-
let triple = matches.opt_str("target").map(|target| {
474-
if target.ends_with(".json") {
475-
TargetTriple::TargetPath(PathBuf::from(target))
476-
} else {
477-
TargetTriple::TargetTriple(target)
478-
}
479-
});
480-
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
481-
let crate_name = matches.opt_str("crate-name");
482-
let crate_version = matches.opt_str("crate-version");
483-
let plugin_path = matches.opt_str("plugin-path");
484-
485430
info!("starting to run rustc");
486-
let display_warnings = matches.opt_present("display-warnings");
487-
488-
let force_unstable_if_unmarked = matches.opt_strs("Z").iter().any(|x| {
489-
*x == "force-unstable-if-unmarked"
490-
});
491-
let treat_err_as_bug = matches.opt_strs("Z").iter().any(|x| {
492-
*x == "treat-err-as-bug"
493-
});
494-
let ui_testing = matches.opt_strs("Z").iter().any(|x| {
495-
*x == "ui-testing"
496-
});
497-
498-
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(&matches, error_format);
499431

500432
let (tx, rx) = channel();
501433

502434
let result = rustc_driver::monitor(move || syntax::with_globals(move || {
503435
use rustc::session::config::Input;
504436

437+
let paths = options.libs;
438+
let cfgs = options.cfgs;
439+
let triple = options.target;
440+
let maybe_sysroot = options.maybe_sysroot;
441+
let crate_name = options.crate_name;
442+
let crate_version = options.crate_version;
443+
let force_unstable_if_unmarked = options.debugging_options.force_unstable_if_unmarked;
444+
let treat_err_as_bug = options.debugging_options.treat_err_as_bug;
445+
let ui_testing = options.debugging_options.ui_testing;
446+
505447
let (mut krate, renderinfo, passes) =
506-
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
507-
display_warnings, crate_name.clone(),
508-
force_unstable_if_unmarked, edition, cg, error_format,
509-
lint_opts, lint_cap, describe_lints, manual_passes, default_passes,
510-
treat_err_as_bug, ui_testing);
448+
core::run_core(paths, cfgs, options.externs, Input::File(options.input), triple, maybe_sysroot,
449+
options.display_warnings, crate_name.clone(),
450+
force_unstable_if_unmarked, options.edition, options.codegen_options, options.error_format,
451+
options.lint_opts, options.lint_cap, options.describe_lints,
452+
options.manual_passes, options.default_passes, treat_err_as_bug,
453+
ui_testing);
511454

512455
info!("finished with rustc");
513456

@@ -517,14 +460,6 @@ where R: 'static + Send,
517460

518461
krate.version = crate_version;
519462

520-
if !plugins.is_empty() {
521-
eprintln!("WARNING: --plugins no longer functions; see CVE-2018-1000622");
522-
}
523-
524-
if !plugin_path.is_none() {
525-
eprintln!("WARNING: --plugin-path no longer functions; see CVE-2018-1000622");
526-
}
527-
528463
info!("Executing passes");
529464

530465
for pass in &passes {
@@ -547,8 +482,7 @@ where R: 'static + Send,
547482
krate = pass(krate);
548483
}
549484

550-
tx.send(f(Output { krate: krate, renderinfo: renderinfo, passes: passes },
551-
&matches)).unwrap();
485+
tx.send(f(Output { krate: krate, renderinfo: renderinfo, passes: passes })).unwrap();
552486
}));
553487

554488
match result {

src/librustdoc/markdown.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::path::{PathBuf, Path};
1515
use std::cell::RefCell;
1616

1717
use errors;
18-
use getopts;
1918
use testing;
2019
use rustc::session::search_paths::SearchPaths;
2120
use rustc::session::config::{Externs, CodegenOptions};
@@ -51,13 +50,14 @@ fn extract_leading_metadata<'a>(s: &'a str) -> (Vec<&'a str>, &'a str) {
5150

5251
/// Render `input` (e.g. "foo.md") into an HTML file in `output`
5352
/// (e.g. output = "bar" => "bar/foo.html").
54-
pub fn render(input: &Path, mut output: PathBuf, matches: &getopts::Matches,
55-
external_html: &ExternalHtml, include_toc: bool, diag: &errors::Handler) -> isize {
53+
pub fn render(input: &Path, mut output: PathBuf, markdown_css: &[String],
54+
playground_url: Option<String>, external_html: &ExternalHtml, include_toc: bool,
55+
diag: &errors::Handler) -> isize {
5656
output.push(input.file_stem().unwrap());
5757
output.set_extension("html");
5858

5959
let mut css = String::new();
60-
for name in &matches.opt_strs("markdown-css") {
60+
for name in markdown_css {
6161
let s = format!("<link rel=\"stylesheet\" type=\"text/css\" href=\"{}\">\n", name);
6262
css.push_str(&s)
6363
}
@@ -67,8 +67,7 @@ pub fn render(input: &Path, mut output: PathBuf, matches: &getopts::Matches,
6767
Err(LoadStringError::ReadFail) => return 1,
6868
Err(LoadStringError::BadUtf8) => return 2,
6969
};
70-
if let Some(playground) = matches.opt_str("markdown-playground-url").or(
71-
matches.opt_str("playground-url")) {
70+
if let Some(playground) = playground_url {
7271
markdown::PLAYGROUND.with(|s| { *s.borrow_mut() = Some((None, playground)); });
7372
}
7473

0 commit comments

Comments
 (0)