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