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.
22 use codemap::{CodeMap, FilePathMapping};
23 use syntax_pos::{self, MacroBacktrace, Span, SpanLabel, MultiSpan};
24 use errors::registry::Registry;
25 use errors::{Level, DiagnosticBuilder, SubDiagnostic, RenderSpan, CodeSuggestion, CodeMapper};
26 use errors::emitter::Emitter;
27 use errors::snippet::Style;
30 use std::io::{self, Write};
33 use rustc_serialize::json::as_json;
35 pub struct JsonEmitter {
36 dst: Box<Write + Send>,
37 registry: Option<Registry>,
38 cm: Rc<CodeMapper + 'static>,
42 pub fn stderr(registry: Option<Registry>,
43 code_map: Rc<CodeMap>) -> JsonEmitter {
45 dst: Box::new(io::stderr()),
51 pub fn basic() -> JsonEmitter {
52 let file_path_mapping = FilePathMapping::empty();
53 JsonEmitter::stderr(None, Rc::new(CodeMap::new(file_path_mapping)))
56 pub fn new(dst: Box<Write + Send>,
57 registry: Option<Registry>,
58 code_map: Rc<CodeMap>) -> JsonEmitter {
67 impl Emitter for JsonEmitter {
68 fn emit(&mut self, db: &DiagnosticBuilder) {
69 let data = Diagnostic::from_diagnostic_builder(db, self);
70 if let Err(e) = writeln!(&mut self.dst, "{}", as_json(&data)) {
71 panic!("failed to print diagnostics: {:?}", e);
76 // The following data types are provided just for serialisation.
78 #[derive(RustcEncodable)]
80 /// The primary error message.
82 code: Option<DiagnosticCode>,
83 /// "error: internal compiler error", "error", "warning", "note", "help".
85 spans: Vec<DiagnosticSpan>,
86 /// Associated diagnostic messages.
87 children: Vec<Diagnostic>,
88 /// The message as rustc would render it. Currently this is only
89 /// `Some` for "suggestions", but eventually it will include all
91 rendered: Option<String>,
94 #[derive(RustcEncodable)]
95 struct DiagnosticSpan {
102 /// 1-based, character offset.
105 /// Is this a "primary" span -- meaning the point, or one of the points,
106 /// where the error occurred?
108 /// Source text from the start of line_start to the end of line_end.
109 text: Vec<DiagnosticSpanLine>,
110 /// Label that should be placed at this location (if any)
111 label: Option<String>,
112 /// If we are suggesting a replacement, this will contain text
113 /// that should be sliced in atop this span. You may prefer to
114 /// load the fully rendered version from the parent `Diagnostic`,
116 suggested_replacement: Option<String>,
117 /// Macro invocations that created the code at this span, if any.
118 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
121 #[derive(RustcEncodable)]
122 struct DiagnosticSpanLine {
125 /// 1-based, character offset in self.text.
126 highlight_start: usize,
128 highlight_end: usize,
131 #[derive(RustcEncodable)]
132 struct DiagnosticSpanMacroExpansion {
133 /// span where macro was applied to generate this code; note that
134 /// this may itself derive from a macro (if
135 /// `span.expansion.is_some()`)
136 span: DiagnosticSpan,
138 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
139 macro_decl_name: String,
141 /// span where macro was defined (if known)
142 def_site_span: Option<DiagnosticSpan>,
145 #[derive(RustcEncodable)]
146 struct DiagnosticCode {
149 /// An explanation for the code.
150 explanation: Option<&'static str>,
154 fn from_diagnostic_builder(db: &DiagnosticBuilder,
157 let sugg = db.suggestion.as_ref().map(|sugg| {
160 message: vec![(sugg.msg.clone(), Style::NoStyle)],
161 span: MultiSpan::new(),
162 render_span: Some(RenderSpan::Suggestion(sugg.clone())),
165 let sugg = sugg.as_ref();
167 message: db.message(),
168 code: DiagnosticCode::map_opt_string(db.code.clone(), je),
169 level: db.level.to_str(),
170 spans: DiagnosticSpan::from_multispan(&db.span, je),
171 children: db.children.iter().chain(sugg).map(|c| {
172 Diagnostic::from_sub_diagnostic(c, je)
178 fn from_sub_diagnostic(db: &SubDiagnostic, je: &JsonEmitter) -> Diagnostic {
180 message: db.message(),
182 level: db.level.to_str(),
183 spans: db.render_span.as_ref()
184 .map(|sp| DiagnosticSpan::from_render_span(sp, je))
185 .unwrap_or_else(|| DiagnosticSpan::from_multispan(&db.span, je)),
187 rendered: db.render_span.as_ref()
188 .and_then(|rsp| je.render(rsp)),
193 impl DiagnosticSpan {
194 fn from_span_label(span: SpanLabel,
195 suggestion: Option<&String>,
198 Self::from_span_etc(span.span,
205 fn from_span_etc(span: Span,
207 label: Option<String>,
208 suggestion: Option<&String>,
211 // obtain the full backtrace from the `macro_backtrace`
212 // helper; in some ways, it'd be better to expand the
213 // backtrace ourselves, but the `macro_backtrace` helper makes
214 // some decision, such as dropping some frames, and I don't
215 // want to duplicate that logic here.
216 let backtrace = span.macro_backtrace().into_iter();
217 DiagnosticSpan::from_span_full(span,
225 fn from_span_full(span: Span,
227 label: Option<String>,
228 suggestion: Option<&String>,
229 mut backtrace: vec::IntoIter<MacroBacktrace>,
232 let start = je.cm.lookup_char_pos(span.lo);
233 let end = je.cm.lookup_char_pos(span.hi);
234 let backtrace_step = backtrace.next().map(|bt| {
236 Self::from_span_full(bt.call_site,
242 let def_site_span = bt.def_site_span.map(|sp| {
243 Self::from_span_full(sp,
250 Box::new(DiagnosticSpanMacroExpansion {
252 macro_decl_name: bt.macro_decl_name,
253 def_site_span: def_site_span,
257 file_name: start.file.name.clone(),
258 byte_start: span.lo.0,
260 line_start: start.line,
262 column_start: start.col.0 + 1,
263 column_end: end.col.0 + 1,
264 is_primary: is_primary,
265 text: DiagnosticSpanLine::from_span(span, je),
266 suggested_replacement: suggestion.cloned(),
267 expansion: backtrace_step,
272 fn from_multispan(msp: &MultiSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
275 .map(|span_str| Self::from_span_label(span_str, None, je))
279 fn from_suggestion(suggestion: &CodeSuggestion, je: &JsonEmitter)
280 -> Vec<DiagnosticSpan> {
281 assert_eq!(suggestion.msp.span_labels().len(), suggestion.substitutes.len());
282 suggestion.msp.span_labels()
284 .zip(&suggestion.substitutes)
285 .map(|(span_label, suggestion)| {
286 DiagnosticSpan::from_span_label(span_label,
293 fn from_render_span(rsp: &RenderSpan, je: &JsonEmitter) -> Vec<DiagnosticSpan> {
295 RenderSpan::FullSpan(ref msp) =>
296 DiagnosticSpan::from_multispan(msp, je),
297 RenderSpan::Suggestion(ref suggestion) =>
298 DiagnosticSpan::from_suggestion(suggestion, je),
303 impl DiagnosticSpanLine {
304 fn line_from_filemap(fm: &syntax_pos::FileMap,
308 -> DiagnosticSpanLine {
310 text: fm.get_line(index).unwrap_or("").to_owned(),
311 highlight_start: h_start,
312 highlight_end: h_end,
316 /// Create a list of DiagnosticSpanLines from span - each line with any part
317 /// of `span` gets a DiagnosticSpanLine, with the highlight indicating the
318 /// `span` within the line.
319 fn from_span(span: Span, je: &JsonEmitter) -> Vec<DiagnosticSpanLine> {
320 je.cm.span_to_lines(span)
322 let fm = &*lines.file;
326 DiagnosticSpanLine::line_from_filemap(fm,
328 line.start_col.0 + 1,
333 .unwrap_or_else(|_| vec![])
337 impl DiagnosticCode {
338 fn map_opt_string(s: Option<String>, je: &JsonEmitter) -> Option<DiagnosticCode> {
341 let explanation = je.registry
343 .and_then(|registry| registry.find_description(&s));
347 explanation: explanation,
354 fn render(&self, render_span: &RenderSpan) -> Option<String> {
355 use std::borrow::Borrow;
358 RenderSpan::FullSpan(_) => {
361 RenderSpan::Suggestion(ref suggestion) => {
362 Some(suggestion.splice_lines(self.cm.borrow()))