X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc%2Fsession%2Fmod.rs;h=975ec0e709b7d272605e9c31b6f9c2fe057c6511;hb=0ac8915875596db90167701c447d9c76396358bb;hp=80b4c1916a81626d9c28cc3b8b5271f29acf5328;hpb=dedaebd5a16a053a8f2926d8b48de1a53ab08c4c;p=rust.git diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 80b4c1916a8..975ec0e709b 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -17,7 +17,8 @@ use syntax::ast::{NodeId, NodeIdAssigner, Name}; use syntax::codemap::Span; use syntax::errors::{self, DiagnosticBuilder}; -use syntax::errors::emitter::{Emitter, BasicEmitter}; +use syntax::errors::emitter::{Emitter, BasicEmitter, EmitterWriter}; +use syntax::errors::json::JsonEmitter; use syntax::diagnostics; use syntax::feature_gate; use syntax::parse; @@ -175,14 +176,15 @@ pub fn has_errors(&self) -> bool { pub fn abort_if_errors(&self) { self.diagnostic().abort_if_errors(); } - pub fn abort_if_new_errors(&self, mut f: F) - where F: FnMut() + pub fn abort_if_new_errors(&self, f: F) -> T + where F: FnOnce() -> T { let count = self.err_count(); - f(); + let result = f(); if self.err_count() > count { self.abort_if_errors(); } + result } pub fn span_warn(&self, sp: Span, msg: &str) { self.diagnostic().span_warn(sp, msg) @@ -405,12 +407,19 @@ pub fn build_session(sopts: config::Options, let treat_err_as_bug = sopts.treat_err_as_bug; let codemap = Rc::new(codemap::CodeMap::new()); + let emitter: Box = match sopts.error_format { + config::ErrorOutputType::HumanReadable(color_config) => { + Box::new(EmitterWriter::stderr(color_config, Some(registry), codemap.clone())) + } + config::ErrorOutputType::Json => { + Box::new(JsonEmitter::stderr(Some(registry), codemap.clone())) + } + }; + let diagnostic_handler = - errors::Handler::new(sopts.color, - Some(registry), - can_print_warnings, - treat_err_as_bug, - codemap.clone()); + errors::Handler::with_emitter(can_print_warnings, + treat_err_as_bug, + emitter); build_session_(sopts, local_crate_source_file, diagnostic_handler, codemap, cstore) } @@ -473,13 +482,23 @@ pub fn build_session_(sopts: config::Options, sess } -pub fn early_error(color: errors::ColorConfig, msg: &str) -> ! { - let mut emitter = BasicEmitter::stderr(color); +pub fn early_error(output: config::ErrorOutputType, msg: &str) -> ! { + let mut emitter: Box = match output { + config::ErrorOutputType::HumanReadable(color_config) => { + Box::new(BasicEmitter::stderr(color_config)) + } + config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()), + }; emitter.emit(None, msg, None, errors::Level::Fatal); panic!(errors::FatalError); } -pub fn early_warn(color: errors::ColorConfig, msg: &str) { - let mut emitter = BasicEmitter::stderr(color); +pub fn early_warn(output: config::ErrorOutputType, msg: &str) { + let mut emitter: Box = match output { + config::ErrorOutputType::HumanReadable(color_config) => { + Box::new(BasicEmitter::stderr(color_config)) + } + config::ErrorOutputType::Json => Box::new(JsonEmitter::basic()), + }; emitter.emit(None, msg, None, errors::Level::Warning); }