1 use std::collections::HashSet;
4 use std::io::prelude::*;
5 use std::io::BufReader;
6 use std::path::{Path, PathBuf};
10 use crate::common::{CompareMode, Config, Debugger, FailMode, Mode, PassMode};
11 use crate::extract_gdb_version;
17 /// The result of parse_cfg_name_directive.
18 #[derive(Clone, Copy, PartialEq, Debug)]
19 enum ParsedNameDirective {
26 /// Properties which must be known very early, before actually running
29 pub struct EarlyProps {
31 pub should_fail: bool,
33 pub aux_crate: Vec<(String, String)>,
34 pub revisions: Vec<String>,
38 pub fn from_file(config: &Config, testfile: &Path) -> Self {
39 let file = File::open(testfile).unwrap();
40 Self::from_reader(config, testfile, file)
43 pub fn from_reader<R: Read>(config: &Config, testfile: &Path, rdr: R) -> Self {
44 let mut props = EarlyProps::default();
45 let rustc_has_profiler_support = env::var_os("RUSTC_PROFILER_SUPPORT").is_some();
46 let rustc_has_sanitizer_support = env::var_os("RUSTC_SANITIZER_SUPPORT").is_some();
47 let has_asan = util::ASAN_SUPPORTED_TARGETS.contains(&&*config.target);
48 let has_lsan = util::LSAN_SUPPORTED_TARGETS.contains(&&*config.target);
49 let has_msan = util::MSAN_SUPPORTED_TARGETS.contains(&&*config.target);
50 let has_tsan = util::TSAN_SUPPORTED_TARGETS.contains(&&*config.target);
52 iter_header(testfile, None, rdr, &mut |ln| {
53 // we should check if any only-<platform> exists and if it exists
54 // and does not matches the current platform, skip the test
56 props.ignore = match config.parse_cfg_name_directive(ln, "ignore") {
57 ParsedNameDirective::Match => true,
58 ParsedNameDirective::NoMatch => props.ignore,
61 if config.has_cfg_prefix(ln, "only") {
62 props.ignore = match config.parse_cfg_name_directive(ln, "only") {
63 ParsedNameDirective::Match => props.ignore,
64 ParsedNameDirective::NoMatch => true,
68 if ignore_llvm(config, ln) {
72 if config.run_clang_based_tests_with.is_none()
73 && config.parse_needs_matching_clang(ln)
78 if !rustc_has_profiler_support && config.parse_needs_profiler_support(ln) {
82 if !rustc_has_sanitizer_support
83 && config.parse_name_directive(ln, "needs-sanitizer-support")
88 if !has_asan && config.parse_name_directive(ln, "needs-sanitizer-address") {
92 if !has_lsan && config.parse_name_directive(ln, "needs-sanitizer-leak") {
96 if !has_msan && config.parse_name_directive(ln, "needs-sanitizer-memory") {
100 if !has_tsan && config.parse_name_directive(ln, "needs-sanitizer-thread") {
104 if config.target == "wasm32-unknown-unknown" && config.parse_check_run_results(ln) {
108 if config.debugger == Some(Debugger::Gdb) && ignore_gdb(config, ln) {
112 if config.debugger == Some(Debugger::Lldb) && ignore_lldb(config, ln) {
117 if let Some(s) = config.parse_aux_build(ln) {
121 if let Some(ac) = config.parse_aux_crate(ln) {
122 props.aux_crate.push(ac);
125 if let Some(r) = config.parse_revisions(ln) {
126 props.revisions.extend(r);
129 props.should_fail = props.should_fail || config.parse_name_directive(ln, "should-fail");
134 fn ignore_gdb(config: &Config, line: &str) -> bool {
135 if let Some(actual_version) = config.gdb_version {
136 if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
137 let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
139 panic!("couldn't parse version range: {:?}", rest);
142 if start_ver != end_ver {
143 panic!("Expected single GDB version")
145 // Ignore if actual version is smaller the minimum required
147 return actual_version < start_ver;
148 } else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
149 let (min_version, max_version) =
150 extract_version_range(rest, extract_gdb_version).unwrap_or_else(|| {
151 panic!("couldn't parse version range: {:?}", rest);
154 if max_version < min_version {
155 panic!("Malformed GDB version range: max < min")
158 return actual_version >= min_version && actual_version <= max_version;
164 fn ignore_lldb(config: &Config, line: &str) -> bool {
165 if let Some(actual_version) = config.lldb_version {
166 if let Some(min_version) = line.strip_prefix("min-lldb-version:").map(str::trim) {
167 let min_version = min_version.parse().unwrap_or_else(|e| {
169 "Unexpected format of LLDB version string: {}\n{:?}",
173 // Ignore if actual version is smaller the minimum required
175 actual_version < min_version
177 line.starts_with("rust-lldb") && !config.lldb_native_rust
184 fn ignore_llvm(config: &Config, line: &str) -> bool {
185 if config.system_llvm && line.starts_with("no-system-llvm") {
188 if let Some(needed_components) =
189 config.parse_name_value_directive(line, "needs-llvm-components")
191 let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
192 if !needed_components
194 .all(|needed_component| components.contains(needed_component))
199 if let Some(actual_version) = config.llvm_version {
200 if let Some(rest) = line.strip_prefix("min-llvm-version:").map(str::trim) {
201 let min_version = extract_llvm_version(rest).unwrap();
202 // Ignore if actual version is smaller the minimum required
204 actual_version < min_version
205 } else if let Some(rest) =
206 line.strip_prefix("min-system-llvm-version:").map(str::trim)
208 let min_version = extract_llvm_version(rest).unwrap();
209 // Ignore if using system LLVM and actual version
210 // is smaller the minimum required version
211 config.system_llvm && actual_version < min_version
212 } else if let Some(rest) = line.strip_prefix("ignore-llvm-version:").map(str::trim)
214 // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
215 let (v_min, v_max) = extract_version_range(rest, extract_llvm_version)
217 panic!("couldn't parse version range: {:?}", rest);
220 panic!("Malformed LLVM version range: max < min")
222 // Ignore if version lies inside of range.
223 actual_version >= v_min && actual_version <= v_max
234 #[derive(Clone, Debug)]
235 pub struct TestProps {
236 // Lines that should be expected, in order, on standard out
237 pub error_patterns: Vec<String>,
238 // Extra flags to pass to the compiler
239 pub compile_flags: Vec<String>,
240 // Extra flags to pass when the compiled code is run (such as --bench)
241 pub run_flags: Option<String>,
242 // If present, the name of a file that this test should match when
244 pub pp_exact: Option<PathBuf>,
245 // Other crates that should be compiled (typically from the same
246 // directory as the test, but for backwards compatibility reasons
247 // we also check the auxiliary directory)
248 pub aux_builds: Vec<String>,
249 // Similar to `aux_builds`, but a list of NAME=somelib.rs of dependencies
250 // to build and pass with the `--extern` flag.
251 pub aux_crates: Vec<(String, String)>,
252 // Environment settings to use for compiling
253 pub rustc_env: Vec<(String, String)>,
254 // Environment variables to unset prior to compiling.
255 // Variables are unset before applying 'rustc_env'.
256 pub unset_rustc_env: Vec<String>,
257 // Environment settings to use during execution
258 pub exec_env: Vec<(String, String)>,
259 // Lines to check if they appear in the expected debugger output
260 pub check_lines: Vec<String>,
261 // Build documentation for all specified aux-builds as well
262 pub build_aux_docs: bool,
263 // Flag to force a crate to be built with the host architecture
264 pub force_host: bool,
265 // Check stdout for error-pattern output as well as stderr
266 pub check_stdout: bool,
267 // Check stdout & stderr for output of run-pass test
268 pub check_run_results: bool,
269 // For UI tests, allows compiler to generate arbitrary output to stdout
270 pub dont_check_compiler_stdout: bool,
271 // For UI tests, allows compiler to generate arbitrary output to stderr
272 pub dont_check_compiler_stderr: bool,
273 // Don't force a --crate-type=dylib flag on the command line
275 // Set this for example if you have an auxiliary test file that contains
276 // a proc-macro and needs `#![crate_type = "proc-macro"]`. This ensures
277 // that the aux file is compiled as a `proc-macro` and not as a `dylib`.
278 pub no_prefer_dynamic: bool,
279 // Run --pretty expanded when running pretty printing tests
280 pub pretty_expanded: bool,
281 // Which pretty mode are we testing with, default to 'normal'
282 pub pretty_mode: String,
283 // Only compare pretty output and don't try compiling
284 pub pretty_compare_only: bool,
285 // Patterns which must not appear in the output of a cfail test.
286 pub forbid_output: Vec<String>,
287 // Revisions to test for incremental compilation.
288 pub revisions: Vec<String>,
289 // Directory (if any) to use for incremental compilation. This is
290 // not set by end-users; rather it is set by the incremental
291 // testing harness and used when generating compilation
292 // arguments. (In particular, it propagates to the aux-builds.)
293 pub incremental_dir: Option<PathBuf>,
294 // How far should the test proceed while still passing.
295 pass_mode: Option<PassMode>,
296 // Ignore `--pass` overrides from the command line for this test.
298 // How far this test should proceed to start failing.
299 pub fail_mode: Option<FailMode>,
300 // rustdoc will test the output of the `--test` option
301 pub check_test_line_numbers_match: bool,
302 // customized normalization rules
303 pub normalize_stdout: Vec<(String, String)>,
304 pub normalize_stderr: Vec<(String, String)>,
305 pub failure_status: i32,
306 // Whether or not `rustfix` should apply the `CodeSuggestion`s of this test and compile the
307 // resulting Rust code.
308 pub run_rustfix: bool,
309 // If true, `rustfix` will only apply `MachineApplicable` suggestions.
310 pub rustfix_only_machine_applicable: bool,
311 pub assembly_output: Option<String>,
312 // If true, the test is expected to ICE
313 pub should_ice: bool,
317 pub fn new() -> Self {
319 error_patterns: vec![],
320 compile_flags: vec![],
327 unset_rustc_env: vec![],
330 build_aux_docs: false,
333 check_run_results: false,
334 dont_check_compiler_stdout: false,
335 dont_check_compiler_stderr: false,
336 no_prefer_dynamic: false,
337 pretty_expanded: false,
338 pretty_mode: "normal".to_string(),
339 pretty_compare_only: false,
340 forbid_output: vec![],
341 incremental_dir: None,
345 check_test_line_numbers_match: false,
346 normalize_stdout: vec![],
347 normalize_stderr: vec![],
350 rustfix_only_machine_applicable: false,
351 assembly_output: None,
356 pub fn from_aux_file(&self, testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
357 let mut props = TestProps::new();
359 // copy over select properties to the aux build:
360 props.incremental_dir = self.incremental_dir.clone();
361 props.load_from(testfile, cfg, config);
366 pub fn from_file(testfile: &Path, cfg: Option<&str>, config: &Config) -> Self {
367 let mut props = TestProps::new();
368 props.load_from(testfile, cfg, config);
370 match (props.pass_mode, props.fail_mode) {
371 (None, None) => props.fail_mode = Some(FailMode::Check),
372 (Some(_), None) | (None, Some(_)) => {}
373 (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
379 /// Loads properties from `testfile` into `props`. If a property is
380 /// tied to a particular revision `foo` (indicated by writing
381 /// `//[foo]`), then the property is ignored unless `cfg` is
383 fn load_from(&mut self, testfile: &Path, cfg: Option<&str>, config: &Config) {
384 if !testfile.is_dir() {
385 let file = File::open(testfile).unwrap();
387 iter_header(testfile, cfg, file, &mut |ln| {
388 if let Some(ep) = config.parse_error_pattern(ln) {
389 self.error_patterns.push(ep);
392 if let Some(flags) = config.parse_compile_flags(ln) {
393 self.compile_flags.extend(flags.split_whitespace().map(|s| s.to_owned()));
396 if let Some(edition) = config.parse_edition(ln) {
397 self.compile_flags.push(format!("--edition={}", edition));
400 if let Some(r) = config.parse_revisions(ln) {
401 self.revisions.extend(r);
404 if self.run_flags.is_none() {
405 self.run_flags = config.parse_run_flags(ln);
408 if self.pp_exact.is_none() {
409 self.pp_exact = config.parse_pp_exact(ln, testfile);
412 if !self.should_ice {
413 self.should_ice = config.parse_should_ice(ln);
416 if !self.build_aux_docs {
417 self.build_aux_docs = config.parse_build_aux_docs(ln);
420 if !self.force_host {
421 self.force_host = config.parse_force_host(ln);
424 if !self.check_stdout {
425 self.check_stdout = config.parse_check_stdout(ln);
428 if !self.check_run_results {
429 self.check_run_results = config.parse_check_run_results(ln);
432 if !self.dont_check_compiler_stdout {
433 self.dont_check_compiler_stdout = config.parse_dont_check_compiler_stdout(ln);
436 if !self.dont_check_compiler_stderr {
437 self.dont_check_compiler_stderr = config.parse_dont_check_compiler_stderr(ln);
440 if !self.no_prefer_dynamic {
441 self.no_prefer_dynamic = config.parse_no_prefer_dynamic(ln);
444 if !self.pretty_expanded {
445 self.pretty_expanded = config.parse_pretty_expanded(ln);
448 if let Some(m) = config.parse_pretty_mode(ln) {
449 self.pretty_mode = m;
452 if !self.pretty_compare_only {
453 self.pretty_compare_only = config.parse_pretty_compare_only(ln);
456 if let Some(ab) = config.parse_aux_build(ln) {
457 self.aux_builds.push(ab);
460 if let Some(ac) = config.parse_aux_crate(ln) {
461 self.aux_crates.push(ac);
464 if let Some(ee) = config.parse_env(ln, "exec-env") {
465 self.exec_env.push(ee);
468 if let Some(ee) = config.parse_env(ln, "rustc-env") {
469 self.rustc_env.push(ee);
472 if let Some(ev) = config.parse_name_value_directive(ln, "unset-rustc-env") {
473 self.unset_rustc_env.push(ev);
476 if let Some(cl) = config.parse_check_line(ln) {
477 self.check_lines.push(cl);
480 if let Some(of) = config.parse_forbid_output(ln) {
481 self.forbid_output.push(of);
484 if !self.check_test_line_numbers_match {
485 self.check_test_line_numbers_match =
486 config.parse_check_test_line_numbers_match(ln);
489 self.update_pass_mode(ln, cfg, config);
490 self.update_fail_mode(ln, config);
492 if !self.ignore_pass {
493 self.ignore_pass = config.parse_ignore_pass(ln);
496 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stdout") {
497 self.normalize_stdout.push(rule);
499 if let Some(rule) = config.parse_custom_normalization(ln, "normalize-stderr") {
500 self.normalize_stderr.push(rule);
503 if let Some(code) = config.parse_failure_status(ln) {
504 self.failure_status = code;
507 if !self.run_rustfix {
508 self.run_rustfix = config.parse_run_rustfix(ln);
511 if !self.rustfix_only_machine_applicable {
512 self.rustfix_only_machine_applicable =
513 config.parse_rustfix_only_machine_applicable(ln);
516 if self.assembly_output.is_none() {
517 self.assembly_output = config.parse_assembly_output(ln);
522 if self.failure_status == -1 {
523 self.failure_status = match config.mode {
524 Mode::RunFail => 101,
529 self.failure_status = 101;
532 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
533 if let Ok(val) = env::var(key) {
534 if self.exec_env.iter().find(|&&(ref x, _)| x == key).is_none() {
535 self.exec_env.push(((*key).to_owned(), val))
541 fn update_fail_mode(&mut self, ln: &str, config: &Config) {
542 let check_ui = |mode: &str| {
543 if config.mode != Mode::Ui {
544 panic!("`{}-fail` header is only supported in UI tests", mode);
547 if config.mode == Mode::Ui && config.parse_name_directive(ln, "compile-fail") {
548 panic!("`compile-fail` header is useless in UI tests");
550 let fail_mode = if config.parse_name_directive(ln, "check-fail") {
552 Some(FailMode::Check)
553 } else if config.parse_name_directive(ln, "build-fail") {
555 Some(FailMode::Build)
556 } else if config.parse_name_directive(ln, "run-fail") {
562 match (self.fail_mode, fail_mode) {
563 (None, Some(_)) => self.fail_mode = fail_mode,
564 (Some(_), Some(_)) => panic!("multiple `*-fail` headers in a single test"),
569 fn update_pass_mode(&mut self, ln: &str, revision: Option<&str>, config: &Config) {
570 let check_no_run = |s| {
571 if config.mode != Mode::Ui && config.mode != Mode::Incremental {
572 panic!("`{}` header is only supported in UI and incremental tests", s);
574 if config.mode == Mode::Incremental
575 && !revision.map_or(false, |r| r.starts_with("cfail"))
576 && !self.revisions.iter().all(|r| r.starts_with("cfail"))
578 panic!("`{}` header is only supported in `cfail` incremental tests", s);
581 let pass_mode = if config.parse_name_directive(ln, "check-pass") {
582 check_no_run("check-pass");
583 Some(PassMode::Check)
584 } else if config.parse_name_directive(ln, "build-pass") {
585 check_no_run("build-pass");
586 Some(PassMode::Build)
587 } else if config.parse_name_directive(ln, "run-pass") {
588 if config.mode != Mode::Ui {
589 panic!("`run-pass` header is only supported in UI tests")
595 match (self.pass_mode, pass_mode) {
596 (None, Some(_)) => self.pass_mode = pass_mode,
597 (Some(_), Some(_)) => panic!("multiple `*-pass` headers in a single test"),
602 pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
603 if !self.ignore_pass && self.fail_mode.is_none() && config.mode == Mode::Ui {
604 if let (mode @ Some(_), Some(_)) = (config.force_pass_mode, self.pass_mode) {
611 // does not consider CLI override for pass mode
612 pub fn local_pass_mode(&self) -> Option<PassMode> {
617 fn iter_header<R: Read>(testfile: &Path, cfg: Option<&str>, rdr: R, it: &mut dyn FnMut(&str)) {
618 if testfile.is_dir() {
622 let comment = if testfile.to_string_lossy().ends_with(".rs") { "//" } else { "#" };
624 let mut rdr = BufReader::new(rdr);
625 let mut ln = String::new();
629 if rdr.read_line(&mut ln).unwrap() == 0 {
633 // Assume that any directives will be found before the first
634 // module or function. This doesn't seem to be an optimization
635 // with a warm page cache. Maybe with a cold one.
637 if ln.starts_with("fn") || ln.starts_with("mod") {
639 } else if ln.starts_with(comment) && ln[comment.len()..].trim_start().starts_with('[') {
640 // A comment like `//[foo]` is specific to revision `foo`
641 if let Some(close_brace) = ln.find(']') {
642 let open_brace = ln.find('[').unwrap();
643 let lncfg = &ln[open_brace + 1..close_brace];
644 let matches = match cfg {
645 Some(s) => s == &lncfg[..],
649 it(ln[(close_brace + 1)..].trim_start());
652 panic!("malformed condition directive: expected `{}[foo]`, found `{}`", comment, ln)
654 } else if ln.starts_with(comment) {
655 it(ln[comment.len()..].trim_start());
661 fn parse_should_ice(&self, line: &str) -> bool {
662 self.parse_name_directive(line, "should-ice")
664 fn parse_error_pattern(&self, line: &str) -> Option<String> {
665 self.parse_name_value_directive(line, "error-pattern")
668 fn parse_forbid_output(&self, line: &str) -> Option<String> {
669 self.parse_name_value_directive(line, "forbid-output")
672 fn parse_aux_build(&self, line: &str) -> Option<String> {
673 self.parse_name_value_directive(line, "aux-build").map(|r| r.trim().to_string())
676 fn parse_aux_crate(&self, line: &str) -> Option<(String, String)> {
677 self.parse_name_value_directive(line, "aux-crate").map(|r| {
678 let mut parts = r.trim().splitn(2, '=');
680 parts.next().expect("aux-crate name").to_string(),
681 parts.next().expect("aux-crate value").to_string(),
686 fn parse_compile_flags(&self, line: &str) -> Option<String> {
687 self.parse_name_value_directive(line, "compile-flags")
690 fn parse_revisions(&self, line: &str) -> Option<Vec<String>> {
691 self.parse_name_value_directive(line, "revisions")
692 .map(|r| r.split_whitespace().map(|t| t.to_string()).collect())
695 fn parse_run_flags(&self, line: &str) -> Option<String> {
696 self.parse_name_value_directive(line, "run-flags")
699 fn parse_check_line(&self, line: &str) -> Option<String> {
700 self.parse_name_value_directive(line, "check")
703 fn parse_force_host(&self, line: &str) -> bool {
704 self.parse_name_directive(line, "force-host")
707 fn parse_build_aux_docs(&self, line: &str) -> bool {
708 self.parse_name_directive(line, "build-aux-docs")
711 fn parse_check_stdout(&self, line: &str) -> bool {
712 self.parse_name_directive(line, "check-stdout")
715 fn parse_check_run_results(&self, line: &str) -> bool {
716 self.parse_name_directive(line, "check-run-results")
719 fn parse_dont_check_compiler_stdout(&self, line: &str) -> bool {
720 self.parse_name_directive(line, "dont-check-compiler-stdout")
723 fn parse_dont_check_compiler_stderr(&self, line: &str) -> bool {
724 self.parse_name_directive(line, "dont-check-compiler-stderr")
727 fn parse_no_prefer_dynamic(&self, line: &str) -> bool {
728 self.parse_name_directive(line, "no-prefer-dynamic")
731 fn parse_pretty_expanded(&self, line: &str) -> bool {
732 self.parse_name_directive(line, "pretty-expanded")
735 fn parse_pretty_mode(&self, line: &str) -> Option<String> {
736 self.parse_name_value_directive(line, "pretty-mode")
739 fn parse_pretty_compare_only(&self, line: &str) -> bool {
740 self.parse_name_directive(line, "pretty-compare-only")
743 fn parse_failure_status(&self, line: &str) -> Option<i32> {
744 match self.parse_name_value_directive(line, "failure-status") {
745 Some(code) => code.trim().parse::<i32>().ok(),
750 fn parse_check_test_line_numbers_match(&self, line: &str) -> bool {
751 self.parse_name_directive(line, "check-test-line-numbers-match")
754 fn parse_ignore_pass(&self, line: &str) -> bool {
755 self.parse_name_directive(line, "ignore-pass")
758 fn parse_assembly_output(&self, line: &str) -> Option<String> {
759 self.parse_name_value_directive(line, "assembly-output").map(|r| r.trim().to_string())
762 fn parse_env(&self, line: &str, name: &str) -> Option<(String, String)> {
763 self.parse_name_value_directive(line, name).map(|nv| {
764 // nv is either FOO or FOO=BAR
765 let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
768 1 => (strs.pop().unwrap(), String::new()),
770 let end = strs.pop().unwrap();
771 (strs.pop().unwrap(), end)
773 n => panic!("Expected 1 or 2 strings, not {}", n),
778 fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
779 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
780 Some(PathBuf::from(&s))
781 } else if self.parse_name_directive(line, "pp-exact") {
782 testfile.file_name().map(PathBuf::from)
788 fn parse_custom_normalization(&self, mut line: &str, prefix: &str) -> Option<(String, String)> {
789 if self.parse_cfg_name_directive(line, prefix) == ParsedNameDirective::Match {
790 let from = parse_normalization_string(&mut line)?;
791 let to = parse_normalization_string(&mut line)?;
798 fn parse_needs_matching_clang(&self, line: &str) -> bool {
799 self.parse_name_directive(line, "needs-matching-clang")
802 fn parse_needs_profiler_support(&self, line: &str) -> bool {
803 self.parse_name_directive(line, "needs-profiler-support")
806 /// Parses a name-value directive which contains config-specific information, e.g., `ignore-x86`
807 /// or `normalize-stderr-32bit`.
808 fn parse_cfg_name_directive(&self, line: &str, prefix: &str) -> ParsedNameDirective {
809 if !line.as_bytes().starts_with(prefix.as_bytes()) {
810 return ParsedNameDirective::NoMatch;
812 if line.as_bytes().get(prefix.len()) != Some(&b'-') {
813 return ParsedNameDirective::NoMatch;
816 let name = line[prefix.len() + 1..].split(&[':', ' '][..]).next().unwrap();
818 let is_match = name == "test" ||
819 self.target == name || // triple
820 util::matches_os(&self.target, name) || // target
821 util::matches_env(&self.target, name) || // env
822 self.target.ends_with(name) || // target and env
823 name == util::get_arch(&self.target) || // architecture
824 name == util::get_pointer_width(&self.target) || // pointer width
825 name == self.stage_id.split('-').next().unwrap() || // stage
826 (self.target != self.host && name == "cross-compile") ||
827 (name == "endian-big" && util::is_big_endian(&self.target)) ||
828 (self.remote_test_client.is_some() && name == "remote") ||
829 match self.compare_mode {
830 Some(CompareMode::Nll) => name == "compare-mode-nll",
831 Some(CompareMode::Polonius) => name == "compare-mode-polonius",
832 Some(CompareMode::Chalk) => name == "compare-mode-chalk",
835 (cfg!(debug_assertions) && name == "debug") ||
836 match self.debugger {
837 Some(Debugger::Cdb) => name == "cdb",
838 Some(Debugger::Gdb) => name == "gdb",
839 Some(Debugger::Lldb) => name == "lldb",
843 if is_match { ParsedNameDirective::Match } else { ParsedNameDirective::NoMatch }
846 fn has_cfg_prefix(&self, line: &str, prefix: &str) -> bool {
847 // returns whether this line contains this prefix or not. For prefix
848 // "ignore", returns true if line says "ignore-x86_64", "ignore-arch",
849 // "ignore-android" etc.
850 line.starts_with(prefix) && line.as_bytes().get(prefix.len()) == Some(&b'-')
853 fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
854 // Ensure the directive is a whole word. Do not match "ignore-x86" when
855 // the line says "ignore-x86_64".
856 line.starts_with(directive)
857 && matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
860 pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
861 let colon = directive.len();
862 if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
863 let value = line[(colon + 1)..].to_owned();
864 debug!("{}: {}", directive, value);
865 Some(expand_variables(value, self))
871 pub fn find_rust_src_root(&self) -> Option<PathBuf> {
872 let mut path = self.src_base.clone();
873 let path_postfix = Path::new("src/etc/lldb_batchmode.py");
876 if path.join(&path_postfix).is_file() {
884 fn parse_run_rustfix(&self, line: &str) -> bool {
885 self.parse_name_directive(line, "run-rustfix")
888 fn parse_rustfix_only_machine_applicable(&self, line: &str) -> bool {
889 self.parse_name_directive(line, "rustfix-only-machine-applicable")
892 fn parse_edition(&self, line: &str) -> Option<String> {
893 self.parse_name_value_directive(line, "edition")
897 fn expand_variables(mut value: String, config: &Config) -> String {
898 const CWD: &str = "{{cwd}}";
899 const SRC_BASE: &str = "{{src-base}}";
900 const BUILD_BASE: &str = "{{build-base}}";
902 if value.contains(CWD) {
903 let cwd = env::current_dir().unwrap();
904 value = value.replace(CWD, &cwd.to_string_lossy());
907 if value.contains(SRC_BASE) {
908 value = value.replace(SRC_BASE, &config.src_base.to_string_lossy());
911 if value.contains(BUILD_BASE) {
912 value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy());
918 /// Finds the next quoted string `"..."` in `line`, and extract the content from it. Move the `line`
919 /// variable after the end of the quoted string.
924 /// let mut s = "normalize-stderr-32bit: \"something (32 bits)\" -> \"something ($WORD bits)\".";
925 /// let first = parse_normalization_string(&mut s);
926 /// assert_eq!(first, Some("something (32 bits)".to_owned()));
927 /// assert_eq!(s, " -> \"something ($WORD bits)\".");
929 fn parse_normalization_string(line: &mut &str) -> Option<String> {
930 // FIXME support escapes in strings.
931 let begin = line.find('"')? + 1;
932 let end = line[begin..].find('"')? + begin;
933 let result = line[begin..end].to_owned();
934 *line = &line[end + 1..];
938 pub fn extract_llvm_version(version: &str) -> Option<u32> {
939 let version_without_suffix = version.trim_end_matches("git").split('-').next().unwrap();
940 let components: Vec<u32> = version_without_suffix
942 .map(|s| s.parse().expect("Malformed version component"))
944 let version = match *components {
946 [a, b] => a * 10_000 + b * 100,
947 [a, b, c] => a * 10_000 + b * 100 + c,
948 _ => panic!("Malformed version"),
953 // Takes a directive of the form "<version1> [- <version2>]",
954 // returns the numeric representation of <version1> and <version2> as
955 // tuple: (<version1> as u32, <version2> as u32)
956 // If the <version2> part is omitted, the second component of the tuple
957 // is the same as <version1>.
958 fn extract_version_range<F>(line: &str, parse: F) -> Option<(u32, u32)>
960 F: Fn(&str) -> Option<u32>,
962 let mut splits = line.splitn(2, "- ").map(str::trim);
963 let min = splits.next().unwrap();
964 if min.ends_with('-') {
968 let max = splits.next();
974 let min = parse(min)?;
975 let max = match max {
976 Some(max) if max.is_empty() => return None,
977 Some(max) => parse(max)?,