From 78e269ee0b8050bf91ed35b08b93b922c334d21f Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 17 Nov 2017 08:55:22 +0100 Subject: Include rendered diagnostic in json --- src/libsyntax/json.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index e739c6d04e1..d3f69a66557 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -24,11 +24,12 @@ use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan}; use errors::registry::Registry; use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, CodeMapper}; use errors::DiagnosticId; -use errors::emitter::Emitter; +use errors::emitter::{Emitter, EmitterWriter}; use std::rc::Rc; use std::io::{self, Write}; use std::vec; +use std::sync::{Arc, Mutex}; use rustc_serialize::json::{as_json, as_pretty_json}; @@ -95,7 +96,7 @@ struct Diagnostic { spans: Vec, /// Associated diagnostic messages. children: Vec, - /// The message as rustc would render it. Currently this is always `None` + /// The message as rustc would render it. rendered: Option, } @@ -170,6 +171,27 @@ impl Diagnostic { rendered: None, } }); + + // generate regular command line output and store it in the json + + // A threadsafe buffer for writing. + #[derive(Default, Clone)] + struct BufWriter(Arc>>); + + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.0.lock().unwrap().write(buf) + } + fn flush(&mut self) -> io::Result<()> { + self.0.lock().unwrap().flush() + } + } + let buf = BufWriter::default(); + let output = buf.clone(); + EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false).emit(db); + let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap(); + let output = String::from_utf8(output).unwrap(); + Diagnostic { message: db.message(), code: DiagnosticCode::map_opt_string(db.code.clone(), je), @@ -178,7 +200,7 @@ impl Diagnostic { children: db.children.iter().map(|c| { Diagnostic::from_sub_diagnostic(c, je) }).chain(sugg).collect(), - rendered: None, + rendered: Some(output.to_owned()), } } -- cgit 1.4.1-3-g733a5 From 3864d8943b99138d46751aadda58e27a1f4e06e8 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 20 Nov 2017 09:40:55 +0100 Subject: Address PR comments --- src/libsyntax/json.rs | 2 +- src/tools/compiletest/src/runtest.rs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'src/libsyntax') diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index d3f69a66557..80ac0cb4faf 100644 --- a/src/libsyntax/json.rs +++ b/src/libsyntax/json.rs @@ -200,7 +200,7 @@ impl Diagnostic { children: db.children.iter().map(|c| { Diagnostic::from_sub_diagnostic(c, je) }).chain(sugg).collect(), - rendered: Some(output.to_owned()), + rendered: Some(output), } } diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index b74867aba2d..633f349ba50 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -2411,7 +2411,11 @@ actual:\n\ let mut normalized = output.replace(&parent_dir_str, "$DIR"); if json { - normalized = normalized.replace("\\n", "\n"); // verbatim newline in json strings + // escaped newlines in json strings should be readable + // in the stderr files. There's no point int being correct, + // since only humans process the stderr files. + // Thus we just turn escaped newlines back into newlines. + normalized = normalized.replace("\\n", "\n"); } normalized = normalized.replace("\\\\", "\\") // denormalize for paths on windows -- cgit 1.4.1-3-g733a5