Skip to content

Commit

Permalink
Add error-format and color-config options to rustdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Apr 16, 2018
1 parent 035ec5b commit a6fefde
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
45 changes: 38 additions & 7 deletions src/librustdoc/core.rs
Expand Up @@ -18,6 +18,7 @@ use rustc::middle::privacy::AccessLevels;
use rustc::ty::{self, TyCtxt, AllArenas};
use rustc::hir::map as hir_map;
use rustc::lint;
use rustc::session::config::ErrorOutputType;
use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc_resolve as resolve;
use rustc_metadata::creader::CrateLoader;
Expand All @@ -28,8 +29,9 @@ use syntax::ast::NodeId;
use syntax::codemap;
use syntax::edition::Edition;
use syntax::feature_gate::UnstableFeatures;
use syntax::json::JsonEmitter;
use errors;
use errors::emitter::ColorConfig;
use errors::emitter::{Emitter, EmitterWriter};

use std::cell::{RefCell, Cell};
use std::mem;
Expand Down Expand Up @@ -115,7 +117,6 @@ impl DocAccessLevels for AccessLevels<DefId> {
}
}


pub fn run_core(search_paths: SearchPaths,
cfgs: Vec<String>,
externs: config::Externs,
Expand All @@ -126,7 +127,8 @@ pub fn run_core(search_paths: SearchPaths,
crate_name: Option<String>,
force_unstable_if_unmarked: bool,
edition: Edition,
cg: CodegenOptions) -> (clean::Crate, RenderInfo)
cg: CodegenOptions,
error_format: ErrorOutputType) -> (clean::Crate, RenderInfo)
{
// Parse, resolve, and typecheck the given crate.

Expand All @@ -138,6 +140,7 @@ pub fn run_core(search_paths: SearchPaths,
let warning_lint = lint::builtin::WARNINGS.name_lower();

let host_triple = TargetTriple::from_triple(config::host_triple());
// plays with error output here!
let sessopts = config::Options {
maybe_sysroot,
search_paths,
Expand All @@ -155,14 +158,42 @@ pub fn run_core(search_paths: SearchPaths,
edition,
..config::basic_debugging_options()
},
error_format,
..config::basic_options().clone()
};

let codemap = Lrc::new(codemap::CodeMap::new(sessopts.file_path_mapping()));
let diagnostic_handler = errors::Handler::with_tty_emitter(ColorConfig::Auto,
true,
false,
Some(codemap.clone()));
let emitter: Box<dyn Emitter> = match error_format {
ErrorOutputType::HumanReadable(color_config) => Box::new(
EmitterWriter::stderr(
color_config,
Some(codemap.clone()),
false,
sessopts.debugging_opts.teach,
).ui_testing(sessopts.debugging_opts.ui_testing)
),
ErrorOutputType::Json(pretty) => Box::new(
JsonEmitter::stderr(
None,
codemap.clone(),
pretty,
sessopts.debugging_opts.approximate_suggestions,
).ui_testing(sessopts.debugging_opts.ui_testing)
),
ErrorOutputType::Short(color_config) => Box::new(
EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false)
),
};

let diagnostic_handler = errors::Handler::with_emitter_and_flags(
emitter,
errors::HandlerFlags {
can_emit_warnings: true,
treat_err_as_bug: false,
external_macro_backtrace: false,
..Default::default()
},
);

let mut sess = session::build_session_(
sessopts, cpath, diagnostic_handler, codemap,
Expand Down
56 changes: 51 additions & 5 deletions src/librustdoc/lib.rs
Expand Up @@ -23,6 +23,7 @@
#![feature(test)]
#![feature(vec_remove_item)]
#![feature(entry_and_modify)]
#![feature(dyn_trait)]

extern crate arena;
extern crate getopts;
Expand All @@ -48,6 +49,8 @@ extern crate tempdir;

extern crate serialize as rustc_serialize; // used by deriving

use errors::ColorConfig;

use std::collections::{BTreeMap, BTreeSet};
use std::default::Default;
use std::env;
Expand Down Expand Up @@ -278,6 +281,21 @@ pub fn opts() -> Vec<RustcOptGroup> {
"edition to use when compiling rust code (default: 2015)",
"EDITION")
}),
unstable("color", |o| {
o.optopt("",
"color",
"Configure coloring of output:
auto = colorize, if output goes to a tty (default);
always = always colorize output;
never = never colorize output",
"auto|always|never")
}),
unstable("error-format", |o| {
o.optopt("",
"error-format",
"How errors and other messages are produced",
"human|json|short")
}),
]
}

Expand Down Expand Up @@ -362,9 +380,33 @@ pub fn main_args(args: &[String]) -> isize {
}
let input = &matches.free[0];

let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
Some("auto") => ColorConfig::Auto,
Some("always") => ColorConfig::Always,
Some("never") => ColorConfig::Never,
None => ColorConfig::Auto,
Some(arg) => {
print_error(&format!("argument for --color must be `auto`, `always` or `never` \
(instead was `{}`)", arg));
return 1;
}
};
let error_format = match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
Some("human") => ErrorOutputType::HumanReadable(color),
Some("json") => ErrorOutputType::Json(false),
Some("pretty-json") => ErrorOutputType::Json(true),
Some("short") => ErrorOutputType::Short(color),
None => ErrorOutputType::HumanReadable(color),
Some(arg) => {
print_error(&format!("argument for --error-format must be `human`, `json` or \
`short` (instead was `{}`)", arg));
return 1;
}
};

let mut libs = SearchPaths::new();
for s in &matches.opt_strs("L") {
libs.add_path(s, ErrorOutputType::default());
libs.add_path(s, error_format);
}
let externs = match parse_externs(&matches) {
Ok(ex) => ex,
Expand Down Expand Up @@ -464,7 +506,9 @@ pub fn main_args(args: &[String]) -> isize {
}

let output_format = matches.opt_str("w");
let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, move |out| {

let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, error_format,
move |out| {
let Output { krate, passes, renderinfo } = out;
info!("going to format");
match output_format.as_ref().map(|s| &**s) {
Expand Down Expand Up @@ -508,13 +552,14 @@ fn acquire_input<R, F>(input: PathBuf,
edition: Edition,
cg: CodegenOptions,
matches: &getopts::Matches,
error_format: ErrorOutputType,
f: F)
-> Result<R, String>
where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
match matches.opt_str("r").as_ref().map(|s| &**s) {
Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, f)),
Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, error_format, f)),
Some(s) => Err(format!("unknown input format: {}", s)),
None => Ok(rust_input(input, externs, edition, cg, matches, f))
None => Ok(rust_input(input, externs, edition, cg, matches, error_format, f))
}
}

Expand Down Expand Up @@ -545,6 +590,7 @@ fn rust_input<R, F>(cratefile: PathBuf,
edition: Edition,
cg: CodegenOptions,
matches: &getopts::Matches,
error_format: ErrorOutputType,
f: F) -> R
where R: 'static + Send,
F: 'static + Send + FnOnce(Output) -> R
Expand Down Expand Up @@ -597,7 +643,7 @@ where R: 'static + Send,
let (mut krate, renderinfo) =
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
display_warnings, crate_name.clone(),
force_unstable_if_unmarked, edition, cg);
force_unstable_if_unmarked, edition, cg, error_format);

info!("finished with rustc");

Expand Down

0 comments on commit a6fefde

Please sign in to comment.