1use std::collections::HashSet;
2use std::process::Command;
3use std::{env, fs};
4
5use camino::{Utf8Path, Utf8PathBuf};
6use semver::Version;
7use tracing::*;
8
9use crate::common::{CodegenBackend, Config, Debugger, FailMode, PassMode, RunFailMode, TestMode};
10use crate::debuggers::{extract_cdb_version, extract_gdb_version};
11use crate::directives::auxiliary::parse_and_update_aux;
12pub(crate) use crate::directives::auxiliary::{AuxCrate, AuxProps};
13use crate::directives::directive_names::{
14 KNOWN_DIRECTIVE_NAMES_SET, KNOWN_HTMLDOCCK_DIRECTIVE_NAMES, KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
15};
16pub(crate) use crate::directives::file::FileDirectives;
17use crate::directives::handlers::DIRECTIVE_HANDLERS_MAP;
18use crate::directives::line::DirectiveLine;
19use crate::directives::needs::CachedNeedsConditions;
20use crate::edition::{Edition, parse_edition};
21use crate::errors::ErrorKind;
22use crate::executor::{CollectedTestDesc, ShouldFail};
23use crate::util::static_regex;
24use crate::{fatal, help};
25
26mod auxiliary;
27mod cfg;
28mod directive_names;
29mod file;
30mod handlers;
31mod line;
32pub(crate) use line::line_directive;
33mod line_number;
34pub(crate) use line_number::LineNumber;
35mod needs;
36#[cfg(test)]
37mod tests;
38
39pub(crate) struct DirectivesCache {
40 cfg_conditions: cfg::PreparedConditions,
43 needs: CachedNeedsConditions,
44}
45
46impl DirectivesCache {
47 pub(crate) fn load(config: &Config) -> Self {
48 Self {
49 cfg_conditions: cfg::prepare_conditions(config),
50 needs: CachedNeedsConditions::load(config),
51 }
52 }
53}
54
55#[derive(Default)]
58pub(crate) struct EarlyProps {
59 pub(crate) revisions: Vec<String>,
60}
61
62impl EarlyProps {
63 pub(crate) fn from_file_directives(
64 config: &Config,
65 file_directives: &FileDirectives<'_>,
66 ) -> Self {
67 let mut props = EarlyProps::default();
68
69 iter_directives(
70 config,
71 file_directives,
72 &mut |ln: &DirectiveLine<'_>| {
74 config.parse_and_update_revisions(ln, &mut props.revisions);
75 },
76 );
77
78 props
79 }
80}
81
82#[derive(Clone, Debug)]
83pub(crate) struct TestProps {
84 pub(crate) error_patterns: Vec<String>,
86 pub(crate) regex_error_patterns: Vec<String>,
88 pub(crate) edition: Option<Edition>,
92 pub(crate) compile_flags: Vec<String>,
94 pub(crate) run_flags: Vec<String>,
96 pub(crate) doc_flags: Vec<String>,
98 pub(crate) pp_exact: Option<Utf8PathBuf>,
101 pub(crate) aux: AuxProps,
103 pub(crate) rustc_env: Vec<(String, String)>,
105 pub(crate) unset_rustc_env: Vec<String>,
108 pub(crate) exec_env: Vec<(String, String)>,
110 pub(crate) unset_exec_env: Vec<String>,
113 pub(crate) build_aux_docs: bool,
115 pub(crate) unique_doc_out_dir: bool,
118 pub(crate) force_host: bool,
120 pub(crate) check_stdout: bool,
122 pub(crate) check_run_results: bool,
124 pub(crate) dont_check_compiler_stdout: bool,
126 pub(crate) dont_check_compiler_stderr: bool,
128 pub(crate) no_prefer_dynamic: bool,
134 pub(crate) pretty_mode: String,
136 pub(crate) pretty_compare_only: bool,
138 pub(crate) forbid_output: Vec<String>,
140 pub(crate) revisions: Vec<String>,
142 pub(crate) incremental_dir: Option<Utf8PathBuf>,
147 pub(crate) incremental: bool,
162 pub(crate) known_bug: bool,
168 pass_mode: Option<PassMode>,
170 ignore_pass: bool,
172 pub(crate) fail_mode: Option<FailMode>,
174 pub(crate) check_test_line_numbers_match: bool,
176 pub(crate) normalize_stdout: Vec<(String, String)>,
178 pub(crate) normalize_stderr: Vec<(String, String)>,
179 pub(crate) failure_status: Option<i32>,
180 pub(crate) dont_check_failure_status: bool,
182 pub(crate) run_rustfix: bool,
185 pub(crate) rustfix_only_machine_applicable: bool,
187 pub(crate) assembly_output: Option<String>,
188 pub(crate) stderr_per_bitwidth: bool,
190 pub(crate) mir_unit_test: Option<String>,
192 pub(crate) remap_src_base: bool,
195 pub(crate) llvm_cov_flags: Vec<String>,
198 pub(crate) filecheck_flags: Vec<String>,
200 pub(crate) no_auto_check_cfg: bool,
202 pub(crate) add_minicore: bool,
205 pub(crate) minicore_compile_flags: Vec<String>,
207 pub(crate) dont_require_annotations: HashSet<ErrorKind>,
209 pub(crate) disable_gdb_pretty_printers: bool,
211 pub(crate) compare_output_by_lines: bool,
213}
214
215mod directives {
216 pub(crate) const ERROR_PATTERN: &str = "error-pattern";
217 pub(crate) const REGEX_ERROR_PATTERN: &str = "regex-error-pattern";
218 pub(crate) const COMPILE_FLAGS: &str = "compile-flags";
219 pub(crate) const RUN_FLAGS: &str = "run-flags";
220 pub(crate) const DOC_FLAGS: &str = "doc-flags";
221 pub(crate) const BUILD_AUX_DOCS: &str = "build-aux-docs";
222 pub(crate) const UNIQUE_DOC_OUT_DIR: &str = "unique-doc-out-dir";
223 pub(crate) const FORCE_HOST: &str = "force-host";
224 pub(crate) const CHECK_STDOUT: &str = "check-stdout";
225 pub(crate) const CHECK_RUN_RESULTS: &str = "check-run-results";
226 pub(crate) const DONT_CHECK_COMPILER_STDOUT: &str = "dont-check-compiler-stdout";
227 pub(crate) const DONT_CHECK_COMPILER_STDERR: &str = "dont-check-compiler-stderr";
228 pub(crate) const DONT_REQUIRE_ANNOTATIONS: &str = "dont-require-annotations";
229 pub(crate) const NO_PREFER_DYNAMIC: &str = "no-prefer-dynamic";
230 pub(crate) const PRETTY_MODE: &str = "pretty-mode";
231 pub(crate) const PRETTY_COMPARE_ONLY: &str = "pretty-compare-only";
232 pub(crate) const AUX_BIN: &str = "aux-bin";
233 pub(crate) const AUX_BUILD: &str = "aux-build";
234 pub(crate) const AUX_CRATE: &str = "aux-crate";
235 pub(crate) const PROC_MACRO: &str = "proc-macro";
236 pub(crate) const AUX_CODEGEN_BACKEND: &str = "aux-codegen-backend";
237 pub(crate) const EXEC_ENV: &str = "exec-env";
238 pub(crate) const RUSTC_ENV: &str = "rustc-env";
239 pub(crate) const UNSET_EXEC_ENV: &str = "unset-exec-env";
240 pub(crate) const UNSET_RUSTC_ENV: &str = "unset-rustc-env";
241 pub(crate) const FORBID_OUTPUT: &str = "forbid-output";
242 pub(crate) const CHECK_TEST_LINE_NUMBERS_MATCH: &str = "check-test-line-numbers-match";
243 pub(crate) const IGNORE_PASS: &str = "ignore-pass";
244 pub(crate) const FAILURE_STATUS: &str = "failure-status";
245 pub(crate) const DONT_CHECK_FAILURE_STATUS: &str = "dont-check-failure-status";
246 pub(crate) const RUN_RUSTFIX: &str = "run-rustfix";
247 pub(crate) const RUSTFIX_ONLY_MACHINE_APPLICABLE: &str = "rustfix-only-machine-applicable";
248 pub(crate) const ASSEMBLY_OUTPUT: &str = "assembly-output";
249 pub(crate) const STDERR_PER_BITWIDTH: &str = "stderr-per-bitwidth";
250 pub(crate) const INCREMENTAL: &str = "incremental";
251 pub(crate) const KNOWN_BUG: &str = "known-bug";
252 pub(crate) const TEST_MIR_PASS: &str = "test-mir-pass";
253 pub(crate) const REMAP_SRC_BASE: &str = "remap-src-base";
254 pub(crate) const LLVM_COV_FLAGS: &str = "llvm-cov-flags";
255 pub(crate) const FILECHECK_FLAGS: &str = "filecheck-flags";
256 pub(crate) const NO_AUTO_CHECK_CFG: &str = "no-auto-check-cfg";
257 pub(crate) const ADD_MINICORE: &str = "add-minicore";
258 pub(crate) const MINICORE_COMPILE_FLAGS: &str = "minicore-compile-flags";
259 pub(crate) const DISABLE_GDB_PRETTY_PRINTERS: &str = "disable-gdb-pretty-printers";
260 pub(crate) const COMPARE_OUTPUT_BY_LINES: &str = "compare-output-by-lines";
261}
262
263impl TestProps {
264 pub(crate) fn new() -> Self {
265 TestProps {
266 error_patterns: vec![],
267 regex_error_patterns: vec![],
268 edition: None,
269 compile_flags: vec![],
270 run_flags: vec![],
271 doc_flags: vec![],
272 pp_exact: None,
273 aux: Default::default(),
274 revisions: vec![],
275 rustc_env: vec![
276 ("RUSTC_ICE".to_string(), "0".to_string()),
277 ("RUST_BACKTRACE".to_string(), "short".to_string()),
278 ],
279 unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
280 exec_env: vec![],
281 unset_exec_env: vec![],
282 build_aux_docs: false,
283 unique_doc_out_dir: false,
284 force_host: false,
285 check_stdout: false,
286 check_run_results: false,
287 dont_check_compiler_stdout: false,
288 dont_check_compiler_stderr: false,
289 no_prefer_dynamic: false,
290 pretty_mode: "normal".to_string(),
291 pretty_compare_only: false,
292 forbid_output: vec![],
293 incremental_dir: None,
294 incremental: false,
295 known_bug: false,
296 pass_mode: None,
297 fail_mode: None,
298 ignore_pass: false,
299 check_test_line_numbers_match: false,
300 normalize_stdout: vec![],
301 normalize_stderr: vec![],
302 failure_status: None,
303 dont_check_failure_status: false,
304 run_rustfix: false,
305 rustfix_only_machine_applicable: false,
306 assembly_output: None,
307 stderr_per_bitwidth: false,
308 mir_unit_test: None,
309 remap_src_base: false,
310 llvm_cov_flags: vec![],
311 filecheck_flags: vec![],
312 no_auto_check_cfg: false,
313 add_minicore: false,
314 minicore_compile_flags: vec![],
315 dont_require_annotations: Default::default(),
316 disable_gdb_pretty_printers: false,
317 compare_output_by_lines: false,
318 }
319 }
320
321 pub(crate) fn from_aux_file(
322 &self,
323 testfile: &Utf8Path,
324 revision: Option<&str>,
325 config: &Config,
326 ) -> Self {
327 let mut props = TestProps::new();
328
329 props.incremental_dir = self.incremental_dir.clone();
331 props.ignore_pass = true;
332 props.load_from(testfile, revision, config);
333
334 props
335 }
336
337 pub(crate) fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
338 let mut props = TestProps::new();
339 props.load_from(testfile, revision, config);
340 props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
341
342 match (props.pass_mode, props.fail_mode) {
343 (None, None) if config.mode == TestMode::Ui => props.fail_mode = Some(FailMode::Check),
344 (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
345 _ => {}
346 }
347
348 props
349 }
350
351 fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
356 if !testfile.is_dir() {
357 let file_contents = fs::read_to_string(testfile).unwrap();
358 let file_directives = FileDirectives::from_file_contents(testfile, &file_contents);
359
360 iter_directives(
361 config,
362 &file_directives,
363 &mut |ln: &DirectiveLine<'_>| {
365 if !ln.applies_to_test_revision(test_revision) {
366 return;
367 }
368
369 if let Some(handler) = DIRECTIVE_HANDLERS_MAP.get(ln.name) {
370 handler.handle(config, ln, self);
371 }
372 },
373 );
374 }
375
376 if config.mode == TestMode::Incremental {
377 self.incremental = true;
378 }
379
380 if config.mode == TestMode::Crashes {
381 self.rustc_env = vec![
385 ("RUST_BACKTRACE".to_string(), "0".to_string()),
386 ("RUSTC_ICE".to_string(), "0".to_string()),
387 ];
388 }
389
390 for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
391 if let Ok(val) = env::var(key) {
392 if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
393 self.exec_env.push(((*key).to_owned(), val))
394 }
395 }
396 }
397
398 if let Some(edition) = self.edition.or(config.edition) {
399 self.compile_flags.insert(0, format!("--edition={edition}"));
402 }
403 }
404
405 fn update_fail_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
406 let check_ui = |mode: &str| {
407 if config.mode != TestMode::Ui && config.mode != TestMode::Crashes {
409 panic!("`{}-fail` directive is only supported in UI tests", mode);
410 }
411 };
412 let fail_mode = if config.parse_name_directive(ln, "check-fail") {
413 check_ui("check");
414 Some(FailMode::Check)
415 } else if config.parse_name_directive(ln, "build-fail") {
416 check_ui("build");
417 Some(FailMode::Build)
418 } else if config.parse_name_directive(ln, "run-fail") {
419 check_ui("run");
420 Some(FailMode::Run(RunFailMode::Fail))
421 } else if config.parse_name_directive(ln, "run-crash") {
422 check_ui("run");
423 Some(FailMode::Run(RunFailMode::Crash))
424 } else if config.parse_name_directive(ln, "run-fail-or-crash") {
425 check_ui("run");
426 Some(FailMode::Run(RunFailMode::FailOrCrash))
427 } else {
428 None
429 };
430 match (self.fail_mode, fail_mode) {
431 (None, Some(_)) => self.fail_mode = fail_mode,
432 (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
433 (_, None) => {}
434 }
435 }
436
437 fn update_pass_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
438 let check_no_run = |s| match (config.mode, s) {
439 (TestMode::Ui, _) => (),
440 (TestMode::Crashes, _) => (),
441 (TestMode::Codegen, "build-pass") => (),
442 (TestMode::Incremental, _) => {
443 if self.revisions.iter().any(|r| !r.starts_with("bfail")) {
446 panic!("`{s}` directive is only supported in `bfail` incremental tests")
447 }
448 }
449 (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
450 };
451 let pass_mode = if config.parse_name_directive(ln, "check-pass") {
452 check_no_run("check-pass");
453 Some(PassMode::Check)
454 } else if config.parse_name_directive(ln, "build-pass") {
455 check_no_run("build-pass");
456 Some(PassMode::Build)
457 } else if config.parse_name_directive(ln, "run-pass") {
458 check_no_run("run-pass");
459 Some(PassMode::Run)
460 } else {
461 None
462 };
463 match (self.pass_mode, pass_mode) {
464 (None, Some(_)) => self.pass_mode = pass_mode,
465 (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
466 (_, None) => {}
467 }
468 }
469
470 pub(crate) fn pass_mode(&self, config: &Config) -> Option<PassMode> {
471 if !self.ignore_pass && self.fail_mode.is_none() {
472 if let mode @ Some(_) = config.force_pass_mode {
473 return mode;
474 }
475 }
476 self.pass_mode
477 }
478
479 pub(crate) fn local_pass_mode(&self) -> Option<PassMode> {
481 self.pass_mode
482 }
483
484 fn update_add_minicore(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
485 let add_minicore = config.parse_name_directive(ln, directives::ADD_MINICORE);
486 if add_minicore {
487 if !matches!(config.mode, TestMode::Ui | TestMode::Codegen | TestMode::Assembly) {
488 panic!(
489 "`add-minicore` is currently only supported for ui, codegen and assembly test modes"
490 );
491 }
492
493 if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
496 panic!("`add-minicore` cannot be used to run the test binary");
499 }
500
501 self.add_minicore = add_minicore;
502 }
503 }
504}
505
506pub(crate) fn do_early_directives_check(
507 mode: TestMode,
508 file_directives: &FileDirectives<'_>,
509) -> Result<(), String> {
510 let testfile = file_directives.path;
511
512 for directive_line @ DirectiveLine { line_number, .. } in &file_directives.lines {
513 let CheckDirectiveResult { is_known_directive, trailing_directive } =
514 check_directive(directive_line, mode);
515
516 if !is_known_directive {
517 return Err(format!(
518 "ERROR: unknown compiletest directive `{directive}` at {testfile}:{line_number}",
519 directive = directive_line.display(),
520 ));
521 }
522
523 if let Some(trailing_directive) = &trailing_directive {
524 return Err(format!(
525 "ERROR: detected trailing compiletest directive `{trailing_directive}` at {testfile}:{line_number}\n\
526 HELP: put the directive on its own line: `//@ {trailing_directive}`"
527 ));
528 }
529 }
530
531 Ok(())
532}
533
534pub(crate) struct CheckDirectiveResult<'ln> {
535 is_known_directive: bool,
536 trailing_directive: Option<&'ln str>,
537}
538
539fn check_directive<'a>(
540 directive_ln: &DirectiveLine<'a>,
541 mode: TestMode,
542) -> CheckDirectiveResult<'a> {
543 let &DirectiveLine { name: directive_name, .. } = directive_ln;
544
545 let is_known_directive = KNOWN_DIRECTIVE_NAMES_SET.contains(&directive_name)
546 || match mode {
547 TestMode::RustdocHtml => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
548 TestMode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
549 _ => false,
550 };
551
552 let trailing_directive = directive_ln
556 .remark_after_space()
557 .map(|remark| remark.trim_start().split(' ').next().unwrap())
558 .filter(|token| KNOWN_DIRECTIVE_NAMES_SET.contains(token));
559
560 CheckDirectiveResult { is_known_directive, trailing_directive }
566}
567
568fn iter_directives(
569 config: &Config,
570 file_directives: &FileDirectives<'_>,
571 it: &mut dyn FnMut(&DirectiveLine<'_>),
572) {
573 let testfile = file_directives.path;
574
575 let extra_directives = match config.mode {
576 TestMode::CoverageRun => {
577 vec![
582 "//@ needs-profiler-runtime",
583 "//@ ignore-cross-compile",
587 ]
588 }
589 TestMode::Codegen if !file_directives.has_explicit_no_std_core_attribute => {
590 vec!["//@ needs-target-std"]
597 }
598 TestMode::Ui if config.parallel_frontend_enabled() => {
599 vec!["//@ compare-output-by-lines"]
602 }
603
604 _ => {
605 vec![]
607 }
608 };
609
610 for directive_str in extra_directives {
611 let directive_line = line_directive(testfile, LineNumber::ZERO, directive_str)
612 .unwrap_or_else(|| panic!("bad extra-directive line: {directive_str:?}"));
613 it(&directive_line);
614 }
615
616 for directive_line in &file_directives.lines {
617 it(directive_line);
618 }
619}
620
621impl Config {
622 fn parse_and_update_revisions(&self, line: &DirectiveLine<'_>, existing: &mut Vec<String>) {
623 const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
624 "true", "false",
628 ];
629
630 const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
631 ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
632
633 if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
634 let &DirectiveLine { file_path: testfile, .. } = line;
635
636 if self.mode == TestMode::RunMake {
637 panic!("`run-make` mode tests do not support revisions: {}", testfile);
638 }
639
640 let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
641 for revision in raw.split_whitespace() {
642 if !duplicates.insert(revision.to_string()) {
643 panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
644 }
645
646 if FORBIDDEN_REVISION_NAMES.contains(&revision) {
647 panic!(
648 "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
649 revision, raw, testfile
650 );
651 }
652
653 if matches!(self.mode, TestMode::Assembly | TestMode::Codegen | TestMode::MirOpt)
654 && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
655 {
656 panic!(
657 "revision name `{revision}` is not permitted in a test suite that uses \
658 `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
659 prefix: `{revision}` in line `{}`: {}",
660 raw, testfile
661 );
662 }
663
664 existing.push(revision.to_string());
665 }
666 }
667 }
668
669 fn parse_env(nv: String) -> (String, String) {
670 let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
674 let name = name.trim();
677 (name.to_owned(), value.to_owned())
678 }
679
680 fn parse_pp_exact(&self, line: &DirectiveLine<'_>) -> Option<Utf8PathBuf> {
681 if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
682 Some(Utf8PathBuf::from(&s))
683 } else if self.parse_name_directive(line, "pp-exact") {
684 line.file_path.file_name().map(Utf8PathBuf::from)
685 } else {
686 None
687 }
688 }
689
690 fn parse_custom_normalization(&self, line: &DirectiveLine<'_>) -> Option<NormalizeRule> {
691 let &DirectiveLine { name, .. } = line;
692
693 let kind = match name {
694 "normalize-stdout" => NormalizeKind::Stdout,
695 "normalize-stderr" => NormalizeKind::Stderr,
696 "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
697 "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
698 _ => return None,
699 };
700
701 let Some((regex, replacement)) = line.value_after_colon().and_then(parse_normalize_rule)
702 else {
703 error!("couldn't parse custom normalization rule: `{}`", line.display());
704 help!("expected syntax is: `{name}: \"REGEX\" -> \"REPLACEMENT\"`");
705 panic!("invalid normalization rule detected");
706 };
707 Some(NormalizeRule { kind, regex, replacement })
708 }
709
710 fn parse_name_directive(&self, line: &DirectiveLine<'_>, directive: &str) -> bool {
711 line.name == directive
715 }
716
717 fn parse_name_value_directive(
718 &self,
719 line: &DirectiveLine<'_>,
720 directive: &str,
721 ) -> Option<String> {
722 let &DirectiveLine { file_path, line_number, .. } = line;
723
724 if line.name != directive {
725 return None;
726 };
727
728 let value = line.value_after_colon()?;
732 debug!("{}: {}", directive, value);
733 let value = expand_variables(value.to_owned(), self);
734
735 if value.is_empty() {
736 error!("{file_path}:{line_number}: empty value for directive `{directive}`");
737 help!("expected syntax is: `{directive}: value`");
738 panic!("empty directive value detected");
739 }
740
741 Some(value)
742 }
743
744 fn set_name_directive(&self, line: &DirectiveLine<'_>, directive: &str, value: &mut bool) {
745 *value = *value || self.parse_name_directive(line, directive);
747 }
748
749 fn set_name_value_directive<T>(
750 &self,
751 line: &DirectiveLine<'_>,
752 directive: &str,
753 value: &mut Option<T>,
754 parse: impl FnOnce(String) -> T,
755 ) {
756 if value.is_none() {
757 *value = self.parse_name_value_directive(line, directive).map(parse);
758 }
759 }
760
761 fn push_name_value_directive<T>(
762 &self,
763 line: &DirectiveLine<'_>,
764 directive: &str,
765 values: &mut Vec<T>,
766 parse: impl FnOnce(String) -> T,
767 ) {
768 if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
769 values.push(value);
770 }
771 }
772}
773
774fn expand_variables(mut value: String, config: &Config) -> String {
776 const CWD: &str = "{{cwd}}";
777 const SRC_BASE: &str = "{{src-base}}";
778 const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
779 const RUST_SRC_BASE: &str = "{{rust-src-base}}";
780 const SYSROOT_BASE: &str = "{{sysroot-base}}";
781 const TARGET_LINKER: &str = "{{target-linker}}";
782 const TARGET: &str = "{{target}}";
783
784 if value.contains(CWD) {
785 let cwd = env::current_dir().unwrap();
786 value = value.replace(CWD, &cwd.to_str().unwrap());
787 }
788
789 if value.contains(SRC_BASE) {
790 value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
791 }
792
793 if value.contains(TEST_SUITE_BUILD_BASE) {
794 value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
795 }
796
797 if value.contains(SYSROOT_BASE) {
798 value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
799 }
800
801 if value.contains(TARGET_LINKER) {
802 value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
803 }
804
805 if value.contains(TARGET) {
806 value = value.replace(TARGET, &config.target);
807 }
808
809 if value.contains(RUST_SRC_BASE) {
810 let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
811 src_base.try_exists().expect(&*format!("{} should exists", src_base));
812 let src_base = src_base.read_link_utf8().unwrap_or(src_base);
813 value = value.replace(RUST_SRC_BASE, &src_base.as_str());
814 }
815
816 value
817}
818
819struct NormalizeRule {
820 kind: NormalizeKind,
821 regex: String,
822 replacement: String,
823}
824
825enum NormalizeKind {
826 Stdout,
827 Stderr,
828 Stderr32bit,
829 Stderr64bit,
830}
831
832fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
837 let captures = static_regex!(
839 r#"(?x) # (verbose mode regex)
840 ^
841 \s* # (leading whitespace)
842 "(?<regex>[^"]*)" # "REGEX"
843 \s+->\s+ # ->
844 "(?<replacement>[^"]*)" # "REPLACEMENT"
845 $
846 "#
847 )
848 .captures(raw_value)?;
849 let regex = captures["regex"].to_owned();
850 let replacement = captures["replacement"].to_owned();
851 let replacement = replacement.replace("\\n", "\n");
855 Some((regex, replacement))
856}
857
858pub(crate) fn extract_llvm_version(version: &str) -> Version {
868 let version = version.trim();
871 let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
872 let version_without_suffix = match version.split_once(uninterested) {
873 Some((prefix, _suffix)) => prefix,
874 None => version,
875 };
876
877 let components: Vec<u64> = version_without_suffix
878 .split('.')
879 .map(|s| s.parse().expect("llvm version component should consist of only digits"))
880 .collect();
881
882 match &components[..] {
883 [major] => Version::new(*major, 0, 0),
884 [major, minor] => Version::new(*major, *minor, 0),
885 [major, minor, patch] => Version::new(*major, *minor, *patch),
886 _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
887 }
888}
889
890pub(crate) fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
891 let output = Command::new(binary_path).arg("--version").output().ok()?;
892 if !output.status.success() {
893 return None;
894 }
895 let version = String::from_utf8(output.stdout).ok()?;
896 for line in version.lines() {
897 if let Some(version) = line.split("LLVM version ").nth(1) {
898 return Some(extract_llvm_version(version));
899 }
900 }
901 None
902}
903
904fn extract_version_range<'a, F, VersionTy: Clone>(
910 line: &'a str,
911 parse: F,
912) -> Option<(VersionTy, VersionTy)>
913where
914 F: Fn(&'a str) -> Option<VersionTy>,
915{
916 let mut splits = line.splitn(2, "- ").map(str::trim);
917 let min = splits.next().unwrap();
918 if min.ends_with('-') {
919 return None;
920 }
921
922 let max = splits.next();
923
924 if min.is_empty() {
925 return None;
926 }
927
928 let min = parse(min)?;
929 let max = match max {
930 Some("") => return None,
931 Some(max) => parse(max)?,
932 _ => min.clone(),
933 };
934
935 Some((min, max))
936}
937
938pub(crate) fn make_test_description(
939 config: &Config,
940 cache: &DirectivesCache,
941 name: String,
942 path: &Utf8Path,
943 filterable_path: &Utf8Path,
944 file_directives: &FileDirectives<'_>,
945 test_revision: Option<&str>,
946 poisoned: &mut bool,
947 aux_props: &mut AuxProps,
948) -> CollectedTestDesc {
949 let mut ignore = false;
950 let mut ignore_message = None;
951 let mut should_fail = false;
952
953 iter_directives(config, file_directives, &mut |ln @ &DirectiveLine { line_number, .. }| {
955 if !ln.applies_to_test_revision(test_revision) {
956 return;
957 }
958
959 parse_and_update_aux(config, ln, aux_props);
961
962 macro_rules! decision {
963 ($e:expr) => {
964 match $e {
965 IgnoreDecision::Ignore { reason } => {
966 ignore = true;
967 ignore_message = Some(reason.into());
968 }
969 IgnoreDecision::Error { message } => {
970 error!("{path}:{line_number}: {message}");
971 *poisoned = true;
972 return;
973 }
974 IgnoreDecision::Continue => {}
975 }
976 };
977 }
978
979 decision!(cfg::handle_ignore(&cache.cfg_conditions, ln));
980 decision!(cfg::handle_only(&cache.cfg_conditions, ln));
981 decision!(needs::handle_needs(&cache.needs, config, ln));
982 decision!(ignore_llvm(config, ln));
983 decision!(ignore_backends(config, ln));
984 decision!(needs_backends(config, ln));
985 decision!(ignore_cdb(config, ln));
986 decision!(ignore_gdb(config, ln));
987 decision!(ignore_lldb(config, ln));
988 decision!(ignore_parallel_frontend(config, ln));
989
990 if config.target == "wasm32-unknown-unknown"
991 && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
992 {
993 decision!(IgnoreDecision::Ignore {
994 reason: "ignored on WASM as the run results cannot be checked there".into(),
995 });
996 }
997
998 should_fail |= config.parse_name_directive(ln, "should-fail");
999 });
1000
1001 let should_fail = if should_fail && config.mode != TestMode::Pretty {
1005 ShouldFail::Yes
1006 } else {
1007 ShouldFail::No
1008 };
1009
1010 CollectedTestDesc {
1011 name,
1012 filterable_path: filterable_path.to_owned(),
1013 ignore,
1014 ignore_message,
1015 should_fail,
1016 }
1017}
1018
1019fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1020 if config.debugger != Some(Debugger::Cdb) {
1021 return IgnoreDecision::Continue;
1022 }
1023
1024 if let Some(actual_version) = config.cdb_version {
1025 if line.name == "min-cdb-version"
1026 && let Some(rest) = line.value_after_colon().map(str::trim)
1027 {
1028 let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1029 panic!("couldn't parse version range: {:?}", rest);
1030 });
1031
1032 if actual_version < min_version {
1035 return IgnoreDecision::Ignore {
1036 reason: format!("ignored when the CDB version is lower than {rest}"),
1037 };
1038 }
1039 }
1040 }
1041 IgnoreDecision::Continue
1042}
1043
1044fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1045 if config.debugger != Some(Debugger::Gdb) {
1046 return IgnoreDecision::Continue;
1047 }
1048
1049 if let Some(actual_version) = config.gdb_version {
1050 if line.name == "min-gdb-version"
1051 && let Some(rest) = line.value_after_colon().map(str::trim)
1052 {
1053 let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1054 .unwrap_or_else(|| {
1055 panic!("couldn't parse version range: {:?}", rest);
1056 });
1057
1058 if start_ver != end_ver {
1059 panic!("Expected single GDB version")
1060 }
1061 if actual_version < start_ver {
1064 return IgnoreDecision::Ignore {
1065 reason: format!("ignored when the GDB version is lower than {rest}"),
1066 };
1067 }
1068 } else if line.name == "ignore-gdb-version"
1069 && let Some(rest) = line.value_after_colon().map(str::trim)
1070 {
1071 let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1072 .unwrap_or_else(|| {
1073 panic!("couldn't parse version range: {:?}", rest);
1074 });
1075
1076 if max_version < min_version {
1077 panic!("Malformed GDB version range: max < min")
1078 }
1079
1080 if actual_version >= min_version && actual_version <= max_version {
1081 if min_version == max_version {
1082 return IgnoreDecision::Ignore {
1083 reason: format!("ignored when the GDB version is {rest}"),
1084 };
1085 } else {
1086 return IgnoreDecision::Ignore {
1087 reason: format!("ignored when the GDB version is between {rest}"),
1088 };
1089 }
1090 }
1091 }
1092 }
1093 IgnoreDecision::Continue
1094}
1095
1096fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1097 if config.debugger != Some(Debugger::Lldb) {
1098 return IgnoreDecision::Continue;
1099 }
1100
1101 if let Some(actual_version) = config.lldb_version {
1102 if line.name == "min-lldb-version"
1103 && let Some(rest) = line.value_after_colon().map(str::trim)
1104 {
1105 let min_version = rest.parse().unwrap_or_else(|e| {
1106 panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1107 });
1108 if actual_version < min_version {
1111 return IgnoreDecision::Ignore {
1112 reason: format!("ignored when the LLDB version is {rest}"),
1113 };
1114 }
1115 }
1116 }
1117 IgnoreDecision::Continue
1118}
1119
1120fn ignore_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1121 let path = line.file_path;
1122 if let Some(backends_to_ignore) = config.parse_name_value_directive(line, "ignore-backends") {
1123 for backend in backends_to_ignore.split_whitespace().map(|backend| {
1124 match CodegenBackend::try_from(backend) {
1125 Ok(backend) => backend,
1126 Err(error) => {
1127 panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1128 }
1129 }
1130 }) {
1131 if !config.bypass_ignore_backends && config.default_codegen_backend == backend {
1132 return IgnoreDecision::Ignore {
1133 reason: format!("{} backend is marked as ignore", backend.as_str()),
1134 };
1135 }
1136 }
1137 }
1138 IgnoreDecision::Continue
1139}
1140
1141fn needs_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1142 let path = line.file_path;
1143 if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends") {
1144 if !needed_backends
1145 .split_whitespace()
1146 .map(|backend| match CodegenBackend::try_from(backend) {
1147 Ok(backend) => backend,
1148 Err(error) => {
1149 panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1150 }
1151 })
1152 .any(|backend| config.default_codegen_backend == backend)
1153 {
1154 return IgnoreDecision::Ignore {
1155 reason: format!(
1156 "{} backend is not part of required backends",
1157 config.default_codegen_backend.as_str()
1158 ),
1159 };
1160 }
1161 }
1162 IgnoreDecision::Continue
1163}
1164
1165fn ignore_llvm(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1166 let path = line.file_path;
1167 if let Some(needed_components) =
1168 config.parse_name_value_directive(line, "needs-llvm-components")
1169 {
1170 let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1171 if let Some(missing_component) = needed_components
1172 .split_whitespace()
1173 .find(|needed_component| !components.contains(needed_component))
1174 {
1175 if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1176 panic!(
1177 "missing LLVM component {missing_component}, \
1178 and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {path}",
1179 );
1180 }
1181 return IgnoreDecision::Ignore {
1182 reason: format!("ignored when the {missing_component} LLVM component is missing"),
1183 };
1184 }
1185 }
1186 if let Some(actual_version) = &config.llvm_version {
1187 if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1190 let min_version = extract_llvm_version(&version_string);
1191 if *actual_version < min_version {
1193 return IgnoreDecision::Ignore {
1194 reason: format!(
1195 "ignored when the LLVM version {actual_version} is older than {min_version}"
1196 ),
1197 };
1198 }
1199 } else if let Some(version_string) =
1200 config.parse_name_value_directive(line, "max-llvm-major-version")
1201 {
1202 let max_version = extract_llvm_version(&version_string);
1203 if actual_version.major > max_version.major {
1205 return IgnoreDecision::Ignore {
1206 reason: format!(
1207 "ignored when the LLVM version ({actual_version}) is newer than major\
1208 version {}",
1209 max_version.major
1210 ),
1211 };
1212 }
1213 } else if let Some(version_string) =
1214 config.parse_name_value_directive(line, "min-system-llvm-version")
1215 {
1216 let min_version = extract_llvm_version(&version_string);
1217 if config.system_llvm && *actual_version < min_version {
1220 return IgnoreDecision::Ignore {
1221 reason: format!(
1222 "ignored when the system LLVM version {actual_version} is older than {min_version}"
1223 ),
1224 };
1225 }
1226 } else if let Some(version_range) =
1227 config.parse_name_value_directive(line, "ignore-llvm-version")
1228 {
1229 let (v_min, v_max) =
1231 extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1232 .unwrap_or_else(|| {
1233 panic!("couldn't parse version range: \"{version_range}\"");
1234 });
1235 if v_max < v_min {
1236 panic!("malformed LLVM version range where {v_max} < {v_min}")
1237 }
1238 if *actual_version >= v_min && *actual_version <= v_max {
1240 if v_min == v_max {
1241 return IgnoreDecision::Ignore {
1242 reason: format!("ignored when the LLVM version is {actual_version}"),
1243 };
1244 } else {
1245 return IgnoreDecision::Ignore {
1246 reason: format!(
1247 "ignored when the LLVM version is between {v_min} and {v_max}"
1248 ),
1249 };
1250 }
1251 }
1252 } else if let Some(version_string) =
1253 config.parse_name_value_directive(line, "exact-llvm-major-version")
1254 {
1255 let version = extract_llvm_version(&version_string);
1257 if actual_version.major != version.major {
1258 return IgnoreDecision::Ignore {
1259 reason: format!(
1260 "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1261 actual_version.major, version.major
1262 ),
1263 };
1264 }
1265 }
1266 }
1267 IgnoreDecision::Continue
1268}
1269
1270fn ignore_parallel_frontend(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1271 if config.parallel_frontend_enabled()
1272 && config.parse_name_directive(line, "ignore-parallel-frontend")
1273 {
1274 return IgnoreDecision::Ignore {
1275 reason: "ignored when the parallel frontend is enabled".into(),
1276 };
1277 }
1278 IgnoreDecision::Continue
1279}
1280
1281enum IgnoreDecision {
1282 Ignore { reason: String },
1283 Continue,
1284 Error { message: String },
1285}
1286
1287fn parse_edition_range(config: &Config, line: &DirectiveLine<'_>) -> Option<EditionRange> {
1288 let raw = config.parse_name_value_directive(line, "edition")?;
1289 let &DirectiveLine { file_path: testfile, line_number, .. } = line;
1290
1291 if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1293 Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1294 (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1295 fatal!(
1296 "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1297 );
1298 }
1299 (Some(lower_bound), Some(upper_bound)) => {
1300 EditionRange::Range { lower_bound, upper_bound }
1301 }
1302 (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1303 (None, Some(_)) => {
1304 fatal!(
1305 "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1306 );
1307 }
1308 (None, None) => {
1309 fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1310 }
1311 })
1312 } else {
1313 match maybe_parse_edition(&raw) {
1314 Some(edition) => Some(EditionRange::Exact(edition)),
1315 None => {
1316 fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1317 }
1318 }
1319 }
1320}
1321
1322fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1323 input = input.trim();
1324 if input.is_empty() {
1325 return None;
1326 }
1327 Some(parse_edition(input))
1328}
1329
1330#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1331enum EditionRange {
1332 Exact(Edition),
1333 RangeFrom(Edition),
1334 Range {
1336 lower_bound: Edition,
1337 upper_bound: Edition,
1338 },
1339}
1340
1341impl EditionRange {
1342 fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1343 let min_edition = Edition::Year(2015);
1344 let requested = requested.into().unwrap_or(min_edition);
1345
1346 match *self {
1347 EditionRange::Exact(exact) => exact,
1348 EditionRange::RangeFrom(lower_bound) => {
1349 if requested >= lower_bound {
1350 requested
1351 } else {
1352 lower_bound
1353 }
1354 }
1355 EditionRange::Range { lower_bound, upper_bound } => {
1356 if requested >= lower_bound && requested < upper_bound {
1357 requested
1358 } else {
1359 lower_bound
1360 }
1361 }
1362 }
1363 }
1364}
1365
1366fn split_flags(flags: &str) -> Vec<String> {
1367 flags
1372 .split('\'')
1373 .enumerate()
1374 .flat_map(|(i, f)| if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() })
1375 .map(move |s| s.to_owned())
1376 .collect::<Vec<_>>()
1377}