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