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