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.mode,
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.mode,
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    mode: TestMode,
578    file_directives: &FileDirectives<'_>,
579    it: &mut dyn FnMut(&DirectiveLine<'_>),
580) {
581    let testfile = file_directives.path;
582
583    // Coverage tests in coverage-run mode always have these extra directives, without needing to
584    // specify them manually in every test file.
585    //
586    // FIXME(jieyouxu): I feel like there's a better way to do this, leaving for later.
587    if mode == TestMode::CoverageRun {
588        let extra_directives: &[&str] = &[
589            "//@ needs-profiler-runtime",
590            // FIXME(pietroalbini): this test currently does not work on cross-compiled targets
591            // because remote-test is not capable of sending back the *.profraw files generated by
592            // the LLVM instrumentation.
593            "//@ ignore-cross-compile",
594        ];
595        // Process the extra implied directives, with a dummy line number of 0.
596        for directive_str in extra_directives {
597            let directive_line = line_directive(testfile, LineNumber::ZERO, directive_str)
598                .unwrap_or_else(|| panic!("bad extra-directive line: {directive_str:?}"));
599            it(&directive_line);
600        }
601    }
602
603    // Note: affects all codegen test suites under test mode `codegen`, e.g. `codegen-llvm`.
604    //
605    // Codegen tests automatically receive implied `//@ needs-target-std`, unless
606    // `#![no_std]`/`#![no_core]` attribute was explicitly seen. The rationale is basically to avoid
607    // having to manually maintain a bunch of `//@ needs-target-std` directives esp. for targets
608    // tested/built out-of-tree.
609    if mode == TestMode::Codegen && !file_directives.has_explicit_no_std_core_attribute {
610        let implied_needs_target_std_line =
611            line_directive(testfile, LineNumber::ZERO, "//@ needs-target-std")
612                .expect("valid `needs-target-std` directive line");
613        it(&implied_needs_target_std_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            // `//@ revisions: true false` Implying `--cfg=true` and `--cfg=false` makes it very
625            // weird for the test, since if the test writer wants a cfg of the same revision name
626            // they'd have to use `cfg(r#true)` and `cfg(r#false)`.
627            "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        // nv is either FOO or FOO=BAR
671        // FIXME(Zalathar): The form without `=` seems to be unused; should
672        // we drop support for it?
673        let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
674        // Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
675        // sees the name as `FOO` and not ` FOO`.
676        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        // FIXME(Zalathar): Ideally, this should raise an error if a name-only
712        // directive is followed by a colon, since that's the wrong syntax.
713        // But we would need to fix tests that rely on the current behaviour.
714        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        // FIXME(Zalathar): This silently discards directives with a matching
729        // name but no colon. Unfortunately, some directives (e.g. "pp-exact")
730        // currently rely on _not_ panicking here.
731        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        // If the flag is already true, don't bother looking at the directive.
746        *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
774// FIXME(jieyouxu): fix some of these variable names to more accurately reflect what they do.
775fn 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
832/// Parses the regex and replacement values of a `//@ normalize-*` directive, in the format:
833/// ```text
834/// "REGEX" -> "REPLACEMENT"
835/// ```
836fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
837    // FIXME: Support escaped double-quotes in strings.
838    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    // A `\n` sequence in the replacement becomes an actual newline.
852    // FIXME: Do unescaping in a less ad-hoc way, and perhaps support escaped
853    // backslashes and double-quotes.
854    let replacement = replacement.replace("\\n", "\n");
855    Some((regex, replacement))
856}
857
858/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this
859/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting
860/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of
861/// having to write `//@ min-llvm-version: 19.0.0`.
862///
863/// Currently panics if the input string is malformed, though we really should not use panic as an
864/// error handling strategy.
865///
866/// FIXME(jieyouxu): improve error handling
867pub fn extract_llvm_version(version: &str) -> Version {
868    // The version substring we're interested in usually looks like the `1.2.3`, without any of the
869    // fancy suffix like `-rc1` or `meow`.
870    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 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
904/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation
905/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`.
906///
907/// If the `<version2>` part is omitted, the second component of the tuple is the same as
908/// `<version1>`.
909fn 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    // Scan through the test file to handle `ignore-*`, `only-*`, and `needs-*` directives.
954    iter_directives(
955        config.mode,
956        file_directives,
957        &mut |ln @ &DirectiveLine { line_number, .. }| {
958            if !ln.applies_to_test_revision(test_revision) {
959                return;
960            }
961
962            // Parse `aux-*` directives, for use by up-to-date checks.
963            parse_and_update_aux(config, ln, aux_props);
964
965            macro_rules! decision {
966                ($e:expr) => {
967                    match $e {
968                        IgnoreDecision::Ignore { reason } => {
969                            ignore = true;
970                            ignore_message = Some(reason.into());
971                        }
972                        IgnoreDecision::Error { message } => {
973                            error!("{path}:{line_number}: {message}");
974                            *poisoned = true;
975                            return;
976                        }
977                        IgnoreDecision::Continue => {}
978                    }
979                };
980            }
981
982            decision!(cfg::handle_ignore(&cache.cfg_conditions, ln));
983            decision!(cfg::handle_only(&cache.cfg_conditions, ln));
984            decision!(needs::handle_needs(&cache.needs, config, ln));
985            decision!(ignore_llvm(config, ln));
986            decision!(ignore_backends(config, ln));
987            decision!(needs_backends(config, ln));
988            decision!(ignore_cdb(config, ln));
989            decision!(ignore_gdb(config, ln));
990            decision!(ignore_lldb(config, ln));
991
992            if config.target == "wasm32-unknown-unknown"
993                && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
994            {
995                decision!(IgnoreDecision::Ignore {
996                    reason: "ignored on WASM as the run results cannot be checked there".into(),
997                });
998            }
999
1000            should_fail |= config.parse_name_directive(ln, "should-fail");
1001        },
1002    );
1003
1004    // The `should-fail` annotation doesn't apply to pretty tests,
1005    // since we run the pretty printer across all tests by default.
1006    // If desired, we could add a `should-fail-pretty` annotation.
1007    let should_fail = if should_fail && config.mode != TestMode::Pretty {
1008        ShouldFail::Yes
1009    } else {
1010        ShouldFail::No
1011    };
1012
1013    CollectedTestDesc {
1014        name,
1015        filterable_path: filterable_path.to_owned(),
1016        ignore,
1017        ignore_message,
1018        should_fail,
1019    }
1020}
1021
1022fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1023    if config.debugger != Some(Debugger::Cdb) {
1024        return IgnoreDecision::Continue;
1025    }
1026
1027    if let Some(actual_version) = config.cdb_version {
1028        if line.name == "min-cdb-version"
1029            && let Some(rest) = line.value_after_colon().map(str::trim)
1030        {
1031            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1032                panic!("couldn't parse version range: {:?}", rest);
1033            });
1034
1035            // Ignore if actual version is smaller than the minimum
1036            // required version
1037            if actual_version < min_version {
1038                return IgnoreDecision::Ignore {
1039                    reason: format!("ignored when the CDB version is lower than {rest}"),
1040                };
1041            }
1042        }
1043    }
1044    IgnoreDecision::Continue
1045}
1046
1047fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1048    if config.debugger != Some(Debugger::Gdb) {
1049        return IgnoreDecision::Continue;
1050    }
1051
1052    if let Some(actual_version) = config.gdb_version {
1053        if line.name == "min-gdb-version"
1054            && let Some(rest) = line.value_after_colon().map(str::trim)
1055        {
1056            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1057                .unwrap_or_else(|| {
1058                    panic!("couldn't parse version range: {:?}", rest);
1059                });
1060
1061            if start_ver != end_ver {
1062                panic!("Expected single GDB version")
1063            }
1064            // Ignore if actual version is smaller than the minimum
1065            // required version
1066            if actual_version < start_ver {
1067                return IgnoreDecision::Ignore {
1068                    reason: format!("ignored when the GDB version is lower than {rest}"),
1069                };
1070            }
1071        } else if line.name == "ignore-gdb-version"
1072            && let Some(rest) = line.value_after_colon().map(str::trim)
1073        {
1074            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1075                .unwrap_or_else(|| {
1076                    panic!("couldn't parse version range: {:?}", rest);
1077                });
1078
1079            if max_version < min_version {
1080                panic!("Malformed GDB version range: max < min")
1081            }
1082
1083            if actual_version >= min_version && actual_version <= max_version {
1084                if min_version == max_version {
1085                    return IgnoreDecision::Ignore {
1086                        reason: format!("ignored when the GDB version is {rest}"),
1087                    };
1088                } else {
1089                    return IgnoreDecision::Ignore {
1090                        reason: format!("ignored when the GDB version is between {rest}"),
1091                    };
1092                }
1093            }
1094        }
1095    }
1096    IgnoreDecision::Continue
1097}
1098
1099fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1100    if config.debugger != Some(Debugger::Lldb) {
1101        return IgnoreDecision::Continue;
1102    }
1103
1104    if let Some(actual_version) = config.lldb_version {
1105        if line.name == "min-lldb-version"
1106            && let Some(rest) = line.value_after_colon().map(str::trim)
1107        {
1108            let min_version = rest.parse().unwrap_or_else(|e| {
1109                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1110            });
1111            // Ignore if actual version is smaller the minimum required
1112            // version
1113            if actual_version < min_version {
1114                return IgnoreDecision::Ignore {
1115                    reason: format!("ignored when the LLDB version is {rest}"),
1116                };
1117            }
1118        }
1119    }
1120    IgnoreDecision::Continue
1121}
1122
1123fn ignore_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1124    let path = line.file_path;
1125    if let Some(backends_to_ignore) = config.parse_name_value_directive(line, "ignore-backends") {
1126        for backend in backends_to_ignore.split_whitespace().map(|backend| {
1127            match CodegenBackend::try_from(backend) {
1128                Ok(backend) => backend,
1129                Err(error) => {
1130                    panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1131                }
1132            }
1133        }) {
1134            if !config.bypass_ignore_backends && config.default_codegen_backend == backend {
1135                return IgnoreDecision::Ignore {
1136                    reason: format!("{} backend is marked as ignore", backend.as_str()),
1137                };
1138            }
1139        }
1140    }
1141    IgnoreDecision::Continue
1142}
1143
1144fn needs_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1145    let path = line.file_path;
1146    if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends") {
1147        if !needed_backends
1148            .split_whitespace()
1149            .map(|backend| match CodegenBackend::try_from(backend) {
1150                Ok(backend) => backend,
1151                Err(error) => {
1152                    panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1153                }
1154            })
1155            .any(|backend| config.default_codegen_backend == backend)
1156        {
1157            return IgnoreDecision::Ignore {
1158                reason: format!(
1159                    "{} backend is not part of required backends",
1160                    config.default_codegen_backend.as_str()
1161                ),
1162            };
1163        }
1164    }
1165    IgnoreDecision::Continue
1166}
1167
1168fn ignore_llvm(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1169    let path = line.file_path;
1170    if let Some(needed_components) =
1171        config.parse_name_value_directive(line, "needs-llvm-components")
1172    {
1173        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1174        if let Some(missing_component) = needed_components
1175            .split_whitespace()
1176            .find(|needed_component| !components.contains(needed_component))
1177        {
1178            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1179                panic!(
1180                    "missing LLVM component {missing_component}, \
1181                    and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {path}",
1182                );
1183            }
1184            return IgnoreDecision::Ignore {
1185                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1186            };
1187        }
1188    }
1189    if let Some(actual_version) = &config.llvm_version {
1190        // Note that these `min` versions will check for not just major versions.
1191
1192        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1193            let min_version = extract_llvm_version(&version_string);
1194            // Ignore if actual version is smaller than the minimum required version.
1195            if *actual_version < min_version {
1196                return IgnoreDecision::Ignore {
1197                    reason: format!(
1198                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1199                    ),
1200                };
1201            }
1202        } else if let Some(version_string) =
1203            config.parse_name_value_directive(line, "max-llvm-major-version")
1204        {
1205            let max_version = extract_llvm_version(&version_string);
1206            // Ignore if actual major version is larger than the maximum required major version.
1207            if actual_version.major > max_version.major {
1208                return IgnoreDecision::Ignore {
1209                    reason: format!(
1210                        "ignored when the LLVM version ({actual_version}) is newer than major\
1211                        version {}",
1212                        max_version.major
1213                    ),
1214                };
1215            }
1216        } else if let Some(version_string) =
1217            config.parse_name_value_directive(line, "min-system-llvm-version")
1218        {
1219            let min_version = extract_llvm_version(&version_string);
1220            // Ignore if using system LLVM and actual version
1221            // is smaller the minimum required version
1222            if config.system_llvm && *actual_version < min_version {
1223                return IgnoreDecision::Ignore {
1224                    reason: format!(
1225                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1226                    ),
1227                };
1228            }
1229        } else if let Some(version_range) =
1230            config.parse_name_value_directive(line, "ignore-llvm-version")
1231        {
1232            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1233            let (v_min, v_max) =
1234                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1235                    .unwrap_or_else(|| {
1236                        panic!("couldn't parse version range: \"{version_range}\"");
1237                    });
1238            if v_max < v_min {
1239                panic!("malformed LLVM version range where {v_max} < {v_min}")
1240            }
1241            // Ignore if version lies inside of range.
1242            if *actual_version >= v_min && *actual_version <= v_max {
1243                if v_min == v_max {
1244                    return IgnoreDecision::Ignore {
1245                        reason: format!("ignored when the LLVM version is {actual_version}"),
1246                    };
1247                } else {
1248                    return IgnoreDecision::Ignore {
1249                        reason: format!(
1250                            "ignored when the LLVM version is between {v_min} and {v_max}"
1251                        ),
1252                    };
1253                }
1254            }
1255        } else if let Some(version_string) =
1256            config.parse_name_value_directive(line, "exact-llvm-major-version")
1257        {
1258            // Syntax is "exact-llvm-major-version: <version>"
1259            let version = extract_llvm_version(&version_string);
1260            if actual_version.major != version.major {
1261                return IgnoreDecision::Ignore {
1262                    reason: format!(
1263                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1264                        actual_version.major, version.major
1265                    ),
1266                };
1267            }
1268        }
1269    }
1270    IgnoreDecision::Continue
1271}
1272
1273enum IgnoreDecision {
1274    Ignore { reason: String },
1275    Continue,
1276    Error { message: String },
1277}
1278
1279fn parse_edition_range(config: &Config, line: &DirectiveLine<'_>) -> Option<EditionRange> {
1280    let raw = config.parse_name_value_directive(line, "edition")?;
1281    let &DirectiveLine { file_path: testfile, line_number, .. } = line;
1282
1283    // Edition range is half-open: `[lower_bound, upper_bound)`
1284    if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1285        Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1286            (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1287                fatal!(
1288                    "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1289                );
1290            }
1291            (Some(lower_bound), Some(upper_bound)) => {
1292                EditionRange::Range { lower_bound, upper_bound }
1293            }
1294            (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1295            (None, Some(_)) => {
1296                fatal!(
1297                    "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1298                );
1299            }
1300            (None, None) => {
1301                fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1302            }
1303        })
1304    } else {
1305        match maybe_parse_edition(&raw) {
1306            Some(edition) => Some(EditionRange::Exact(edition)),
1307            None => {
1308                fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1309            }
1310        }
1311    }
1312}
1313
1314fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1315    input = input.trim();
1316    if input.is_empty() {
1317        return None;
1318    }
1319    Some(parse_edition(input))
1320}
1321
1322#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1323enum EditionRange {
1324    Exact(Edition),
1325    RangeFrom(Edition),
1326    /// Half-open range: `[lower_bound, upper_bound)`
1327    Range {
1328        lower_bound: Edition,
1329        upper_bound: Edition,
1330    },
1331}
1332
1333impl EditionRange {
1334    fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1335        let min_edition = Edition::Year(2015);
1336        let requested = requested.into().unwrap_or(min_edition);
1337
1338        match *self {
1339            EditionRange::Exact(exact) => exact,
1340            EditionRange::RangeFrom(lower_bound) => {
1341                if requested >= lower_bound {
1342                    requested
1343                } else {
1344                    lower_bound
1345                }
1346            }
1347            EditionRange::Range { lower_bound, upper_bound } => {
1348                if requested >= lower_bound && requested < upper_bound {
1349                    requested
1350                } else {
1351                    lower_bound
1352                }
1353            }
1354        }
1355    }
1356}
1357
1358fn split_flags(flags: &str) -> Vec<String> {
1359    // Individual flags can be single-quoted to preserve spaces; see
1360    // <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
1361    // FIXME(#147955): Replace this ad-hoc quoting with an escape/quote system that
1362    // is closer to what actual shells do, so that it's more flexible and familiar.
1363    flags
1364        .split('\'')
1365        .enumerate()
1366        .flat_map(|(i, f)| if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() })
1367        .map(move |s| s.to_owned())
1368        .collect::<Vec<_>>()
1369}