Skip to content

Commit a6fefde

Browse files
Add error-format and color-config options to rustdoc
1 parent 035ec5b commit a6fefde

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

src/librustdoc/core.rs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc::middle::privacy::AccessLevels;
1818
use rustc::ty::{self, TyCtxt, AllArenas};
1919
use rustc::hir::map as hir_map;
2020
use rustc::lint;
21+
use rustc::session::config::ErrorOutputType;
2122
use rustc::util::nodemap::{FxHashMap, FxHashSet};
2223
use rustc_resolve as resolve;
2324
use rustc_metadata::creader::CrateLoader;
@@ -28,8 +29,9 @@ use syntax::ast::NodeId;
2829
use syntax::codemap;
2930
use syntax::edition::Edition;
3031
use syntax::feature_gate::UnstableFeatures;
32+
use syntax::json::JsonEmitter;
3133
use errors;
32-
use errors::emitter::ColorConfig;
34+
use errors::emitter::{Emitter, EmitterWriter};
3335

3436
use std::cell::{RefCell, Cell};
3537
use std::mem;
@@ -115,7 +117,6 @@ impl DocAccessLevels for AccessLevels<DefId> {
115117
}
116118
}
117119

118-
119120
pub fn run_core(search_paths: SearchPaths,
120121
cfgs: Vec<String>,
121122
externs: config::Externs,
@@ -126,7 +127,8 @@ pub fn run_core(search_paths: SearchPaths,
126127
crate_name: Option<String>,
127128
force_unstable_if_unmarked: bool,
128129
edition: Edition,
129-
cg: CodegenOptions) -> (clean::Crate, RenderInfo)
130+
cg: CodegenOptions,
131+
error_format: ErrorOutputType) -> (clean::Crate, RenderInfo)
130132
{
131133
// Parse, resolve, and typecheck the given crate.
132134

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

140142
let host_triple = TargetTriple::from_triple(config::host_triple());
143+
// plays with error output here!
141144
let sessopts = config::Options {
142145
maybe_sysroot,
143146
search_paths,
@@ -155,14 +158,42 @@ pub fn run_core(search_paths: SearchPaths,
155158
edition,
156159
..config::basic_debugging_options()
157160
},
161+
error_format,
158162
..config::basic_options().clone()
159163
};
160164

161165
let codemap = Lrc::new(codemap::CodeMap::new(sessopts.file_path_mapping()));
162-
let diagnostic_handler = errors::Handler::with_tty_emitter(ColorConfig::Auto,
163-
true,
164-
false,
165-
Some(codemap.clone()));
166+
let emitter: Box<dyn Emitter> = match error_format {
167+
ErrorOutputType::HumanReadable(color_config) => Box::new(
168+
EmitterWriter::stderr(
169+
color_config,
170+
Some(codemap.clone()),
171+
false,
172+
sessopts.debugging_opts.teach,
173+
).ui_testing(sessopts.debugging_opts.ui_testing)
174+
),
175+
ErrorOutputType::Json(pretty) => Box::new(
176+
JsonEmitter::stderr(
177+
None,
178+
codemap.clone(),
179+
pretty,
180+
sessopts.debugging_opts.approximate_suggestions,
181+
).ui_testing(sessopts.debugging_opts.ui_testing)
182+
),
183+
ErrorOutputType::Short(color_config) => Box::new(
184+
EmitterWriter::stderr(color_config, Some(codemap.clone()), true, false)
185+
),
186+
};
187+
188+
let diagnostic_handler = errors::Handler::with_emitter_and_flags(
189+
emitter,
190+
errors::HandlerFlags {
191+
can_emit_warnings: true,
192+
treat_err_as_bug: false,
193+
external_macro_backtrace: false,
194+
..Default::default()
195+
},
196+
);
166197

167198
let mut sess = session::build_session_(
168199
sessopts, cpath, diagnostic_handler, codemap,

src/librustdoc/lib.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#![feature(test)]
2424
#![feature(vec_remove_item)]
2525
#![feature(entry_and_modify)]
26+
#![feature(dyn_trait)]
2627

2728
extern crate arena;
2829
extern crate getopts;
@@ -48,6 +49,8 @@ extern crate tempdir;
4849

4950
extern crate serialize as rustc_serialize; // used by deriving
5051

52+
use errors::ColorConfig;
53+
5154
use std::collections::{BTreeMap, BTreeSet};
5255
use std::default::Default;
5356
use std::env;
@@ -278,6 +281,21 @@ pub fn opts() -> Vec<RustcOptGroup> {
278281
"edition to use when compiling rust code (default: 2015)",
279282
"EDITION")
280283
}),
284+
unstable("color", |o| {
285+
o.optopt("",
286+
"color",
287+
"Configure coloring of output:
288+
auto = colorize, if output goes to a tty (default);
289+
always = always colorize output;
290+
never = never colorize output",
291+
"auto|always|never")
292+
}),
293+
unstable("error-format", |o| {
294+
o.optopt("",
295+
"error-format",
296+
"How errors and other messages are produced",
297+
"human|json|short")
298+
}),
281299
]
282300
}
283301

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

383+
let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
384+
Some("auto") => ColorConfig::Auto,
385+
Some("always") => ColorConfig::Always,
386+
Some("never") => ColorConfig::Never,
387+
None => ColorConfig::Auto,
388+
Some(arg) => {
389+
print_error(&format!("argument for --color must be `auto`, `always` or `never` \
390+
(instead was `{}`)", arg));
391+
return 1;
392+
}
393+
};
394+
let error_format = match matches.opt_str("error-format").as_ref().map(|s| &s[..]) {
395+
Some("human") => ErrorOutputType::HumanReadable(color),
396+
Some("json") => ErrorOutputType::Json(false),
397+
Some("pretty-json") => ErrorOutputType::Json(true),
398+
Some("short") => ErrorOutputType::Short(color),
399+
None => ErrorOutputType::HumanReadable(color),
400+
Some(arg) => {
401+
print_error(&format!("argument for --error-format must be `human`, `json` or \
402+
`short` (instead was `{}`)", arg));
403+
return 1;
404+
}
405+
};
406+
365407
let mut libs = SearchPaths::new();
366408
for s in &matches.opt_strs("L") {
367-
libs.add_path(s, ErrorOutputType::default());
409+
libs.add_path(s, error_format);
368410
}
369411
let externs = match parse_externs(&matches) {
370412
Ok(ex) => ex,
@@ -464,7 +506,9 @@ pub fn main_args(args: &[String]) -> isize {
464506
}
465507

466508
let output_format = matches.opt_str("w");
467-
let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, move |out| {
509+
510+
let res = acquire_input(PathBuf::from(input), externs, edition, cg, &matches, error_format,
511+
move |out| {
468512
let Output { krate, passes, renderinfo } = out;
469513
info!("going to format");
470514
match output_format.as_ref().map(|s| &**s) {
@@ -508,13 +552,14 @@ fn acquire_input<R, F>(input: PathBuf,
508552
edition: Edition,
509553
cg: CodegenOptions,
510554
matches: &getopts::Matches,
555+
error_format: ErrorOutputType,
511556
f: F)
512557
-> Result<R, String>
513558
where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
514559
match matches.opt_str("r").as_ref().map(|s| &**s) {
515-
Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, f)),
560+
Some("rust") => Ok(rust_input(input, externs, edition, cg, matches, error_format, f)),
516561
Some(s) => Err(format!("unknown input format: {}", s)),
517-
None => Ok(rust_input(input, externs, edition, cg, matches, f))
562+
None => Ok(rust_input(input, externs, edition, cg, matches, error_format, f))
518563
}
519564
}
520565

@@ -545,6 +590,7 @@ fn rust_input<R, F>(cratefile: PathBuf,
545590
edition: Edition,
546591
cg: CodegenOptions,
547592
matches: &getopts::Matches,
593+
error_format: ErrorOutputType,
548594
f: F) -> R
549595
where R: 'static + Send,
550596
F: 'static + Send + FnOnce(Output) -> R
@@ -597,7 +643,7 @@ where R: 'static + Send,
597643
let (mut krate, renderinfo) =
598644
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
599645
display_warnings, crate_name.clone(),
600-
force_unstable_if_unmarked, edition, cg);
646+
force_unstable_if_unmarked, edition, cg, error_format);
601647

602648
info!("finished with rustc");
603649

0 commit comments

Comments
 (0)