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