|
| 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 | + |
| 15 | +// These structs are a subset of the ones found in |
| 16 | +// `syntax::errors::json`. |
| 17 | + |
| 18 | +#[derive(RustcEncodable, RustcDecodable)] |
| 19 | +struct Diagnostic { |
| 20 | + message: String, |
| 21 | + code: Option<DiagnosticCode>, |
| 22 | + level: String, |
| 23 | + spans: Vec<DiagnosticSpan>, |
| 24 | + children: Vec<Diagnostic>, |
| 25 | + rendered: Option<String>, |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(RustcEncodable, RustcDecodable, Clone)] |
| 29 | +struct DiagnosticSpan { |
| 30 | + file_name: String, |
| 31 | + line_start: usize, |
| 32 | + line_end: usize, |
| 33 | + column_start: usize, |
| 34 | + column_end: usize, |
| 35 | + expansion: Option<Box<DiagnosticSpanMacroExpansion>>, |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(RustcEncodable, RustcDecodable, Clone)] |
| 39 | +struct DiagnosticSpanMacroExpansion { |
| 40 | + /// span where macro was applied to generate this code |
| 41 | + span: DiagnosticSpan, |
| 42 | + |
| 43 | + /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]") |
| 44 | + macro_decl_name: String, |
| 45 | +} |
| 46 | + |
| 47 | +#[derive(RustcEncodable, RustcDecodable, Clone)] |
| 48 | +struct DiagnosticCode { |
| 49 | + /// The code itself. |
| 50 | + code: String, |
| 51 | + /// An explanation for the code. |
| 52 | + explanation: Option<String>, |
| 53 | +} |
| 54 | + |
| 55 | +pub fn parse_output(file_name: &str, output: &str) -> Vec<Error> { |
| 56 | + output.lines() |
| 57 | + .flat_map(|line| parse_line(file_name, line)) |
| 58 | + .collect() |
| 59 | +} |
| 60 | + |
| 61 | +fn parse_line(file_name: &str, line: &str) -> Vec<Error> { |
| 62 | + // The compiler sometimes intermingles non-JSON stuff into the |
| 63 | + // output. This hack just skips over such lines. Yuck. |
| 64 | + if line.chars().next() == Some('{') { |
| 65 | + match json::decode::<Diagnostic>(line) { |
| 66 | + Ok(diagnostic) => { |
| 67 | + let mut expected_errors = vec![]; |
| 68 | + push_expected_errors(&mut expected_errors, &diagnostic, file_name); |
| 69 | + expected_errors |
| 70 | + } |
| 71 | + Err(error) => { |
| 72 | + println!("failed to decode compiler output as json: `{}`", error); |
| 73 | + panic!("failed to decode compiler output as json"); |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + vec![] |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +fn push_expected_errors(expected_errors: &mut Vec<Error>, |
| 82 | + diagnostic: &Diagnostic, |
| 83 | + file_name: &str) { |
| 84 | + // We only consider messages pertaining to the current file. |
| 85 | + let matching_spans = |
| 86 | + || diagnostic.spans.iter().filter(|span| span.file_name == file_name); |
| 87 | + let with_code = |
| 88 | + |span: &DiagnosticSpan, text: &str| match diagnostic.code { |
| 89 | + Some(ref code) => |
| 90 | + // FIXME(#33000) -- it'd be better to use a dedicated |
| 91 | + // UI harness than to include the line/col number like |
| 92 | + // this, but some current tests rely on it. |
| 93 | + // |
| 94 | + // Note: Do NOT include the filename. These can easily |
| 95 | + // cause false matches where the expected message |
| 96 | + // appears in the filename, and hence the message |
| 97 | + // changes but the test still passes. |
| 98 | + format!("{}:{}: {}:{}: {} [{}]", |
| 99 | + span.line_start, span.column_start, |
| 100 | + span.line_end, span.column_end, |
| 101 | + text, code.code.clone()), |
| 102 | + None => |
| 103 | + // FIXME(#33000) -- it'd be better to use a dedicated UI harness |
| 104 | + format!("{}:{}: {}:{}: {}", |
| 105 | + span.line_start, span.column_start, |
| 106 | + span.line_end, span.column_end, |
| 107 | + text), |
| 108 | + }; |
| 109 | + |
| 110 | + // Convert multi-line messages into multiple expected |
| 111 | + // errors. We expect to replace these with something |
| 112 | + // more structured shortly anyhow. |
| 113 | + let mut message_lines = diagnostic.message.lines(); |
| 114 | + if let Some(first_line) = message_lines.next() { |
| 115 | + for span in matching_spans() { |
| 116 | + let msg = with_code(span, first_line); |
| 117 | + let kind = ErrorKind::from_str(&diagnostic.level).ok(); |
| 118 | + expected_errors.push( |
| 119 | + Error { |
| 120 | + line_num: span.line_start, |
| 121 | + kind: kind, |
| 122 | + msg: msg, |
| 123 | + } |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + for next_line in message_lines { |
| 128 | + for span in matching_spans() { |
| 129 | + expected_errors.push( |
| 130 | + Error { |
| 131 | + line_num: span.line_start, |
| 132 | + kind: None, |
| 133 | + msg: with_code(span, next_line), |
| 134 | + } |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // If the message has a suggestion, register that. |
| 140 | + if let Some(ref rendered) = diagnostic.rendered { |
| 141 | + let start_line = matching_spans().map(|s| s.line_start).min().expect("\ |
| 142 | + every suggestion should have at least one span"); |
| 143 | + for (index, line) in rendered.lines().enumerate() { |
| 144 | + expected_errors.push( |
| 145 | + Error { |
| 146 | + line_num: start_line + index, |
| 147 | + kind: Some(ErrorKind::Suggestion), |
| 148 | + msg: line.to_string() |
| 149 | + } |
| 150 | + ); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + // Add notes for the backtrace |
| 155 | + for span in matching_spans() { |
| 156 | + for frame in &span.expansion { |
| 157 | + push_backtrace(expected_errors, |
| 158 | + frame, |
| 159 | + file_name); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // Flatten out the children. |
| 164 | + for child in &diagnostic.children { |
| 165 | + push_expected_errors(expected_errors, child, file_name); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn push_backtrace(expected_errors: &mut Vec<Error>, |
| 170 | + expansion: &DiagnosticSpanMacroExpansion, |
| 171 | + file_name: &str) { |
| 172 | + if expansion.span.file_name == file_name { |
| 173 | + expected_errors.push( |
| 174 | + Error { |
| 175 | + line_num: expansion.span.line_start, |
| 176 | + kind: Some(ErrorKind::Note), |
| 177 | + msg: format!("in this expansion of {}", expansion.macro_decl_name), |
| 178 | + } |
| 179 | + ); |
| 180 | + } |
| 181 | + |
| 182 | + for previous_expansion in &expansion.span.expansion { |
| 183 | + push_backtrace(expected_errors, previous_expansion, file_name); |
| 184 | + } |
| 185 | +} |
0 commit comments