Skip to content

Commit 4d941e5

Browse files
Add error-format and color-config options to rustdoc
1 parent d54039a commit 4d941e5

File tree

2 files changed

+88
-12
lines changed

2 files changed

+88
-12
lines changed

src/librustdoc/core.rs

+38-7
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,
@@ -125,7 +126,8 @@ pub fn run_core(search_paths: SearchPaths,
125126
allow_warnings: bool,
126127
crate_name: Option<String>,
127128
force_unstable_if_unmarked: bool,
128-
edition: Edition) -> (clean::Crate, RenderInfo)
129+
edition: Edition,
130+
error_format: ErrorOutputType) -> (clean::Crate, RenderInfo)
129131
{
130132
// Parse, resolve, and typecheck the given crate.
131133

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

139141
let host_triple = TargetTriple::from_triple(config::host_triple());
142+
// plays with error output here!
140143
let sessopts = config::Options {
141144
maybe_sysroot,
142145
search_paths,
@@ -153,14 +156,42 @@ pub fn run_core(search_paths: SearchPaths,
153156
edition,
154157
..config::basic_debugging_options()
155158
},
159+
error_format,
156160
..config::basic_options().clone()
157161
};
158162

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

165196
let mut sess = session::build_session_(
166197
sessopts, cpath, diagnostic_handler, codemap,

src/librustdoc/lib.rs

+50-5
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;
@@ -274,6 +277,21 @@ pub fn opts() -> Vec<RustcOptGroup> {
274277
"edition to use when compiling rust code (default: 2015)",
275278
"EDITION")
276279
}),
280+
unstable("color", |o| {
281+
o.optopt("",
282+
"color",
283+
"Configure coloring of output:
284+
auto = colorize, if output goes to a tty (default);
285+
always = always colorize output;
286+
never = never colorize output",
287+
"auto|always|never")
288+
}),
289+
unstable("error-format", |o| {
290+
o.optopt("",
291+
"error-format",
292+
"How errors and other messages are produced",
293+
"human|json|short")
294+
}),
277295
]
278296
}
279297

@@ -358,9 +376,33 @@ pub fn main_args(args: &[String]) -> isize {
358376
}
359377
let input = &matches.free[0];
360378

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

460502
let output_format = matches.opt_str("w");
461-
let res = acquire_input(PathBuf::from(input), externs, edition, &matches, move |out| {
503+
let res = acquire_input(PathBuf::from(input), externs, edition, &matches, error_format,
504+
move |out| {
462505
let Output { krate, passes, renderinfo } = out;
463506
info!("going to format");
464507
match output_format.as_ref().map(|s| &**s) {
@@ -501,13 +544,14 @@ fn acquire_input<R, F>(input: PathBuf,
501544
externs: Externs,
502545
edition: Edition,
503546
matches: &getopts::Matches,
547+
error_format: ErrorOutputType,
504548
f: F)
505549
-> Result<R, String>
506550
where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
507551
match matches.opt_str("r").as_ref().map(|s| &**s) {
508-
Some("rust") => Ok(rust_input(input, externs, edition, matches, f)),
552+
Some("rust") => Ok(rust_input(input, externs, edition, matches, error_format, f)),
509553
Some(s) => Err(format!("unknown input format: {}", s)),
510-
None => Ok(rust_input(input, externs, edition, matches, f))
554+
None => Ok(rust_input(input, externs, edition, matches, error_format, f))
511555
}
512556
}
513557

@@ -537,6 +581,7 @@ fn rust_input<R, F>(cratefile: PathBuf,
537581
externs: Externs,
538582
edition: Edition,
539583
matches: &getopts::Matches,
584+
error_format: ErrorOutputType,
540585
f: F) -> R
541586
where R: 'static + Send,
542587
F: 'static + Send + FnOnce(Output) -> R
@@ -589,7 +634,7 @@ where R: 'static + Send,
589634
let (mut krate, renderinfo) =
590635
core::run_core(paths, cfgs, externs, Input::File(cratefile), triple, maybe_sysroot,
591636
display_warnings, crate_name.clone(),
592-
force_unstable_if_unmarked, edition);
637+
force_unstable_if_unmarked, edition, error_format);
593638

594639
info!("finished with rustc");
595640

0 commit comments

Comments
 (0)