Skip to content

Commit 6bc1db0

Browse files
replace error/warning println with structured diag
1 parent 3566ea8 commit 6bc1db0

File tree

4 files changed

+90
-90
lines changed

4 files changed

+90
-90
lines changed

src/librustdoc/externalfiles.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use std::fs;
1212
use std::path::Path;
1313
use std::str;
14+
use errors;
1415
use html::markdown::Markdown;
1516

1617
#[derive(Clone)]
@@ -28,23 +29,23 @@ pub struct ExternalHtml {
2829

2930
impl ExternalHtml {
3031
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
31-
md_before_content: &[String], md_after_content: &[String])
32+
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
3233
-> Option<ExternalHtml> {
33-
load_external_files(in_header)
34+
load_external_files(in_header, diag)
3435
.and_then(|ih|
35-
load_external_files(before_content)
36+
load_external_files(before_content, diag)
3637
.map(|bc| (ih, bc))
3738
)
3839
.and_then(|(ih, bc)|
39-
load_external_files(md_before_content)
40+
load_external_files(md_before_content, diag)
4041
.map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[]))))
4142
)
4243
.and_then(|(ih, bc)|
43-
load_external_files(after_content)
44+
load_external_files(after_content, diag)
4445
.map(|ac| (ih, bc, ac))
4546
)
4647
.and_then(|(ih, bc, ac)|
47-
load_external_files(md_after_content)
48+
load_external_files(md_after_content, diag)
4849
.map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[]))))
4950
)
5051
.map(|(ih, bc, ac)|
@@ -62,28 +63,30 @@ pub enum LoadStringError {
6263
BadUtf8,
6364
}
6465

65-
pub fn load_string<P: AsRef<Path>>(file_path: P) -> Result<String, LoadStringError> {
66+
pub fn load_string<P: AsRef<Path>>(file_path: P, diag: &errors::Handler)
67+
-> Result<String, LoadStringError>
68+
{
6669
let file_path = file_path.as_ref();
6770
let contents = match fs::read(file_path) {
6871
Ok(bytes) => bytes,
6972
Err(e) => {
70-
eprintln!("error reading `{}`: {}", file_path.display(), e);
73+
diag.struct_err(&format!("error reading `{}`: {}", file_path.display(), e)).emit();
7174
return Err(LoadStringError::ReadFail);
7275
}
7376
};
7477
match str::from_utf8(&contents) {
7578
Ok(s) => Ok(s.to_string()),
7679
Err(_) => {
77-
eprintln!("error reading `{}`: not UTF-8", file_path.display());
80+
diag.struct_err(&format!("error reading `{}`: not UTF-8", file_path.display())).emit();
7881
Err(LoadStringError::BadUtf8)
7982
}
8083
}
8184
}
8285

83-
fn load_external_files(names: &[String]) -> Option<String> {
86+
fn load_external_files(names: &[String], diag: &errors::Handler) -> Option<String> {
8487
let mut out = String::new();
8588
for name in names {
86-
let s = match load_string(name) {
89+
let s = match load_string(name, diag) {
8790
Ok(s) => s,
8891
Err(_) => return None,
8992
};

src/librustdoc/lib.rs

+59-67
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,13 @@ use errors::ColorConfig;
5656
use std::collections::{BTreeMap, BTreeSet};
5757
use std::default::Default;
5858
use std::env;
59-
use std::fmt::Display;
60-
use std::io;
61-
use std::io::Write;
6259
use std::path::{Path, PathBuf};
6360
use std::process;
6461
use std::sync::mpsc::channel;
6562

6663
use syntax::edition::Edition;
6764
use externalfiles::ExternalHtml;
65+
use rustc::session::{early_warn, early_error};
6866
use rustc::session::search_paths::SearchPaths;
6967
use rustc::session::config::{ErrorOutputType, RustcOptGroup, Externs, CodegenOptions};
7068
use rustc::session::config::{nightly_options, build_codegen_options};
@@ -118,7 +116,8 @@ pub fn main() {
118116
fn get_args() -> Option<Vec<String>> {
119117
env::args_os().enumerate()
120118
.map(|(i, arg)| arg.into_string().map_err(|arg| {
121-
print_error(format!("Argument {} is not valid Unicode: {:?}", i, arg));
119+
early_warn(ErrorOutputType::default(),
120+
&format!("Argument {} is not valid Unicode: {:?}", i, arg));
122121
}).ok())
123122
.collect()
124123
}
@@ -318,16 +317,12 @@ pub fn main_args(args: &[String]) -> isize {
318317
let matches = match options.parse(&args[1..]) {
319318
Ok(m) => m,
320319
Err(err) => {
321-
print_error(err);
322-
return 1;
320+
early_error(ErrorOutputType::default(), &err.to_string());
323321
}
324322
};
325323
// Check for unstable options.
326324
nightly_options::check_nightly_options(&matches, &opts());
327325

328-
// check for deprecated options
329-
check_deprecated_options(&matches);
330-
331326
if matches.opt_present("h") || matches.opt_present("help") {
332327
usage("rustdoc");
333328
return 0;
@@ -348,6 +343,35 @@ pub fn main_args(args: &[String]) -> isize {
348343
return 0;
349344
}
350345

346+
let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
347+
Some("auto") => ColorConfig::Auto,
348+
Some("always") => ColorConfig::Always,
349+
Some("never") => ColorConfig::Never,
350+
None => ColorConfig::Auto,
351+
Some(arg) => {
352+
early_error(ErrorOutputType::default(),
353+
&format!("argument for --color must be `auto`, `always` or `never` \
354+
(instead was `{}`)", arg));
355+
}
356+
};
357+
let error_format = match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
358+
Some("human") => ErrorOutputType::HumanReadable(color),
359+
Some("json") => ErrorOutputType::Json(false),
360+
Some("pretty-json") => ErrorOutputType::Json(true),
361+
Some("short") => ErrorOutputType::Short(color),
362+
None => ErrorOutputType::HumanReadable(color),
363+
Some(arg) => {
364+
early_error(ErrorOutputType::default(),
365+
&format!("argument for --error-format must be `human`, `json` or \
366+
`short` (instead was `{}`)", arg));
367+
}
368+
};
369+
370+
let diag = core::new_handler(error_format, None);
371+
372+
// check for deprecated options
373+
check_deprecated_options(&matches, &diag);
374+
351375
let to_check = matches.opt_strs("theme-checker");
352376
if !to_check.is_empty() {
353377
let paths = theme::load_css_paths(include_bytes!("html/static/themes/light.css"));
@@ -356,7 +380,7 @@ pub fn main_args(args: &[String]) -> isize {
356380
println!("rustdoc: [theme-checker] Starting tests!");
357381
for theme_file in to_check.iter() {
358382
print!(" - Checking \"{}\"...", theme_file);
359-
let (success, differences) = theme::test_theme_against(theme_file, &paths);
383+
let (success, differences) = theme::test_theme_against(theme_file, &paths, &diag);
360384
if !differences.is_empty() || !success {
361385
println!(" FAILED");
362386
errors += 1;
@@ -374,47 +398,23 @@ pub fn main_args(args: &[String]) -> isize {
374398
}
375399

376400
if matches.free.is_empty() {
377-
print_error("missing file operand");
401+
diag.struct_err("missing file operand").emit();
378402
return 1;
379403
}
380404
if matches.free.len() > 1 {
381-
print_error("too many file operands");
405+
diag.struct_err("too many file operands").emit();
382406
return 1;
383407
}
384408
let input = &matches.free[0];
385409

386-
let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
387-
Some("auto") => ColorConfig::Auto,
388-
Some("always") => ColorConfig::Always,
389-
Some("never") => ColorConfig::Never,
390-
None => ColorConfig::Auto,
391-
Some(arg) => {
392-
print_error(&format!("argument for --color must be `auto`, `always` or `never` \
393-
(instead was `{}`)", arg));
394-
return 1;
395-
}
396-
};
397-
let error_format = match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
398-
Some("human") => ErrorOutputType::HumanReadable(color),
399-
Some("json") => ErrorOutputType::Json(false),
400-
Some("pretty-json") => ErrorOutputType::Json(true),
401-
Some("short") => ErrorOutputType::Short(color),
402-
None => ErrorOutputType::HumanReadable(color),
403-
Some(arg) => {
404-
print_error(&format!("argument for --error-format must be `human`, `json` or \
405-
`short` (instead was `{}`)", arg));
406-
return 1;
407-
}
408-
};
409-
410410
let mut libs = SearchPaths::new();
411411
for s in &matches.opt_strs("L") {
412412
libs.add_path(s, error_format);
413413
}
414414
let externs = match parse_externs(&matches) {
415415
Ok(ex) => ex,
416416
Err(err) => {
417-
print_error(err);
417+
diag.struct_err(&err.to_string()).emit();
418418
return 1;
419419
}
420420
};
@@ -435,10 +435,7 @@ pub fn main_args(args: &[String]) -> isize {
435435

436436
if let Some(ref p) = css_file_extension {
437437
if !p.is_file() {
438-
writeln!(
439-
&mut io::stderr(),
440-
"rustdoc: option --extend-css argument must be a file."
441-
).unwrap();
438+
diag.struct_err("option --extend-css argument must be a file").emit();
442439
return 1;
443440
}
444441
}
@@ -451,13 +448,14 @@ pub fn main_args(args: &[String]) -> isize {
451448
.iter()
452449
.map(|s| (PathBuf::from(&s), s.to_owned())) {
453450
if !theme_file.is_file() {
454-
println!("rustdoc: option --themes arguments must all be files");
451+
diag.struct_err("option --themes arguments must all be files").emit();
455452
return 1;
456453
}
457-
let (success, ret) = theme::test_theme_against(&theme_file, &paths);
454+
let (success, ret) = theme::test_theme_against(&theme_file, &paths, &diag);
458455
if !success || !ret.is_empty() {
459-
println!("rustdoc: invalid theme: \"{}\"", theme_s);
460-
println!(" Check what's wrong with the \"theme-checker\" option");
456+
diag.struct_err(&format!("invalid theme: \"{}\"", theme_s))
457+
.help("check what's wrong with the --theme-checker option")
458+
.emit();
461459
return 1;
462460
}
463461
themes.push(theme_file);
@@ -469,7 +467,7 @@ pub fn main_args(args: &[String]) -> isize {
469467
&matches.opt_strs("html-before-content"),
470468
&matches.opt_strs("html-after-content"),
471469
&matches.opt_strs("markdown-before-content"),
472-
&matches.opt_strs("markdown-after-content")) {
470+
&matches.opt_strs("markdown-after-content"), &diag) {
473471
Some(eh) => eh,
474472
None => return 3,
475473
};
@@ -485,7 +483,7 @@ pub fn main_args(args: &[String]) -> isize {
485483
let edition = match edition.parse() {
486484
Ok(e) => e,
487485
Err(_) => {
488-
print_error("could not parse edition");
486+
diag.struct_err("could not parse edition").emit();
489487
return 1;
490488
}
491489
};
@@ -495,7 +493,7 @@ pub fn main_args(args: &[String]) -> isize {
495493
match (should_test, markdown_input) {
496494
(true, true) => {
497495
return markdown::test(input, cfgs, libs, externs, test_args, maybe_sysroot,
498-
display_warnings, linker, edition, cg)
496+
display_warnings, linker, edition, cg, &diag)
499497
}
500498
(true, false) => {
501499
return test::run(Path::new(input), cfgs, libs, externs, test_args, crate_name,
@@ -504,7 +502,7 @@ pub fn main_args(args: &[String]) -> isize {
504502
(false, true) => return markdown::render(Path::new(input),
505503
output.unwrap_or(PathBuf::from("doc")),
506504
&matches, &external_html,
507-
!matches.opt_present("markdown-no-toc")),
505+
!matches.opt_present("markdown-no-toc"), &diag),
508506
(false, false) => {}
509507
}
510508

@@ -513,6 +511,7 @@ pub fn main_args(args: &[String]) -> isize {
513511
let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, error_format,
514512
move |out| {
515513
let Output { krate, passes, renderinfo } = out;
514+
let diag = core::new_handler(error_format, None);
516515
info!("going to format");
517516
match output_format.as_ref().map(|s| &**s) {
518517
Some("html") | None => {
@@ -528,26 +527,17 @@ pub fn main_args(args: &[String]) -> isize {
528527
0
529528
}
530529
Some(s) => {
531-
print_error(format!("unknown output format: {}", s));
530+
diag.struct_err(&format!("unknown output format: {}", s)).emit();
532531
1
533532
}
534533
}
535534
});
536535
res.unwrap_or_else(|s| {
537-
print_error(format!("input error: {}", s));
536+
diag.struct_err(&format!("input error: {}", s)).emit();
538537
1
539538
})
540539
}
541540

542-
/// Prints an uniformized error message on the standard error output
543-
fn print_error<T>(error_message: T) where T: Display {
544-
writeln!(
545-
&mut io::stderr(),
546-
"rustdoc: {}\nTry 'rustdoc --help' for more information.",
547-
error_message
548-
).unwrap();
549-
}
550-
551541
/// Looks inside the command line arguments to extract the relevant input format
552542
/// and files and then generates the necessary rustdoc output for formatting.
553543
fn acquire_input<R, F>(input: PathBuf,
@@ -714,7 +704,7 @@ where R: 'static + Send,
714704
}
715705

716706
/// Prints deprecation warnings for deprecated options
717-
fn check_deprecated_options(matches: &getopts::Matches) {
707+
fn check_deprecated_options(matches: &getopts::Matches, diag: &errors::Handler) {
718708
let deprecated_flags = [
719709
"input-format",
720710
"output-format",
@@ -726,12 +716,14 @@ fn check_deprecated_options(matches: &getopts::Matches) {
726716

727717
for flag in deprecated_flags.into_iter() {
728718
if matches.opt_present(flag) {
729-
eprintln!("WARNING: the '{}' flag is considered deprecated", flag);
730-
eprintln!("WARNING: please see https://github.com/rust-lang/rust/issues/44136");
731-
}
732-
}
719+
let mut err = diag.struct_warn(&format!("the '{}' flag is considered deprecated", flag));
720+
err.warn("please see https://github.com/rust-lang/rust/issues/44136");
733721

734-
if matches.opt_present("no-defaults") {
735-
eprintln!("WARNING: (you may want to use --document-private-items)");
722+
if *flag == "no-defaults" {
723+
err.help("you may want to use --document-private-items");
724+
}
725+
726+
err.emit();
727+
}
736728
}
737729
}

0 commit comments

Comments
 (0)