Add error-format and color-config options to rustdoc
This commit is contained in:
@@ -18,6 +18,7 @@ use rustc::middle::privacy::AccessLevels;
|
|||||||
use rustc::ty::{self, TyCtxt, AllArenas};
|
use rustc::ty::{self, TyCtxt, AllArenas};
|
||||||
use rustc::hir::map as hir_map;
|
use rustc::hir::map as hir_map;
|
||||||
use rustc::lint;
|
use rustc::lint;
|
||||||
|
use rustc::session::config::ErrorOutputType;
|
||||||
use rustc::util::nodemap::{FxHashMap, FxHashSet};
|
use rustc::util::nodemap::{FxHashMap, FxHashSet};
|
||||||
use rustc_resolve as resolve;
|
use rustc_resolve as resolve;
|
||||||
use rustc_metadata::creader::CrateLoader;
|
use rustc_metadata::creader::CrateLoader;
|
||||||
@@ -28,8 +29,9 @@ use syntax::ast::NodeId;
|
|||||||
use syntax::codemap;
|
use syntax::codemap;
|
||||||
use syntax::edition::Edition;
|
use syntax::edition::Edition;
|
||||||
use syntax::feature_gate::UnstableFeatures;
|
use syntax::feature_gate::UnstableFeatures;
|
||||||
|
use syntax::json::JsonEmitter;
|
||||||
use errors;
|
use errors;
|
||||||
use errors::emitter::ColorConfig;
|
use errors::emitter::{Emitter, EmitterWriter};
|
||||||
|
|
||||||
use std::cell::{RefCell, Cell};
|
use std::cell::{RefCell, Cell};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
@@ -115,7 +117,6 @@ impl DocAccessLevels for AccessLevels<DefId> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn run_core(search_paths: SearchPaths,
|
pub fn run_core(search_paths: SearchPaths,
|
||||||
cfgs: Vec<String>,
|
cfgs: Vec<String>,
|
||||||
externs: config::Externs,
|
externs: config::Externs,
|
||||||
@@ -126,7 +127,8 @@ pub fn run_core(search_paths: SearchPaths,
|
|||||||
crate_name: Option<String>,
|
crate_name: Option<String>,
|
||||||
force_unstable_if_unmarked: bool,
|
force_unstable_if_unmarked: bool,
|
||||||
edition: Edition,
|
edition: Edition,
|
||||||
cg: CodegenOptions) -> (clean::Crate, RenderInfo)
|
cg: CodegenOptions,
|
||||||
|
error_format: ErrorOutputType) -> (clean::Crate, RenderInfo)
|
||||||
{
|
{
|
||||||
// Parse, resolve, and typecheck the given crate.
|
// Parse, resolve, and typecheck the given crate.
|
||||||
|
|
||||||
@@ -138,6 +140,7 @@ pub fn run_core(search_paths: SearchPaths,
|
|||||||
let warning_lint = lint::builtin::WARNINGS.name_lower();
|
let warning_lint = lint::builtin::WARNINGS.name_lower();
|
||||||
|
|
||||||
let host_triple = TargetTriple::from_triple(config::host_triple());
|
let host_triple = TargetTriple::from_triple(config::host_triple());
|
||||||
|
// plays with error output here!
|
||||||
let sessopts = config::Options {
|
let sessopts = config::Options {
|
||||||
maybe_sysroot,
|
maybe_sysroot,
|
||||||
search_paths,
|
search_paths,
|
||||||
@@ -155,14 +158,42 @@ pub fn run_core(search_paths: SearchPaths,
|
|||||||
edition,
|
edition,
|
||||||
..config::basic_debugging_options()
|
..config::basic_debugging_options()
|
||||||
},
|
},
|
||||||
|
error_format,
|
||||||
..config::basic_options().clone()
|
..config::basic_options().clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
let codemap = Lrc::new(codemap::CodeMap::new(sessopts.file_path_mapping()));
|
let codemap = Lrc::new(codemap::CodeMap::new(sessopts.file_path_mapping()));
|
||||||
let diagnostic_handler = errors::Handler::with_tty_emitter(ColorConfig::Auto,
|
let emitter: Box<dyn Emitter> = match error_format {
|
||||||
true,
|
ErrorOutputType::HumanReadable(color_config) => Box::new(
|
||||||
false,
|
EmitterWriter::stderr(
|
||||||
Some(codemap.clone()));
|
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_(
|
let mut sess = session::build_session_(
|
||||||
sessopts, cpath, diagnostic_handler, codemap,
|
sessopts, cpath, diagnostic_handler, codemap,
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
#![feature(vec_remove_item)]
|
#![feature(vec_remove_item)]
|
||||||
#![feature(entry_and_modify)]
|
#![feature(entry_and_modify)]
|
||||||
|
#![feature(dyn_trait)]
|
||||||
|
|
||||||
extern crate arena;
|
extern crate arena;
|
||||||
extern crate getopts;
|
extern crate getopts;
|
||||||
@@ -48,6 +49,8 @@ extern crate tempdir;
|
|||||||
|
|
||||||
extern crate serialize as rustc_serialize; // used by deriving
|
extern crate serialize as rustc_serialize; // used by deriving
|
||||||
|
|
||||||
|
use errors::ColorConfig;
|
||||||
|
|
||||||
use std::collections::{BTreeMap, BTreeSet};
|
use std::collections::{BTreeMap, BTreeSet};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::env;
|
use std::env;
|
||||||
@@ -278,6 +281,21 @@ pub fn opts() -> Vec<RustcOptGroup> {
|
|||||||
"edition to use when compiling rust code (default: 2015)",
|
"edition to use when compiling rust code (default: 2015)",
|
||||||
"EDITION")
|
"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")
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,9 +380,33 @@ pub fn main_args(args: &[String]) -> isize {
|
|||||||
}
|
}
|
||||||
let input = &matches.free[0];
|
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();
|
let mut libs = SearchPaths::new();
|
||||||
for s in &matches.opt_strs("L") {
|
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) {
|
let externs = match parse_externs(&matches) {
|
||||||
Ok(ex) => ex,
|
Ok(ex) => ex,
|
||||||
@@ -464,7 +506,9 @@ pub fn main_args(args: &[String]) -> isize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let output_format = matches.opt_str("w");
|
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;
|
let Output { krate, passes, renderinfo } = out;
|
||||||
info!("going to format");
|
info!("going to format");
|
||||||
match output_format.as_ref().map(|s| &**s) {
|
match output_format.as_ref().map(|s| &**s) {
|
||||||
@@ -508,13 +552,14 @@ fn acquire_input<R, F>(input: PathBuf,
|
|||||||
edition: Edition,
|
edition: Edition,
|
||||||
cg: CodegenOptions,
|
cg: CodegenOptions,
|
||||||
matches: &getopts::Matches,
|
matches: &getopts::Matches,
|
||||||
|
error_format: ErrorOutputType,
|
||||||
f: F)
|
f: F)
|
||||||
-> Result<R, String>
|
-> Result<R, String>
|
||||||
where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
|
where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
|
||||||
match matches.opt_str("r").as_ref().map(|s| &**s) {
|
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)),
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -545,6 +590,7 @@ fn rust_input<R, F>(cratefile: PathBuf,
|
|||||||
edition: Edition,
|
edition: Edition,
|
||||||
cg: CodegenOptions,
|
cg: CodegenOptions,
|
||||||
matches: &getopts::Matches,
|
matches: &getopts::Matches,
|
||||||
|
error_format: ErrorOutputType,
|
||||||
f: F) -> R
|
f: F) -> R
|
||||||
where R: 'static + Send,
|
where R: 'static + Send,
|
||||||
F: 'static + Send + FnOnce(Output) -> R
|
F: 'static + Send + FnOnce(Output) -> R
|
||||||
@@ -597,7 +643,7 @@ where R: 'static + Send,
|
|||||||
let (mut krate, renderinfo) =
|
let (mut krate, renderinfo) =
|
||||||
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
|
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
|
||||||
display_warnings, crate_name.clone(),
|
display_warnings, crate_name.clone(),
|
||||||
force_unstable_if_unmarked, edition, cg);
|
force_unstable_if_unmarked, edition, cg, error_format);
|
||||||
|
|
||||||
info!("finished with rustc");
|
info!("finished with rustc");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user