]>
Commit | Line | Data |
---|---|---|
a7813a04 XL |
1 | use std::env; |
2 | use std::fs::File; | |
a7813a04 | 3 | use std::io::prelude::*; |
94b46f34 | 4 | use std::io::BufReader; |
a7813a04 XL |
5 | use std::path::{Path, PathBuf}; |
6 | ||
9fa01778 XL |
7 | use crate::common::{self, CompareMode, Config, Mode}; |
8 | use crate::util; | |
a7813a04 | 9 | |
9fa01778 | 10 | use crate::extract_gdb_version; |
c30ab7b3 | 11 | |
0bf4aa26 XL |
12 | /// Whether to ignore the test. |
13 | #[derive(Clone, Copy, PartialEq, Debug)] | |
14 | pub enum Ignore { | |
9fa01778 | 15 | /// Runs it. |
0bf4aa26 XL |
16 | Run, |
17 | /// Ignore it totally. | |
18 | Ignore, | |
19 | /// Ignore only the gdb test, but run the lldb test. | |
20 | IgnoreGdb, | |
21 | /// Ignore only the lldb test, but run the gdb test. | |
22 | IgnoreLldb, | |
23 | } | |
24 | ||
25 | impl Ignore { | |
26 | pub fn can_run_gdb(&self) -> bool { | |
27 | *self == Ignore::Run || *self == Ignore::IgnoreLldb | |
28 | } | |
29 | ||
30 | pub fn can_run_lldb(&self) -> bool { | |
31 | *self == Ignore::Run || *self == Ignore::IgnoreGdb | |
32 | } | |
33 | ||
34 | pub fn no_gdb(&self) -> Ignore { | |
35 | match *self { | |
36 | Ignore::Run => Ignore::IgnoreGdb, | |
37 | Ignore::IgnoreGdb => Ignore::IgnoreGdb, | |
38 | _ => Ignore::Ignore, | |
39 | } | |
40 | } | |
41 | ||
42 | pub fn no_lldb(&self) -> Ignore { | |
43 | match *self { | |
44 | Ignore::Run => Ignore::IgnoreLldb, | |
45 | Ignore::IgnoreLldb => Ignore::IgnoreLldb, | |
46 | _ => Ignore::Ignore, | |
47 | } | |
48 | } | |
49 | } | |
50 | ||
51 | /// The result of parse_cfg_name_directive. | |
52 | #[derive(Clone, Copy, PartialEq, Debug)] | |
53 | enum ParsedNameDirective { | |
54 | /// No match. | |
55 | NoMatch, | |
56 | /// Match. | |
57 | Match, | |
58 | /// Mode was DebugInfoBoth and this matched gdb. | |
59 | MatchGdb, | |
60 | /// Mode was DebugInfoBoth and this matched lldb. | |
61 | MatchLldb, | |
62 | } | |
63 | ||
a7813a04 XL |
64 | /// Properties which must be known very early, before actually running |
65 | /// the test. | |
66 | pub struct EarlyProps { | |
0bf4aa26 | 67 | pub ignore: Ignore, |
a7813a04 | 68 | pub should_fail: bool, |
8bb4bdeb | 69 | pub aux: Vec<String>, |
ff7c6d11 | 70 | pub revisions: Vec<String>, |
a7813a04 XL |
71 | } |
72 | ||
73 | impl EarlyProps { | |
74 | pub fn from_file(config: &Config, testfile: &Path) -> Self { | |
75 | let mut props = EarlyProps { | |
0bf4aa26 | 76 | ignore: Ignore::Run, |
a7813a04 | 77 | should_fail: false, |
8bb4bdeb | 78 | aux: Vec::new(), |
ff7c6d11 | 79 | revisions: vec![], |
a7813a04 XL |
80 | }; |
81 | ||
0bf4aa26 XL |
82 | if config.mode == common::DebugInfoBoth { |
83 | if config.lldb_python_dir.is_none() { | |
84 | props.ignore = props.ignore.no_lldb(); | |
85 | } | |
86 | if config.gdb_version.is_none() { | |
87 | props.ignore = props.ignore.no_gdb(); | |
88 | } | |
89 | } | |
90 | ||
94b46f34 | 91 | iter_header(testfile, None, &mut |ln| { |
2c00a5a8 XL |
92 | // we should check if any only-<platform> exists and if it exists |
93 | // and does not matches the current platform, skip the test | |
0bf4aa26 XL |
94 | if props.ignore != Ignore::Ignore { |
95 | props.ignore = match config.parse_cfg_name_directive(ln, "ignore") { | |
96 | ParsedNameDirective::Match => Ignore::Ignore, | |
97 | ParsedNameDirective::NoMatch => props.ignore, | |
98 | ParsedNameDirective::MatchGdb => props.ignore.no_gdb(), | |
99 | ParsedNameDirective::MatchLldb => props.ignore.no_lldb(), | |
100 | }; | |
101 | ||
102 | if config.has_cfg_prefix(ln, "only") { | |
103 | props.ignore = match config.parse_cfg_name_directive(ln, "only") { | |
104 | ParsedNameDirective::Match => props.ignore, | |
105 | ParsedNameDirective::NoMatch => Ignore::Ignore, | |
106 | ParsedNameDirective::MatchLldb => props.ignore.no_gdb(), | |
107 | ParsedNameDirective::MatchGdb => props.ignore.no_lldb(), | |
108 | }; | |
109 | } | |
110 | ||
111 | if ignore_llvm(config, ln) { | |
112 | props.ignore = Ignore::Ignore; | |
113 | } | |
9fa01778 XL |
114 | |
115 | if config.run_clang_based_tests_with.is_none() && | |
116 | config.parse_needs_matching_clang(ln) { | |
117 | props.ignore = Ignore::Ignore; | |
118 | } | |
0bf4aa26 XL |
119 | } |
120 | ||
121 | if (config.mode == common::DebugInfoGdb || config.mode == common::DebugInfoBoth) && | |
122 | props.ignore.can_run_gdb() && ignore_gdb(config, ln) { | |
123 | props.ignore = props.ignore.no_gdb(); | |
124 | } | |
125 | ||
126 | if (config.mode == common::DebugInfoLldb || config.mode == common::DebugInfoBoth) && | |
127 | props.ignore.can_run_lldb() && ignore_lldb(config, ln) { | |
128 | props.ignore = props.ignore.no_lldb(); | |
129 | } | |
a7813a04 | 130 | |
7cac9316 | 131 | if let Some(s) = config.parse_aux_build(ln) { |
8bb4bdeb XL |
132 | props.aux.push(s); |
133 | } | |
134 | ||
ff7c6d11 XL |
135 | if let Some(r) = config.parse_revisions(ln) { |
136 | props.revisions.extend(r); | |
137 | } | |
138 | ||
7cac9316 | 139 | props.should_fail = props.should_fail || config.parse_name_directive(ln, "should-fail"); |
a7813a04 XL |
140 | }); |
141 | ||
142 | return props; | |
143 | ||
a7813a04 | 144 | fn ignore_gdb(config: &Config, line: &str) -> bool { |
c30ab7b3 | 145 | if let Some(actual_version) = config.gdb_version { |
7cac9316 | 146 | if line.starts_with("min-gdb-version") { |
32a655c1 SL |
147 | let (start_ver, end_ver) = extract_gdb_version_range(line); |
148 | ||
149 | if start_ver != end_ver { | |
150 | panic!("Expected single GDB version") | |
151 | } | |
a7813a04 XL |
152 | // Ignore if actual version is smaller the minimum required |
153 | // version | |
32a655c1 | 154 | actual_version < start_ver |
7cac9316 | 155 | } else if line.starts_with("ignore-gdb-version") { |
32a655c1 SL |
156 | let (min_version, max_version) = extract_gdb_version_range(line); |
157 | ||
158 | if max_version < min_version { | |
159 | panic!("Malformed GDB version range: max < min") | |
160 | } | |
161 | ||
162 | actual_version >= min_version && actual_version <= max_version | |
a7813a04 XL |
163 | } else { |
164 | false | |
165 | } | |
166 | } else { | |
167 | false | |
168 | } | |
169 | } | |
170 | ||
32a655c1 SL |
171 | // Takes a directive of the form "ignore-gdb-version <version1> [- <version2>]", |
172 | // returns the numeric representation of <version1> and <version2> as | |
173 | // tuple: (<version1> as u32, <version2> as u32) | |
174 | // If the <version2> part is omitted, the second component of the tuple | |
175 | // is the same as <version1>. | |
176 | fn extract_gdb_version_range(line: &str) -> (u32, u32) { | |
177 | const ERROR_MESSAGE: &'static str = "Malformed GDB version directive"; | |
178 | ||
7cac9316 XL |
179 | let range_components = line.split(&[' ', '-'][..]) |
180 | .filter(|word| !word.is_empty()) | |
181 | .map(extract_gdb_version) | |
182 | .skip_while(Option::is_none) | |
183 | .take(3) // 3 or more = invalid, so take at most 3. | |
184 | .collect::<Vec<Option<u32>>>(); | |
32a655c1 SL |
185 | |
186 | match range_components.len() { | |
187 | 1 => { | |
7cac9316 | 188 | let v = range_components[0].unwrap(); |
32a655c1 SL |
189 | (v, v) |
190 | } | |
191 | 2 => { | |
7cac9316 XL |
192 | let v_min = range_components[0].unwrap(); |
193 | let v_max = range_components[1].expect(ERROR_MESSAGE); | |
32a655c1 SL |
194 | (v_min, v_max) |
195 | } | |
196 | _ => panic!(ERROR_MESSAGE), | |
197 | } | |
198 | } | |
199 | ||
a7813a04 | 200 | fn ignore_lldb(config: &Config, line: &str) -> bool { |
a7813a04 | 201 | if let Some(ref actual_version) = config.lldb_version { |
7cac9316 | 202 | if line.starts_with("min-lldb-version") { |
0731742a | 203 | let min_version = line.trim_end() |
7cac9316 XL |
204 | .rsplit(' ') |
205 | .next() | |
5bcae85e | 206 | .expect("Malformed lldb version directive"); |
a7813a04 XL |
207 | // Ignore if actual version is smaller the minimum required |
208 | // version | |
5bcae85e | 209 | lldb_version_to_int(actual_version) < lldb_version_to_int(min_version) |
0bf4aa26 XL |
210 | } else if line.starts_with("rust-lldb") && !config.lldb_native_rust { |
211 | true | |
a7813a04 XL |
212 | } else { |
213 | false | |
214 | } | |
215 | } else { | |
216 | false | |
217 | } | |
218 | } | |
9e0c209e SL |
219 | |
220 | fn ignore_llvm(config: &Config, line: &str) -> bool { | |
041b39d2 | 221 | if config.system_llvm && line.starts_with("no-system-llvm") { |
94b46f34 | 222 | return true; |
041b39d2 | 223 | } |
9e0c209e | 224 | if let Some(ref actual_version) = config.llvm_version { |
7cac9316 | 225 | if line.starts_with("min-llvm-version") { |
0731742a | 226 | let min_version = line.trim_end() |
7cac9316 XL |
227 | .rsplit(' ') |
228 | .next() | |
9e0c209e SL |
229 | .expect("Malformed llvm version directive"); |
230 | // Ignore if actual version is smaller the minimum required | |
231 | // version | |
232 | &actual_version[..] < min_version | |
abe05a73 | 233 | } else if line.starts_with("min-system-llvm-version") { |
0731742a | 234 | let min_version = line.trim_end() |
abe05a73 XL |
235 | .rsplit(' ') |
236 | .next() | |
237 | .expect("Malformed llvm version directive"); | |
238 | // Ignore if using system LLVM and actual version | |
239 | // is smaller the minimum required version | |
2c00a5a8 | 240 | config.system_llvm && &actual_version[..] < min_version |
a1dfa0c6 XL |
241 | } else if line.starts_with("ignore-llvm-version") { |
242 | // Syntax is: "ignore-llvm-version <version1> [- <version2>]" | |
243 | let range_components = line.split(' ') | |
244 | .skip(1) // Skip the directive. | |
245 | .map(|s| s.trim()) | |
246 | .filter(|word| !word.is_empty() && word != &"-") | |
247 | .take(3) // 3 or more = invalid, so take at most 3. | |
248 | .collect::<Vec<&str>>(); | |
249 | match range_components.len() { | |
250 | 1 => { | |
251 | &actual_version[..] == range_components[0] | |
252 | } | |
253 | 2 => { | |
254 | let v_min = range_components[0]; | |
255 | let v_max = range_components[1]; | |
256 | if v_max < v_min { | |
257 | panic!("Malformed LLVM version range: max < min") | |
258 | } | |
259 | // Ignore if version lies inside of range. | |
260 | &actual_version[..] >= v_min && &actual_version[..] <= v_max | |
261 | } | |
262 | _ => panic!("Malformed LLVM version directive"), | |
263 | } | |
9e0c209e SL |
264 | } else { |
265 | false | |
266 | } | |
267 | } else { | |
268 | false | |
269 | } | |
270 | } | |
a7813a04 XL |
271 | } |
272 | } | |
273 | ||
274 | #[derive(Clone, Debug)] | |
275 | pub struct TestProps { | |
276 | // Lines that should be expected, in order, on standard out | |
5bcae85e | 277 | pub error_patterns: Vec<String>, |
a7813a04 XL |
278 | // Extra flags to pass to the compiler |
279 | pub compile_flags: Vec<String>, | |
280 | // Extra flags to pass when the compiled code is run (such as --bench) | |
281 | pub run_flags: Option<String>, | |
282 | // If present, the name of a file that this test should match when | |
283 | // pretty-printed | |
284 | pub pp_exact: Option<PathBuf>, | |
285 | // Other crates that should be compiled (typically from the same | |
286 | // directory as the test, but for backwards compatibility reasons | |
287 | // we also check the auxiliary directory) | |
5bcae85e | 288 | pub aux_builds: Vec<String>, |
a7813a04 | 289 | // Environment settings to use for compiling |
5bcae85e | 290 | pub rustc_env: Vec<(String, String)>, |
a7813a04 | 291 | // Environment settings to use during execution |
5bcae85e | 292 | pub exec_env: Vec<(String, String)>, |
a7813a04 | 293 | // Lines to check if they appear in the expected debugger output |
5bcae85e | 294 | pub check_lines: Vec<String>, |
a7813a04 XL |
295 | // Build documentation for all specified aux-builds as well |
296 | pub build_aux_docs: bool, | |
297 | // Flag to force a crate to be built with the host architecture | |
298 | pub force_host: bool, | |
299 | // Check stdout for error-pattern output as well as stderr | |
300 | pub check_stdout: bool, | |
0bf4aa26 XL |
301 | // For UI tests, allows compiler to generate arbitrary output to stdout |
302 | pub dont_check_compiler_stdout: bool, | |
303 | // For UI tests, allows compiler to generate arbitrary output to stderr | |
304 | pub dont_check_compiler_stderr: bool, | |
a7813a04 XL |
305 | // Don't force a --crate-type=dylib flag on the command line |
306 | pub no_prefer_dynamic: bool, | |
307 | // Run --pretty expanded when running pretty printing tests | |
308 | pub pretty_expanded: bool, | |
309 | // Which pretty mode are we testing with, default to 'normal' | |
310 | pub pretty_mode: String, | |
311 | // Only compare pretty output and don't try compiling | |
312 | pub pretty_compare_only: bool, | |
313 | // Patterns which must not appear in the output of a cfail test. | |
314 | pub forbid_output: Vec<String>, | |
315 | // Revisions to test for incremental compilation. | |
316 | pub revisions: Vec<String>, | |
317 | // Directory (if any) to use for incremental compilation. This is | |
318 | // not set by end-users; rather it is set by the incremental | |
319 | // testing harness and used when generating compilation | |
320 | // arguments. (In particular, it propagates to the aux-builds.) | |
321 | pub incremental_dir: Option<PathBuf>, | |
ff7c6d11 | 322 | // Specifies that a test must actually compile without errors. |
83c7162d | 323 | pub compile_pass: bool, |
8bb4bdeb XL |
324 | // rustdoc will test the output of the `--test` option |
325 | pub check_test_line_numbers_match: bool, | |
0531ce1d | 326 | // The test must be compiled and run successfully. Only used in UI tests for now. |
7cac9316 | 327 | pub run_pass: bool, |
83c7162d | 328 | // Skip any codegen step and running the executable. Only for run-pass. |
94b46f34 | 329 | pub skip_codegen: bool, |
0531ce1d XL |
330 | // Do not pass `-Z ui-testing` to UI tests |
331 | pub disable_ui_testing_normalization: bool, | |
041b39d2 XL |
332 | // customized normalization rules |
333 | pub normalize_stdout: Vec<(String, String)>, | |
334 | pub normalize_stderr: Vec<(String, String)>, | |
0531ce1d | 335 | pub failure_status: i32, |
532ac7d7 XL |
336 | // Whether or not `rustfix` should apply the `CodeSuggestion`s of this test and compile the |
337 | // resulting Rust code. | |
83c7162d | 338 | pub run_rustfix: bool, |
532ac7d7 | 339 | // If true, `rustfix` will only apply `MachineApplicable` suggestions. |
b7449926 | 340 | pub rustfix_only_machine_applicable: bool, |
532ac7d7 | 341 | pub assembly_output: Option<String>, |
a7813a04 XL |
342 | } |
343 | ||
344 | impl TestProps { | |
345 | pub fn new() -> Self { | |
a7813a04 | 346 | TestProps { |
9e0c209e | 347 | error_patterns: vec![], |
a7813a04 | 348 | compile_flags: vec![], |
9e0c209e SL |
349 | run_flags: None, |
350 | pp_exact: None, | |
351 | aux_builds: vec![], | |
a7813a04 XL |
352 | revisions: vec![], |
353 | rustc_env: vec![], | |
9e0c209e SL |
354 | exec_env: vec![], |
355 | check_lines: vec![], | |
356 | build_aux_docs: false, | |
357 | force_host: false, | |
358 | check_stdout: false, | |
0bf4aa26 XL |
359 | dont_check_compiler_stdout: false, |
360 | dont_check_compiler_stderr: false, | |
9e0c209e SL |
361 | no_prefer_dynamic: false, |
362 | pretty_expanded: false, | |
041b39d2 | 363 | pretty_mode: "normal".to_string(), |
9e0c209e SL |
364 | pretty_compare_only: false, |
365 | forbid_output: vec![], | |
a7813a04 | 366 | incremental_dir: None, |
83c7162d | 367 | compile_pass: false, |
8bb4bdeb | 368 | check_test_line_numbers_match: false, |
7cac9316 | 369 | run_pass: false, |
94b46f34 | 370 | skip_codegen: false, |
0531ce1d | 371 | disable_ui_testing_normalization: false, |
041b39d2 XL |
372 | normalize_stdout: vec![], |
373 | normalize_stderr: vec![], | |
8faf50e0 | 374 | failure_status: -1, |
83c7162d | 375 | run_rustfix: false, |
b7449926 | 376 | rustfix_only_machine_applicable: false, |
532ac7d7 | 377 | assembly_output: None, |
a7813a04 XL |
378 | } |
379 | } | |
380 | ||
94b46f34 | 381 | pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>, config: &Config) -> Self { |
a7813a04 XL |
382 | let mut props = TestProps::new(); |
383 | ||
384 | // copy over select properties to the aux build: | |
385 | props.incremental_dir = self.incremental_dir.clone(); | |
7cac9316 | 386 | props.load_from(testfile, cfg, config); |
a7813a04 XL |
387 | |
388 | props | |
389 | } | |
390 | ||
abe05a73 | 391 | pub fn from_file(testfile: &Path, cfg: Option<&str>, config: &Config) -> Self { |
a7813a04 | 392 | let mut props = TestProps::new(); |
abe05a73 | 393 | props.load_from(testfile, cfg, config); |
a7813a04 XL |
394 | props |
395 | } | |
396 | ||
9fa01778 | 397 | /// Loads properties from `testfile` into `props`. If a property is |
a7813a04 XL |
398 | /// tied to a particular revision `foo` (indicated by writing |
399 | /// `//[foo]`), then the property is ignored unless `cfg` is | |
400 | /// `Some("foo")`. | |
94b46f34 XL |
401 | fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) { |
402 | iter_header(testfile, cfg, &mut |ln| { | |
7cac9316 | 403 | if let Some(ep) = config.parse_error_pattern(ln) { |
a7813a04 XL |
404 | self.error_patterns.push(ep); |
405 | } | |
406 | ||
7cac9316 | 407 | if let Some(flags) = config.parse_compile_flags(ln) { |
94b46f34 XL |
408 | self.compile_flags |
409 | .extend(flags.split_whitespace().map(|s| s.to_owned())); | |
a7813a04 XL |
410 | } |
411 | ||
8faf50e0 XL |
412 | if let Some(edition) = config.parse_edition(ln) { |
413 | self.compile_flags.push(format!("--edition={}", edition)); | |
414 | } | |
415 | ||
7cac9316 | 416 | if let Some(r) = config.parse_revisions(ln) { |
a7813a04 XL |
417 | self.revisions.extend(r); |
418 | } | |
419 | ||
420 | if self.run_flags.is_none() { | |
7cac9316 | 421 | self.run_flags = config.parse_run_flags(ln); |
a7813a04 XL |
422 | } |
423 | ||
424 | if self.pp_exact.is_none() { | |
7cac9316 | 425 | self.pp_exact = config.parse_pp_exact(ln, testfile); |
a7813a04 XL |
426 | } |
427 | ||
428 | if !self.build_aux_docs { | |
7cac9316 | 429 | self.build_aux_docs = config.parse_build_aux_docs(ln); |
a7813a04 XL |
430 | } |
431 | ||
432 | if !self.force_host { | |
7cac9316 | 433 | self.force_host = config.parse_force_host(ln); |
a7813a04 XL |
434 | } |
435 | ||
436 | if !self.check_stdout { | |
7cac9316 | 437 | self.check_stdout = config.parse_check_stdout(ln); |
a7813a04 XL |
438 | } |
439 | ||
0bf4aa26 XL |
440 | if !self.dont_check_compiler_stdout { |
441 | self.dont_check_compiler_stdout = config.parse_dont_check_compiler_stdout(ln); | |
442 | } | |
443 | ||
444 | if !self.dont_check_compiler_stderr { | |
445 | self.dont_check_compiler_stderr = config.parse_dont_check_compiler_stderr(ln); | |
446 | } | |
447 | ||
a7813a04 | 448 | if !self.no_prefer_dynamic { |
7cac9316 | 449 | self.no_prefer_dynamic = config.parse_no_prefer_dynamic(ln); |
a7813a04 XL |
450 | } |
451 | ||
452 | if !self.pretty_expanded { | |
7cac9316 | 453 | self.pretty_expanded = config.parse_pretty_expanded(ln); |
a7813a04 XL |
454 | } |
455 | ||
7cac9316 | 456 | if let Some(m) = config.parse_pretty_mode(ln) { |
a7813a04 XL |
457 | self.pretty_mode = m; |
458 | } | |
459 | ||
460 | if !self.pretty_compare_only { | |
7cac9316 | 461 | self.pretty_compare_only = config.parse_pretty_compare_only(ln); |
a7813a04 XL |
462 | } |
463 | ||
7cac9316 | 464 | if let Some(ab) = config.parse_aux_build(ln) { |
a7813a04 XL |
465 | self.aux_builds.push(ab); |
466 | } | |
467 | ||
7cac9316 | 468 | if let Some(ee) = config.parse_env(ln, "exec-env") { |
a7813a04 XL |
469 | self.exec_env.push(ee); |
470 | } | |
471 | ||
7cac9316 | 472 | if let Some(ee) = config.parse_env(ln, "rustc-env") { |
a7813a04 XL |
473 | self.rustc_env.push(ee); |
474 | } | |
475 | ||
7cac9316 | 476 | if let Some(cl) = config.parse_check_line(ln) { |
a7813a04 XL |
477 | self.check_lines.push(cl); |
478 | } | |
479 | ||
7cac9316 | 480 | if let Some(of) = config.parse_forbid_output(ln) { |
a7813a04 XL |
481 | self.forbid_output.push(of); |
482 | } | |
9e0c209e | 483 | |
8bb4bdeb | 484 | if !self.check_test_line_numbers_match { |
7cac9316 XL |
485 | self.check_test_line_numbers_match = config.parse_check_test_line_numbers_match(ln); |
486 | } | |
487 | ||
488 | if !self.run_pass { | |
489 | self.run_pass = config.parse_run_pass(ln); | |
8bb4bdeb | 490 | } |
041b39d2 | 491 | |
83c7162d | 492 | if !self.compile_pass { |
a1dfa0c6 | 493 | // run-pass implies must_compile_successfully |
94b46f34 | 494 | self.compile_pass = config.parse_compile_pass(ln) || self.run_pass; |
ff7c6d11 XL |
495 | } |
496 | ||
8faf50e0 XL |
497 | if !self.skip_codegen { |
498 | self.skip_codegen = config.parse_skip_codegen(ln); | |
499 | } | |
83c7162d | 500 | |
0531ce1d XL |
501 | if !self.disable_ui_testing_normalization { |
502 | self.disable_ui_testing_normalization = | |
503 | config.parse_disable_ui_testing_normalization(ln); | |
504 | } | |
505 | ||
041b39d2 XL |
506 | if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") { |
507 | self.normalize_stdout.push(rule); | |
508 | } | |
509 | if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") { | |
510 | self.normalize_stderr.push(rule); | |
511 | } | |
0531ce1d XL |
512 | |
513 | if let Some(code) = config.parse_failure_status(ln) { | |
514 | self.failure_status = code; | |
515 | } | |
83c7162d XL |
516 | |
517 | if !self.run_rustfix { | |
518 | self.run_rustfix = config.parse_run_rustfix(ln); | |
519 | } | |
b7449926 XL |
520 | |
521 | if !self.rustfix_only_machine_applicable { | |
522 | self.rustfix_only_machine_applicable = | |
523 | config.parse_rustfix_only_machine_applicable(ln); | |
524 | } | |
532ac7d7 XL |
525 | |
526 | if self.assembly_output.is_none() { | |
527 | self.assembly_output = config.parse_assembly_output(ln); | |
528 | } | |
a7813a04 XL |
529 | }); |
530 | ||
74d20737 XL |
531 | if self.failure_status == -1 { |
532 | self.failure_status = match config.mode { | |
533 | Mode::RunFail => 101, | |
534 | _ => 1, | |
535 | }; | |
536 | } | |
537 | ||
041b39d2 XL |
538 | for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] { |
539 | if let Ok(val) = env::var(key) { | |
540 | if self.exec_env.iter().find(|&&(ref x, _)| x == key).is_none() { | |
541 | self.exec_env.push(((*key).to_owned(), val)) | |
5bcae85e | 542 | } |
a7813a04 XL |
543 | } |
544 | } | |
545 | } | |
546 | } | |
547 | ||
8faf50e0 | 548 | fn iter_header(testfile: &Path, cfg: Option<&str>, it: &mut dyn FnMut(&str)) { |
a7813a04 | 549 | if testfile.is_dir() { |
5bcae85e | 550 | return; |
a7813a04 | 551 | } |
83c7162d XL |
552 | |
553 | let comment = if testfile.to_string_lossy().ends_with(".rs") { | |
554 | "//" | |
555 | } else { | |
556 | "#" | |
557 | }; | |
558 | ||
9fa01778 XL |
559 | // FIXME: would be nice to allow some whitespace between comment and brace :) |
560 | // It took me like 2 days to debug why compile-flags weren’t taken into account for my test :) | |
83c7162d XL |
561 | let comment_with_brace = comment.to_string() + "["; |
562 | ||
a7813a04 XL |
563 | let rdr = BufReader::new(File::open(testfile).unwrap()); |
564 | for ln in rdr.lines() { | |
565 | // Assume that any directives will be found before the first | |
566 | // module or function. This doesn't seem to be an optimization | |
567 | // with a warm page cache. Maybe with a cold one. | |
568 | let ln = ln.unwrap(); | |
569 | let ln = ln.trim(); | |
570 | if ln.starts_with("fn") || ln.starts_with("mod") { | |
571 | return; | |
83c7162d | 572 | } else if ln.starts_with(&comment_with_brace) { |
a7813a04 | 573 | // A comment like `//[foo]` is specific to revision `foo` |
041b39d2 | 574 | if let Some(close_brace) = ln.find(']') { |
83c7162d XL |
575 | let open_brace = ln.find('[').unwrap(); |
576 | let lncfg = &ln[open_brace + 1 .. close_brace]; | |
a7813a04 XL |
577 | let matches = match cfg { |
578 | Some(s) => s == &lncfg[..], | |
579 | None => false, | |
580 | }; | |
581 | if matches { | |
0731742a | 582 | it(ln[(close_brace + 1)..].trim_start()); |
a7813a04 XL |
583 | } |
584 | } else { | |
83c7162d XL |
585 | panic!("malformed condition directive: expected `{}foo]`, found `{}`", |
586 | comment_with_brace, ln) | |
a7813a04 | 587 | } |
83c7162d | 588 | } else if ln.starts_with(comment) { |
0731742a | 589 | it(ln[comment.len() ..].trim_start()); |
a7813a04 XL |
590 | } |
591 | } | |
592 | return; | |
593 | } | |
594 | ||
7cac9316 | 595 | impl Config { |
7cac9316 XL |
596 | fn parse_error_pattern(&self, line: &str) -> Option<String> { |
597 | self.parse_name_value_directive(line, "error-pattern") | |
598 | } | |
a7813a04 | 599 | |
7cac9316 XL |
600 | fn parse_forbid_output(&self, line: &str) -> Option<String> { |
601 | self.parse_name_value_directive(line, "forbid-output") | |
602 | } | |
a7813a04 | 603 | |
7cac9316 XL |
604 | fn parse_aux_build(&self, line: &str) -> Option<String> { |
605 | self.parse_name_value_directive(line, "aux-build") | |
532ac7d7 | 606 | .map(|r| r.trim().to_string()) |
7cac9316 | 607 | } |
a7813a04 | 608 | |
7cac9316 XL |
609 | fn parse_compile_flags(&self, line: &str) -> Option<String> { |
610 | self.parse_name_value_directive(line, "compile-flags") | |
611 | } | |
a7813a04 | 612 | |
7cac9316 XL |
613 | fn parse_revisions(&self, line: &str) -> Option<Vec<String>> { |
614 | self.parse_name_value_directive(line, "revisions") | |
615 | .map(|r| r.split_whitespace().map(|t| t.to_string()).collect()) | |
616 | } | |
a7813a04 | 617 | |
7cac9316 XL |
618 | fn parse_run_flags(&self, line: &str) -> Option<String> { |
619 | self.parse_name_value_directive(line, "run-flags") | |
620 | } | |
a7813a04 | 621 | |
7cac9316 XL |
622 | fn parse_check_line(&self, line: &str) -> Option<String> { |
623 | self.parse_name_value_directive(line, "check") | |
624 | } | |
a7813a04 | 625 | |
7cac9316 XL |
626 | fn parse_force_host(&self, line: &str) -> bool { |
627 | self.parse_name_directive(line, "force-host") | |
628 | } | |
a7813a04 | 629 | |
7cac9316 XL |
630 | fn parse_build_aux_docs(&self, line: &str) -> bool { |
631 | self.parse_name_directive(line, "build-aux-docs") | |
632 | } | |
a7813a04 | 633 | |
7cac9316 XL |
634 | fn parse_check_stdout(&self, line: &str) -> bool { |
635 | self.parse_name_directive(line, "check-stdout") | |
636 | } | |
a7813a04 | 637 | |
0bf4aa26 XL |
638 | fn parse_dont_check_compiler_stdout(&self, line: &str) -> bool { |
639 | self.parse_name_directive(line, "dont-check-compiler-stdout") | |
640 | } | |
641 | ||
642 | fn parse_dont_check_compiler_stderr(&self, line: &str) -> bool { | |
643 | self.parse_name_directive(line, "dont-check-compiler-stderr") | |
644 | } | |
645 | ||
7cac9316 XL |
646 | fn parse_no_prefer_dynamic(&self, line: &str) -> bool { |
647 | self.parse_name_directive(line, "no-prefer-dynamic") | |
648 | } | |
a7813a04 | 649 | |
7cac9316 XL |
650 | fn parse_pretty_expanded(&self, line: &str) -> bool { |
651 | self.parse_name_directive(line, "pretty-expanded") | |
652 | } | |
a7813a04 | 653 | |
7cac9316 XL |
654 | fn parse_pretty_mode(&self, line: &str) -> Option<String> { |
655 | self.parse_name_value_directive(line, "pretty-mode") | |
656 | } | |
a7813a04 | 657 | |
7cac9316 XL |
658 | fn parse_pretty_compare_only(&self, line: &str) -> bool { |
659 | self.parse_name_directive(line, "pretty-compare-only") | |
660 | } | |
9e0c209e | 661 | |
0531ce1d XL |
662 | fn parse_failure_status(&self, line: &str) -> Option<i32> { |
663 | match self.parse_name_value_directive(line, "failure-status") { | |
664 | Some(code) => code.trim().parse::<i32>().ok(), | |
665 | _ => None, | |
666 | } | |
667 | } | |
668 | ||
83c7162d XL |
669 | fn parse_compile_pass(&self, line: &str) -> bool { |
670 | self.parse_name_directive(line, "compile-pass") | |
7cac9316 XL |
671 | } |
672 | ||
0531ce1d XL |
673 | fn parse_disable_ui_testing_normalization(&self, line: &str) -> bool { |
674 | self.parse_name_directive(line, "disable-ui-testing-normalization") | |
675 | } | |
676 | ||
7cac9316 XL |
677 | fn parse_check_test_line_numbers_match(&self, line: &str) -> bool { |
678 | self.parse_name_directive(line, "check-test-line-numbers-match") | |
679 | } | |
8bb4bdeb | 680 | |
7cac9316 XL |
681 | fn parse_run_pass(&self, line: &str) -> bool { |
682 | self.parse_name_directive(line, "run-pass") | |
683 | } | |
684 | ||
94b46f34 XL |
685 | fn parse_skip_codegen(&self, line: &str) -> bool { |
686 | self.parse_name_directive(line, "skip-codegen") | |
83c7162d XL |
687 | } |
688 | ||
532ac7d7 XL |
689 | fn parse_assembly_output(&self, line: &str) -> Option<String> { |
690 | self.parse_name_value_directive(line, "assembly-output") | |
691 | .map(|r| r.trim().to_string()) | |
692 | } | |
693 | ||
7cac9316 XL |
694 | fn parse_env(&self, line: &str, name: &str) -> Option<(String, String)> { |
695 | self.parse_name_value_directive(line, name).map(|nv| { | |
696 | // nv is either FOO or FOO=BAR | |
94b46f34 | 697 | let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect(); |
a7813a04 | 698 | |
7cac9316 | 699 | match strs.len() { |
b7449926 | 700 | 1 => (strs.pop().unwrap(), String::new()), |
7cac9316 XL |
701 | 2 => { |
702 | let end = strs.pop().unwrap(); | |
703 | (strs.pop().unwrap(), end) | |
704 | } | |
705 | n => panic!("Expected 1 or 2 strings, not {}", n), | |
5bcae85e | 706 | } |
7cac9316 XL |
707 | }) |
708 | } | |
a7813a04 | 709 | |
7cac9316 XL |
710 | fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> { |
711 | if let Some(s) = self.parse_name_value_directive(line, "pp-exact") { | |
712 | Some(PathBuf::from(&s)) | |
041b39d2 XL |
713 | } else if self.parse_name_directive(line, "pp-exact") { |
714 | testfile.file_name().map(PathBuf::from) | |
a7813a04 | 715 | } else { |
041b39d2 XL |
716 | None |
717 | } | |
718 | } | |
719 | ||
720 | fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> { | |
0bf4aa26 | 721 | if self.parse_cfg_name_directive(line, prefix) == ParsedNameDirective::Match { |
0731742a XL |
722 | let from = parse_normalization_string(&mut line)?; |
723 | let to = parse_normalization_string(&mut line)?; | |
041b39d2 XL |
724 | Some((from, to)) |
725 | } else { | |
726 | None | |
727 | } | |
728 | } | |
729 | ||
9fa01778 XL |
730 | fn parse_needs_matching_clang(&self, line: &str) -> bool { |
731 | self.parse_name_directive(line, "needs-matching-clang") | |
732 | } | |
733 | ||
0731742a | 734 | /// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86` |
0bf4aa26 XL |
735 | /// or `normalize-stderr-32bit`. |
736 | fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> ParsedNameDirective { | |
041b39d2 | 737 | if line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-') { |
94b46f34 XL |
738 | let name = line[prefix.len() + 1..] |
739 | .split(&[':', ' '][..]) | |
740 | .next() | |
741 | .unwrap(); | |
041b39d2 | 742 | |
0bf4aa26 | 743 | if name == "test" || |
abe05a73 | 744 | util::matches_os(&self.target, name) || // target |
041b39d2 XL |
745 | name == util::get_arch(&self.target) || // architecture |
746 | name == util::get_pointer_width(&self.target) || // pointer width | |
747 | name == self.stage_id.split('-').next().unwrap() || // stage | |
748 | Some(name) == util::get_env(&self.target) || // env | |
b7449926 | 749 | (self.target != self.host && name == "cross-compile") || |
8faf50e0 XL |
750 | match self.compare_mode { |
751 | Some(CompareMode::Nll) => name == "compare-mode-nll", | |
752 | Some(CompareMode::Polonius) => name == "compare-mode-polonius", | |
753 | None => false, | |
b7449926 | 754 | } || |
0bf4aa26 XL |
755 | (cfg!(debug_assertions) && name == "debug") { |
756 | ParsedNameDirective::Match | |
757 | } else { | |
758 | match self.mode { | |
759 | common::DebugInfoBoth => { | |
760 | if name == "gdb" { | |
761 | ParsedNameDirective::MatchGdb | |
762 | } else if name == "lldb" { | |
763 | ParsedNameDirective::MatchLldb | |
764 | } else { | |
765 | ParsedNameDirective::NoMatch | |
766 | } | |
767 | }, | |
768 | common::DebugInfoGdb => if name == "gdb" { | |
769 | ParsedNameDirective::Match | |
770 | } else { | |
771 | ParsedNameDirective::NoMatch | |
772 | }, | |
773 | common::DebugInfoLldb => if name == "lldb" { | |
774 | ParsedNameDirective::Match | |
775 | } else { | |
776 | ParsedNameDirective::NoMatch | |
777 | }, | |
778 | common::Pretty => if name == "pretty" { | |
779 | ParsedNameDirective::Match | |
780 | } else { | |
781 | ParsedNameDirective::NoMatch | |
782 | }, | |
783 | _ => ParsedNameDirective::NoMatch, | |
784 | } | |
785 | } | |
041b39d2 | 786 | } else { |
0bf4aa26 | 787 | ParsedNameDirective::NoMatch |
a7813a04 XL |
788 | } |
789 | } | |
a7813a04 | 790 | |
2c00a5a8 XL |
791 | fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool { |
792 | // returns whether this line contains this prefix or not. For prefix | |
793 | // "ignore", returns true if line says "ignore-x86_64", "ignore-arch", | |
83c7162d | 794 | // "ignore-android" etc. |
2c00a5a8 XL |
795 | line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-') |
796 | } | |
797 | ||
7cac9316 XL |
798 | fn parse_name_directive(&self, line: &str, directive: &str) -> bool { |
799 | // Ensure the directive is a whole word. Do not match "ignore-x86" when | |
800 | // the line says "ignore-x86_64". | |
801 | line.starts_with(directive) && match line.as_bytes().get(directive.len()) { | |
802 | None | Some(&b' ') | Some(&b':') => true, | |
94b46f34 | 803 | _ => false, |
7cac9316 XL |
804 | } |
805 | } | |
a7813a04 | 806 | |
7cac9316 XL |
807 | pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> { |
808 | let colon = directive.len(); | |
809 | if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') { | |
94b46f34 | 810 | let value = line[(colon + 1)..].to_owned(); |
7cac9316 XL |
811 | debug!("{}: {}", directive, value); |
812 | Some(expand_variables(value, self)) | |
813 | } else { | |
814 | None | |
815 | } | |
a7813a04 | 816 | } |
abe05a73 XL |
817 | |
818 | pub fn find_rust_src_root(&self) -> Option<PathBuf> { | |
819 | let mut path = self.src_base.clone(); | |
820 | let path_postfix = Path::new("src/etc/lldb_batchmode.py"); | |
821 | ||
822 | while path.pop() { | |
823 | if path.join(&path_postfix).is_file() { | |
824 | return Some(path); | |
825 | } | |
826 | } | |
827 | ||
828 | None | |
829 | } | |
83c7162d XL |
830 | |
831 | fn parse_run_rustfix(&self, line: &str) -> bool { | |
832 | self.parse_name_directive(line, "run-rustfix") | |
833 | } | |
8faf50e0 | 834 | |
b7449926 XL |
835 | fn parse_rustfix_only_machine_applicable(&self, line: &str) -> bool { |
836 | self.parse_name_directive(line, "rustfix-only-machine-applicable") | |
837 | } | |
838 | ||
8faf50e0 XL |
839 | fn parse_edition(&self, line: &str) -> Option<String> { |
840 | self.parse_name_value_directive(line, "edition") | |
841 | } | |
a7813a04 XL |
842 | } |
843 | ||
a7813a04 | 844 | pub fn lldb_version_to_int(version_string: &str) -> isize { |
94b46f34 XL |
845 | let error_string = format!( |
846 | "Encountered LLDB version string with unexpected format: {}", | |
847 | version_string | |
848 | ); | |
041b39d2 | 849 | version_string.parse().expect(&error_string) |
a7813a04 | 850 | } |
7cac9316 XL |
851 | |
852 | fn expand_variables(mut value: String, config: &Config) -> String { | |
853 | const CWD: &'static str = "{{cwd}}"; | |
854 | const SRC_BASE: &'static str = "{{src-base}}"; | |
855 | const BUILD_BASE: &'static str = "{{build-base}}"; | |
856 | ||
857 | if value.contains(CWD) { | |
858 | let cwd = env::current_dir().unwrap(); | |
859 | value = value.replace(CWD, &cwd.to_string_lossy()); | |
860 | } | |
861 | ||
862 | if value.contains(SRC_BASE) { | |
863 | value = value.replace(SRC_BASE, &config.src_base.to_string_lossy()); | |
864 | } | |
865 | ||
866 | if value.contains(BUILD_BASE) { | |
867 | value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy()); | |
868 | } | |
869 | ||
870 | value | |
871 | } | |
041b39d2 XL |
872 | |
873 | /// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line` | |
874 | /// variable after the end of the quoted string. | |
875 | /// | |
876 | /// # Examples | |
877 | /// | |
878 | /// ``` | |
879 | /// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\"."; | |
880 | /// let first = parse_normalization_string(&mut s); | |
881 | /// assert_eq!(first, Some("something (32 bits)".to_owned())); | |
882 | /// assert_eq!(s, " -> \"something ($WORD bits)\"."); | |
883 | /// ``` | |
884 | fn parse_normalization_string(line: &mut &str) -> Option<String> { | |
885 | // FIXME support escapes in strings. | |
0731742a XL |
886 | let begin = line.find('"')? + 1; |
887 | let end = line[begin..].find('"')? + begin; | |
041b39d2 | 888 | let result = line[begin..end].to_owned(); |
94b46f34 | 889 | *line = &line[end + 1..]; |
041b39d2 XL |
890 | Some(result) |
891 | } | |
0731742a XL |
892 | |
893 | #[test] | |
894 | fn test_parse_normalization_string() { | |
895 | let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\"."; | |
896 | let first = parse_normalization_string(&mut s); | |
897 | assert_eq!(first, Some("something (32 bits)".to_owned())); | |
898 | assert_eq!(s, " -> \"something ($WORD bits)\"."); | |
899 | ||
900 | // Nothing to normalize (No quotes) | |
901 | let mut s = "normalize-stderr-32bit: something (32 bits) -> something ($WORD bits)."; | |
902 | let first = parse_normalization_string(&mut s); | |
903 | assert_eq!(first, None); | |
904 | assert_eq!(s, r#"normalize-stderr-32bit: something (32 bits) -> something ($WORD bits)."#); | |
905 | ||
906 | // Nothing to normalize (Only a single quote) | |
907 | let mut s = "normalize-stderr-32bit: \"something (32 bits) -> something ($WORD bits)."; | |
908 | let first = parse_normalization_string(&mut s); | |
909 | assert_eq!(first, None); | |
910 | assert_eq!(s, "normalize-stderr-32bit: \"something (32 bits) -> something ($WORD bits)."); | |
911 | ||
912 | // Nothing to normalize (Three quotes) | |
913 | let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)."; | |
914 | let first = parse_normalization_string(&mut s); | |
915 | assert_eq!(first, Some("something (32 bits)".to_owned())); | |
916 | assert_eq!(s, " -> \"something ($WORD bits)."); | |
917 | } |