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