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