]> git.proxmox.com Git - rustc.git/blob - src/tools/compiletest/src/json.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / tools / compiletest / src / json.rs
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.
4 //
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.
10
11 use errors::{Error, ErrorKind};
12 use rustc_serialize::json;
13 use std::str::FromStr;
14 use std::path::Path;
15 use runtest::ProcRes;
16
17 // These structs are a subset of the ones found in
18 // `syntax::json`.
19
20 #[derive(RustcEncodable, RustcDecodable)]
21 struct Diagnostic {
22 message: String,
23 code: Option<DiagnosticCode>,
24 level: String,
25 spans: Vec<DiagnosticSpan>,
26 children: Vec<Diagnostic>,
27 rendered: Option<String>,
28 }
29
30 #[derive(RustcEncodable, RustcDecodable, Clone)]
31 struct DiagnosticSpan {
32 file_name: String,
33 line_start: usize,
34 line_end: usize,
35 column_start: usize,
36 column_end: usize,
37 is_primary: bool,
38 label: Option<String>,
39 suggested_replacement: Option<String>,
40 expansion: Option<Box<DiagnosticSpanMacroExpansion>>,
41 }
42
43 #[derive(RustcEncodable, RustcDecodable, Clone)]
44 struct DiagnosticSpanMacroExpansion {
45 /// span where macro was applied to generate this code
46 span: DiagnosticSpan,
47
48 /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
49 macro_decl_name: String,
50 }
51
52 #[derive(RustcEncodable, RustcDecodable, Clone)]
53 struct DiagnosticCode {
54 /// The code itself.
55 code: String,
56 /// An explanation for the code.
57 explanation: Option<String>,
58 }
59
60 pub fn parse_output(file_name: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
61 output.lines()
62 .flat_map(|line| parse_line(file_name, line, output, proc_res))
63 .collect()
64 }
65
66 fn parse_line(file_name: &str, line: &str, output: &str, proc_res: &ProcRes) -> Vec<Error> {
67 // The compiler sometimes intermingles non-JSON stuff into the
68 // output. This hack just skips over such lines. Yuck.
69 if line.starts_with('{') {
70 match json::decode::<Diagnostic>(line) {
71 Ok(diagnostic) => {
72 let mut expected_errors = vec![];
73 push_expected_errors(&mut expected_errors, &diagnostic, &[], file_name);
74 expected_errors
75 }
76 Err(error) => {
77 proc_res.fatal(Some(&format!("failed to decode compiler output as json: \
78 `{}`\noutput: {}\nline: {}",
79 error,
80 line,
81 output)));
82 }
83 }
84 } else {
85 vec![]
86 }
87 }
88
89 fn push_expected_errors(expected_errors: &mut Vec<Error>,
90 diagnostic: &Diagnostic,
91 default_spans: &[&DiagnosticSpan],
92 file_name: &str) {
93 let spans_in_this_file: Vec<_> = diagnostic.spans
94 .iter()
95 .filter(|span| Path::new(&span.file_name) == Path::new(&file_name))
96 .collect();
97
98 let primary_spans: Vec<_> = spans_in_this_file.iter()
99 .cloned()
100 .filter(|span| span.is_primary)
101 .take(1) // sometimes we have more than one showing up in the json; pick first
102 .collect();
103 let primary_spans = if primary_spans.is_empty() {
104 // subdiagnostics often don't have a span of their own;
105 // inherit the span from the parent in that case
106 default_spans
107 } else {
108 &primary_spans
109 };
110
111 // We break the output into multiple lines, and then append the
112 // [E123] to every line in the output. This may be overkill. The
113 // intention was to match existing tests that do things like "//|
114 // found `i32` [E123]" and expect to match that somewhere, and yet
115 // also ensure that `//~ ERROR E123` *always* works. The
116 // assumption is that these multi-line error messages are on their
117 // way out anyhow.
118 let with_code = |span: &DiagnosticSpan, text: &str| {
119 match diagnostic.code {
120 Some(ref code) =>
121 // FIXME(#33000) -- it'd be better to use a dedicated
122 // UI harness than to include the line/col number like
123 // this, but some current tests rely on it.
124 //
125 // Note: Do NOT include the filename. These can easily
126 // cause false matches where the expected message
127 // appears in the filename, and hence the message
128 // changes but the test still passes.
129 format!("{}:{}: {}:{}: {} [{}]",
130 span.line_start, span.column_start,
131 span.line_end, span.column_end,
132 text, code.code.clone()),
133 None =>
134 // FIXME(#33000) -- it'd be better to use a dedicated UI harness
135 format!("{}:{}: {}:{}: {}",
136 span.line_start, span.column_start,
137 span.line_end, span.column_end,
138 text),
139 }
140 };
141
142 // Convert multi-line messages into multiple expected
143 // errors. We expect to replace these with something
144 // more structured shortly anyhow.
145 let mut message_lines = diagnostic.message.lines();
146 if let Some(first_line) = message_lines.next() {
147 for span in primary_spans {
148 let msg = with_code(span, first_line);
149 let kind = ErrorKind::from_str(&diagnostic.level).ok();
150 expected_errors.push(Error {
151 line_num: span.line_start,
152 kind,
153 msg,
154 });
155 }
156 }
157 for next_line in message_lines {
158 for span in primary_spans {
159 expected_errors.push(Error {
160 line_num: span.line_start,
161 kind: None,
162 msg: with_code(span, next_line),
163 });
164 }
165 }
166
167 // If the message has a suggestion, register that.
168 for span in primary_spans {
169 if let Some(ref suggested_replacement) = span.suggested_replacement {
170 for (index, line) in suggested_replacement.lines().enumerate() {
171 expected_errors.push(Error {
172 line_num: span.line_start + index,
173 kind: Some(ErrorKind::Suggestion),
174 msg: line.to_string(),
175 });
176 }
177 }
178 }
179
180 // Add notes for the backtrace
181 for span in primary_spans {
182 for frame in &span.expansion {
183 push_backtrace(expected_errors, frame, file_name);
184 }
185 }
186
187 // Add notes for any labels that appear in the message.
188 for span in spans_in_this_file.iter()
189 .filter(|span| span.label.is_some()) {
190 expected_errors.push(Error {
191 line_num: span.line_start,
192 kind: Some(ErrorKind::Note),
193 msg: span.label.clone().unwrap(),
194 });
195 }
196
197 // Flatten out the children.
198 for child in &diagnostic.children {
199 push_expected_errors(expected_errors, child, primary_spans, file_name);
200 }
201 }
202
203 fn push_backtrace(expected_errors: &mut Vec<Error>,
204 expansion: &DiagnosticSpanMacroExpansion,
205 file_name: &str) {
206 if Path::new(&expansion.span.file_name) == Path::new(&file_name) {
207 expected_errors.push(Error {
208 line_num: expansion.span.line_start,
209 kind: Some(ErrorKind::Note),
210 msg: format!("in this expansion of {}", expansion.macro_decl_name),
211 });
212 }
213
214 for previous_expansion in &expansion.span.expansion {
215 push_backtrace(expected_errors, previous_expansion, file_name);
216 }
217 }