1use std::collections::HashSet;
2use std::env;
3use std::fs::File;
4use std::io::BufReader;
5use std::io::prelude::*;
6use std::process::Command;
7
8use camino::{Utf8Path, Utf8PathBuf};
9use semver::Version;
10use tracing::*;
11
12use crate::common::{CodegenBackend, Config, Debugger, FailMode, PassMode, RunFailMode, TestMode};
13use crate::debuggers::{extract_cdb_version, extract_gdb_version};
14use crate::directives::auxiliary::{AuxProps, parse_and_update_aux};
15use crate::directives::directive_names::{
16 KNOWN_DIRECTIVE_NAMES, KNOWN_HTMLDOCCK_DIRECTIVE_NAMES, KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
17};
18use crate::directives::needs::CachedNeedsConditions;
19use crate::errors::ErrorKind;
20use crate::executor::{CollectedTestDesc, ShouldPanic};
21use crate::help;
22use crate::util::static_regex;
23
24pub(crate) mod auxiliary;
25mod cfg;
26mod directive_names;
27mod needs;
28#[cfg(test)]
29mod tests;
30
31pub struct DirectivesCache {
32 needs: CachedNeedsConditions,
33}
34
35impl DirectivesCache {
36 pub fn load(config: &Config) -> Self {
37 Self { needs: CachedNeedsConditions::load(config) }
38 }
39}
40
41#[derive(Default)]
44pub struct EarlyProps {
45 pub(crate) aux: AuxProps,
49 pub revisions: Vec<String>,
50}
51
52impl EarlyProps {
53 pub fn from_file(config: &Config, testfile: &Utf8Path) -> Self {
54 let file = File::open(testfile.as_std_path()).expect("open test file to parse earlyprops");
55 Self::from_reader(config, testfile, file)
56 }
57
58 pub fn from_reader<R: Read>(config: &Config, testfile: &Utf8Path, rdr: R) -> Self {
59 let mut props = EarlyProps::default();
60 let mut poisoned = false;
61 iter_directives(
62 config.mode,
63 &mut poisoned,
64 testfile,
65 rdr,
66 &mut |DirectiveLine { line_number, raw_directive: ln, .. }| {
67 parse_and_update_aux(config, ln, testfile, line_number, &mut props.aux);
68 config.parse_and_update_revisions(testfile, line_number, ln, &mut props.revisions);
69 },
70 );
71
72 if poisoned {
73 eprintln!("errors encountered during EarlyProps parsing: {}", testfile);
74 panic!("errors encountered during EarlyProps parsing");
75 }
76
77 props
78 }
79}
80
81#[derive(Clone, Debug)]
82pub struct TestProps {
83 pub error_patterns: Vec<String>,
85 pub regex_error_patterns: Vec<String>,
87 pub compile_flags: Vec<String>,
89 pub run_flags: Vec<String>,
91 pub doc_flags: Vec<String>,
93 pub pp_exact: Option<Utf8PathBuf>,
96 pub(crate) aux: AuxProps,
98 pub rustc_env: Vec<(String, String)>,
100 pub unset_rustc_env: Vec<String>,
103 pub exec_env: Vec<(String, String)>,
105 pub unset_exec_env: Vec<String>,
108 pub build_aux_docs: bool,
110 pub unique_doc_out_dir: bool,
113 pub force_host: bool,
115 pub check_stdout: bool,
117 pub check_run_results: bool,
119 pub dont_check_compiler_stdout: bool,
121 pub dont_check_compiler_stderr: bool,
123 pub no_prefer_dynamic: bool,
129 pub pretty_mode: String,
131 pub pretty_compare_only: bool,
133 pub forbid_output: Vec<String>,
135 pub revisions: Vec<String>,
137 pub incremental_dir: Option<Utf8PathBuf>,
142 pub incremental: bool,
157 pub known_bug: bool,
163 pass_mode: Option<PassMode>,
165 ignore_pass: bool,
167 pub fail_mode: Option<FailMode>,
169 pub check_test_line_numbers_match: bool,
171 pub normalize_stdout: Vec<(String, String)>,
173 pub normalize_stderr: Vec<(String, String)>,
174 pub failure_status: Option<i32>,
175 pub dont_check_failure_status: bool,
177 pub run_rustfix: bool,
180 pub rustfix_only_machine_applicable: bool,
182 pub assembly_output: Option<String>,
183 pub should_ice: bool,
185 pub stderr_per_bitwidth: bool,
187 pub mir_unit_test: Option<String>,
189 pub remap_src_base: bool,
192 pub llvm_cov_flags: Vec<String>,
195 pub filecheck_flags: Vec<String>,
197 pub no_auto_check_cfg: bool,
199 pub has_enzyme: bool,
201 pub add_core_stubs: bool,
204 pub core_stubs_compile_flags: Vec<String>,
206 pub dont_require_annotations: HashSet<ErrorKind>,
208 pub disable_gdb_pretty_printers: bool,
210 pub compare_output_by_lines: bool,
212}
213
214mod directives {
215 pub const ERROR_PATTERN: &'static str = "error-pattern";
216 pub const REGEX_ERROR_PATTERN: &'static str = "regex-error-pattern";
217 pub const COMPILE_FLAGS: &'static str = "compile-flags";
218 pub const RUN_FLAGS: &'static str = "run-flags";
219 pub const DOC_FLAGS: &'static str = "doc-flags";
220 pub const SHOULD_ICE: &'static str = "should-ice";
221 pub const BUILD_AUX_DOCS: &'static str = "build-aux-docs";
222 pub const UNIQUE_DOC_OUT_DIR: &'static str = "unique-doc-out-dir";
223 pub const FORCE_HOST: &'static str = "force-host";
224 pub const CHECK_STDOUT: &'static str = "check-stdout";
225 pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
226 pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
227 pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
228 pub const DONT_REQUIRE_ANNOTATIONS: &'static str = "dont-require-annotations";
229 pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
230 pub const PRETTY_MODE: &'static str = "pretty-mode";
231 pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
232 pub const AUX_BIN: &'static str = "aux-bin";
233 pub const AUX_BUILD: &'static str = "aux-build";
234 pub const AUX_CRATE: &'static str = "aux-crate";
235 pub const PROC_MACRO: &'static str = "proc-macro";
236 pub const AUX_CODEGEN_BACKEND: &'static str = "aux-codegen-backend";
237 pub const EXEC_ENV: &'static str = "exec-env";
238 pub const RUSTC_ENV: &'static str = "rustc-env";
239 pub const UNSET_EXEC_ENV: &'static str = "unset-exec-env";
240 pub const UNSET_RUSTC_ENV: &'static str = "unset-rustc-env";
241 pub const FORBID_OUTPUT: &'static str = "forbid-output";
242 pub const CHECK_TEST_LINE_NUMBERS_MATCH: &'static str = "check-test-line-numbers-match";
243 pub const IGNORE_PASS: &'static str = "ignore-pass";
244 pub const FAILURE_STATUS: &'static str = "failure-status";
245 pub const DONT_CHECK_FAILURE_STATUS: &'static str = "dont-check-failure-status";
246 pub const RUN_RUSTFIX: &'static str = "run-rustfix";
247 pub const RUSTFIX_ONLY_MACHINE_APPLICABLE: &'static str = "rustfix-only-machine-applicable";
248 pub const ASSEMBLY_OUTPUT: &'static str = "assembly-output";
249 pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth";
250 pub const INCREMENTAL: &'static str = "incremental";
251 pub const KNOWN_BUG: &'static str = "known-bug";
252 pub const TEST_MIR_PASS: &'static str = "test-mir-pass";
253 pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
254 pub const LLVM_COV_FLAGS: &'static str = "llvm-cov-flags";
255 pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
256 pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
257 pub const ADD_CORE_STUBS: &'static str = "add-core-stubs";
258 pub const CORE_STUBS_COMPILE_FLAGS: &'static str = "core-stubs-compile-flags";
259 pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
261 pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
262 pub const COMPARE_OUTPUT_BY_LINES: &'static str = "compare-output-by-lines";
263}
264
265impl TestProps {
266 pub fn new() -> Self {
267 TestProps {
268 error_patterns: vec![],
269 regex_error_patterns: vec![],
270 compile_flags: vec![],
271 run_flags: vec![],
272 doc_flags: vec![],
273 pp_exact: None,
274 aux: Default::default(),
275 revisions: vec![],
276 rustc_env: vec![
277 ("RUSTC_ICE".to_string(), "0".to_string()),
278 ("RUST_BACKTRACE".to_string(), "short".to_string()),
279 ],
280 unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
281 exec_env: vec![],
282 unset_exec_env: vec![],
283 build_aux_docs: false,
284 unique_doc_out_dir: false,
285 force_host: false,
286 check_stdout: false,
287 check_run_results: false,
288 dont_check_compiler_stdout: false,
289 dont_check_compiler_stderr: false,
290 no_prefer_dynamic: false,
291 pretty_mode: "normal".to_string(),
292 pretty_compare_only: false,
293 forbid_output: vec![],
294 incremental_dir: None,
295 incremental: false,
296 known_bug: false,
297 pass_mode: None,
298 fail_mode: None,
299 ignore_pass: false,
300 check_test_line_numbers_match: false,
301 normalize_stdout: vec![],
302 normalize_stderr: vec![],
303 failure_status: None,
304 dont_check_failure_status: false,
305 run_rustfix: false,
306 rustfix_only_machine_applicable: false,
307 assembly_output: None,
308 should_ice: false,
309 stderr_per_bitwidth: false,
310 mir_unit_test: None,
311 remap_src_base: false,
312 llvm_cov_flags: vec![],
313 filecheck_flags: vec![],
314 no_auto_check_cfg: false,
315 has_enzyme: false,
316 add_core_stubs: false,
317 core_stubs_compile_flags: vec![],
318 dont_require_annotations: Default::default(),
319 disable_gdb_pretty_printers: false,
320 compare_output_by_lines: false,
321 }
322 }
323
324 pub fn from_aux_file(
325 &self,
326 testfile: &Utf8Path,
327 revision: Option<&str>,
328 config: &Config,
329 ) -> Self {
330 let mut props = TestProps::new();
331
332 props.incremental_dir = self.incremental_dir.clone();
334 props.ignore_pass = true;
335 props.load_from(testfile, revision, config);
336
337 props
338 }
339
340 pub fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
341 let mut props = TestProps::new();
342 props.load_from(testfile, revision, config);
343 props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
344
345 match (props.pass_mode, props.fail_mode) {
346 (None, None) if config.mode == TestMode::Ui => props.fail_mode = Some(FailMode::Check),
347 (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
348 _ => {}
349 }
350
351 props
352 }
353
354 fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
359 let mut has_edition = false;
360 if !testfile.is_dir() {
361 let file = File::open(testfile.as_std_path()).unwrap();
362
363 let mut poisoned = false;
364
365 iter_directives(
366 config.mode,
367 &mut poisoned,
368 testfile,
369 file,
370 &mut |directive @ DirectiveLine { line_number, raw_directive: ln, .. }| {
371 if !directive.applies_to_test_revision(test_revision) {
372 return;
373 }
374
375 use directives::*;
376
377 config.push_name_value_directive(
378 ln,
379 ERROR_PATTERN,
380 testfile,
381 line_number,
382 &mut self.error_patterns,
383 |r| r,
384 );
385 config.push_name_value_directive(
386 ln,
387 REGEX_ERROR_PATTERN,
388 testfile,
389 line_number,
390 &mut self.regex_error_patterns,
391 |r| r,
392 );
393
394 config.push_name_value_directive(
395 ln,
396 DOC_FLAGS,
397 testfile,
398 line_number,
399 &mut self.doc_flags,
400 |r| r,
401 );
402
403 fn split_flags(flags: &str) -> Vec<String> {
404 flags
407 .split('\'')
408 .enumerate()
409 .flat_map(|(i, f)| {
410 if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() }
411 })
412 .map(move |s| s.to_owned())
413 .collect::<Vec<_>>()
414 }
415
416 if let Some(flags) =
417 config.parse_name_value_directive(ln, COMPILE_FLAGS, testfile, line_number)
418 {
419 let flags = split_flags(&flags);
420 for flag in &flags {
421 if flag == "--edition" || flag.starts_with("--edition=") {
422 panic!("you must use `//@ edition` to configure the edition");
423 }
424 }
425 self.compile_flags.extend(flags);
426 }
427 if config
428 .parse_name_value_directive(
429 ln,
430 INCORRECT_COMPILER_FLAGS,
431 testfile,
432 line_number,
433 )
434 .is_some()
435 {
436 panic!("`compiler-flags` directive should be spelled `compile-flags`");
437 }
438
439 if let Some(edition) = config.parse_edition(ln, testfile, line_number) {
440 self.compile_flags.insert(0, format!("--edition={}", edition.trim()));
443 has_edition = true;
444 }
445
446 config.parse_and_update_revisions(
447 testfile,
448 line_number,
449 ln,
450 &mut self.revisions,
451 );
452
453 if let Some(flags) =
454 config.parse_name_value_directive(ln, RUN_FLAGS, testfile, line_number)
455 {
456 self.run_flags.extend(split_flags(&flags));
457 }
458
459 if self.pp_exact.is_none() {
460 self.pp_exact = config.parse_pp_exact(ln, testfile, line_number);
461 }
462
463 config.set_name_directive(ln, SHOULD_ICE, &mut self.should_ice);
464 config.set_name_directive(ln, BUILD_AUX_DOCS, &mut self.build_aux_docs);
465 config.set_name_directive(ln, UNIQUE_DOC_OUT_DIR, &mut self.unique_doc_out_dir);
466
467 config.set_name_directive(ln, FORCE_HOST, &mut self.force_host);
468 config.set_name_directive(ln, CHECK_STDOUT, &mut self.check_stdout);
469 config.set_name_directive(ln, CHECK_RUN_RESULTS, &mut self.check_run_results);
470 config.set_name_directive(
471 ln,
472 DONT_CHECK_COMPILER_STDOUT,
473 &mut self.dont_check_compiler_stdout,
474 );
475 config.set_name_directive(
476 ln,
477 DONT_CHECK_COMPILER_STDERR,
478 &mut self.dont_check_compiler_stderr,
479 );
480 config.set_name_directive(ln, NO_PREFER_DYNAMIC, &mut self.no_prefer_dynamic);
481
482 if let Some(m) =
483 config.parse_name_value_directive(ln, PRETTY_MODE, testfile, line_number)
484 {
485 self.pretty_mode = m;
486 }
487
488 config.set_name_directive(
489 ln,
490 PRETTY_COMPARE_ONLY,
491 &mut self.pretty_compare_only,
492 );
493
494 parse_and_update_aux(config, ln, testfile, line_number, &mut self.aux);
496
497 config.push_name_value_directive(
498 ln,
499 EXEC_ENV,
500 testfile,
501 line_number,
502 &mut self.exec_env,
503 Config::parse_env,
504 );
505 config.push_name_value_directive(
506 ln,
507 UNSET_EXEC_ENV,
508 testfile,
509 line_number,
510 &mut self.unset_exec_env,
511 |r| r.trim().to_owned(),
512 );
513 config.push_name_value_directive(
514 ln,
515 RUSTC_ENV,
516 testfile,
517 line_number,
518 &mut self.rustc_env,
519 Config::parse_env,
520 );
521 config.push_name_value_directive(
522 ln,
523 UNSET_RUSTC_ENV,
524 testfile,
525 line_number,
526 &mut self.unset_rustc_env,
527 |r| r.trim().to_owned(),
528 );
529 config.push_name_value_directive(
530 ln,
531 FORBID_OUTPUT,
532 testfile,
533 line_number,
534 &mut self.forbid_output,
535 |r| r,
536 );
537 config.set_name_directive(
538 ln,
539 CHECK_TEST_LINE_NUMBERS_MATCH,
540 &mut self.check_test_line_numbers_match,
541 );
542
543 self.update_pass_mode(ln, test_revision, config);
544 self.update_fail_mode(ln, config);
545
546 config.set_name_directive(ln, IGNORE_PASS, &mut self.ignore_pass);
547
548 if let Some(NormalizeRule { kind, regex, replacement }) =
549 config.parse_custom_normalization(ln)
550 {
551 let rule_tuple = (regex, replacement);
552 match kind {
553 NormalizeKind::Stdout => self.normalize_stdout.push(rule_tuple),
554 NormalizeKind::Stderr => self.normalize_stderr.push(rule_tuple),
555 NormalizeKind::Stderr32bit => {
556 if config.target_cfg().pointer_width == 32 {
557 self.normalize_stderr.push(rule_tuple);
558 }
559 }
560 NormalizeKind::Stderr64bit => {
561 if config.target_cfg().pointer_width == 64 {
562 self.normalize_stderr.push(rule_tuple);
563 }
564 }
565 }
566 }
567
568 if let Some(code) = config
569 .parse_name_value_directive(ln, FAILURE_STATUS, testfile, line_number)
570 .and_then(|code| code.trim().parse::<i32>().ok())
571 {
572 self.failure_status = Some(code);
573 }
574
575 config.set_name_directive(
576 ln,
577 DONT_CHECK_FAILURE_STATUS,
578 &mut self.dont_check_failure_status,
579 );
580
581 config.set_name_directive(ln, RUN_RUSTFIX, &mut self.run_rustfix);
582 config.set_name_directive(
583 ln,
584 RUSTFIX_ONLY_MACHINE_APPLICABLE,
585 &mut self.rustfix_only_machine_applicable,
586 );
587 config.set_name_value_directive(
588 ln,
589 ASSEMBLY_OUTPUT,
590 testfile,
591 line_number,
592 &mut self.assembly_output,
593 |r| r.trim().to_string(),
594 );
595 config.set_name_directive(
596 ln,
597 STDERR_PER_BITWIDTH,
598 &mut self.stderr_per_bitwidth,
599 );
600 config.set_name_directive(ln, INCREMENTAL, &mut self.incremental);
601
602 if let Some(known_bug) =
605 config.parse_name_value_directive(ln, KNOWN_BUG, testfile, line_number)
606 {
607 let known_bug = known_bug.trim();
608 if known_bug == "unknown"
609 || known_bug.split(',').all(|issue_ref| {
610 issue_ref
611 .trim()
612 .split_once('#')
613 .filter(|(_, number)| {
614 number.chars().all(|digit| digit.is_numeric())
615 })
616 .is_some()
617 })
618 {
619 self.known_bug = true;
620 } else {
621 panic!(
622 "Invalid known-bug value: {known_bug}\nIt requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
623 );
624 }
625 } else if config.parse_name_directive(ln, KNOWN_BUG) {
626 panic!(
627 "Invalid known-bug attribute, requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
628 );
629 }
630
631 config.set_name_value_directive(
632 ln,
633 TEST_MIR_PASS,
634 testfile,
635 line_number,
636 &mut self.mir_unit_test,
637 |s| s.trim().to_string(),
638 );
639 config.set_name_directive(ln, REMAP_SRC_BASE, &mut self.remap_src_base);
640
641 if let Some(flags) =
642 config.parse_name_value_directive(ln, LLVM_COV_FLAGS, testfile, line_number)
643 {
644 self.llvm_cov_flags.extend(split_flags(&flags));
645 }
646
647 if let Some(flags) = config.parse_name_value_directive(
648 ln,
649 FILECHECK_FLAGS,
650 testfile,
651 line_number,
652 ) {
653 self.filecheck_flags.extend(split_flags(&flags));
654 }
655
656 config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut self.no_auto_check_cfg);
657
658 self.update_add_core_stubs(ln, config);
659
660 if let Some(flags) = config.parse_name_value_directive(
661 ln,
662 directives::CORE_STUBS_COMPILE_FLAGS,
663 testfile,
664 line_number,
665 ) {
666 let flags = split_flags(&flags);
667 for flag in &flags {
668 if flag == "--edition" || flag.starts_with("--edition=") {
669 panic!("you must use `//@ edition` to configure the edition");
670 }
671 }
672 self.core_stubs_compile_flags.extend(flags);
673 }
674
675 if let Some(err_kind) = config.parse_name_value_directive(
676 ln,
677 DONT_REQUIRE_ANNOTATIONS,
678 testfile,
679 line_number,
680 ) {
681 self.dont_require_annotations
682 .insert(ErrorKind::expect_from_user_str(err_kind.trim()));
683 }
684
685 config.set_name_directive(
686 ln,
687 DISABLE_GDB_PRETTY_PRINTERS,
688 &mut self.disable_gdb_pretty_printers,
689 );
690 config.set_name_directive(
691 ln,
692 COMPARE_OUTPUT_BY_LINES,
693 &mut self.compare_output_by_lines,
694 );
695 },
696 );
697
698 if poisoned {
699 eprintln!("errors encountered during TestProps parsing: {}", testfile);
700 panic!("errors encountered during TestProps parsing");
701 }
702 }
703
704 if self.should_ice {
705 self.failure_status = Some(101);
706 }
707
708 if config.mode == TestMode::Incremental {
709 self.incremental = true;
710 }
711
712 if config.mode == TestMode::Crashes {
713 self.rustc_env = vec![
717 ("RUST_BACKTRACE".to_string(), "0".to_string()),
718 ("RUSTC_ICE".to_string(), "0".to_string()),
719 ];
720 }
721
722 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
723 if let Ok(val) = env::var(key) {
724 if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
725 self.exec_env.push(((*key).to_owned(), val))
726 }
727 }
728 }
729
730 if let (Some(edition), false) = (&config.edition, has_edition) {
731 self.compile_flags.insert(0, format!("--edition={}", edition));
734 }
735 }
736
737 fn update_fail_mode(&mut self, ln: &str, config: &Config) {
738 let check_ui = |mode: &str| {
739 if config.mode != TestMode::Ui && config.mode != TestMode::Crashes {
741 panic!("`{}-fail` directive is only supported in UI tests", mode);
742 }
743 };
744 if config.mode == TestMode::Ui && config.parse_name_directive(ln, "compile-fail") {
745 panic!("`compile-fail` directive is useless in UI tests");
746 }
747 let fail_mode = if config.parse_name_directive(ln, "check-fail") {
748 check_ui("check");
749 Some(FailMode::Check)
750 } else if config.parse_name_directive(ln, "build-fail") {
751 check_ui("build");
752 Some(FailMode::Build)
753 } else if config.parse_name_directive(ln, "run-fail") {
754 check_ui("run");
755 Some(FailMode::Run(RunFailMode::Fail))
756 } else if config.parse_name_directive(ln, "run-crash") {
757 check_ui("run");
758 Some(FailMode::Run(RunFailMode::Crash))
759 } else if config.parse_name_directive(ln, "run-fail-or-crash") {
760 check_ui("run");
761 Some(FailMode::Run(RunFailMode::FailOrCrash))
762 } else {
763 None
764 };
765 match (self.fail_mode, fail_mode) {
766 (None, Some(_)) => self.fail_mode = fail_mode,
767 (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
768 (_, None) => {}
769 }
770 }
771
772 fn update_pass_mode(&mut self, ln: &str, revision: Option<&str>, config: &Config) {
773 let check_no_run = |s| match (config.mode, s) {
774 (TestMode::Ui, _) => (),
775 (TestMode::Crashes, _) => (),
776 (TestMode::Codegen, "build-pass") => (),
777 (TestMode::Incremental, _) => {
778 if revision.is_some() && !self.revisions.iter().all(|r| r.starts_with("cfail")) {
779 panic!("`{s}` directive is only supported in `cfail` incremental tests")
780 }
781 }
782 (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
783 };
784 let pass_mode = if config.parse_name_directive(ln, "check-pass") {
785 check_no_run("check-pass");
786 Some(PassMode::Check)
787 } else if config.parse_name_directive(ln, "build-pass") {
788 check_no_run("build-pass");
789 Some(PassMode::Build)
790 } else if config.parse_name_directive(ln, "run-pass") {
791 check_no_run("run-pass");
792 Some(PassMode::Run)
793 } else {
794 None
795 };
796 match (self.pass_mode, pass_mode) {
797 (None, Some(_)) => self.pass_mode = pass_mode,
798 (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
799 (_, None) => {}
800 }
801 }
802
803 pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
804 if !self.ignore_pass && self.fail_mode.is_none() {
805 if let mode @ Some(_) = config.force_pass_mode {
806 return mode;
807 }
808 }
809 self.pass_mode
810 }
811
812 pub fn local_pass_mode(&self) -> Option<PassMode> {
814 self.pass_mode
815 }
816
817 pub fn update_add_core_stubs(&mut self, ln: &str, config: &Config) {
818 let add_core_stubs = config.parse_name_directive(ln, directives::ADD_CORE_STUBS);
819 if add_core_stubs {
820 if !matches!(config.mode, TestMode::Ui | TestMode::Codegen | TestMode::Assembly) {
821 panic!(
822 "`add-core-stubs` is currently only supported for ui, codegen and assembly test modes"
823 );
824 }
825
826 if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
829 panic!("`add-core-stubs` cannot be used to run the test binary");
832 }
833
834 self.add_core_stubs = add_core_stubs;
835 }
836 }
837}
838
839fn line_directive<'line>(
842 line_number: usize,
843 original_line: &'line str,
844) -> Option<DirectiveLine<'line>> {
845 let after_comment =
847 original_line.trim_start().strip_prefix(COMPILETEST_DIRECTIVE_PREFIX)?.trim_start();
848
849 let revision;
850 let raw_directive;
851
852 if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
853 let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
855 panic!(
856 "malformed condition directive: expected `{COMPILETEST_DIRECTIVE_PREFIX}[foo]`, found `{original_line}`"
857 )
858 };
859
860 revision = Some(line_revision);
861 raw_directive = after_close_bracket.trim_start();
862 } else {
863 revision = None;
864 raw_directive = after_comment;
865 };
866
867 Some(DirectiveLine { line_number, revision, raw_directive })
868}
869
870struct DirectiveLine<'ln> {
884 line_number: usize,
885 revision: Option<&'ln str>,
889 raw_directive: &'ln str,
895}
896
897impl<'ln> DirectiveLine<'ln> {
898 fn applies_to_test_revision(&self, test_revision: Option<&str>) -> bool {
899 self.revision.is_none() || self.revision == test_revision
900 }
901}
902
903pub(crate) struct CheckDirectiveResult<'ln> {
904 is_known_directive: bool,
905 trailing_directive: Option<&'ln str>,
906}
907
908pub(crate) fn check_directive<'a>(
909 directive_ln: &'a str,
910 mode: TestMode,
911) -> CheckDirectiveResult<'a> {
912 let (directive_name, post) = directive_ln.split_once([':', ' ']).unwrap_or((directive_ln, ""));
913
914 let is_known_directive = KNOWN_DIRECTIVE_NAMES.contains(&directive_name)
915 || match mode {
916 TestMode::Rustdoc => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
917 TestMode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
918 _ => false,
919 };
920
921 let trailing = post.trim().split_once(' ').map(|(pre, _)| pre).unwrap_or(post);
922 let trailing_directive = {
923 directive_ln.get(directive_name.len()..).is_some_and(|s| s.starts_with(' '))
925 && KNOWN_DIRECTIVE_NAMES.contains(&trailing)
927 }
928 .then_some(trailing);
929
930 CheckDirectiveResult { is_known_directive, trailing_directive }
931}
932
933const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
934
935fn iter_directives(
936 mode: TestMode,
937 poisoned: &mut bool,
938 testfile: &Utf8Path,
939 rdr: impl Read,
940 it: &mut dyn FnMut(DirectiveLine<'_>),
941) {
942 if testfile.is_dir() {
943 return;
944 }
945
946 if mode == TestMode::CoverageRun {
951 let extra_directives: &[&str] = &[
952 "needs-profiler-runtime",
953 "ignore-cross-compile",
957 ];
958 for raw_directive in extra_directives {
960 it(DirectiveLine { line_number: 0, revision: None, raw_directive });
961 }
962 }
963
964 let mut rdr = BufReader::with_capacity(1024, rdr);
965 let mut ln = String::new();
966 let mut line_number = 0;
967
968 loop {
969 line_number += 1;
970 ln.clear();
971 if rdr.read_line(&mut ln).unwrap() == 0 {
972 break;
973 }
974 let ln = ln.trim();
975
976 let Some(directive_line) = line_directive(line_number, ln) else {
977 continue;
978 };
979
980 if testfile.extension() == Some("rs") {
982 let CheckDirectiveResult { is_known_directive, trailing_directive } =
983 check_directive(directive_line.raw_directive, mode);
984
985 if !is_known_directive {
986 *poisoned = true;
987
988 error!(
989 "{testfile}:{line_number}: detected unknown compiletest test directive `{}`",
990 directive_line.raw_directive,
991 );
992
993 return;
994 }
995
996 if let Some(trailing_directive) = &trailing_directive {
997 *poisoned = true;
998
999 error!(
1000 "{testfile}:{line_number}: detected trailing compiletest test directive `{}`",
1001 trailing_directive,
1002 );
1003 help!("put the trailing directive in its own line: `//@ {}`", trailing_directive);
1004
1005 return;
1006 }
1007 }
1008
1009 it(directive_line);
1010 }
1011}
1012
1013impl Config {
1014 fn parse_and_update_revisions(
1015 &self,
1016 testfile: &Utf8Path,
1017 line_number: usize,
1018 line: &str,
1019 existing: &mut Vec<String>,
1020 ) {
1021 const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
1022 "true", "false",
1026 ];
1027
1028 const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
1029 ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
1030
1031 if let Some(raw) = self.parse_name_value_directive(line, "revisions", testfile, line_number)
1032 {
1033 if self.mode == TestMode::RunMake {
1034 panic!("`run-make` mode tests do not support revisions: {}", testfile);
1035 }
1036
1037 let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
1038 for revision in raw.split_whitespace() {
1039 if !duplicates.insert(revision.to_string()) {
1040 panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
1041 }
1042
1043 if FORBIDDEN_REVISION_NAMES.contains(&revision) {
1044 panic!(
1045 "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
1046 revision, raw, testfile
1047 );
1048 }
1049
1050 if matches!(self.mode, TestMode::Assembly | TestMode::Codegen | TestMode::MirOpt)
1051 && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
1052 {
1053 panic!(
1054 "revision name `{revision}` is not permitted in a test suite that uses \
1055 `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
1056 prefix: `{revision}` in line `{}`: {}",
1057 raw, testfile
1058 );
1059 }
1060
1061 existing.push(revision.to_string());
1062 }
1063 }
1064 }
1065
1066 fn parse_env(nv: String) -> (String, String) {
1067 let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
1071 let name = name.trim();
1074 (name.to_owned(), value.to_owned())
1075 }
1076
1077 fn parse_pp_exact(
1078 &self,
1079 line: &str,
1080 testfile: &Utf8Path,
1081 line_number: usize,
1082 ) -> Option<Utf8PathBuf> {
1083 if let Some(s) = self.parse_name_value_directive(line, "pp-exact", testfile, line_number) {
1084 Some(Utf8PathBuf::from(&s))
1085 } else if self.parse_name_directive(line, "pp-exact") {
1086 testfile.file_name().map(Utf8PathBuf::from)
1087 } else {
1088 None
1089 }
1090 }
1091
1092 fn parse_custom_normalization(&self, raw_directive: &str) -> Option<NormalizeRule> {
1093 let (directive_name, raw_value) = raw_directive.split_once(':')?;
1096
1097 let kind = match directive_name {
1098 "normalize-stdout" => NormalizeKind::Stdout,
1099 "normalize-stderr" => NormalizeKind::Stderr,
1100 "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
1101 "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
1102 _ => return None,
1103 };
1104
1105 let Some((regex, replacement)) = parse_normalize_rule(raw_value) else {
1106 error!("couldn't parse custom normalization rule: `{raw_directive}`");
1107 help!("expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`");
1108 panic!("invalid normalization rule detected");
1109 };
1110 Some(NormalizeRule { kind, regex, replacement })
1111 }
1112
1113 fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
1114 line.starts_with(directive)
1117 && matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
1118 }
1119
1120 fn parse_negative_name_directive(&self, line: &str, directive: &str) -> bool {
1121 line.starts_with("no-") && self.parse_name_directive(&line[3..], directive)
1122 }
1123
1124 pub fn parse_name_value_directive(
1125 &self,
1126 line: &str,
1127 directive: &str,
1128 testfile: &Utf8Path,
1129 line_number: usize,
1130 ) -> Option<String> {
1131 let colon = directive.len();
1132 if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
1133 let value = line[(colon + 1)..].to_owned();
1134 debug!("{}: {}", directive, value);
1135 let value = expand_variables(value, self);
1136 if value.is_empty() {
1137 error!("{testfile}:{line_number}: empty value for directive `{directive}`");
1138 help!("expected syntax is: `{directive}: value`");
1139 panic!("empty directive value detected");
1140 }
1141 Some(value)
1142 } else {
1143 None
1144 }
1145 }
1146
1147 fn parse_edition(&self, line: &str, testfile: &Utf8Path, line_number: usize) -> Option<String> {
1148 self.parse_name_value_directive(line, "edition", testfile, line_number)
1149 }
1150
1151 fn set_name_directive(&self, line: &str, directive: &str, value: &mut bool) {
1152 match value {
1153 true => {
1154 if self.parse_negative_name_directive(line, directive) {
1155 *value = false;
1156 }
1157 }
1158 false => {
1159 if self.parse_name_directive(line, directive) {
1160 *value = true;
1161 }
1162 }
1163 }
1164 }
1165
1166 fn set_name_value_directive<T>(
1167 &self,
1168 line: &str,
1169 directive: &str,
1170 testfile: &Utf8Path,
1171 line_number: usize,
1172 value: &mut Option<T>,
1173 parse: impl FnOnce(String) -> T,
1174 ) {
1175 if value.is_none() {
1176 *value =
1177 self.parse_name_value_directive(line, directive, testfile, line_number).map(parse);
1178 }
1179 }
1180
1181 fn push_name_value_directive<T>(
1182 &self,
1183 line: &str,
1184 directive: &str,
1185 testfile: &Utf8Path,
1186 line_number: usize,
1187 values: &mut Vec<T>,
1188 parse: impl FnOnce(String) -> T,
1189 ) {
1190 if let Some(value) =
1191 self.parse_name_value_directive(line, directive, testfile, line_number).map(parse)
1192 {
1193 values.push(value);
1194 }
1195 }
1196}
1197
1198fn expand_variables(mut value: String, config: &Config) -> String {
1200 const CWD: &str = "{{cwd}}";
1201 const SRC_BASE: &str = "{{src-base}}";
1202 const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
1203 const RUST_SRC_BASE: &str = "{{rust-src-base}}";
1204 const SYSROOT_BASE: &str = "{{sysroot-base}}";
1205 const TARGET_LINKER: &str = "{{target-linker}}";
1206 const TARGET: &str = "{{target}}";
1207
1208 if value.contains(CWD) {
1209 let cwd = env::current_dir().unwrap();
1210 value = value.replace(CWD, &cwd.to_str().unwrap());
1211 }
1212
1213 if value.contains(SRC_BASE) {
1214 value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
1215 }
1216
1217 if value.contains(TEST_SUITE_BUILD_BASE) {
1218 value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
1219 }
1220
1221 if value.contains(SYSROOT_BASE) {
1222 value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
1223 }
1224
1225 if value.contains(TARGET_LINKER) {
1226 value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
1227 }
1228
1229 if value.contains(TARGET) {
1230 value = value.replace(TARGET, &config.target);
1231 }
1232
1233 if value.contains(RUST_SRC_BASE) {
1234 let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
1235 src_base.try_exists().expect(&*format!("{} should exists", src_base));
1236 let src_base = src_base.read_link_utf8().unwrap_or(src_base);
1237 value = value.replace(RUST_SRC_BASE, &src_base.as_str());
1238 }
1239
1240 value
1241}
1242
1243struct NormalizeRule {
1244 kind: NormalizeKind,
1245 regex: String,
1246 replacement: String,
1247}
1248
1249enum NormalizeKind {
1250 Stdout,
1251 Stderr,
1252 Stderr32bit,
1253 Stderr64bit,
1254}
1255
1256fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
1261 let captures = static_regex!(
1263 r#"(?x) # (verbose mode regex)
1264 ^
1265 \s* # (leading whitespace)
1266 "(?<regex>[^"]*)" # "REGEX"
1267 \s+->\s+ # ->
1268 "(?<replacement>[^"]*)" # "REPLACEMENT"
1269 $
1270 "#
1271 )
1272 .captures(raw_value)?;
1273 let regex = captures["regex"].to_owned();
1274 let replacement = captures["replacement"].to_owned();
1275 let replacement = replacement.replace("\\n", "\n");
1279 Some((regex, replacement))
1280}
1281
1282pub fn extract_llvm_version(version: &str) -> Version {
1292 let version = version.trim();
1295 let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
1296 let version_without_suffix = match version.split_once(uninterested) {
1297 Some((prefix, _suffix)) => prefix,
1298 None => version,
1299 };
1300
1301 let components: Vec<u64> = version_without_suffix
1302 .split('.')
1303 .map(|s| s.parse().expect("llvm version component should consist of only digits"))
1304 .collect();
1305
1306 match &components[..] {
1307 [major] => Version::new(*major, 0, 0),
1308 [major, minor] => Version::new(*major, *minor, 0),
1309 [major, minor, patch] => Version::new(*major, *minor, *patch),
1310 _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
1311 }
1312}
1313
1314pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
1315 let output = Command::new(binary_path).arg("--version").output().ok()?;
1316 if !output.status.success() {
1317 return None;
1318 }
1319 let version = String::from_utf8(output.stdout).ok()?;
1320 for line in version.lines() {
1321 if let Some(version) = line.split("LLVM version ").nth(1) {
1322 return Some(extract_llvm_version(version));
1323 }
1324 }
1325 None
1326}
1327
1328pub fn llvm_has_libzstd(config: &Config) -> bool {
1332 fn is_zstd_in_config(llvm_bin_dir: &Utf8Path) -> Option<()> {
1340 let llvm_config_path = llvm_bin_dir.join("llvm-config");
1341 let output = Command::new(llvm_config_path).arg("--system-libs").output().ok()?;
1342 assert!(output.status.success(), "running llvm-config --system-libs failed");
1343
1344 let libs = String::from_utf8(output.stdout).ok()?;
1345 for lib in libs.split_whitespace() {
1346 if lib.ends_with("libzstd.a") && Utf8Path::new(lib).exists() {
1347 return Some(());
1348 }
1349 }
1350
1351 None
1352 }
1353
1354 #[cfg(unix)]
1364 fn is_lld_built_with_zstd(llvm_bin_dir: &Utf8Path) -> Option<()> {
1365 let lld_path = llvm_bin_dir.join("lld");
1366 if lld_path.exists() {
1367 let lld_symlink_path = llvm_bin_dir.join("ld.lld");
1370 if !lld_symlink_path.exists() {
1371 std::os::unix::fs::symlink(lld_path, &lld_symlink_path).ok()?;
1372 }
1373
1374 let output = Command::new(&lld_symlink_path)
1377 .arg("--compress-debug-sections=zstd")
1378 .output()
1379 .ok()?;
1380 assert!(!output.status.success());
1381
1382 let stderr = String::from_utf8(output.stderr).ok()?;
1385 let zstd_available = !stderr.contains("LLVM was not built with LLVM_ENABLE_ZSTD");
1386
1387 std::fs::remove_file(lld_symlink_path).ok()?;
1390
1391 if zstd_available {
1392 return Some(());
1393 }
1394 }
1395
1396 None
1397 }
1398
1399 #[cfg(not(unix))]
1400 fn is_lld_built_with_zstd(_llvm_bin_dir: &Utf8Path) -> Option<()> {
1401 None
1402 }
1403
1404 if let Some(llvm_bin_dir) = &config.llvm_bin_dir {
1405 if is_zstd_in_config(llvm_bin_dir).is_some() {
1407 return true;
1408 }
1409
1410 if config.target == "x86_64-unknown-linux-gnu"
1418 && config.host == config.target
1419 && is_lld_built_with_zstd(llvm_bin_dir).is_some()
1420 {
1421 return true;
1422 }
1423 }
1424
1425 false
1427}
1428
1429fn extract_version_range<'a, F, VersionTy: Clone>(
1435 line: &'a str,
1436 parse: F,
1437) -> Option<(VersionTy, VersionTy)>
1438where
1439 F: Fn(&'a str) -> Option<VersionTy>,
1440{
1441 let mut splits = line.splitn(2, "- ").map(str::trim);
1442 let min = splits.next().unwrap();
1443 if min.ends_with('-') {
1444 return None;
1445 }
1446
1447 let max = splits.next();
1448
1449 if min.is_empty() {
1450 return None;
1451 }
1452
1453 let min = parse(min)?;
1454 let max = match max {
1455 Some("") => return None,
1456 Some(max) => parse(max)?,
1457 _ => min.clone(),
1458 };
1459
1460 Some((min, max))
1461}
1462
1463pub(crate) fn make_test_description<R: Read>(
1464 config: &Config,
1465 cache: &DirectivesCache,
1466 name: String,
1467 path: &Utf8Path,
1468 filterable_path: &Utf8Path,
1469 src: R,
1470 test_revision: Option<&str>,
1471 poisoned: &mut bool,
1472) -> CollectedTestDesc {
1473 let mut ignore = false;
1474 let mut ignore_message = None;
1475 let mut should_fail = false;
1476
1477 let mut local_poisoned = false;
1478
1479 iter_directives(
1481 config.mode,
1482 &mut local_poisoned,
1483 path,
1484 src,
1485 &mut |directive @ DirectiveLine { line_number, raw_directive: ln, .. }| {
1486 if !directive.applies_to_test_revision(test_revision) {
1487 return;
1488 }
1489
1490 macro_rules! decision {
1491 ($e:expr) => {
1492 match $e {
1493 IgnoreDecision::Ignore { reason } => {
1494 ignore = true;
1495 ignore_message = Some(reason.into());
1496 }
1497 IgnoreDecision::Error { message } => {
1498 error!("{path}:{line_number}: {message}");
1499 *poisoned = true;
1500 return;
1501 }
1502 IgnoreDecision::Continue => {}
1503 }
1504 };
1505 }
1506
1507 decision!(cfg::handle_ignore(config, ln));
1508 decision!(cfg::handle_only(config, ln));
1509 decision!(needs::handle_needs(&cache.needs, config, ln));
1510 decision!(ignore_llvm(config, path, ln, line_number));
1511 decision!(ignore_backends(config, path, ln, line_number));
1512 decision!(needs_backends(config, path, ln, line_number));
1513 decision!(ignore_cdb(config, ln));
1514 decision!(ignore_gdb(config, ln));
1515 decision!(ignore_lldb(config, ln));
1516
1517 if config.target == "wasm32-unknown-unknown"
1518 && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
1519 {
1520 decision!(IgnoreDecision::Ignore {
1521 reason: "ignored on WASM as the run results cannot be checked there".into(),
1522 });
1523 }
1524
1525 should_fail |= config.parse_name_directive(ln, "should-fail");
1526 },
1527 );
1528
1529 if local_poisoned {
1530 eprintln!("errors encountered when trying to make test description: {}", path);
1531 panic!("errors encountered when trying to make test description");
1532 }
1533
1534 let should_panic = match config.mode {
1538 TestMode::Pretty => ShouldPanic::No,
1539 _ if should_fail => ShouldPanic::Yes,
1540 _ => ShouldPanic::No,
1541 };
1542
1543 CollectedTestDesc {
1544 name,
1545 filterable_path: filterable_path.to_owned(),
1546 ignore,
1547 ignore_message,
1548 should_panic,
1549 }
1550}
1551
1552fn ignore_cdb(config: &Config, line: &str) -> IgnoreDecision {
1553 if config.debugger != Some(Debugger::Cdb) {
1554 return IgnoreDecision::Continue;
1555 }
1556
1557 if let Some(actual_version) = config.cdb_version {
1558 if let Some(rest) = line.strip_prefix("min-cdb-version:").map(str::trim) {
1559 let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1560 panic!("couldn't parse version range: {:?}", rest);
1561 });
1562
1563 if actual_version < min_version {
1566 return IgnoreDecision::Ignore {
1567 reason: format!("ignored when the CDB version is lower than {rest}"),
1568 };
1569 }
1570 }
1571 }
1572 IgnoreDecision::Continue
1573}
1574
1575fn ignore_gdb(config: &Config, line: &str) -> IgnoreDecision {
1576 if config.debugger != Some(Debugger::Gdb) {
1577 return IgnoreDecision::Continue;
1578 }
1579
1580 if let Some(actual_version) = config.gdb_version {
1581 if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
1582 let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1583 .unwrap_or_else(|| {
1584 panic!("couldn't parse version range: {:?}", rest);
1585 });
1586
1587 if start_ver != end_ver {
1588 panic!("Expected single GDB version")
1589 }
1590 if actual_version < start_ver {
1593 return IgnoreDecision::Ignore {
1594 reason: format!("ignored when the GDB version is lower than {rest}"),
1595 };
1596 }
1597 } else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
1598 let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1599 .unwrap_or_else(|| {
1600 panic!("couldn't parse version range: {:?}", rest);
1601 });
1602
1603 if max_version < min_version {
1604 panic!("Malformed GDB version range: max < min")
1605 }
1606
1607 if actual_version >= min_version && actual_version <= max_version {
1608 if min_version == max_version {
1609 return IgnoreDecision::Ignore {
1610 reason: format!("ignored when the GDB version is {rest}"),
1611 };
1612 } else {
1613 return IgnoreDecision::Ignore {
1614 reason: format!("ignored when the GDB version is between {rest}"),
1615 };
1616 }
1617 }
1618 }
1619 }
1620 IgnoreDecision::Continue
1621}
1622
1623fn ignore_lldb(config: &Config, line: &str) -> IgnoreDecision {
1624 if config.debugger != Some(Debugger::Lldb) {
1625 return IgnoreDecision::Continue;
1626 }
1627
1628 if let Some(actual_version) = config.lldb_version {
1629 if let Some(rest) = line.strip_prefix("min-lldb-version:").map(str::trim) {
1630 let min_version = rest.parse().unwrap_or_else(|e| {
1631 panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1632 });
1633 if actual_version < min_version {
1636 return IgnoreDecision::Ignore {
1637 reason: format!("ignored when the LLDB version is {rest}"),
1638 };
1639 }
1640 }
1641 }
1642 IgnoreDecision::Continue
1643}
1644
1645fn ignore_backends(
1646 config: &Config,
1647 path: &Utf8Path,
1648 line: &str,
1649 line_number: usize,
1650) -> IgnoreDecision {
1651 if let Some(backends_to_ignore) =
1652 config.parse_name_value_directive(line, "ignore-backends", path, line_number)
1653 {
1654 for backend in backends_to_ignore.split_whitespace().map(|backend| {
1655 match CodegenBackend::try_from(backend) {
1656 Ok(backend) => backend,
1657 Err(error) => {
1658 panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1659 }
1660 }
1661 }) {
1662 if config.default_codegen_backend == backend {
1663 return IgnoreDecision::Ignore {
1664 reason: format!("{} backend is marked as ignore", backend.as_str()),
1665 };
1666 }
1667 }
1668 }
1669 IgnoreDecision::Continue
1670}
1671
1672fn needs_backends(
1673 config: &Config,
1674 path: &Utf8Path,
1675 line: &str,
1676 line_number: usize,
1677) -> IgnoreDecision {
1678 if let Some(needed_backends) =
1679 config.parse_name_value_directive(line, "needs-backends", path, line_number)
1680 {
1681 if !needed_backends
1682 .split_whitespace()
1683 .map(|backend| match CodegenBackend::try_from(backend) {
1684 Ok(backend) => backend,
1685 Err(error) => {
1686 panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1687 }
1688 })
1689 .any(|backend| config.default_codegen_backend == backend)
1690 {
1691 return IgnoreDecision::Ignore {
1692 reason: format!(
1693 "{} backend is not part of required backends",
1694 config.default_codegen_backend.as_str()
1695 ),
1696 };
1697 }
1698 }
1699 IgnoreDecision::Continue
1700}
1701
1702fn ignore_llvm(config: &Config, path: &Utf8Path, line: &str, line_number: usize) -> IgnoreDecision {
1703 if let Some(needed_components) =
1704 config.parse_name_value_directive(line, "needs-llvm-components", path, line_number)
1705 {
1706 let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1707 if let Some(missing_component) = needed_components
1708 .split_whitespace()
1709 .find(|needed_component| !components.contains(needed_component))
1710 {
1711 if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1712 panic!(
1713 "missing LLVM component {}, and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {}",
1714 missing_component, path
1715 );
1716 }
1717 return IgnoreDecision::Ignore {
1718 reason: format!("ignored when the {missing_component} LLVM component is missing"),
1719 };
1720 }
1721 }
1722 if let Some(actual_version) = &config.llvm_version {
1723 if let Some(version_string) =
1726 config.parse_name_value_directive(line, "min-llvm-version", path, line_number)
1727 {
1728 let min_version = extract_llvm_version(&version_string);
1729 if *actual_version < min_version {
1731 return IgnoreDecision::Ignore {
1732 reason: format!(
1733 "ignored when the LLVM version {actual_version} is older than {min_version}"
1734 ),
1735 };
1736 }
1737 } else if let Some(version_string) =
1738 config.parse_name_value_directive(line, "max-llvm-major-version", path, line_number)
1739 {
1740 let max_version = extract_llvm_version(&version_string);
1741 if actual_version.major > max_version.major {
1743 return IgnoreDecision::Ignore {
1744 reason: format!(
1745 "ignored when the LLVM version ({actual_version}) is newer than major\
1746 version {}",
1747 max_version.major
1748 ),
1749 };
1750 }
1751 } else if let Some(version_string) =
1752 config.parse_name_value_directive(line, "min-system-llvm-version", path, line_number)
1753 {
1754 let min_version = extract_llvm_version(&version_string);
1755 if config.system_llvm && *actual_version < min_version {
1758 return IgnoreDecision::Ignore {
1759 reason: format!(
1760 "ignored when the system LLVM version {actual_version} is older than {min_version}"
1761 ),
1762 };
1763 }
1764 } else if let Some(version_range) =
1765 config.parse_name_value_directive(line, "ignore-llvm-version", path, line_number)
1766 {
1767 let (v_min, v_max) =
1769 extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1770 .unwrap_or_else(|| {
1771 panic!("couldn't parse version range: \"{version_range}\"");
1772 });
1773 if v_max < v_min {
1774 panic!("malformed LLVM version range where {v_max} < {v_min}")
1775 }
1776 if *actual_version >= v_min && *actual_version <= v_max {
1778 if v_min == v_max {
1779 return IgnoreDecision::Ignore {
1780 reason: format!("ignored when the LLVM version is {actual_version}"),
1781 };
1782 } else {
1783 return IgnoreDecision::Ignore {
1784 reason: format!(
1785 "ignored when the LLVM version is between {v_min} and {v_max}"
1786 ),
1787 };
1788 }
1789 }
1790 } else if let Some(version_string) =
1791 config.parse_name_value_directive(line, "exact-llvm-major-version", path, line_number)
1792 {
1793 let version = extract_llvm_version(&version_string);
1795 if actual_version.major != version.major {
1796 return IgnoreDecision::Ignore {
1797 reason: format!(
1798 "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1799 actual_version.major, version.major
1800 ),
1801 };
1802 }
1803 }
1804 }
1805 IgnoreDecision::Continue
1806}
1807
1808enum IgnoreDecision {
1809 Ignore { reason: String },
1810 Continue,
1811 Error { message: String },
1812}