]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/json.rs
Auto merge of #40829 - mgattozzi:ChildStderr, r=steveklabnik
[rust.git] / src / libsyntax / json.rs
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.
4 //
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.
10
11 //! A JSON emitter for errors.
12 //!
13 //! This works by converting errors to a simplified structural format (see the
14 //! structs at the start of the file) and then serialising them. These should
15 //! contain as much information about the error as possible.
16 //!
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.
19
20 // FIXME spec the JSON output properly.
21
22 use codemap::CodeMap;
23 use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
24 use errors::registry::Registry;
25 use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
26 use errors::emitter::Emitter;
27
28 use std::rc::Rc;
29 use std::io::{self, Write};
30 use std::vec;
31
32 use rustc_serialize::json::as_json;
33
34 pub struct JsonEmitter {
35     dst: Box<Write + Send>,
36     registry: Option<Registry>,
37     cm: Rc<CodeMapper + 'static>,
38 }
39
40 impl JsonEmitter {
41     pub fn stderr(registry: Option<Registry>,
42                   code_map: Rc<CodeMap>) -> JsonEmitter {
43         JsonEmitter {
44             dst: Box::new(io::stderr()),
45             registry: registry,
46             cm: code_map,
47         }
48     }
49
50     pub fn basic() -> JsonEmitter {
51         JsonEmitter::stderr(None, Rc::new(CodeMap::new()))
52     }
53
54     pub fn new(dst: Box<Write + Send>,
55                registry: Option<Registry>,
56                code_map: Rc<CodeMap>) -> JsonEmitter {
57         JsonEmitter {
58             dst: dst,
59             registry: registry,
60             cm: code_map,
61         }
62     }
63 }
64
65 impl Emitter for JsonEmitter {
66     fn emit(&mut self, db: &DiagnosticBuilder) {
67         let data = Diagnostic::from_diagnostic_builder(db, self);
68         if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
69             panic!("failed to print diagnostics: {:?}", e);
70         }
71     }
72 }
73
74 // The following data types are provided just for serialisation.
75
76 #[derive(RustcEncodable)]
77 struct Diagnostic {
78     /// The primary error message.
79     message: String,
80     code: Option<DiagnosticCode>,
81     /// "error: internal compiler error", "error", "warning", "note", "help".
82     level: &'static str,
83     spans: Vec<DiagnosticSpan>,
84     /// Associated diagnostic messages.
85     children: Vec<Diagnostic>,
86     /// The message as rustc would render it. Currently this is only
87     /// `Some` for "suggestions", but eventually it will include all
88     /// snippets.
89     rendered: Option<String>,
90 }
91
92 #[derive(RustcEncodable)]
93 struct DiagnosticSpan {
94     file_name: String,
95     byte_start: u32,
96     byte_end: u32,
97     /// 1-based.
98     line_start: usize,
99     line_end: usize,
100     /// 1-based, character offset.
101     column_start: usize,
102     column_end: usize,
103     /// Is this a "primary" span -- meaning the point, or one of the points,
104     /// where the error occurred?
105     is_primary: bool,
106     /// Source text from the start of line_start to the end of line_end.
107     text: Vec<DiagnosticSpanLine>,
108     /// Label that should be placed at this location (if any)
109     label: Option<String>,
110     /// If we are suggesting a replacement, this will contain text
111     /// that should be sliced in atop this span. You may prefer to
112     /// load the fully rendered version from the parent `Diagnostic`,
113     /// however.
114     suggested_replacement: Option<String>,
115     /// Macro invocations that created the code at this span, if any.
116     expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
117 }
118
119 #[derive(RustcEncodable)]
120 struct DiagnosticSpanLine {
121     text: String,
122
123     /// 1-based, character offset in self.text.
124     highlight_start: usize,
125
126     highlight_end: usize,
127 }
128
129 #[derive(RustcEncodable)]
130 struct DiagnosticSpanMacroExpansion {
131     /// span where macro was applied to generate this code; note that
132     /// this may itself derive from a macro (if
133     /// `span.expansion.is_some()`)
134     span: DiagnosticSpan,
135
136     /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
137     macro_decl_name: String,
138
139     /// span where macro was defined (if known)
140     def_site_span: Option<DiagnosticSpan>,
141 }
142
143 #[derive(RustcEncodable)]
144 struct DiagnosticCode {
145     /// The code itself.
146     code: String,
147     /// An explanation for the code.
148     explanation: Option<&'static str>,
149 }
150
151 impl Diagnostic {
152     fn from_diagnostic_builder(db: &DiagnosticBuilder,
153                                je: &JsonEmitter)
154                                -> Diagnostic {
155         Diagnostic {
156             message: db.message(),
157             code: DiagnosticCode::map_opt_string(db.code.clone(), je),
158             level: db.level.to_str(),
159             spans: DiagnosticSpan::from_multispan(&db.span, je),
160             children: db.children.iter().map(|c| {
161                 Diagnostic::from_sub_diagnostic(c, je)
162             }).collect(),
163             rendered: None,
164         }
165     }
166
167     fn from_sub_diagnostic(db: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
168         Diagnostic {
169             message: db.message(),
170             code: None,
171             level: db.level.to_str(),
172             spans: db.render_span.as_ref()
173                      .map(|sp| DiagnosticSpan::from_render_span(sp, je))
174                      .unwrap_or_else(|| DiagnosticSpan::from_multispan(&db.span, je)),
175             children: vec![],
176             rendered: db.render_span.as_ref()
177                                     .and_then(|rsp| je.render(rsp)),
178         }
179     }
180 }
181
182 impl DiagnosticSpan {
183     fn from_span_label(span: SpanLabel,
184                        suggestion: Option<&String>,
185                        je: &JsonEmitter)
186                        -> DiagnosticSpan {
187         Self::from_span_etc(span.span,
188                             span.is_primary,
189                             span.label,
190                             suggestion,
191                             je)
192     }
193
194     fn from_span_etc(span: Span,
195                      is_primary: bool,
196                      label: Option<String>,
197                      suggestion: Option<&String>,
198                      je: &JsonEmitter)
199                      -> DiagnosticSpan {
200         // obtain the full backtrace from the `macro_backtrace`
201         // helper; in some ways, it'd be better to expand the
202         // backtrace ourselves, but the `macro_backtrace` helper makes
203         // some decision, such as dropping some frames, and I don't
204         // want to duplicate that logic here.
205         let backtrace = span.macro_backtrace().into_iter();
206         DiagnosticSpan::from_span_full(span,
207                                        is_primary,
208                                        label,
209                                        suggestion,
210                                        backtrace,
211                                        je)
212     }
213
214     fn from_span_full(span: Span,
215                       is_primary: bool,
216                       label: Option<String>,
217                       suggestion: Option<&String>,
218                       mut backtrace: vec::IntoIter<MacroBacktrace>,
219                       je: &JsonEmitter)
220                       -> DiagnosticSpan {
221         let start = je.cm.lookup_char_pos(span.lo);
222         let end = je.cm.lookup_char_pos(span.hi);
223         let backtrace_step = backtrace.next().map(|bt| {
224             let call_site =
225                 Self::from_span_full(bt.call_site,
226                                      false,
227                                      None,
228                                      None,
229                                      backtrace,
230                                      je);
231             let def_site_span = bt.def_site_span.map(|sp| {
232                 Self::from_span_full(sp,
233                                      false,
234                                      None,
235                                      None,
236                                      vec![].into_iter(),
237                                      je)
238             });
239             Box::new(DiagnosticSpanMacroExpansion {
240                 span: call_site,
241                 macro_decl_name: bt.macro_decl_name,
242                 def_site_span: def_site_span,
243             })
244         });
245         DiagnosticSpan {
246             file_name: start.file.name.clone(),
247             byte_start: span.lo.0,
248             byte_end: span.hi.0,
249             line_start: start.line,
250             line_end: end.line,
251             column_start: start.col.0 + 1,
252             column_end: end.col.0 + 1,
253             is_primary: is_primary,
254             text: DiagnosticSpanLine::from_span(span, je),
255             suggested_replacement: suggestion.cloned(),
256             expansion: backtrace_step,
257             label: label,
258         }
259     }
260
261     fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
262         msp.span_labels()
263            .into_iter()
264            .map(|span_str| Self::from_span_label(span_str, None, je))
265            .collect()
266     }
267
268     fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
269                        -> Vec<DiagnosticSpan> {
270         assert_eq!(suggestion.msp.span_labels().len(), suggestion.substitutes.len());
271         suggestion.msp.span_labels()
272                       .into_iter()
273                       .zip(&suggestion.substitutes)
274                       .map(|(span_label, suggestion)| {
275                           DiagnosticSpan::from_span_label(span_label,
276                                                           Some(suggestion),
277                                                           je)
278                       })
279                       .collect()
280     }
281
282     fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
283         match *rsp {
284             RenderSpan::FullSpan(ref msp) =>
285                 DiagnosticSpan::from_multispan(msp, je),
286             RenderSpan::Suggestion(ref suggestion) =>
287                 DiagnosticSpan::from_suggestion(suggestion, je),
288         }
289     }
290 }
291
292 impl DiagnosticSpanLine {
293     fn line_from_filemap(fm: &syntax_pos::FileMap,
294                          index: usize,
295                          h_start: usize,
296                          h_end: usize)
297                          -> DiagnosticSpanLine {
298         DiagnosticSpanLine {
299             text: fm.get_line(index).unwrap_or("").to_owned(),
300             highlight_start: h_start,
301             highlight_end: h_end,
302         }
303     }
304
305     /// Create a list of DiagnosticSpanLines from span - each line with any part
306     /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
307     /// `span` within the line.
308     fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
309         je.cm.span_to_lines(span)
310              .map(|lines| {
311                  let fm = &*lines.file;
312                  lines.lines
313                       .iter()
314                       .map(|line| {
315                           DiagnosticSpanLine::line_from_filemap(fm,
316                                                                 line.line_index,
317                                                                 line.start_col.0 + 1,
318                                                                 line.end_col.0 + 1)
319                       })
320                      .collect()
321              })
322             .unwrap_or(vec![])
323     }
324 }
325
326 impl DiagnosticCode {
327     fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
328         s.map(|s| {
329
330             let explanation = je.registry
331                                 .as_ref()
332                                 .and_then(|registry| registry.find_description(&s));
333
334             DiagnosticCode {
335                 code: s,
336                 explanation: explanation,
337             }
338         })
339     }
340 }
341
342 impl JsonEmitter {
343     fn render(&self, render_span: &RenderSpan) -> Option<String> {
344         use std::borrow::Borrow;
345
346         match *render_span {
347             RenderSpan::FullSpan(_) => {
348                 None
349             }
350             RenderSpan::Suggestion(ref suggestion) => {
351                 Some(suggestion.splice_lines(self.cm.borrow()))
352             }
353         }
354     }
355 }
356