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