diff options
| author | kennytm <kennytm@gmail.com> | 2017-11-22 01:12:59 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-11-22 01:12:59 +0800 |
| commit | 0af67a4df0219d518ecba6711ff8ff6680a9a2ec (patch) | |
| tree | 850fd666fcd77c62c2cdf792282e7bd106ef8ebc /src/libsyntax | |
| parent | 9b090a026108fab89cfe5f39bfd3492597e76ad4 (diff) | |
| parent | e7b2702172b91624406e8c90716a225e8ec1a299 (diff) | |
| download | rust-0af67a4df0219d518ecba6711ff8ff6680a9a2ec.tar.gz rust-0af67a4df0219d518ecba6711ff8ff6680a9a2ec.zip | |
Rollup merge of #46052 - oli-obk:rendered_diagnostics_in_json, r=petrochenkov
Include rendered diagnostic in json r? @petrochenkov
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/json.rs | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/libsyntax/json.rs b/src/libsyntax/json.rs index e739c6d04e1..80ac0cb4faf 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<DiagnosticSpan>, /// Associated diagnostic messages. children: Vec<Diagnostic>, - /// The message as rustc would render it. Currently this is always `None` + /// The message as rustc would render it. rendered: Option<String>, } @@ -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<Mutex<Vec<u8>>>); + + impl Write for BufWriter { + fn write(&mut self, buf: &[u8]) -> io::Result<usize> { + 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), } } |
