X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flibrustc%2Fsession%2Fmod.rs;h=975ec0e709b7d272605e9c31b6f9c2fe057c6511;hb=0ac8915875596db90167701c447d9c76396358bb;hp=a8587e83736812314e8d66673e78cf38130da892;hpb=67a2d1f34f917b6e583440b0a86767b2ff8e4d4f;p=rust.git diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index a8587e83736..975ec0e709b 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -16,8 +16,9 @@ use syntax::ast::{NodeId, NodeIdAssigner, Name}; use syntax::codemap::Span; -use syntax::errors; -use syntax::errors::emitter::{Emitter, BasicEmitter}; +use syntax::errors::{self, DiagnosticBuilder}; +use syntax::errors::emitter::{Emitter, BasicEmitter, EmitterWriter}; +use syntax::errors::json::JsonEmitter; use syntax::diagnostics; use syntax::feature_gate; use syntax::parse; @@ -80,6 +81,61 @@ pub struct Session { } impl Session { + pub fn struct_span_warn<'a>(&'a self, + sp: Span, + msg: &str) + -> DiagnosticBuilder<'a> { + self.diagnostic().struct_span_warn(sp, msg) + } + pub fn struct_span_warn_with_code<'a>(&'a self, + sp: Span, + msg: &str, + code: &str) + -> DiagnosticBuilder<'a> { + self.diagnostic().struct_span_warn_with_code(sp, msg, code) + } + pub fn struct_warn<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> { + self.diagnostic().struct_warn(msg) + } + pub fn struct_span_err<'a>(&'a self, + sp: Span, + msg: &str) + -> DiagnosticBuilder<'a> { + match split_msg_into_multilines(msg) { + Some(ref msg) => self.diagnostic().struct_span_err(sp, msg), + None => self.diagnostic().struct_span_err(sp, msg), + } + } + pub fn struct_span_err_with_code<'a>(&'a self, + sp: Span, + msg: &str, + code: &str) + -> DiagnosticBuilder<'a> { + match split_msg_into_multilines(msg) { + Some(ref msg) => self.diagnostic().struct_span_err_with_code(sp, msg, code), + None => self.diagnostic().struct_span_err_with_code(sp, msg, code), + } + } + pub fn struct_err<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> { + self.diagnostic().struct_err(msg) + } + pub fn struct_span_fatal<'a>(&'a self, + sp: Span, + msg: &str) + -> DiagnosticBuilder<'a> { + self.diagnostic().struct_span_fatal(sp, msg) + } + pub fn struct_span_fatal_with_code<'a>(&'a self, + sp: Span, + msg: &str, + code: &str) + -> DiagnosticBuilder<'a> { + self.diagnostic().struct_span_fatal_with_code(sp, msg, code) + } + pub fn struct_fatal<'a>(&'a self, msg: &str) -> DiagnosticBuilder<'a> { + self.diagnostic().struct_fatal(msg) + } + pub fn span_fatal(&self, sp: Span, msg: &str) -> ! { panic!(self.diagnostic().span_fatal(sp, msg)) } @@ -98,13 +154,13 @@ pub fn span_err_or_warn(&self, is_warning: bool, sp: Span, msg: &str) { } pub fn span_err(&self, sp: Span, msg: &str) { match split_msg_into_multilines(msg) { - Some(msg) => self.diagnostic().span_err(sp, &msg[..]), + Some(msg) => self.diagnostic().span_err(sp, &msg), None => self.diagnostic().span_err(sp, msg) } } pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) { match split_msg_into_multilines(msg) { - Some(msg) => self.diagnostic().span_err_with_code(sp, &msg[..], code), + Some(msg) => self.diagnostic().span_err_with_code(sp, &msg, code), None => self.diagnostic().span_err_with_code(sp, msg, code) } } @@ -120,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) @@ -144,34 +201,6 @@ pub fn opt_span_warn(&self, opt_sp: Option, msg: &str) { None => self.warn(msg), } } - pub fn span_note(&self, sp: Span, msg: &str) { - self.diagnostic().span_note(sp, msg) - } - pub fn span_end_note(&self, sp: Span, msg: &str) { - self.diagnostic().span_end_note(sp, msg) - } - - /// Prints out a message with a suggested edit of the code. - /// - /// See `errors::RenderSpan::Suggestion` for more information. - pub fn span_suggestion(&self, sp: Span, msg: &str, suggestion: String) { - self.diagnostic().span_suggestion(sp, msg, suggestion) - } - pub fn span_help(&self, sp: Span, msg: &str) { - self.diagnostic().span_help(sp, msg) - } - pub fn fileline_note(&self, sp: Span, msg: &str) { - self.diagnostic().fileline_note(sp, msg) - } - pub fn fileline_help(&self, sp: Span, msg: &str) { - self.diagnostic().fileline_help(sp, msg) - } - pub fn note(&self, msg: &str) { - self.diagnostic().note(msg) - } - pub fn help(&self, msg: &str) { - self.diagnostic().help(msg) - } pub fn opt_span_bug(&self, opt_sp: Option, msg: &str) -> ! { match opt_sp { Some(sp) => self.span_bug(sp, msg), @@ -188,6 +217,12 @@ pub fn span_bug(&self, sp: Span, msg: &str) -> ! { pub fn bug(&self, msg: &str) -> ! { self.diagnostic().bug(msg) } + pub fn note_without_error(&self, msg: &str) { + self.diagnostic().note_without_error(msg) + } + pub fn span_note_without_error(&self, sp: Span, msg: &str) { + self.diagnostic().span_note_without_error(sp, msg) + } pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! { self.diagnostic().span_unimpl(sp, msg) } @@ -372,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) } @@ -440,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); }