Skip to content

Commit 182c96c

Browse files
committed
auto merge of #14040 : hannobraun/rust/force-color-output, r=alexcrichton
This pull request fixes #12881. Two caveats: 1. As explained in the commit message, this doesn't include a regression test. If this is unacceptable, please let me know, I'll see what I can do. 1. I'm getting some test failures on make check, all from debuginfo. I suspect this is due to #13680 and not related to my changes (I have GDB 7.7). This is the list of failed tests: > [debuginfo-gdb] debuginfo/basic-types-globals.rs > [debuginfo-gdb] debuginfo/basic-types-mut-globals.rs > [debuginfo-gdb] debuginfo/basic-types.rs > [debuginfo-gdb] debuginfo/borrowed-basic.rs > [debuginfo-gdb] debuginfo/borrowed-managed-basic.rs > [debuginfo-gdb] debuginfo/borrowed-struct.rs > [debuginfo-gdb] debuginfo/borrowed-unique-basic.rs > [debuginfo-gdb] debuginfo/box.rs > [debuginfo-gdb] debuginfo/by-value-non-immediate-argument.rs > [debuginfo-gdb] debuginfo/by-value-self-argument-in-trait-impl.rs > [debuginfo-gdb] debuginfo/closure-in-generic-function.rs > [debuginfo-gdb] debuginfo/evec-in-struct.rs > [debuginfo-gdb] debuginfo/function-arg-initialization.rs > [debuginfo-gdb] debuginfo/function-prologue-stepping-no-split-stack.rs > [debuginfo-gdb] debuginfo/generic-function.rs > [debuginfo-gdb] debuginfo/generic-functions-nested.rs > [debuginfo-gdb] debuginfo/generic-method-on-generic-struct.rs > [debuginfo-gdb] debuginfo/generic-static-method-on-struct-and-enum.rs > [debuginfo-gdb] debuginfo/generic-struct.rs > [debuginfo-gdb] debuginfo/lexical-scope-in-stack-closure.rs > [debuginfo-gdb] debuginfo/lexical-scope-in-unique-closure.rs > [debuginfo-gdb] debuginfo/method-on-generic-struct.rs > [debuginfo-gdb] debuginfo/method-on-tuple-struct.rs > [debuginfo-gdb] debuginfo/name-shadowing-and-scope-nesting.rs > [debuginfo-gdb] debuginfo/recursive-struct.rs > [debuginfo-gdb] debuginfo/self-in-generic-default-method.rs > [debuginfo-gdb] debuginfo/shadowed-argument.rs > [debuginfo-gdb] debuginfo/shadowed-variable.rs > [debuginfo-gdb] debuginfo/simd.rs > [debuginfo-gdb] debuginfo/simple-lexical-scope.rs > [debuginfo-gdb] debuginfo/simple-struct.rs > [debuginfo-gdb] debuginfo/simple-tuple.rs > [debuginfo-gdb] debuginfo/static-method-on-struct-and-enum.rs > [debuginfo-gdb] debuginfo/tuple-struct.rs > [debuginfo-gdb] debuginfo/var-captured-in-nested-closure.rs > [debuginfo-gdb] debuginfo/var-captured-in-sendable-closure.rs > [debuginfo-gdb] debuginfo/var-captured-in-stack-closure.rs I can provide the full output on request.
2 parents ba5f530 + b7676f2 commit 182c96c

File tree

7 files changed

+46
-12
lines changed

7 files changed

+46
-12
lines changed

src/librustc/driver/config.rs

+21-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ use syntax::ast;
2727
use syntax::ast::{IntTy, UintTy};
2828
use syntax::attr;
2929
use syntax::attr::AttrMetaMethods;
30+
use syntax::diagnostic::{ColorConfig, Auto, Always, Never};
3031
use syntax::parse;
3132
use syntax::parse::token::InternedString;
3233

@@ -92,6 +93,7 @@ pub struct Options {
9293
/// Crate id-related things to maybe print. It's (crate_id, crate_name, crate_file_name).
9394
pub print_metas: (bool, bool, bool),
9495
pub cg: CodegenOptions,
96+
pub color: ColorConfig,
9597
}
9698

9799
/// Some reasonable defaults
@@ -115,6 +117,7 @@ pub fn basic_options() -> Options {
115117
write_dependency_info: (false, None),
116118
print_metas: (false, false, false),
117119
cg: basic_codegen_options(),
120+
color: Auto,
118121
}
119122
}
120123

@@ -536,7 +539,11 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
536539
optmulti("F", "forbid", "Set lint forbidden", "OPT"),
537540
optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"),
538541
optmulti("Z", "", "Set internal debugging options", "FLAG"),
539-
optflag( "v", "version", "Print version info and exit")
542+
optflag("v", "version", "Print version info and exit"),
543+
optopt("", "color", "Configure coloring of output:
544+
auto = colorize, if output goes to a tty (default);
545+
always = always colorize output;
546+
never = never colorize output", "auto|always|never")
540547
)
541548
}
542549

@@ -707,6 +714,18 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
707714
matches.opt_present("crate-file-name"));
708715
let cg = build_codegen_options(matches);
709716

717+
let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) {
718+
Some("auto") => Auto,
719+
Some("always") => Always,
720+
Some("never") => Never,
721+
722+
None => Auto,
723+
724+
Some(arg) => early_error(format!(
725+
"argument for --color must be auto, always or never (instead was `{}`)",
726+
arg))
727+
};
728+
710729
Options {
711730
crate_types: crate_types,
712731
gc: gc,
@@ -726,6 +745,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
726745
write_dependency_info: write_dependency_info,
727746
print_metas: print_metas,
728747
cg: cg,
748+
color: color
729749
}
730750
}
731751

src/librustc/driver/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ fn parse_crate_attrs(sess: &Session, input: &Input) ->
323323
}
324324

325325
pub fn early_error(msg: &str) -> ! {
326-
let mut emitter = diagnostic::EmitterWriter::stderr();
326+
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto);
327327
emitter.emit(None, msg, diagnostic::Fatal);
328328
fail!(diagnostic::FatalError);
329329
}
@@ -368,7 +368,7 @@ fn monitor(f: proc():Send) {
368368
Err(value) => {
369369
// Task failed without emitting a fatal diagnostic
370370
if !value.is::<diagnostic::FatalError>() {
371-
let mut emitter = diagnostic::EmitterWriter::stderr();
371+
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto);
372372

373373
// a .span_bug or .bug call has already printed what
374374
// it wants to print.

src/librustc/driver/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub fn build_session(sopts: config::Options,
196196
-> Session {
197197
let codemap = codemap::CodeMap::new();
198198
let diagnostic_handler =
199-
diagnostic::default_handler();
199+
diagnostic::default_handler(sopts.color);
200200
let span_diagnostic_handler =
201201
diagnostic::mk_span_handler(diagnostic_handler, codemap);
202202

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<StrBuf>)
7878

7979

8080
let codemap = syntax::codemap::CodeMap::new();
81-
let diagnostic_handler = syntax::diagnostic::default_handler();
81+
let diagnostic_handler = syntax::diagnostic::default_handler(syntax::diagnostic::Auto);
8282
let span_diagnostic_handler =
8383
syntax::diagnostic::mk_span_handler(diagnostic_handler, codemap);
8484

src/librustdoc/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn run(input: &str,
5353

5454

5555
let codemap = CodeMap::new();
56-
let diagnostic_handler = diagnostic::default_handler();
56+
let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto);
5757
let span_diagnostic_handler =
5858
diagnostic::mk_span_handler(diagnostic_handler, codemap);
5959

src/libsyntax/diagnostic.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ impl RenderSpan {
4949
}
5050
}
5151

52+
#[deriving(Clone)]
53+
pub enum ColorConfig {
54+
Auto,
55+
Always,
56+
Never
57+
}
58+
5259
pub trait Emitter {
5360
fn emit(&mut self, cmsp: Option<(&codemap::CodeMap, Span)>,
5461
msg: &str, lvl: Level);
@@ -176,8 +183,8 @@ pub fn mk_span_handler(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
176183
}
177184
}
178185

179-
pub fn default_handler() -> Handler {
180-
mk_handler(box EmitterWriter::stderr())
186+
pub fn default_handler(color_config: ColorConfig) -> Handler {
187+
mk_handler(box EmitterWriter::stderr(color_config))
181188
}
182189

183190
pub fn mk_handler(e: Box<Emitter:Send>) -> Handler {
@@ -257,9 +264,16 @@ enum Destination {
257264
}
258265

259266
impl EmitterWriter {
260-
pub fn stderr() -> EmitterWriter {
267+
pub fn stderr(color_config: ColorConfig) -> EmitterWriter {
261268
let stderr = io::stderr();
262-
if stderr.get_ref().isatty() {
269+
270+
let use_color = match color_config {
271+
Always => true,
272+
Never => false,
273+
Auto => stderr.get_ref().isatty()
274+
};
275+
276+
if use_color {
263277
let dst = match term::Terminal::new(stderr.unwrap()) {
264278
Ok(t) => Terminal(t),
265279
Err(..) => Raw(box io::stderr()),

src/libsyntax/parse/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use ast;
1515
use codemap::{Span, CodeMap, FileMap};
16-
use diagnostic::{SpanHandler, mk_span_handler, default_handler};
16+
use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto};
1717
use parse::attr::ParserAttr;
1818
use parse::parser::Parser;
1919

@@ -41,7 +41,7 @@ pub struct ParseSess {
4141

4242
pub fn new_parse_sess() -> ParseSess {
4343
ParseSess {
44-
span_diagnostic: mk_span_handler(default_handler(), CodeMap::new()),
44+
span_diagnostic: mk_span_handler(default_handler(Auto), CodeMap::new()),
4545
included_mod_stack: RefCell::new(Vec::new()),
4646
}
4747
}

0 commit comments

Comments
 (0)