]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/json.rs
Auto merge of #50234 - cramertj:extend, r=alexcrichton
[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 serializing 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::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, CodeMapper};
26 use errors::{DiagnosticId, Applicability};
27 use errors::emitter::{Emitter, EmitterWriter};
28
29 use rustc_data_structures::sync::{self, Lrc};
30 use std::io::{self, Write};
31 use std::vec;
32 use std::sync::{Arc, Mutex};
33
34 use rustc_serialize::json::{as_json, as_pretty_json};
35
36 pub struct JsonEmitter {
37     dst: Box<Write + Send>,
38     registry: Option<Registry>,
39     cm: Lrc<CodeMapper + sync::Send + sync::Sync>,
40     pretty: bool,
41     /// Whether "approximate suggestions" are enabled in the config
42     suggestion_applicability: bool,
43     ui_testing: bool,
44 }
45
46 impl JsonEmitter {
47     pub fn stderr(registry: Option<Registry>,
48                   code_map: Lrc<CodeMap>,
49                   pretty: bool,
50                   suggestion_applicability: bool) -> JsonEmitter {
51         JsonEmitter {
52             dst: Box::new(io::stderr()),
53             registry,
54             cm: code_map,
55             pretty,
56             suggestion_applicability,
57             ui_testing: false,
58         }
59     }
60
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)),
64                             pretty, false)
65     }
66
67     pub fn new(dst: Box<Write + Send>,
68                registry: Option<Registry>,
69                code_map: Lrc<CodeMap>,
70                pretty: bool,
71                suggestion_applicability: bool) -> JsonEmitter {
72         JsonEmitter {
73             dst,
74             registry,
75             cm: code_map,
76             pretty,
77             suggestion_applicability,
78             ui_testing: false,
79         }
80     }
81
82     pub fn ui_testing(self, ui_testing: bool) -> Self {
83         Self { ui_testing, ..self }
84     }
85 }
86
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))
92         } else {
93             writeln!(&mut self.dst, "{}", as_json(&data))
94         };
95         if let Err(e) = result {
96             panic!("failed to print diagnostics: {:?}", e);
97         }
98     }
99 }
100
101 // The following data types are provided just for serialisation.
102
103 #[derive(RustcEncodable)]
104 struct Diagnostic {
105     /// The primary error message.
106     message: String,
107     code: Option<DiagnosticCode>,
108     /// "error: internal compiler error", "error", "warning", "note", "help".
109     level: &'static str,
110     spans: Vec<DiagnosticSpan>,
111     /// Associated diagnostic messages.
112     children: Vec<Diagnostic>,
113     /// The message as rustc would render it.
114     rendered: Option<String>,
115 }
116
117 #[derive(RustcEncodable)]
118 #[allow(unused_attributes)]
119 struct DiagnosticSpan {
120     file_name: String,
121     byte_start: u32,
122     byte_end: u32,
123     /// 1-based.
124     line_start: usize,
125     line_end: usize,
126     /// 1-based, character offset.
127     column_start: usize,
128     column_end: usize,
129     /// Is this a "primary" span -- meaning the point, or one of the points,
130     /// where the error occurred?
131     is_primary: bool,
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_applicability: Option<Applicability>,
142     /// Macro invocations that created the code at this span, if any.
143     expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
144 }
145
146 #[derive(RustcEncodable)]
147 struct DiagnosticSpanLine {
148     text: String,
149
150     /// 1-based, character offset in self.text.
151     highlight_start: usize,
152
153     highlight_end: usize,
154 }
155
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,
162
163     /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
164     macro_decl_name: String,
165
166     /// span where macro was defined (if known)
167     def_site_span: Option<DiagnosticSpan>,
168 }
169
170 #[derive(RustcEncodable)]
171 struct DiagnosticCode {
172     /// The code itself.
173     code: String,
174     /// An explanation for the code.
175     explanation: Option<&'static str>,
176 }
177
178 impl Diagnostic {
179     fn from_diagnostic_builder(db: &DiagnosticBuilder,
180                                je: &JsonEmitter)
181                                -> Diagnostic {
182         let sugg = db.suggestions.iter().map(|sugg| {
183             Diagnostic {
184                 message: sugg.msg.clone(),
185                 code: None,
186                 level: "help",
187                 spans: DiagnosticSpan::from_suggestion(sugg, je),
188                 children: vec![],
189                 rendered: None,
190             }
191         });
192
193         // generate regular command line output and store it in the json
194
195         // A threadsafe buffer for writing.
196         #[derive(Default, Clone)]
197         struct BufWriter(Arc<Mutex<Vec<u8>>>);
198
199         impl Write for BufWriter {
200             fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
201                 self.0.lock().unwrap().write(buf)
202             }
203             fn flush(&mut self) -> io::Result<()> {
204                 self.0.lock().unwrap().flush()
205             }
206         }
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();
213
214         Diagnostic {
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),
223         }
224     }
225
226     fn from_sub_diagnostic(db: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
227         Diagnostic {
228             message: db.message(),
229             code: None,
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)),
234             children: vec![],
235             rendered: None,
236         }
237     }
238 }
239
240 impl DiagnosticSpan {
241     fn from_span_label(span: SpanLabel,
242                        suggestion: Option<(&String, Applicability)>,
243                        je: &JsonEmitter)
244                        -> DiagnosticSpan {
245         Self::from_span_etc(span.span,
246                             span.is_primary,
247                             span.label,
248                             suggestion,
249                             je)
250     }
251
252     fn from_span_etc(span: Span,
253                      is_primary: bool,
254                      label: Option<String>,
255                      suggestion: Option<(&String, Applicability)>,
256                      je: &JsonEmitter)
257                      -> DiagnosticSpan {
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,
265                                        is_primary,
266                                        label,
267                                        suggestion,
268                                        backtrace,
269                                        je)
270     }
271
272     fn from_span_full(span: Span,
273                       is_primary: bool,
274                       label: Option<String>,
275                       suggestion: Option<(&String, Applicability)>,
276                       mut backtrace: vec::IntoIter<MacroBacktrace>,
277                       je: &JsonEmitter)
278                       -> DiagnosticSpan {
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| {
282             let call_site =
283                 Self::from_span_full(bt.call_site,
284                                      false,
285                                      None,
286                                      None,
287                                      backtrace,
288                                      je);
289             let def_site_span = bt.def_site_span.map(|sp| {
290                 Self::from_span_full(sp,
291                                      false,
292                                      None,
293                                      None,
294                                      vec![].into_iter(),
295                                      je)
296             });
297             Box::new(DiagnosticSpanMacroExpansion {
298                 span: call_site,
299                 macro_decl_name: bt.macro_decl_name,
300                 def_site_span,
301             })
302         });
303
304         let suggestion_applicability = if je.suggestion_applicability {
305              suggestion.map(|x| x.1)
306         } else {
307             None
308         };
309
310         DiagnosticSpan {
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,
315             line_end: end.line,
316             column_start: start.col.0 + 1,
317             column_end: end.col.0 + 1,
318             is_primary,
319             text: DiagnosticSpanLine::from_span(span, je),
320             suggested_replacement: suggestion.map(|x| x.0.clone()),
321             suggestion_applicability,
322             expansion: backtrace_step,
323             label,
324         }
325     }
326
327     fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
328         msp.span_labels()
329            .into_iter()
330            .map(|span_str| Self::from_span_label(span_str, None, je))
331            .collect()
332     }
333
334     fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
335                        -> Vec<DiagnosticSpan> {
336         suggestion.substitutions
337                       .iter()
338                       .flat_map(|substitution| {
339                           substitution.parts.iter().map(move |suggestion_inner| {
340                               let span_label = SpanLabel {
341                                   span: suggestion_inner.span,
342                                   is_primary: true,
343                                   label: None,
344                               };
345                               DiagnosticSpan::from_span_label(span_label,
346                                                               Some((&suggestion_inner.snippet,
347                                                                    suggestion.applicability)),
348                                                               je)
349                           })
350                       })
351                       .collect()
352     }
353 }
354
355 impl DiagnosticSpanLine {
356     fn line_from_filemap(fm: &syntax_pos::FileMap,
357                          index: usize,
358                          h_start: usize,
359                          h_end: usize)
360                          -> DiagnosticSpanLine {
361         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,
365         }
366     }
367
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)
373              .map(|lines| {
374                  let fm = &*lines.file;
375                  lines.lines
376                       .iter()
377                       .map(|line| {
378                           DiagnosticSpanLine::line_from_filemap(fm,
379                                                                 line.line_index,
380                                                                 line.start_col.0 + 1,
381                                                                 line.end_col.0 + 1)
382                       })
383                      .collect()
384              })
385             .unwrap_or_else(|_| vec![])
386     }
387 }
388
389 impl DiagnosticCode {
390     fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
391         s.map(|s| {
392             let s = match s {
393                 DiagnosticId::Error(s) => s,
394                 DiagnosticId::Lint(s) => s,
395             };
396             let explanation = je.registry
397                                 .as_ref()
398                                 .and_then(|registry| registry.find_description(&s));
399
400             DiagnosticCode {
401                 code: s,
402                 explanation,
403             }
404         })
405     }
406 }