Skip to main content

compiletest/
directives.rs

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 struct DirectivesCache {
40    /// "Conditions" used by `ignore-*` and `only-*` directives, prepared in
41    /// advance so that they don't have to be evaluated repeatedly.
42    cfg_conditions: cfg::PreparedConditions,
43    needs: CachedNeedsConditions,
44}
45
46impl DirectivesCache {
47    pub fn load(config: &Config) -> Self {
48        Self {
49            cfg_conditions: cfg::prepare_conditions(config),
50            needs: CachedNeedsConditions::load(config),
51        }
52    }
53}
54
55/// Properties which must be known very early, before actually running
56/// the test.
57#[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            // (dummy comment to force args into vertical layout)
73            &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    // Lines that should be expected, in order, on standard out
85    pub error_patterns: Vec<String>,
86    // Regexes that should be expected, in order, on standard out
87    pub regex_error_patterns: Vec<String>,
88    /// Edition selected by an `//@ edition` directive, if any.
89    ///
90    /// Automatically added to `compile_flags` during directive processing.
91    pub edition: Option<Edition>,
92    // Extra flags to pass to the compiler
93    pub compile_flags: Vec<String>,
94    // Extra flags to pass when the compiled code is run (such as --bench)
95    pub run_flags: Vec<String>,
96    /// Extra flags to pass to rustdoc but not the compiler.
97    pub doc_flags: Vec<String>,
98    // If present, the name of a file that this test should match when
99    // pretty-printed
100    pub pp_exact: Option<Utf8PathBuf>,
101    /// Auxiliary crates that should be built and made available to this test.
102    pub(crate) aux: AuxProps,
103    // Environment settings to use for compiling
104    pub rustc_env: Vec<(String, String)>,
105    // Environment variables to unset prior to compiling.
106    // Variables are unset before applying 'rustc_env'.
107    pub unset_rustc_env: Vec<String>,
108    // Environment settings to use during execution
109    pub exec_env: Vec<(String, String)>,
110    // Environment variables to unset prior to execution.
111    // Variables are unset before applying 'exec_env'
112    pub unset_exec_env: Vec<String>,
113    // Build documentation for all specified aux-builds as well
114    pub build_aux_docs: bool,
115    /// Build the documentation for each crate in a unique output directory.
116    /// Uses `<root output directory>/docs/<test name>/doc`.
117    pub unique_doc_out_dir: bool,
118    // Flag to force a crate to be built with the host architecture
119    pub force_host: bool,
120    // Check stdout for error-pattern output as well as stderr
121    pub check_stdout: bool,
122    // Check stdout & stderr for output of run-pass test
123    pub check_run_results: bool,
124    // For UI tests, allows compiler to generate arbitrary output to stdout
125    pub dont_check_compiler_stdout: bool,
126    // For UI tests, allows compiler to generate arbitrary output to stderr
127    pub dont_check_compiler_stderr: bool,
128    // Don't force a --crate-type=dylib flag on the command line
129    //
130    // Set this for example if you have an auxiliary test file that contains
131    // a proc-macro and needs `#![crate_type = "proc-macro"]`. This ensures
132    // that the aux file is compiled as a `proc-macro` and not as a `dylib`.
133    pub no_prefer_dynamic: bool,
134    // Which pretty mode are we testing with, default to 'normal'
135    pub pretty_mode: String,
136    // Only compare pretty output and don't try compiling
137    pub pretty_compare_only: bool,
138    // Patterns which must not appear in the output of a cfail test.
139    pub forbid_output: Vec<String>,
140    // Revisions to test for incremental compilation.
141    pub revisions: Vec<String>,
142    // Directory (if any) to use for incremental compilation.  This is
143    // not set by end-users; rather it is set by the incremental
144    // testing harness and used when generating compilation
145    // arguments. (In particular, it propagates to the aux-builds.)
146    pub incremental_dir: Option<Utf8PathBuf>,
147    // If `true`, this test will use incremental compilation.
148    //
149    // This can be set manually with the `incremental` directive, or implicitly
150    // by being a part of an incremental mode test. Using the `incremental`
151    // directive should be avoided if possible; using an incremental mode test is
152    // preferred. Incremental mode tests support multiple passes, which can
153    // verify that the incremental cache can be loaded properly after being
154    // created. Just setting the directive will only verify the behavior with
155    // creating an incremental cache, but doesn't check that it is created
156    // correctly.
157    //
158    // Compiletest will create the incremental directory, and ensure it is
159    // empty before the test starts. Incremental mode tests will reuse the
160    // incremental directory between passes in the same test.
161    pub incremental: bool,
162    // If `true`, this test is a known bug.
163    //
164    // When set, some requirements are relaxed. Currently, this only means no
165    // error annotations are needed, but this may be updated in the future to
166    // include other relaxations.
167    pub known_bug: bool,
168    // How far should the test proceed while still passing.
169    pass_mode: Option<PassMode>,
170    // Ignore `--pass` overrides from the command line for this test.
171    ignore_pass: bool,
172    // How far this test should proceed to start failing.
173    pub fail_mode: Option<FailMode>,
174    // rustdoc will test the output of the `--test` option
175    pub check_test_line_numbers_match: bool,
176    // customized normalization rules
177    pub normalize_stdout: Vec<(String, String)>,
178    pub normalize_stderr: Vec<(String, String)>,
179    pub failure_status: Option<i32>,
180    // For UI tests, allows compiler to exit with arbitrary failure status
181    pub dont_check_failure_status: bool,
182    // Whether or not `rustfix` should apply the `CodeSuggestion`s of this test and compile the
183    // resulting Rust code.
184    pub run_rustfix: bool,
185    // If true, `rustfix` will only apply `MachineApplicable` suggestions.
186    pub rustfix_only_machine_applicable: bool,
187    pub assembly_output: Option<String>,
188    // If true, the test is expected to ICE
189    pub should_ice: bool,
190    // If true, the stderr is expected to be different across bit-widths.
191    pub stderr_per_bitwidth: bool,
192    // The MIR opt to unit test, if any
193    pub mir_unit_test: Option<String>,
194    // Whether to tell `rustc` to remap the "src base" directory to a fake
195    // directory.
196    pub remap_src_base: bool,
197    /// Extra flags to pass to `llvm-cov` when producing coverage reports.
198    /// Only used by the "coverage-run" test mode.
199    pub llvm_cov_flags: Vec<String>,
200    /// Extra flags to pass to LLVM's `filecheck` tool, in tests that use it.
201    pub filecheck_flags: Vec<String>,
202    /// Don't automatically insert any `--check-cfg` args
203    pub no_auto_check_cfg: bool,
204    /// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
205    /// that don't otherwise want/need `-Z build-std`.
206    pub add_minicore: bool,
207    /// Add these flags to the build of `minicore`.
208    pub minicore_compile_flags: Vec<String>,
209    /// Whether line annotations are required for the given error kind.
210    pub dont_require_annotations: HashSet<ErrorKind>,
211    /// Whether pretty printers should be disabled in gdb.
212    pub disable_gdb_pretty_printers: bool,
213    /// Compare the output by lines, rather than as a single string.
214    pub compare_output_by_lines: bool,
215}
216
217mod directives {
218    pub const ERROR_PATTERN: &'static str = "error-pattern";
219    pub const REGEX_ERROR_PATTERN: &'static str = "regex-error-pattern";
220    pub const COMPILE_FLAGS: &'static str = "compile-flags";
221    pub const RUN_FLAGS: &'static str = "run-flags";
222    pub const DOC_FLAGS: &'static str = "doc-flags";
223    pub const SHOULD_ICE: &'static str = "should-ice";
224    pub const BUILD_AUX_DOCS: &'static str = "build-aux-docs";
225    pub const UNIQUE_DOC_OUT_DIR: &'static str = "unique-doc-out-dir";
226    pub const FORCE_HOST: &'static str = "force-host";
227    pub const CHECK_STDOUT: &'static str = "check-stdout";
228    pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
229    pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
230    pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
231    pub const DONT_REQUIRE_ANNOTATIONS: &'static str = "dont-require-annotations";
232    pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
233    pub const PRETTY_MODE: &'static str = "pretty-mode";
234    pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
235    pub const AUX_BIN: &'static str = "aux-bin";
236    pub const AUX_BUILD: &'static str = "aux-build";
237    pub const AUX_CRATE: &'static str = "aux-crate";
238    pub const PROC_MACRO: &'static str = "proc-macro";
239    pub const AUX_CODEGEN_BACKEND: &'static str = "aux-codegen-backend";
240    pub const EXEC_ENV: &'static str = "exec-env";
241    pub const RUSTC_ENV: &'static str = "rustc-env";
242    pub const UNSET_EXEC_ENV: &'static str = "unset-exec-env";
243    pub const UNSET_RUSTC_ENV: &'static str = "unset-rustc-env";
244    pub const FORBID_OUTPUT: &'static str = "forbid-output";
245    pub const CHECK_TEST_LINE_NUMBERS_MATCH: &'static str = "check-test-line-numbers-match";
246    pub const IGNORE_PASS: &'static str = "ignore-pass";
247    pub const FAILURE_STATUS: &'static str = "failure-status";
248    pub const DONT_CHECK_FAILURE_STATUS: &'static str = "dont-check-failure-status";
249    pub const RUN_RUSTFIX: &'static str = "run-rustfix";
250    pub const RUSTFIX_ONLY_MACHINE_APPLICABLE: &'static str = "rustfix-only-machine-applicable";
251    pub const ASSEMBLY_OUTPUT: &'static str = "assembly-output";
252    pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth";
253    pub const INCREMENTAL: &'static str = "incremental";
254    pub const KNOWN_BUG: &'static str = "known-bug";
255    pub const TEST_MIR_PASS: &'static str = "test-mir-pass";
256    pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
257    pub const LLVM_COV_FLAGS: &'static str = "llvm-cov-flags";
258    pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
259    pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
260    pub const ADD_MINICORE: &'static str = "add-minicore";
261    pub const MINICORE_COMPILE_FLAGS: &'static str = "minicore-compile-flags";
262    pub const DISABLE_GDB_PRETTY_PRINTERS: &'static str = "disable-gdb-pretty-printers";
263    pub const COMPARE_OUTPUT_BY_LINES: &'static str = "compare-output-by-lines";
264}
265
266impl TestProps {
267    pub fn new() -> Self {
268        TestProps {
269            error_patterns: vec![],
270            regex_error_patterns: vec![],
271            edition: None,
272            compile_flags: vec![],
273            run_flags: vec![],
274            doc_flags: vec![],
275            pp_exact: None,
276            aux: Default::default(),
277            revisions: vec![],
278            rustc_env: vec![
279                ("RUSTC_ICE".to_string(), "0".to_string()),
280                ("RUST_BACKTRACE".to_string(), "short".to_string()),
281            ],
282            unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
283            exec_env: vec![],
284            unset_exec_env: vec![],
285            build_aux_docs: false,
286            unique_doc_out_dir: false,
287            force_host: false,
288            check_stdout: false,
289            check_run_results: false,
290            dont_check_compiler_stdout: false,
291            dont_check_compiler_stderr: false,
292            no_prefer_dynamic: false,
293            pretty_mode: "normal".to_string(),
294            pretty_compare_only: false,
295            forbid_output: vec![],
296            incremental_dir: None,
297            incremental: false,
298            known_bug: false,
299            pass_mode: None,
300            fail_mode: None,
301            ignore_pass: false,
302            check_test_line_numbers_match: false,
303            normalize_stdout: vec![],
304            normalize_stderr: vec![],
305            failure_status: None,
306            dont_check_failure_status: false,
307            run_rustfix: false,
308            rustfix_only_machine_applicable: false,
309            assembly_output: None,
310            should_ice: false,
311            stderr_per_bitwidth: false,
312            mir_unit_test: None,
313            remap_src_base: false,
314            llvm_cov_flags: vec![],
315            filecheck_flags: vec![],
316            no_auto_check_cfg: false,
317            add_minicore: false,
318            minicore_compile_flags: vec![],
319            dont_require_annotations: Default::default(),
320            disable_gdb_pretty_printers: false,
321            compare_output_by_lines: false,
322        }
323    }
324
325    pub fn from_aux_file(
326        &self,
327        testfile: &Utf8Path,
328        revision: Option<&str>,
329        config: &Config,
330    ) -> Self {
331        let mut props = TestProps::new();
332
333        // copy over select properties to the aux build:
334        props.incremental_dir = self.incremental_dir.clone();
335        props.ignore_pass = true;
336        props.load_from(testfile, revision, config);
337
338        props
339    }
340
341    pub fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
342        let mut props = TestProps::new();
343        props.load_from(testfile, revision, config);
344        props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
345
346        match (props.pass_mode, props.fail_mode) {
347            (None, None) if config.mode == TestMode::Ui => props.fail_mode = Some(FailMode::Check),
348            (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
349            _ => {}
350        }
351
352        props
353    }
354
355    /// Loads properties from `testfile` into `props`. If a property is
356    /// tied to a particular revision `foo` (indicated by writing
357    /// `//@[foo]`), then the property is ignored unless `test_revision` is
358    /// `Some("foo")`.
359    fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
360        if !testfile.is_dir() {
361            let file_contents = fs::read_to_string(testfile).unwrap();
362            let file_directives = FileDirectives::from_file_contents(testfile, &file_contents);
363
364            iter_directives(
365                config,
366                &file_directives,
367                // (dummy comment to force args into vertical layout)
368                &mut |ln: &DirectiveLine<'_>| {
369                    if !ln.applies_to_test_revision(test_revision) {
370                        return;
371                    }
372
373                    if let Some(handler) = DIRECTIVE_HANDLERS_MAP.get(ln.name) {
374                        handler.handle(config, ln, self);
375                    }
376                },
377            );
378        }
379
380        if self.should_ice {
381            self.failure_status = Some(101);
382        }
383
384        if config.mode == TestMode::Incremental {
385            self.incremental = true;
386        }
387
388        if config.mode == TestMode::Crashes {
389            // we don't want to pollute anything with backtrace-files
390            // also turn off backtraces in order to save some execution
391            // time on the tests; we only need to know IF it crashes
392            self.rustc_env = vec![
393                ("RUST_BACKTRACE".to_string(), "0".to_string()),
394                ("RUSTC_ICE".to_string(), "0".to_string()),
395            ];
396        }
397
398        for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
399            if let Ok(val) = env::var(key) {
400                if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
401                    self.exec_env.push(((*key).to_owned(), val))
402                }
403            }
404        }
405
406        if let Some(edition) = self.edition.or(config.edition) {
407            // The edition is added at the start, since flags from //@compile-flags must be passed
408            // to rustc last.
409            self.compile_flags.insert(0, format!("--edition={edition}"));
410        }
411    }
412
413    fn update_fail_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
414        let check_ui = |mode: &str| {
415            // Mode::Crashes may need build-fail in order to trigger llvm errors or stack overflows
416            if config.mode != TestMode::Ui && config.mode != TestMode::Crashes {
417                panic!("`{}-fail` directive is only supported in UI tests", mode);
418            }
419        };
420        let fail_mode = if config.parse_name_directive(ln, "check-fail") {
421            check_ui("check");
422            Some(FailMode::Check)
423        } else if config.parse_name_directive(ln, "build-fail") {
424            check_ui("build");
425            Some(FailMode::Build)
426        } else if config.parse_name_directive(ln, "run-fail") {
427            check_ui("run");
428            Some(FailMode::Run(RunFailMode::Fail))
429        } else if config.parse_name_directive(ln, "run-crash") {
430            check_ui("run");
431            Some(FailMode::Run(RunFailMode::Crash))
432        } else if config.parse_name_directive(ln, "run-fail-or-crash") {
433            check_ui("run");
434            Some(FailMode::Run(RunFailMode::FailOrCrash))
435        } else {
436            None
437        };
438        match (self.fail_mode, fail_mode) {
439            (None, Some(_)) => self.fail_mode = fail_mode,
440            (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
441            (_, None) => {}
442        }
443    }
444
445    fn update_pass_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
446        let check_no_run = |s| match (config.mode, s) {
447            (TestMode::Ui, _) => (),
448            (TestMode::Crashes, _) => (),
449            (TestMode::Codegen, "build-pass") => (),
450            (TestMode::Incremental, _) => {
451                // FIXME(Zalathar): This only detects forbidden directives that are
452                // declared _after_ the incompatible `//@ revisions:` directive(s).
453                if self.revisions.iter().any(|r| !r.starts_with("cfail")) {
454                    panic!("`{s}` directive is only supported in `cfail` incremental tests")
455                }
456            }
457            (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
458        };
459        let pass_mode = if config.parse_name_directive(ln, "check-pass") {
460            check_no_run("check-pass");
461            Some(PassMode::Check)
462        } else if config.parse_name_directive(ln, "build-pass") {
463            check_no_run("build-pass");
464            Some(PassMode::Build)
465        } else if config.parse_name_directive(ln, "run-pass") {
466            check_no_run("run-pass");
467            Some(PassMode::Run)
468        } else {
469            None
470        };
471        match (self.pass_mode, pass_mode) {
472            (None, Some(_)) => self.pass_mode = pass_mode,
473            (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
474            (_, None) => {}
475        }
476    }
477
478    pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
479        if !self.ignore_pass && self.fail_mode.is_none() {
480            if let mode @ Some(_) = config.force_pass_mode {
481                return mode;
482            }
483        }
484        self.pass_mode
485    }
486
487    // does not consider CLI override for pass mode
488    pub fn local_pass_mode(&self) -> Option<PassMode> {
489        self.pass_mode
490    }
491
492    fn update_add_minicore(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
493        let add_minicore = config.parse_name_directive(ln, directives::ADD_MINICORE);
494        if add_minicore {
495            if !matches!(config.mode, TestMode::Ui | TestMode::Codegen | TestMode::Assembly) {
496                panic!(
497                    "`add-minicore` is currently only supported for ui, codegen and assembly test modes"
498                );
499            }
500
501            // FIXME(jieyouxu): this check is currently order-dependent, but we should probably
502            // collect all directives in one go then perform a validation pass after that.
503            if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
504                // `minicore` can only be used with non-run modes, because it's `core` prelude stubs
505                // and can't run.
506                panic!("`add-minicore` cannot be used to run the test binary");
507            }
508
509            self.add_minicore = add_minicore;
510        }
511    }
512}
513
514pub(crate) fn do_early_directives_check(
515    mode: TestMode,
516    file_directives: &FileDirectives<'_>,
517) -> Result<(), String> {
518    let testfile = file_directives.path;
519
520    for directive_line @ DirectiveLine { line_number, .. } in &file_directives.lines {
521        let CheckDirectiveResult { is_known_directive, trailing_directive } =
522            check_directive(directive_line, mode);
523
524        if !is_known_directive {
525            return Err(format!(
526                "ERROR: unknown compiletest directive `{directive}` at {testfile}:{line_number}",
527                directive = directive_line.display(),
528            ));
529        }
530
531        if let Some(trailing_directive) = &trailing_directive {
532            return Err(format!(
533                "ERROR: detected trailing compiletest directive `{trailing_directive}` at {testfile}:{line_number}\n\
534                HELP: put the directive on its own line: `//@ {trailing_directive}`"
535            ));
536        }
537    }
538
539    Ok(())
540}
541
542pub(crate) struct CheckDirectiveResult<'ln> {
543    is_known_directive: bool,
544    trailing_directive: Option<&'ln str>,
545}
546
547fn check_directive<'a>(
548    directive_ln: &DirectiveLine<'a>,
549    mode: TestMode,
550) -> CheckDirectiveResult<'a> {
551    let &DirectiveLine { name: directive_name, .. } = directive_ln;
552
553    let is_known_directive = KNOWN_DIRECTIVE_NAMES_SET.contains(&directive_name)
554        || match mode {
555            TestMode::RustdocHtml => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
556            TestMode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
557            _ => false,
558        };
559
560    // If it looks like the user tried to put two directives on the same line
561    // (e.g. `//@ only-linux only-x86_64`), signal an error, because the
562    // second "directive" would actually be ignored with no effect.
563    let trailing_directive = directive_ln
564        .remark_after_space()
565        .map(|remark| remark.trim_start().split(' ').next().unwrap())
566        .filter(|token| KNOWN_DIRECTIVE_NAMES_SET.contains(token));
567
568    // FIXME(Zalathar): Consider emitting specialized error/help messages for
569    // bogus directive names that are similar to real ones, e.g.:
570    // - *`compiler-flags` => `compile-flags`
571    // - *`compile-fail` => `check-fail` or `build-fail`
572
573    CheckDirectiveResult { is_known_directive, trailing_directive }
574}
575
576fn iter_directives(
577    config: &Config,
578    file_directives: &FileDirectives<'_>,
579    it: &mut dyn FnMut(&DirectiveLine<'_>),
580) {
581    let testfile = file_directives.path;
582
583    let extra_directives = match config.mode {
584        TestMode::CoverageRun => {
585            // Coverage tests in coverage-run mode always have these extra directives, without needing to
586            // specify them manually in every test file.
587            //
588            // FIXME(jieyouxu): I feel like there's a better way to do this, leaving for later.
589            vec![
590                "//@ needs-profiler-runtime",
591                // FIXME(pietroalbini): this test currently does not work on cross-compiled targets
592                // because remote-test is not capable of sending back the *.profraw files generated by
593                // the LLVM instrumentation.
594                "//@ ignore-cross-compile",
595            ]
596        }
597        TestMode::Codegen if !file_directives.has_explicit_no_std_core_attribute => {
598            // Note: affects all codegen test suites under test mode `codegen`, e.g. `codegen-llvm`.
599            //
600            // Codegen tests automatically receive implied `//@ needs-target-std`, unless
601            // `#![no_std]`/`#![no_core]` attribute was explicitly seen. The rationale is basically to avoid
602            // having to manually maintain a bunch of `//@ needs-target-std` directives esp. for targets
603            // tested/built out-of-tree.
604            vec!["//@ needs-target-std"]
605        }
606        TestMode::Ui if config.parallel_frontend_enabled() => {
607            // UI tests in parallel-frontend mode always have this extra directive, without needing to
608            // specify it manually in every test file.
609            vec!["//@ compare-output-by-lines"]
610        }
611
612        _ => {
613            // No extra directives for other test modes.
614            vec![]
615        }
616    };
617
618    for directive_str in extra_directives {
619        let directive_line = line_directive(testfile, LineNumber::ZERO, directive_str)
620            .unwrap_or_else(|| panic!("bad extra-directive line: {directive_str:?}"));
621        it(&directive_line);
622    }
623
624    for directive_line in &file_directives.lines {
625        it(directive_line);
626    }
627}
628
629impl Config {
630    fn parse_and_update_revisions(&self, line: &DirectiveLine<'_>, existing: &mut Vec<String>) {
631        const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
632            // `//@ revisions: true false` Implying `--cfg=true` and `--cfg=false` makes it very
633            // weird for the test, since if the test writer wants a cfg of the same revision name
634            // they'd have to use `cfg(r#true)` and `cfg(r#false)`.
635            "true", "false",
636        ];
637
638        const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
639            ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
640
641        if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
642            let &DirectiveLine { file_path: testfile, .. } = line;
643
644            if self.mode == TestMode::RunMake {
645                panic!("`run-make` mode tests do not support revisions: {}", testfile);
646            }
647
648            let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
649            for revision in raw.split_whitespace() {
650                if !duplicates.insert(revision.to_string()) {
651                    panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
652                }
653
654                if FORBIDDEN_REVISION_NAMES.contains(&revision) {
655                    panic!(
656                        "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
657                        revision, raw, testfile
658                    );
659                }
660
661                if matches!(self.mode, TestMode::Assembly | TestMode::Codegen | TestMode::MirOpt)
662                    && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
663                {
664                    panic!(
665                        "revision name `{revision}` is not permitted in a test suite that uses \
666                        `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
667                        prefix: `{revision}` in line `{}`: {}",
668                        raw, testfile
669                    );
670                }
671
672                existing.push(revision.to_string());
673            }
674        }
675    }
676
677    fn parse_env(nv: String) -> (String, String) {
678        // nv is either FOO or FOO=BAR
679        // FIXME(Zalathar): The form without `=` seems to be unused; should
680        // we drop support for it?
681        let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
682        // Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
683        // sees the name as `FOO` and not ` FOO`.
684        let name = name.trim();
685        (name.to_owned(), value.to_owned())
686    }
687
688    fn parse_pp_exact(&self, line: &DirectiveLine<'_>) -> Option<Utf8PathBuf> {
689        if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
690            Some(Utf8PathBuf::from(&s))
691        } else if self.parse_name_directive(line, "pp-exact") {
692            line.file_path.file_name().map(Utf8PathBuf::from)
693        } else {
694            None
695        }
696    }
697
698    fn parse_custom_normalization(&self, line: &DirectiveLine<'_>) -> Option<NormalizeRule> {
699        let &DirectiveLine { name, .. } = line;
700
701        let kind = match name {
702            "normalize-stdout" => NormalizeKind::Stdout,
703            "normalize-stderr" => NormalizeKind::Stderr,
704            "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
705            "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
706            _ => return None,
707        };
708
709        let Some((regex, replacement)) = line.value_after_colon().and_then(parse_normalize_rule)
710        else {
711            error!("couldn't parse custom normalization rule: `{}`", line.display());
712            help!("expected syntax is: `{name}: \"REGEX\" -> \"REPLACEMENT\"`");
713            panic!("invalid normalization rule detected");
714        };
715        Some(NormalizeRule { kind, regex, replacement })
716    }
717
718    fn parse_name_directive(&self, line: &DirectiveLine<'_>, directive: &str) -> bool {
719        // FIXME(Zalathar): Ideally, this should raise an error if a name-only
720        // directive is followed by a colon, since that's the wrong syntax.
721        // But we would need to fix tests that rely on the current behaviour.
722        line.name == directive
723    }
724
725    fn parse_name_value_directive(
726        &self,
727        line: &DirectiveLine<'_>,
728        directive: &str,
729    ) -> Option<String> {
730        let &DirectiveLine { file_path, line_number, .. } = line;
731
732        if line.name != directive {
733            return None;
734        };
735
736        // FIXME(Zalathar): This silently discards directives with a matching
737        // name but no colon. Unfortunately, some directives (e.g. "pp-exact")
738        // currently rely on _not_ panicking here.
739        let value = line.value_after_colon()?;
740        debug!("{}: {}", directive, value);
741        let value = expand_variables(value.to_owned(), self);
742
743        if value.is_empty() {
744            error!("{file_path}:{line_number}: empty value for directive `{directive}`");
745            help!("expected syntax is: `{directive}: value`");
746            panic!("empty directive value detected");
747        }
748
749        Some(value)
750    }
751
752    fn set_name_directive(&self, line: &DirectiveLine<'_>, directive: &str, value: &mut bool) {
753        // If the flag is already true, don't bother looking at the directive.
754        *value = *value || self.parse_name_directive(line, directive);
755    }
756
757    fn set_name_value_directive<T>(
758        &self,
759        line: &DirectiveLine<'_>,
760        directive: &str,
761        value: &mut Option<T>,
762        parse: impl FnOnce(String) -> T,
763    ) {
764        if value.is_none() {
765            *value = self.parse_name_value_directive(line, directive).map(parse);
766        }
767    }
768
769    fn push_name_value_directive<T>(
770        &self,
771        line: &DirectiveLine<'_>,
772        directive: &str,
773        values: &mut Vec<T>,
774        parse: impl FnOnce(String) -> T,
775    ) {
776        if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
777            values.push(value);
778        }
779    }
780}
781
782// FIXME(jieyouxu): fix some of these variable names to more accurately reflect what they do.
783fn expand_variables(mut value: String, config: &Config) -> String {
784    const CWD: &str = "{{cwd}}";
785    const SRC_BASE: &str = "{{src-base}}";
786    const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
787    const RUST_SRC_BASE: &str = "{{rust-src-base}}";
788    const SYSROOT_BASE: &str = "{{sysroot-base}}";
789    const TARGET_LINKER: &str = "{{target-linker}}";
790    const TARGET: &str = "{{target}}";
791
792    if value.contains(CWD) {
793        let cwd = env::current_dir().unwrap();
794        value = value.replace(CWD, &cwd.to_str().unwrap());
795    }
796
797    if value.contains(SRC_BASE) {
798        value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
799    }
800
801    if value.contains(TEST_SUITE_BUILD_BASE) {
802        value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
803    }
804
805    if value.contains(SYSROOT_BASE) {
806        value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
807    }
808
809    if value.contains(TARGET_LINKER) {
810        value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
811    }
812
813    if value.contains(TARGET) {
814        value = value.replace(TARGET, &config.target);
815    }
816
817    if value.contains(RUST_SRC_BASE) {
818        let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
819        src_base.try_exists().expect(&*format!("{} should exists", src_base));
820        let src_base = src_base.read_link_utf8().unwrap_or(src_base);
821        value = value.replace(RUST_SRC_BASE, &src_base.as_str());
822    }
823
824    value
825}
826
827struct NormalizeRule {
828    kind: NormalizeKind,
829    regex: String,
830    replacement: String,
831}
832
833enum NormalizeKind {
834    Stdout,
835    Stderr,
836    Stderr32bit,
837    Stderr64bit,
838}
839
840/// Parses the regex and replacement values of a `//@ normalize-*` directive, in the format:
841/// ```text
842/// "REGEX" -> "REPLACEMENT"
843/// ```
844fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
845    // FIXME: Support escaped double-quotes in strings.
846    let captures = static_regex!(
847        r#"(?x) # (verbose mode regex)
848        ^
849        \s*                     # (leading whitespace)
850        "(?<regex>[^"]*)"       # "REGEX"
851        \s+->\s+                # ->
852        "(?<replacement>[^"]*)" # "REPLACEMENT"
853        $
854        "#
855    )
856    .captures(raw_value)?;
857    let regex = captures["regex"].to_owned();
858    let replacement = captures["replacement"].to_owned();
859    // A `\n` sequence in the replacement becomes an actual newline.
860    // FIXME: Do unescaping in a less ad-hoc way, and perhaps support escaped
861    // backslashes and double-quotes.
862    let replacement = replacement.replace("\\n", "\n");
863    Some((regex, replacement))
864}
865
866/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this
867/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting
868/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of
869/// having to write `//@ min-llvm-version: 19.0.0`.
870///
871/// Currently panics if the input string is malformed, though we really should not use panic as an
872/// error handling strategy.
873///
874/// FIXME(jieyouxu): improve error handling
875pub fn extract_llvm_version(version: &str) -> Version {
876    // The version substring we're interested in usually looks like the `1.2.3`, without any of the
877    // fancy suffix like `-rc1` or `meow`.
878    let version = version.trim();
879    let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
880    let version_without_suffix = match version.split_once(uninterested) {
881        Some((prefix, _suffix)) => prefix,
882        None => version,
883    };
884
885    let components: Vec<u64> = version_without_suffix
886        .split('.')
887        .map(|s| s.parse().expect("llvm version component should consist of only digits"))
888        .collect();
889
890    match &components[..] {
891        [major] => Version::new(*major, 0, 0),
892        [major, minor] => Version::new(*major, *minor, 0),
893        [major, minor, patch] => Version::new(*major, *minor, *patch),
894        _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
895    }
896}
897
898pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
899    let output = Command::new(binary_path).arg("--version").output().ok()?;
900    if !output.status.success() {
901        return None;
902    }
903    let version = String::from_utf8(output.stdout).ok()?;
904    for line in version.lines() {
905        if let Some(version) = line.split("LLVM version ").nth(1) {
906            return Some(extract_llvm_version(version));
907        }
908    }
909    None
910}
911
912/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation
913/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`.
914///
915/// If the `<version2>` part is omitted, the second component of the tuple is the same as
916/// `<version1>`.
917fn extract_version_range<'a, F, VersionTy: Clone>(
918    line: &'a str,
919    parse: F,
920) -> Option<(VersionTy, VersionTy)>
921where
922    F: Fn(&'a str) -> Option<VersionTy>,
923{
924    let mut splits = line.splitn(2, "- ").map(str::trim);
925    let min = splits.next().unwrap();
926    if min.ends_with('-') {
927        return None;
928    }
929
930    let max = splits.next();
931
932    if min.is_empty() {
933        return None;
934    }
935
936    let min = parse(min)?;
937    let max = match max {
938        Some("") => return None,
939        Some(max) => parse(max)?,
940        _ => min.clone(),
941    };
942
943    Some((min, max))
944}
945
946pub(crate) fn make_test_description(
947    config: &Config,
948    cache: &DirectivesCache,
949    name: String,
950    path: &Utf8Path,
951    filterable_path: &Utf8Path,
952    file_directives: &FileDirectives<'_>,
953    test_revision: Option<&str>,
954    poisoned: &mut bool,
955    aux_props: &mut AuxProps,
956) -> CollectedTestDesc {
957    let mut ignore = false;
958    let mut ignore_message = None;
959    let mut should_fail = false;
960
961    // Scan through the test file to handle `ignore-*`, `only-*`, and `needs-*` directives.
962    iter_directives(config, file_directives, &mut |ln @ &DirectiveLine { line_number, .. }| {
963        if !ln.applies_to_test_revision(test_revision) {
964            return;
965        }
966
967        // Parse `aux-*` directives, for use by up-to-date checks.
968        parse_and_update_aux(config, ln, aux_props);
969
970        macro_rules! decision {
971            ($e:expr) => {
972                match $e {
973                    IgnoreDecision::Ignore { reason } => {
974                        ignore = true;
975                        ignore_message = Some(reason.into());
976                    }
977                    IgnoreDecision::Error { message } => {
978                        error!("{path}:{line_number}: {message}");
979                        *poisoned = true;
980                        return;
981                    }
982                    IgnoreDecision::Continue => {}
983                }
984            };
985        }
986
987        decision!(cfg::handle_ignore(&cache.cfg_conditions, ln));
988        decision!(cfg::handle_only(&cache.cfg_conditions, ln));
989        decision!(needs::handle_needs(&cache.needs, config, ln));
990        decision!(ignore_llvm(config, ln));
991        decision!(ignore_backends(config, ln));
992        decision!(needs_backends(config, ln));
993        decision!(ignore_cdb(config, ln));
994        decision!(ignore_gdb(config, ln));
995        decision!(ignore_lldb(config, ln));
996        decision!(ignore_parallel_frontend(config, ln));
997
998        if config.target == "wasm32-unknown-unknown"
999            && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
1000        {
1001            decision!(IgnoreDecision::Ignore {
1002                reason: "ignored on WASM as the run results cannot be checked there".into(),
1003            });
1004        }
1005
1006        should_fail |= config.parse_name_directive(ln, "should-fail");
1007    });
1008
1009    // The `should-fail` annotation doesn't apply to pretty tests,
1010    // since we run the pretty printer across all tests by default.
1011    // If desired, we could add a `should-fail-pretty` annotation.
1012    let should_fail = if should_fail && config.mode != TestMode::Pretty {
1013        ShouldFail::Yes
1014    } else {
1015        ShouldFail::No
1016    };
1017
1018    CollectedTestDesc {
1019        name,
1020        filterable_path: filterable_path.to_owned(),
1021        ignore,
1022        ignore_message,
1023        should_fail,
1024    }
1025}
1026
1027fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1028    if config.debugger != Some(Debugger::Cdb) {
1029        return IgnoreDecision::Continue;
1030    }
1031
1032    if let Some(actual_version) = config.cdb_version {
1033        if line.name == "min-cdb-version"
1034            && let Some(rest) = line.value_after_colon().map(str::trim)
1035        {
1036            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1037                panic!("couldn't parse version range: {:?}", rest);
1038            });
1039
1040            // Ignore if actual version is smaller than the minimum
1041            // required version
1042            if actual_version < min_version {
1043                return IgnoreDecision::Ignore {
1044                    reason: format!("ignored when the CDB version is lower than {rest}"),
1045                };
1046            }
1047        }
1048    }
1049    IgnoreDecision::Continue
1050}
1051
1052fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1053    if config.debugger != Some(Debugger::Gdb) {
1054        return IgnoreDecision::Continue;
1055    }
1056
1057    if let Some(actual_version) = config.gdb_version {
1058        if line.name == "min-gdb-version"
1059            && let Some(rest) = line.value_after_colon().map(str::trim)
1060        {
1061            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1062                .unwrap_or_else(|| {
1063                    panic!("couldn't parse version range: {:?}", rest);
1064                });
1065
1066            if start_ver != end_ver {
1067                panic!("Expected single GDB version")
1068            }
1069            // Ignore if actual version is smaller than the minimum
1070            // required version
1071            if actual_version < start_ver {
1072                return IgnoreDecision::Ignore {
1073                    reason: format!("ignored when the GDB version is lower than {rest}"),
1074                };
1075            }
1076        } else if line.name == "ignore-gdb-version"
1077            && let Some(rest) = line.value_after_colon().map(str::trim)
1078        {
1079            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1080                .unwrap_or_else(|| {
1081                    panic!("couldn't parse version range: {:?}", rest);
1082                });
1083
1084            if max_version < min_version {
1085                panic!("Malformed GDB version range: max < min")
1086            }
1087
1088            if actual_version >= min_version && actual_version <= max_version {
1089                if min_version == max_version {
1090                    return IgnoreDecision::Ignore {
1091                        reason: format!("ignored when the GDB version is {rest}"),
1092                    };
1093                } else {
1094                    return IgnoreDecision::Ignore {
1095                        reason: format!("ignored when the GDB version is between {rest}"),
1096                    };
1097                }
1098            }
1099        }
1100    }
1101    IgnoreDecision::Continue
1102}
1103
1104fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1105    if config.debugger != Some(Debugger::Lldb) {
1106        return IgnoreDecision::Continue;
1107    }
1108
1109    if let Some(actual_version) = config.lldb_version {
1110        if line.name == "min-lldb-version"
1111            && let Some(rest) = line.value_after_colon().map(str::trim)
1112        {
1113            let min_version = rest.parse().unwrap_or_else(|e| {
1114                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1115            });
1116            // Ignore if actual version is smaller the minimum required
1117            // version
1118            if actual_version < min_version {
1119                return IgnoreDecision::Ignore {
1120                    reason: format!("ignored when the LLDB version is {rest}"),
1121                };
1122            }
1123        }
1124    }
1125    IgnoreDecision::Continue
1126}
1127
1128fn ignore_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1129    let path = line.file_path;
1130    if let Some(backends_to_ignore) = config.parse_name_value_directive(line, "ignore-backends") {
1131        for backend in backends_to_ignore.split_whitespace().map(|backend| {
1132            match CodegenBackend::try_from(backend) {
1133                Ok(backend) => backend,
1134                Err(error) => {
1135                    panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1136                }
1137            }
1138        }) {
1139            if !config.bypass_ignore_backends && config.default_codegen_backend == backend {
1140                return IgnoreDecision::Ignore {
1141                    reason: format!("{} backend is marked as ignore", backend.as_str()),
1142                };
1143            }
1144        }
1145    }
1146    IgnoreDecision::Continue
1147}
1148
1149fn needs_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1150    let path = line.file_path;
1151    if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends") {
1152        if !needed_backends
1153            .split_whitespace()
1154            .map(|backend| match CodegenBackend::try_from(backend) {
1155                Ok(backend) => backend,
1156                Err(error) => {
1157                    panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1158                }
1159            })
1160            .any(|backend| config.default_codegen_backend == backend)
1161        {
1162            return IgnoreDecision::Ignore {
1163                reason: format!(
1164                    "{} backend is not part of required backends",
1165                    config.default_codegen_backend.as_str()
1166                ),
1167            };
1168        }
1169    }
1170    IgnoreDecision::Continue
1171}
1172
1173fn ignore_llvm(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1174    let path = line.file_path;
1175    if let Some(needed_components) =
1176        config.parse_name_value_directive(line, "needs-llvm-components")
1177    {
1178        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1179        if let Some(missing_component) = needed_components
1180            .split_whitespace()
1181            .find(|needed_component| !components.contains(needed_component))
1182        {
1183            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1184                panic!(
1185                    "missing LLVM component {missing_component}, \
1186                    and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {path}",
1187                );
1188            }
1189            return IgnoreDecision::Ignore {
1190                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1191            };
1192        }
1193    }
1194    if let Some(actual_version) = &config.llvm_version {
1195        // Note that these `min` versions will check for not just major versions.
1196
1197        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1198            let min_version = extract_llvm_version(&version_string);
1199            // Ignore if actual version is smaller than the minimum required version.
1200            if *actual_version < min_version {
1201                return IgnoreDecision::Ignore {
1202                    reason: format!(
1203                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1204                    ),
1205                };
1206            }
1207        } else if let Some(version_string) =
1208            config.parse_name_value_directive(line, "max-llvm-major-version")
1209        {
1210            let max_version = extract_llvm_version(&version_string);
1211            // Ignore if actual major version is larger than the maximum required major version.
1212            if actual_version.major > max_version.major {
1213                return IgnoreDecision::Ignore {
1214                    reason: format!(
1215                        "ignored when the LLVM version ({actual_version}) is newer than major\
1216                        version {}",
1217                        max_version.major
1218                    ),
1219                };
1220            }
1221        } else if let Some(version_string) =
1222            config.parse_name_value_directive(line, "min-system-llvm-version")
1223        {
1224            let min_version = extract_llvm_version(&version_string);
1225            // Ignore if using system LLVM and actual version
1226            // is smaller the minimum required version
1227            if config.system_llvm && *actual_version < min_version {
1228                return IgnoreDecision::Ignore {
1229                    reason: format!(
1230                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1231                    ),
1232                };
1233            }
1234        } else if let Some(version_range) =
1235            config.parse_name_value_directive(line, "ignore-llvm-version")
1236        {
1237            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1238            let (v_min, v_max) =
1239                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1240                    .unwrap_or_else(|| {
1241                        panic!("couldn't parse version range: \"{version_range}\"");
1242                    });
1243            if v_max < v_min {
1244                panic!("malformed LLVM version range where {v_max} < {v_min}")
1245            }
1246            // Ignore if version lies inside of range.
1247            if *actual_version >= v_min && *actual_version <= v_max {
1248                if v_min == v_max {
1249                    return IgnoreDecision::Ignore {
1250                        reason: format!("ignored when the LLVM version is {actual_version}"),
1251                    };
1252                } else {
1253                    return IgnoreDecision::Ignore {
1254                        reason: format!(
1255                            "ignored when the LLVM version is between {v_min} and {v_max}"
1256                        ),
1257                    };
1258                }
1259            }
1260        } else if let Some(version_string) =
1261            config.parse_name_value_directive(line, "exact-llvm-major-version")
1262        {
1263            // Syntax is "exact-llvm-major-version: <version>"
1264            let version = extract_llvm_version(&version_string);
1265            if actual_version.major != version.major {
1266                return IgnoreDecision::Ignore {
1267                    reason: format!(
1268                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1269                        actual_version.major, version.major
1270                    ),
1271                };
1272            }
1273        }
1274    }
1275    IgnoreDecision::Continue
1276}
1277
1278fn ignore_parallel_frontend(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1279    if config.parallel_frontend_enabled()
1280        && config.parse_name_directive(line, "ignore-parallel-frontend")
1281    {
1282        return IgnoreDecision::Ignore {
1283            reason: "ignored when the parallel frontend is enabled".into(),
1284        };
1285    }
1286    IgnoreDecision::Continue
1287}
1288
1289enum IgnoreDecision {
1290    Ignore { reason: String },
1291    Continue,
1292    Error { message: String },
1293}
1294
1295fn parse_edition_range(config: &Config, line: &DirectiveLine<'_>) -> Option<EditionRange> {
1296    let raw = config.parse_name_value_directive(line, "edition")?;
1297    let &DirectiveLine { file_path: testfile, line_number, .. } = line;
1298
1299    // Edition range is half-open: `[lower_bound, upper_bound)`
1300    if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1301        Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1302            (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1303                fatal!(
1304                    "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1305                );
1306            }
1307            (Some(lower_bound), Some(upper_bound)) => {
1308                EditionRange::Range { lower_bound, upper_bound }
1309            }
1310            (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1311            (None, Some(_)) => {
1312                fatal!(
1313                    "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1314                );
1315            }
1316            (None, None) => {
1317                fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1318            }
1319        })
1320    } else {
1321        match maybe_parse_edition(&raw) {
1322            Some(edition) => Some(EditionRange::Exact(edition)),
1323            None => {
1324                fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1325            }
1326        }
1327    }
1328}
1329
1330fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1331    input = input.trim();
1332    if input.is_empty() {
1333        return None;
1334    }
1335    Some(parse_edition(input))
1336}
1337
1338#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1339enum EditionRange {
1340    Exact(Edition),
1341    RangeFrom(Edition),
1342    /// Half-open range: `[lower_bound, upper_bound)`
1343    Range {
1344        lower_bound: Edition,
1345        upper_bound: Edition,
1346    },
1347}
1348
1349impl EditionRange {
1350    fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1351        let min_edition = Edition::Year(2015);
1352        let requested = requested.into().unwrap_or(min_edition);
1353
1354        match *self {
1355            EditionRange::Exact(exact) => exact,
1356            EditionRange::RangeFrom(lower_bound) => {
1357                if requested >= lower_bound {
1358                    requested
1359                } else {
1360                    lower_bound
1361                }
1362            }
1363            EditionRange::Range { lower_bound, upper_bound } => {
1364                if requested >= lower_bound && requested < upper_bound {
1365                    requested
1366                } else {
1367                    lower_bound
1368                }
1369            }
1370        }
1371    }
1372}
1373
1374fn split_flags(flags: &str) -> Vec<String> {
1375    // Individual flags can be single-quoted to preserve spaces; see
1376    // <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
1377    // FIXME(#147955): Replace this ad-hoc quoting with an escape/quote system that
1378    // is closer to what actual shells do, so that it's more flexible and familiar.
1379    flags
1380        .split('\'')
1381        .enumerate()
1382        .flat_map(|(i, f)| if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() })
1383        .map(move |s| s.to_owned())
1384        .collect::<Vec<_>>()
1385}