1 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A JSON emitter for errors.
13 //! This works by converting errors to a simplified structural format (see the
14 //! structs at the start of the file) and then serialising them. These should
15 //! contain as much information about the error as possible.
17 //! The format of the JSON output should be considered *unstable*. For now the
18 //! structs at the end of this file (Diagnostic*) specify the error format.
20 // FIXME spec the JSON output properly.
23 use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
24 use errors::registry::Registry;
25 use errors::{DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
26 use errors::emitter::Emitter;
29 use std::io::{self, Write};
32 use rustc_serialize::json::as_json;
34 pub struct JsonEmitter {
35 dst: Box<Write + Send>,
36 registry: Option<Registry>,
37 cm: Rc<CodeMapper + 'static>,
41 pub fn basic() -> JsonEmitter {
42 JsonEmitter::stderr(None, Rc::new(CodeMap::new()))
45 pub fn stderr(registry: Option<Registry>,
46 code_map: Rc<CodeMap>) -> JsonEmitter {
48 dst: Box::new(io::stderr()),
55 impl Emitter for JsonEmitter {
56 fn emit(&mut self, db: &DiagnosticBuilder) {
57 let data = Diagnostic::from_diagnostic_builder(db, self);
58 if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
59 panic!("failed to print diagnostics: {:?}", e);
64 // The following data types are provided just for serialisation.
66 #[derive(RustcEncodable)]
67 struct Diagnostic<'a> {
68 /// The primary error message.
70 code: Option<DiagnosticCode>,
71 /// "error: internal compiler error", "error", "warning", "note", "help".
73 spans: Vec<DiagnosticSpan>,
74 /// Associated diagnostic messages.
75 children: Vec<Diagnostic<'a>>,
76 /// The message as rustc would render it. Currently this is only
77 /// `Some` for "suggestions", but eventually it will include all
79 rendered: Option<String>,
82 #[derive(RustcEncodable)]
83 struct DiagnosticSpan {
90 /// 1-based, character offset.
93 /// Is this a "primary" span -- meaning the point, or one of the points,
94 /// where the error occurred?
96 /// Source text from the start of line_start to the end of line_end.
97 text: Vec<DiagnosticSpanLine>,
98 /// Label that should be placed at this location (if any)
99 label: Option<String>,
100 /// If we are suggesting a replacement, this will contain text
101 /// that should be sliced in atop this span. You may prefer to
102 /// load the fully rendered version from the parent `Diagnostic`,
104 suggested_replacement: Option<String>,
105 /// Macro invocations that created the code at this span, if any.
106 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
109 #[derive(RustcEncodable)]
110 struct DiagnosticSpanLine {
113 /// 1-based, character offset in self.text.
114 highlight_start: usize,
116 highlight_end: usize,
119 #[derive(RustcEncodable)]
120 struct DiagnosticSpanMacroExpansion {
121 /// span where macro was applied to generate this code; note that
122 /// this may itself derive from a macro (if
123 /// `span.expansion.is_some()`)
124 span: DiagnosticSpan,
126 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
127 macro_decl_name: String,
129 /// span where macro was defined (if known)
130 def_site_span: Option<DiagnosticSpan>,
133 #[derive(RustcEncodable)]
134 struct DiagnosticCode {
137 /// An explanation for the code.
138 explanation: Option<&'static str>,
141 impl<'a> Diagnostic<'a> {
142 fn from_diagnostic_builder<'c>(db: &'c DiagnosticBuilder,
146 message: &db.message,
147 code: DiagnosticCode::map_opt_string(db.code.clone(), je),
148 level: db.level.to_str(),
149 spans: DiagnosticSpan::from_multispan(&db.span, je),
150 children: db.children.iter().map(|c| {
151 Diagnostic::from_sub_diagnostic(c, je)
157 fn from_sub_diagnostic<'c>(db: &'c SubDiagnostic, je: &JsonEmitter) -> Diagnostic<'c> {
159 message: &db.message,
161 level: db.level.to_str(),
162 spans: db.render_span.as_ref()
163 .map(|sp| DiagnosticSpan::from_render_span(sp, je))
164 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&db.span, je)),
166 rendered: db.render_span.as_ref()
167 .and_then(|rsp| je.render(rsp)),
172 impl DiagnosticSpan {
173 fn from_span_label(span: SpanLabel,
174 suggestion: Option<&String>,
177 Self::from_span_etc(span.span,
184 fn from_span_etc(span: Span,
186 label: Option<String>,
187 suggestion: Option<&String>,
190 // obtain the full backtrace from the `macro_backtrace`
191 // helper; in some ways, it'd be better to expand the
192 // backtrace ourselves, but the `macro_backtrace` helper makes
193 // some decision, such as dropping some frames, and I don't
194 // want to duplicate that logic here.
195 let backtrace = je.cm.macro_backtrace(span).into_iter();
196 DiagnosticSpan::from_span_full(span,
204 fn from_span_full(span: Span,
206 label: Option<String>,
207 suggestion: Option<&String>,
208 mut backtrace: vec::IntoIter<MacroBacktrace>,
211 let start = je.cm.lookup_char_pos(span.lo);
212 let end = je.cm.lookup_char_pos(span.hi);
213 let backtrace_step = backtrace.next().map(|bt| {
215 Self::from_span_full(bt.call_site,
221 let def_site_span = bt.def_site_span.map(|sp| {
222 Self::from_span_full(sp,
229 Box::new(DiagnosticSpanMacroExpansion {
231 macro_decl_name: bt.macro_decl_name,
232 def_site_span: def_site_span,
236 file_name: start.file.name.clone(),
237 byte_start: span.lo.0,
239 line_start: start.line,
241 column_start: start.col.0 + 1,
242 column_end: end.col.0 + 1,
243 is_primary: is_primary,
244 text: DiagnosticSpanLine::from_span(span, je),
245 suggested_replacement: suggestion.cloned(),
246 expansion: backtrace_step,
251 fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
254 .map(|span_str| Self::from_span_label(span_str, None, je))
258 fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
259 -> Vec<DiagnosticSpan> {
260 assert_eq!(suggestion.msp.span_labels().len(), suggestion.substitutes.len());
261 suggestion.msp.span_labels()
263 .zip(&suggestion.substitutes)
264 .map(|(span_label, suggestion)| {
265 DiagnosticSpan::from_span_label(span_label,
272 fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
274 RenderSpan::FullSpan(ref msp) =>
275 DiagnosticSpan::from_multispan(msp, je),
276 RenderSpan::Suggestion(ref suggestion) =>
277 DiagnosticSpan::from_suggestion(suggestion, je),
282 impl DiagnosticSpanLine {
283 fn line_from_filemap(fm: &syntax_pos::FileMap,
287 -> DiagnosticSpanLine {
289 text: fm.get_line(index).unwrap().to_owned(),
290 highlight_start: h_start,
291 highlight_end: h_end,
295 /// Create a list of DiagnosticSpanLines from span - each line with any part
296 /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
297 /// `span` within the line.
298 fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
299 je.cm.span_to_lines(span)
301 let fm = &*lines.file;
305 DiagnosticSpanLine::line_from_filemap(fm,
307 line.start_col.0 + 1,
316 impl DiagnosticCode {
317 fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
320 let explanation = je.registry
322 .and_then(|registry| registry.find_description(&s));
326 explanation: explanation,
333 fn render(&self, render_span: &RenderSpan) -> Option<String> {
334 use std::borrow::Borrow;
337 RenderSpan::FullSpan(_) => {
340 RenderSpan::Suggestion(ref suggestion) => {
341 Some(suggestion.splice_lines(self.cm.borrow()))