1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A JSON emitter for errors.
13 //! This works by converting errors to a simplified structural format (see the
14 //! structs at the start of the file) and then serializing them. These should
15 //! contain as much information about the error as possible.
17 //! The format of the JSON output should be considered *unstable*. For now the
18 //! structs at the end of this file (Diagnostic*) specify the error format.
20 // FIXME spec the JSON output properly.
22 use codemap::{CodeMap, FilePathMapping};
23 use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
24 use errors::registry::Registry;
25 use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, CodeMapper};
26 use errors::DiagnosticId;
27 use errors::emitter::{Emitter, EmitterWriter};
29 use rustc_data_structures::sync::{self, Lrc};
30 use std::io::{self, Write};
32 use std::sync::{Arc, Mutex};
34 use rustc_serialize::json::{as_json, as_pretty_json};
36 pub struct JsonEmitter {
37 dst: Box<Write + Send>,
38 registry: Option<Registry>,
39 cm: Lrc<CodeMapper + sync::Send + sync::Sync>,
41 /// Whether "approximate suggestions" are enabled in the config
42 approximate_suggestions: bool,
47 pub fn stderr(registry: Option<Registry>,
48 code_map: Lrc<CodeMap>,
50 approximate_suggestions: bool) -> JsonEmitter {
52 dst: Box::new(io::stderr()),
56 approximate_suggestions,
61 pub fn basic(pretty: bool) -> JsonEmitter {
62 let file_path_mapping = FilePathMapping::empty();
63 JsonEmitter::stderr(None, Lrc::new(CodeMap::new(file_path_mapping)),
67 pub fn new(dst: Box<Write + Send>,
68 registry: Option<Registry>,
69 code_map: Lrc<CodeMap>,
71 approximate_suggestions: bool) -> JsonEmitter {
77 approximate_suggestions,
82 pub fn ui_testing(self, ui_testing: bool) -> Self {
83 Self { ui_testing, ..self }
87 impl Emitter for JsonEmitter {
88 fn emit(&mut self, db: &DiagnosticBuilder) {
89 let data = Diagnostic::from_diagnostic_builder(db, self);
90 let result = if self.pretty {
91 writeln!(&mut self.dst, "{}", as_pretty_json(&data))
93 writeln!(&mut self.dst, "{}", as_json(&data))
95 if let Err(e) = result {
96 panic!("failed to print diagnostics: {:?}", e);
101 // The following data types are provided just for serialisation.
103 #[derive(RustcEncodable)]
105 /// The primary error message.
107 code: Option<DiagnosticCode>,
108 /// "error: internal compiler error", "error", "warning", "note", "help".
110 spans: Vec<DiagnosticSpan>,
111 /// Associated diagnostic messages.
112 children: Vec<Diagnostic>,
113 /// The message as rustc would render it.
114 rendered: Option<String>,
117 #[derive(RustcEncodable)]
118 #[allow(unused_attributes)]
119 struct DiagnosticSpan {
126 /// 1-based, character offset.
129 /// Is this a "primary" span -- meaning the point, or one of the points,
130 /// where the error occurred?
132 /// Source text from the start of line_start to the end of line_end.
133 text: Vec<DiagnosticSpanLine>,
134 /// Label that should be placed at this location (if any)
135 label: Option<String>,
136 /// If we are suggesting a replacement, this will contain text
137 /// that should be sliced in atop this span.
138 suggested_replacement: Option<String>,
139 /// If the suggestion is approximate
140 #[rustc_serialize_exclude_null]
141 suggestion_approximate: Option<bool>,
142 /// Macro invocations that created the code at this span, if any.
143 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
146 #[derive(RustcEncodable)]
147 struct DiagnosticSpanLine {
150 /// 1-based, character offset in self.text.
151 highlight_start: usize,
153 highlight_end: usize,
156 #[derive(RustcEncodable)]
157 struct DiagnosticSpanMacroExpansion {
158 /// span where macro was applied to generate this code; note that
159 /// this may itself derive from a macro (if
160 /// `span.expansion.is_some()`)
161 span: DiagnosticSpan,
163 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
164 macro_decl_name: String,
166 /// span where macro was defined (if known)
167 def_site_span: Option<DiagnosticSpan>,
170 #[derive(RustcEncodable)]
171 struct DiagnosticCode {
174 /// An explanation for the code.
175 explanation: Option<&'static str>,
179 fn from_diagnostic_builder(db: &DiagnosticBuilder,
182 let sugg = db.suggestions.iter().map(|sugg| {
184 message: sugg.msg.clone(),
187 spans: DiagnosticSpan::from_suggestion(sugg, je),
193 // generate regular command line output and store it in the json
195 // A threadsafe buffer for writing.
196 #[derive(Default, Clone)]
197 struct BufWriter(Arc<Mutex<Vec<u8>>>);
199 impl Write for BufWriter {
200 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
201 self.0.lock().unwrap().write(buf)
203 fn flush(&mut self) -> io::Result<()> {
204 self.0.lock().unwrap().flush()
207 let buf = BufWriter::default();
208 let output = buf.clone();
209 EmitterWriter::new(Box::new(buf), Some(je.cm.clone()), false, false)
210 .ui_testing(je.ui_testing).emit(db);
211 let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
212 let output = String::from_utf8(output).unwrap();
215 message: db.message(),
216 code: DiagnosticCode::map_opt_string(db.code.clone(), je),
217 level: db.level.to_str(),
218 spans: DiagnosticSpan::from_multispan(&db.span, je),
219 children: db.children.iter().map(|c| {
220 Diagnostic::from_sub_diagnostic(c, je)
221 }).chain(sugg).collect(),
222 rendered: Some(output),
226 fn from_sub_diagnostic(db: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
228 message: db.message(),
230 level: db.level.to_str(),
231 spans: db.render_span.as_ref()
232 .map(|sp| DiagnosticSpan::from_multispan(sp, je))
233 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&db.span, je)),
240 impl DiagnosticSpan {
241 fn from_span_label(span: SpanLabel,
242 suggestion: Option<(&String, bool)>,
245 Self::from_span_etc(span.span,
252 fn from_span_etc(span: Span,
254 label: Option<String>,
255 suggestion: Option<(&String, bool)>,
258 // obtain the full backtrace from the `macro_backtrace`
259 // helper; in some ways, it'd be better to expand the
260 // backtrace ourselves, but the `macro_backtrace` helper makes
261 // some decision, such as dropping some frames, and I don't
262 // want to duplicate that logic here.
263 let backtrace = span.macro_backtrace().into_iter();
264 DiagnosticSpan::from_span_full(span,
272 fn from_span_full(span: Span,
274 label: Option<String>,
275 suggestion: Option<(&String, bool)>,
276 mut backtrace: vec::IntoIter<MacroBacktrace>,
279 let start = je.cm.lookup_char_pos(span.lo());
280 let end = je.cm.lookup_char_pos(span.hi());
281 let backtrace_step = backtrace.next().map(|bt| {
283 Self::from_span_full(bt.call_site,
289 let def_site_span = bt.def_site_span.map(|sp| {
290 Self::from_span_full(sp,
297 Box::new(DiagnosticSpanMacroExpansion {
299 macro_decl_name: bt.macro_decl_name,
304 let suggestion_approximate = if je.approximate_suggestions {
305 suggestion.map(|x| x.1)
311 file_name: start.file.name.to_string(),
312 byte_start: span.lo().0 - start.file.start_pos.0,
313 byte_end: span.hi().0 - start.file.start_pos.0,
314 line_start: start.line,
316 column_start: start.col.0 + 1,
317 column_end: end.col.0 + 1,
319 text: DiagnosticSpanLine::from_span(span, je),
320 suggested_replacement: suggestion.map(|x| x.0.clone()),
321 suggestion_approximate,
322 expansion: backtrace_step,
327 fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
330 .map(|span_str| Self::from_span_label(span_str, None, je))
334 fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
335 -> Vec<DiagnosticSpan> {
336 suggestion.substitutions
338 .flat_map(|substitution| {
339 substitution.parts.iter().map(move |suggestion_inner| {
340 let span_label = SpanLabel {
341 span: suggestion_inner.span,
345 DiagnosticSpan::from_span_label(span_label,
346 Some((&suggestion_inner.snippet,
347 suggestion.approximate)),
355 impl DiagnosticSpanLine {
356 fn line_from_filemap(fm: &syntax_pos::FileMap,
360 -> DiagnosticSpanLine {
362 text: fm.get_line(index).map_or(String::new(), |l| l.into_owned()),
363 highlight_start: h_start,
364 highlight_end: h_end,
368 /// Create a list of DiagnosticSpanLines from span - each line with any part
369 /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
370 /// `span` within the line.
371 fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
372 je.cm.span_to_lines(span)
374 let fm = &*lines.file;
378 DiagnosticSpanLine::line_from_filemap(fm,
380 line.start_col.0 + 1,
385 .unwrap_or_else(|_| vec![])
389 impl DiagnosticCode {
390 fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
393 DiagnosticId::Error(s) => s,
394 DiagnosticId::Lint(s) => s,
396 let explanation = je.registry
398 .and_then(|registry| registry.find_description(&s));