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