]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/json.rs
c19b408442ad12b16b224dbd3ab4f36201c0d6f9
[rust.git] / src / libsyntax / json.rs
1 //! A JSON emitter for errors.
2 //!
3 //! This works by converting errors to a simplified structural format (see the
4 //! structs at the start of the file) and then serializing them. These should
5 //! contain as much information about the error as possible.
6 //!
7 //! The format of the JSON output should be considered *unstable*. For now the
8 //! structs at the end of this file (Diagnostic*) specify the error format.
9
10 // FIXME: spec the JSON output properly.
11
12 use crate::source_map::{SourceMap, FilePathMapping};
13
14 use errors::registry::Registry;
15 use errors::{DiagnosticBuilder, SubDiagnostic, CodeSuggestion, SourceMapper};
16 use errors::{DiagnosticId, Applicability};
17 use errors::emitter::{Emitter, HumanReadableErrorType};
18
19 use syntax_pos::{MacroBacktrace, Span, SpanLabel, MultiSpan};
20 use rustc_data_structures::sync::{self, Lrc};
21 use std::io::{self, Write};
22 use std::vec;
23 use std::sync::{Arc, Mutex};
24
25 use rustc_serialize::json::{as_json, as_pretty_json};
26
27 pub struct JsonEmitter {
28     dst: Box<dyn Write + Send>,
29     registry: Option<Registry>,
30     sm: Lrc<dyn SourceMapper + sync::Send + sync::Sync>,
31     pretty: bool,
32     ui_testing: bool,
33     json_rendered: HumanReadableErrorType,
34 }
35
36 impl JsonEmitter {
37     pub fn stderr(
38         registry: Option<Registry>,
39         source_map: Lrc<SourceMap>,
40         pretty: bool,
41         json_rendered: HumanReadableErrorType,
42     ) -> JsonEmitter {
43         JsonEmitter {
44             dst: Box::new(io::stderr()),
45             registry,
46             sm: source_map,
47             pretty,
48             ui_testing: false,
49             json_rendered,
50         }
51     }
52
53     pub fn basic(pretty: bool, json_rendered: HumanReadableErrorType) -> JsonEmitter {
54         let file_path_mapping = FilePathMapping::empty();
55         JsonEmitter::stderr(None, Lrc::new(SourceMap::new(file_path_mapping)),
56                             pretty, json_rendered)
57     }
58
59     pub fn new(
60         dst: Box<dyn Write + Send>,
61         registry: Option<Registry>,
62         source_map: Lrc<SourceMap>,
63         pretty: bool,
64         json_rendered: HumanReadableErrorType,
65     ) -> JsonEmitter {
66         JsonEmitter {
67             dst,
68             registry,
69             sm: source_map,
70             pretty,
71             ui_testing: false,
72             json_rendered,
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         je.json_rendered.new_emitter(Box::new(buf), Some(je.sm.clone()), 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.sm.lookup_char_pos(span.lo());
273         let end = je.sm.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_source_file(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     /// Creates 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.sm.span_to_lines(span)
360             .map(|lines| {
361                 let fm = &*lines.file;
362                 lines.lines
363                     .iter()
364                     .map(|line| DiagnosticSpanLine::line_from_source_file(
365                         fm,
366                         line.line_index,
367                         line.start_col.0 + 1,
368                         line.end_col.0 + 1,
369                     )).collect()
370             }).unwrap_or_else(|_| vec![])
371     }
372 }
373
374 impl DiagnosticCode {
375     fn map_opt_string(s: Option<DiagnosticId>, je: &JsonEmitter) -> Option<DiagnosticCode> {
376         s.map(|s| {
377             let s = match s {
378                 DiagnosticId::Error(s) => s,
379                 DiagnosticId::Lint(s) => s,
380             };
381             let explanation = je.registry
382                                 .as_ref()
383                                 .and_then(|registry| registry.find_description(&s));
384
385             DiagnosticCode {
386                 code: s,
387                 explanation,
388             }
389         })
390     }
391 }