hkalbasi_rustc_ap_compiletest/
errors.rs1use self::WhichLine::*;
2
3use std::fmt;
4use std::fs::File;
5use std::io::prelude::*;
6use std::io::BufReader;
7use std::path::Path;
8use std::str::FromStr;
9
10use once_cell::sync::Lazy;
11use regex::Regex;
12use tracing::*;
13
14#[derive(Clone, Debug, PartialEq)]
15pub enum ErrorKind {
16 Help,
17 Error,
18 Note,
19 Suggestion,
20 Warning,
21}
22
23impl FromStr for ErrorKind {
24 type Err = ();
25 fn from_str(s: &str) -> Result<Self, Self::Err> {
26 let s = s.to_uppercase();
27 let part0: &str = s.split(':').next().unwrap();
28 match part0 {
29 "HELP" => Ok(ErrorKind::Help),
30 "ERROR" => Ok(ErrorKind::Error),
31 "NOTE" => Ok(ErrorKind::Note),
32 "SUGGESTION" => Ok(ErrorKind::Suggestion),
33 "WARN" | "WARNING" => Ok(ErrorKind::Warning),
34 _ => Err(()),
35 }
36 }
37}
38
39impl fmt::Display for ErrorKind {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 match *self {
42 ErrorKind::Help => write!(f, "help message"),
43 ErrorKind::Error => write!(f, "error"),
44 ErrorKind::Note => write!(f, "note"),
45 ErrorKind::Suggestion => write!(f, "suggestion"),
46 ErrorKind::Warning => write!(f, "warning"),
47 }
48 }
49}
50
51#[derive(Debug)]
52pub struct Error {
53 pub line_num: usize,
54 pub kind: Option<ErrorKind>,
57 pub msg: String,
58}
59
60#[derive(PartialEq, Debug)]
61enum WhichLine {
62 ThisLine,
63 FollowPrevious(usize),
64 AdjustBackward(usize),
65}
66
67pub fn load_errors(testfile: &Path, cfg: Option<&str>) -> Vec<Error> {
78 let rdr = BufReader::new(File::open(testfile).unwrap());
79
80 let mut last_nonfollow_error = None;
89
90 rdr.lines()
91 .enumerate()
92 .filter_map(|(line_num, line)| {
93 parse_expected(last_nonfollow_error, line_num + 1, &line.unwrap(), cfg).map(
94 |(which, error)| {
95 match which {
96 FollowPrevious(_) => {}
97 _ => last_nonfollow_error = Some(error.line_num),
98 }
99
100 error
101 },
102 )
103 })
104 .collect()
105}
106
107fn parse_expected(
108 last_nonfollow_error: Option<usize>,
109 line_num: usize,
110 line: &str,
111 cfg: Option<&str>,
112) -> Option<(WhichLine, Error)> {
113 static RE: Lazy<Regex> =
121 Lazy::new(|| Regex::new(r"//(?:\[(?P<cfgs>[\w,]+)])?~(?P<adjust>\||\^*)").unwrap());
122
123 let captures = RE.captures(line)?;
124
125 match (cfg, captures.name("cfgs")) {
126 (Some(cfg), Some(filter)) if !filter.as_str().split(',').any(|s| s == cfg) => return None,
128 (Some(_), Some(_)) => {}
129
130 (None, Some(_)) => panic!("Only tests with revisions should use `//[X]~`"),
131
132 (Some(_), None) | (None, None) => {}
134 }
135
136 let (follow, adjusts) = match &captures["adjust"] {
137 "|" => (true, 0),
138 circumflexes => (false, circumflexes.len()),
139 };
140
141 let whole_match = captures.get(0).unwrap();
143 let (_, mut msg) = line.split_at(whole_match.end());
144
145 let first_word = msg.split_whitespace().next().expect("Encountered unexpected empty comment");
146
147 let kind = first_word.parse::<ErrorKind>().ok();
149 if kind.is_some() {
150 msg = &msg.trim_start().split_at(first_word.len()).1;
151 }
152
153 let msg = msg.trim().to_owned();
154
155 let (which, line_num) = if follow {
156 assert_eq!(adjusts, 0, "use either //~| or //~^, not both.");
157 let line_num = last_nonfollow_error.expect(
158 "encountered //~| without \
159 preceding //~^ line.",
160 );
161 (FollowPrevious(line_num), line_num)
162 } else {
163 let which = if adjusts > 0 { AdjustBackward(adjusts) } else { ThisLine };
164 let line_num = line_num - adjusts;
165 (which, line_num)
166 };
167
168 debug!(
169 "line={} tag={:?} which={:?} kind={:?} msg={:?}",
170 line_num,
171 whole_match.as_str(),
172 which,
173 kind,
174 msg
175 );
176 Some((which, Error { line_num, kind, msg }))
177}