2014-05-20 18:15:34 +01:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
2014-10-07 18:30:35 +02:00
|
|
|
use self::WhichLine::*;
|
2012-12-03 16:48:01 -08:00
|
|
|
|
2015-02-26 21:00:43 -08:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufReader;
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::path::Path;
|
2013-10-25 17:04:37 -07:00
|
|
|
|
2014-03-28 11:10:15 -07:00
|
|
|
pub struct ExpectedError {
|
2015-03-25 17:06:52 -07:00
|
|
|
pub line: usize,
|
2014-05-22 16:57:53 -07:00
|
|
|
pub kind: String,
|
|
|
|
pub msg: String,
|
2014-03-28 11:10:15 -07:00
|
|
|
}
|
2012-01-03 21:01:48 -08:00
|
|
|
|
2015-01-28 08:34:18 -05:00
|
|
|
#[derive(PartialEq, Debug)]
|
2015-03-25 17:06:52 -07:00
|
|
|
enum WhichLine { ThisLine, FollowPrevious(usize), AdjustBackward(usize) }
|
2015-01-20 10:45:29 -08:00
|
|
|
|
2014-10-07 18:30:35 +02:00
|
|
|
/// Looks for either "//~| KIND MESSAGE" or "//~^^... KIND MESSAGE"
|
|
|
|
/// The former is a "follow" that inherits its target from the preceding line;
|
|
|
|
/// the latter is an "adjusts" that goes that many lines up.
|
|
|
|
///
|
|
|
|
/// Goal is to enable tests both like: //~^^^ ERROR go up three
|
|
|
|
/// and also //~^ ERROR message one for the preceding line, and
|
|
|
|
/// //~| ERROR message two for that same line.
|
2014-05-20 18:15:34 +01:00
|
|
|
// Load any test directives embedded in the file
|
2015-01-20 10:45:29 -08:00
|
|
|
pub fn load_errors(testfile: &Path) -> Vec<ExpectedError> {
|
2015-02-26 21:00:43 -08:00
|
|
|
let rdr = BufReader::new(File::open(testfile).unwrap());
|
2012-01-03 21:01:48 -08:00
|
|
|
|
2014-10-07 18:30:35 +02:00
|
|
|
// `last_nonfollow_error` tracks the most recently seen
|
|
|
|
// line with an error template that did not use the
|
|
|
|
// follow-syntax, "//~| ...".
|
|
|
|
//
|
|
|
|
// (pnkfelix could not find an easy way to compose Iterator::scan
|
|
|
|
// and Iterator::filter_map to pass along this information into
|
|
|
|
// `parse_expected`. So instead I am storing that state here and
|
|
|
|
// updating it in the map callback below.)
|
|
|
|
let mut last_nonfollow_error = None;
|
|
|
|
|
2014-05-20 18:15:34 +01:00
|
|
|
rdr.lines().enumerate().filter_map(|(line_no, ln)| {
|
2014-10-07 18:30:35 +02:00
|
|
|
parse_expected(last_nonfollow_error,
|
|
|
|
line_no + 1,
|
2015-02-01 21:53:25 -05:00
|
|
|
&ln.unwrap())
|
2014-10-07 18:30:35 +02:00
|
|
|
.map(|(which, error)| {
|
|
|
|
match which {
|
|
|
|
FollowPrevious(_) => {}
|
|
|
|
_ => last_nonfollow_error = Some(error.line),
|
|
|
|
}
|
|
|
|
error
|
|
|
|
})
|
2014-05-20 18:15:34 +01:00
|
|
|
}).collect()
|
|
|
|
}
|
2012-01-03 21:01:48 -08:00
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
fn parse_expected(last_nonfollow_error: Option<usize>,
|
|
|
|
line_num: usize,
|
2015-01-20 10:45:29 -08:00
|
|
|
line: &str) -> Option<(WhichLine, ExpectedError)> {
|
2015-02-19 14:36:58 +01:00
|
|
|
let start = match line.find("//~") { Some(i) => i, None => return None };
|
2015-01-20 10:45:29 -08:00
|
|
|
let (follow, adjusts) = if line.char_at(start + 3) == '|' {
|
|
|
|
(true, 0)
|
|
|
|
} else {
|
|
|
|
(false, line[start + 3..].chars().take_while(|c| *c == '^').count())
|
|
|
|
};
|
|
|
|
let kind_start = start + 3 + adjusts + (follow as usize);
|
|
|
|
let letters = line[kind_start..].chars();
|
|
|
|
let kind = letters.skip_while(|c| c.is_whitespace())
|
|
|
|
.take_while(|c| !c.is_whitespace())
|
2015-03-05 18:23:57 -08:00
|
|
|
.flat_map(|c| c.to_lowercase())
|
2015-01-20 10:45:29 -08:00
|
|
|
.collect::<String>();
|
|
|
|
let letters = line[kind_start..].chars();
|
|
|
|
let msg = letters.skip_while(|c| c.is_whitespace())
|
|
|
|
.skip_while(|c| !c.is_whitespace())
|
|
|
|
.collect::<String>().trim().to_string();
|
2014-10-07 18:30:35 +02:00
|
|
|
|
2015-01-20 10:45:29 -08:00
|
|
|
let (which, line) = if follow {
|
|
|
|
assert!(adjusts == 0, "use either //~| or //~^, not both.");
|
|
|
|
let line = last_nonfollow_error.unwrap_or_else(|| {
|
|
|
|
panic!("encountered //~| without preceding //~^ line.")
|
|
|
|
});
|
|
|
|
(FollowPrevious(line), line)
|
|
|
|
} else {
|
|
|
|
let which =
|
|
|
|
if adjusts > 0 { AdjustBackward(adjusts) } else { ThisLine };
|
|
|
|
let line = line_num - adjusts;
|
|
|
|
(which, line)
|
|
|
|
};
|
2014-05-20 18:15:34 +01:00
|
|
|
|
2015-01-20 10:45:29 -08:00
|
|
|
debug!("line={} which={:?} kind={:?} msg={:?}", line_num, which, kind, msg);
|
|
|
|
Some((which, ExpectedError { line: line,
|
|
|
|
kind: kind,
|
|
|
|
msg: msg, }))
|
2012-01-03 21:01:48 -08:00
|
|
|
}
|