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};
11pub(crate) use crate::directives::auxiliary::AuxProps;
12use crate::directives::auxiliary::parse_and_update_aux;
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 annotatins 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    for directive_line in &file_directives.lines {
604        it(directive_line);
605    }
606}
607
608impl Config {
609    fn parse_and_update_revisions(&self, line: &DirectiveLine<'_>, existing: &mut Vec<String>) {
610        const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
611            // `//@ revisions: true false` Implying `--cfg=true` and `--cfg=false` makes it very
612            // weird for the test, since if the test writer wants a cfg of the same revision name
613            // they'd have to use `cfg(r#true)` and `cfg(r#false)`.
614            "true", "false",
615        ];
616
617        const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
618            ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
619
620        if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
621            let &DirectiveLine { file_path: testfile, .. } = line;
622
623            if self.mode == TestMode::RunMake {
624                panic!("`run-make` mode tests do not support revisions: {}", testfile);
625            }
626
627            let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
628            for revision in raw.split_whitespace() {
629                if !duplicates.insert(revision.to_string()) {
630                    panic!("duplicate revision: `{}` in line `{}`: {}", revision, raw, testfile);
631                }
632
633                if FORBIDDEN_REVISION_NAMES.contains(&revision) {
634                    panic!(
635                        "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
636                        revision, raw, testfile
637                    );
638                }
639
640                if matches!(self.mode, TestMode::Assembly | TestMode::Codegen | TestMode::MirOpt)
641                    && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
642                {
643                    panic!(
644                        "revision name `{revision}` is not permitted in a test suite that uses \
645                        `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
646                        prefix: `{revision}` in line `{}`: {}",
647                        raw, testfile
648                    );
649                }
650
651                existing.push(revision.to_string());
652            }
653        }
654    }
655
656    fn parse_env(nv: String) -> (String, String) {
657        // nv is either FOO or FOO=BAR
658        // FIXME(Zalathar): The form without `=` seems to be unused; should
659        // we drop support for it?
660        let (name, value) = nv.split_once('=').unwrap_or((&nv, ""));
661        // Trim whitespace from the name, so that `//@ exec-env: FOO=BAR`
662        // sees the name as `FOO` and not ` FOO`.
663        let name = name.trim();
664        (name.to_owned(), value.to_owned())
665    }
666
667    fn parse_pp_exact(&self, line: &DirectiveLine<'_>) -> Option<Utf8PathBuf> {
668        if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
669            Some(Utf8PathBuf::from(&s))
670        } else if self.parse_name_directive(line, "pp-exact") {
671            line.file_path.file_name().map(Utf8PathBuf::from)
672        } else {
673            None
674        }
675    }
676
677    fn parse_custom_normalization(&self, line: &DirectiveLine<'_>) -> Option<NormalizeRule> {
678        let &DirectiveLine { name, .. } = line;
679
680        let kind = match name {
681            "normalize-stdout" => NormalizeKind::Stdout,
682            "normalize-stderr" => NormalizeKind::Stderr,
683            "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
684            "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
685            _ => return None,
686        };
687
688        let Some((regex, replacement)) = line.value_after_colon().and_then(parse_normalize_rule)
689        else {
690            error!("couldn't parse custom normalization rule: `{}`", line.display());
691            help!("expected syntax is: `{name}: \"REGEX\" -> \"REPLACEMENT\"`");
692            panic!("invalid normalization rule detected");
693        };
694        Some(NormalizeRule { kind, regex, replacement })
695    }
696
697    fn parse_name_directive(&self, line: &DirectiveLine<'_>, directive: &str) -> bool {
698        // FIXME(Zalathar): Ideally, this should raise an error if a name-only
699        // directive is followed by a colon, since that's the wrong syntax.
700        // But we would need to fix tests that rely on the current behaviour.
701        line.name == directive
702    }
703
704    fn parse_name_value_directive(
705        &self,
706        line: &DirectiveLine<'_>,
707        directive: &str,
708    ) -> Option<String> {
709        let &DirectiveLine { file_path, line_number, .. } = line;
710
711        if line.name != directive {
712            return None;
713        };
714
715        // FIXME(Zalathar): This silently discards directives with a matching
716        // name but no colon. Unfortunately, some directives (e.g. "pp-exact")
717        // currently rely on _not_ panicking here.
718        let value = line.value_after_colon()?;
719        debug!("{}: {}", directive, value);
720        let value = expand_variables(value.to_owned(), self);
721
722        if value.is_empty() {
723            error!("{file_path}:{line_number}: empty value for directive `{directive}`");
724            help!("expected syntax is: `{directive}: value`");
725            panic!("empty directive value detected");
726        }
727
728        Some(value)
729    }
730
731    fn set_name_directive(&self, line: &DirectiveLine<'_>, directive: &str, value: &mut bool) {
732        // If the flag is already true, don't bother looking at the directive.
733        *value = *value || self.parse_name_directive(line, directive);
734    }
735
736    fn set_name_value_directive<T>(
737        &self,
738        line: &DirectiveLine<'_>,
739        directive: &str,
740        value: &mut Option<T>,
741        parse: impl FnOnce(String) -> T,
742    ) {
743        if value.is_none() {
744            *value = self.parse_name_value_directive(line, directive).map(parse);
745        }
746    }
747
748    fn push_name_value_directive<T>(
749        &self,
750        line: &DirectiveLine<'_>,
751        directive: &str,
752        values: &mut Vec<T>,
753        parse: impl FnOnce(String) -> T,
754    ) {
755        if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
756            values.push(value);
757        }
758    }
759}
760
761// FIXME(jieyouxu): fix some of these variable names to more accurately reflect what they do.
762fn expand_variables(mut value: String, config: &Config) -> String {
763    const CWD: &str = "{{cwd}}";
764    const SRC_BASE: &str = "{{src-base}}";
765    const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
766    const RUST_SRC_BASE: &str = "{{rust-src-base}}";
767    const SYSROOT_BASE: &str = "{{sysroot-base}}";
768    const TARGET_LINKER: &str = "{{target-linker}}";
769    const TARGET: &str = "{{target}}";
770
771    if value.contains(CWD) {
772        let cwd = env::current_dir().unwrap();
773        value = value.replace(CWD, &cwd.to_str().unwrap());
774    }
775
776    if value.contains(SRC_BASE) {
777        value = value.replace(SRC_BASE, &config.src_test_suite_root.as_str());
778    }
779
780    if value.contains(TEST_SUITE_BUILD_BASE) {
781        value = value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.as_str());
782    }
783
784    if value.contains(SYSROOT_BASE) {
785        value = value.replace(SYSROOT_BASE, &config.sysroot_base.as_str());
786    }
787
788    if value.contains(TARGET_LINKER) {
789        value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
790    }
791
792    if value.contains(TARGET) {
793        value = value.replace(TARGET, &config.target);
794    }
795
796    if value.contains(RUST_SRC_BASE) {
797        let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
798        src_base.try_exists().expect(&*format!("{} should exists", src_base));
799        let src_base = src_base.read_link_utf8().unwrap_or(src_base);
800        value = value.replace(RUST_SRC_BASE, &src_base.as_str());
801    }
802
803    value
804}
805
806struct NormalizeRule {
807    kind: NormalizeKind,
808    regex: String,
809    replacement: String,
810}
811
812enum NormalizeKind {
813    Stdout,
814    Stderr,
815    Stderr32bit,
816    Stderr64bit,
817}
818
819/// Parses the regex and replacement values of a `//@ normalize-*` directive, in the format:
820/// ```text
821/// "REGEX" -> "REPLACEMENT"
822/// ```
823fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
824    // FIXME: Support escaped double-quotes in strings.
825    let captures = static_regex!(
826        r#"(?x) # (verbose mode regex)
827        ^
828        \s*                     # (leading whitespace)
829        "(?<regex>[^"]*)"       # "REGEX"
830        \s+->\s+                # ->
831        "(?<replacement>[^"]*)" # "REPLACEMENT"
832        $
833        "#
834    )
835    .captures(raw_value)?;
836    let regex = captures["regex"].to_owned();
837    let replacement = captures["replacement"].to_owned();
838    // A `\n` sequence in the replacement becomes an actual newline.
839    // FIXME: Do unescaping in a less ad-hoc way, and perhaps support escaped
840    // backslashes and double-quotes.
841    let replacement = replacement.replace("\\n", "\n");
842    Some((regex, replacement))
843}
844
845/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this
846/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting
847/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of
848/// having to write `//@ min-llvm-version: 19.0.0`.
849///
850/// Currently panics if the input string is malformed, though we really should not use panic as an
851/// error handling strategy.
852///
853/// FIXME(jieyouxu): improve error handling
854pub fn extract_llvm_version(version: &str) -> Version {
855    // The version substring we're interested in usually looks like the `1.2.3`, without any of the
856    // fancy suffix like `-rc1` or `meow`.
857    let version = version.trim();
858    let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
859    let version_without_suffix = match version.split_once(uninterested) {
860        Some((prefix, _suffix)) => prefix,
861        None => version,
862    };
863
864    let components: Vec<u64> = version_without_suffix
865        .split('.')
866        .map(|s| s.parse().expect("llvm version component should consist of only digits"))
867        .collect();
868
869    match &components[..] {
870        [major] => Version::new(*major, 0, 0),
871        [major, minor] => Version::new(*major, *minor, 0),
872        [major, minor, patch] => Version::new(*major, *minor, *patch),
873        _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
874    }
875}
876
877pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
878    let output = Command::new(binary_path).arg("--version").output().ok()?;
879    if !output.status.success() {
880        return None;
881    }
882    let version = String::from_utf8(output.stdout).ok()?;
883    for line in version.lines() {
884        if let Some(version) = line.split("LLVM version ").nth(1) {
885            return Some(extract_llvm_version(version));
886        }
887    }
888    None
889}
890
891/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation
892/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`.
893///
894/// If the `<version2>` part is omitted, the second component of the tuple is the same as
895/// `<version1>`.
896fn extract_version_range<'a, F, VersionTy: Clone>(
897    line: &'a str,
898    parse: F,
899) -> Option<(VersionTy, VersionTy)>
900where
901    F: Fn(&'a str) -> Option<VersionTy>,
902{
903    let mut splits = line.splitn(2, "- ").map(str::trim);
904    let min = splits.next().unwrap();
905    if min.ends_with('-') {
906        return None;
907    }
908
909    let max = splits.next();
910
911    if min.is_empty() {
912        return None;
913    }
914
915    let min = parse(min)?;
916    let max = match max {
917        Some("") => return None,
918        Some(max) => parse(max)?,
919        _ => min.clone(),
920    };
921
922    Some((min, max))
923}
924
925pub(crate) fn make_test_description(
926    config: &Config,
927    cache: &DirectivesCache,
928    name: String,
929    path: &Utf8Path,
930    filterable_path: &Utf8Path,
931    file_directives: &FileDirectives<'_>,
932    test_revision: Option<&str>,
933    poisoned: &mut bool,
934    aux_props: &mut AuxProps,
935) -> CollectedTestDesc {
936    let mut ignore = false;
937    let mut ignore_message = None;
938    let mut should_fail = false;
939
940    // Scan through the test file to handle `ignore-*`, `only-*`, and `needs-*` directives.
941    iter_directives(
942        config.mode,
943        file_directives,
944        &mut |ln @ &DirectiveLine { line_number, .. }| {
945            if !ln.applies_to_test_revision(test_revision) {
946                return;
947            }
948
949            // Parse `aux-*` directives, for use by up-to-date checks.
950            parse_and_update_aux(config, ln, aux_props);
951
952            macro_rules! decision {
953                ($e:expr) => {
954                    match $e {
955                        IgnoreDecision::Ignore { reason } => {
956                            ignore = true;
957                            ignore_message = Some(reason.into());
958                        }
959                        IgnoreDecision::Error { message } => {
960                            error!("{path}:{line_number}: {message}");
961                            *poisoned = true;
962                            return;
963                        }
964                        IgnoreDecision::Continue => {}
965                    }
966                };
967            }
968
969            decision!(cfg::handle_ignore(&cache.cfg_conditions, ln));
970            decision!(cfg::handle_only(&cache.cfg_conditions, ln));
971            decision!(needs::handle_needs(&cache.needs, config, ln));
972            decision!(ignore_llvm(config, ln));
973            decision!(ignore_backends(config, ln));
974            decision!(needs_backends(config, ln));
975            decision!(ignore_cdb(config, ln));
976            decision!(ignore_gdb(config, ln));
977            decision!(ignore_lldb(config, ln));
978
979            if config.target == "wasm32-unknown-unknown"
980                && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
981            {
982                decision!(IgnoreDecision::Ignore {
983                    reason: "ignored on WASM as the run results cannot be checked there".into(),
984                });
985            }
986
987            should_fail |= config.parse_name_directive(ln, "should-fail");
988        },
989    );
990
991    // The `should-fail` annotation doesn't apply to pretty tests,
992    // since we run the pretty printer across all tests by default.
993    // If desired, we could add a `should-fail-pretty` annotation.
994    let should_fail = if should_fail && config.mode != TestMode::Pretty {
995        ShouldFail::Yes
996    } else {
997        ShouldFail::No
998    };
999
1000    CollectedTestDesc {
1001        name,
1002        filterable_path: filterable_path.to_owned(),
1003        ignore,
1004        ignore_message,
1005        should_fail,
1006    }
1007}
1008
1009fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1010    if config.debugger != Some(Debugger::Cdb) {
1011        return IgnoreDecision::Continue;
1012    }
1013
1014    if let Some(actual_version) = config.cdb_version {
1015        if line.name == "min-cdb-version"
1016            && let Some(rest) = line.value_after_colon().map(str::trim)
1017        {
1018            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1019                panic!("couldn't parse version range: {:?}", rest);
1020            });
1021
1022            // Ignore if actual version is smaller than the minimum
1023            // required version
1024            if actual_version < min_version {
1025                return IgnoreDecision::Ignore {
1026                    reason: format!("ignored when the CDB version is lower than {rest}"),
1027                };
1028            }
1029        }
1030    }
1031    IgnoreDecision::Continue
1032}
1033
1034fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1035    if config.debugger != Some(Debugger::Gdb) {
1036        return IgnoreDecision::Continue;
1037    }
1038
1039    if let Some(actual_version) = config.gdb_version {
1040        if line.name == "min-gdb-version"
1041            && let Some(rest) = line.value_after_colon().map(str::trim)
1042        {
1043            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1044                .unwrap_or_else(|| {
1045                    panic!("couldn't parse version range: {:?}", rest);
1046                });
1047
1048            if start_ver != end_ver {
1049                panic!("Expected single GDB version")
1050            }
1051            // Ignore if actual version is smaller than the minimum
1052            // required version
1053            if actual_version < start_ver {
1054                return IgnoreDecision::Ignore {
1055                    reason: format!("ignored when the GDB version is lower than {rest}"),
1056                };
1057            }
1058        } else if line.name == "ignore-gdb-version"
1059            && let Some(rest) = line.value_after_colon().map(str::trim)
1060        {
1061            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1062                .unwrap_or_else(|| {
1063                    panic!("couldn't parse version range: {:?}", rest);
1064                });
1065
1066            if max_version < min_version {
1067                panic!("Malformed GDB version range: max < min")
1068            }
1069
1070            if actual_version >= min_version && actual_version <= max_version {
1071                if min_version == max_version {
1072                    return IgnoreDecision::Ignore {
1073                        reason: format!("ignored when the GDB version is {rest}"),
1074                    };
1075                } else {
1076                    return IgnoreDecision::Ignore {
1077                        reason: format!("ignored when the GDB version is between {rest}"),
1078                    };
1079                }
1080            }
1081        }
1082    }
1083    IgnoreDecision::Continue
1084}
1085
1086fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1087    if config.debugger != Some(Debugger::Lldb) {
1088        return IgnoreDecision::Continue;
1089    }
1090
1091    if let Some(actual_version) = config.lldb_version {
1092        if line.name == "min-lldb-version"
1093            && let Some(rest) = line.value_after_colon().map(str::trim)
1094        {
1095            let min_version = rest.parse().unwrap_or_else(|e| {
1096                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1097            });
1098            // Ignore if actual version is smaller the minimum required
1099            // version
1100            if actual_version < min_version {
1101                return IgnoreDecision::Ignore {
1102                    reason: format!("ignored when the LLDB version is {rest}"),
1103                };
1104            }
1105        }
1106    }
1107    IgnoreDecision::Continue
1108}
1109
1110fn ignore_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1111    let path = line.file_path;
1112    if let Some(backends_to_ignore) = config.parse_name_value_directive(line, "ignore-backends") {
1113        for backend in backends_to_ignore.split_whitespace().map(|backend| {
1114            match CodegenBackend::try_from(backend) {
1115                Ok(backend) => backend,
1116                Err(error) => {
1117                    panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1118                }
1119            }
1120        }) {
1121            if !config.bypass_ignore_backends && config.default_codegen_backend == backend {
1122                return IgnoreDecision::Ignore {
1123                    reason: format!("{} backend is marked as ignore", backend.as_str()),
1124                };
1125            }
1126        }
1127    }
1128    IgnoreDecision::Continue
1129}
1130
1131fn needs_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1132    let path = line.file_path;
1133    if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends") {
1134        if !needed_backends
1135            .split_whitespace()
1136            .map(|backend| match CodegenBackend::try_from(backend) {
1137                Ok(backend) => backend,
1138                Err(error) => {
1139                    panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1140                }
1141            })
1142            .any(|backend| config.default_codegen_backend == backend)
1143        {
1144            return IgnoreDecision::Ignore {
1145                reason: format!(
1146                    "{} backend is not part of required backends",
1147                    config.default_codegen_backend.as_str()
1148                ),
1149            };
1150        }
1151    }
1152    IgnoreDecision::Continue
1153}
1154
1155fn ignore_llvm(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1156    let path = line.file_path;
1157    if let Some(needed_components) =
1158        config.parse_name_value_directive(line, "needs-llvm-components")
1159    {
1160        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1161        if let Some(missing_component) = needed_components
1162            .split_whitespace()
1163            .find(|needed_component| !components.contains(needed_component))
1164        {
1165            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1166                panic!(
1167                    "missing LLVM component {missing_component}, \
1168                    and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {path}",
1169                );
1170            }
1171            return IgnoreDecision::Ignore {
1172                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1173            };
1174        }
1175    }
1176    if let Some(actual_version) = &config.llvm_version {
1177        // Note that these `min` versions will check for not just major versions.
1178
1179        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1180            let min_version = extract_llvm_version(&version_string);
1181            // Ignore if actual version is smaller than the minimum required version.
1182            if *actual_version < min_version {
1183                return IgnoreDecision::Ignore {
1184                    reason: format!(
1185                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1186                    ),
1187                };
1188            }
1189        } else if let Some(version_string) =
1190            config.parse_name_value_directive(line, "max-llvm-major-version")
1191        {
1192            let max_version = extract_llvm_version(&version_string);
1193            // Ignore if actual major version is larger than the maximum required major version.
1194            if actual_version.major > max_version.major {
1195                return IgnoreDecision::Ignore {
1196                    reason: format!(
1197                        "ignored when the LLVM version ({actual_version}) is newer than major\
1198                        version {}",
1199                        max_version.major
1200                    ),
1201                };
1202            }
1203        } else if let Some(version_string) =
1204            config.parse_name_value_directive(line, "min-system-llvm-version")
1205        {
1206            let min_version = extract_llvm_version(&version_string);
1207            // Ignore if using system LLVM and actual version
1208            // is smaller the minimum required version
1209            if config.system_llvm && *actual_version < min_version {
1210                return IgnoreDecision::Ignore {
1211                    reason: format!(
1212                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1213                    ),
1214                };
1215            }
1216        } else if let Some(version_range) =
1217            config.parse_name_value_directive(line, "ignore-llvm-version")
1218        {
1219            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1220            let (v_min, v_max) =
1221                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1222                    .unwrap_or_else(|| {
1223                        panic!("couldn't parse version range: \"{version_range}\"");
1224                    });
1225            if v_max < v_min {
1226                panic!("malformed LLVM version range where {v_max} < {v_min}")
1227            }
1228            // Ignore if version lies inside of range.
1229            if *actual_version >= v_min && *actual_version <= v_max {
1230                if v_min == v_max {
1231                    return IgnoreDecision::Ignore {
1232                        reason: format!("ignored when the LLVM version is {actual_version}"),
1233                    };
1234                } else {
1235                    return IgnoreDecision::Ignore {
1236                        reason: format!(
1237                            "ignored when the LLVM version is between {v_min} and {v_max}"
1238                        ),
1239                    };
1240                }
1241            }
1242        } else if let Some(version_string) =
1243            config.parse_name_value_directive(line, "exact-llvm-major-version")
1244        {
1245            // Syntax is "exact-llvm-major-version: <version>"
1246            let version = extract_llvm_version(&version_string);
1247            if actual_version.major != version.major {
1248                return IgnoreDecision::Ignore {
1249                    reason: format!(
1250                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1251                        actual_version.major, version.major
1252                    ),
1253                };
1254            }
1255        }
1256    }
1257    IgnoreDecision::Continue
1258}
1259
1260enum IgnoreDecision {
1261    Ignore { reason: String },
1262    Continue,
1263    Error { message: String },
1264}
1265
1266fn parse_edition_range(config: &Config, line: &DirectiveLine<'_>) -> Option<EditionRange> {
1267    let raw = config.parse_name_value_directive(line, "edition")?;
1268    let &DirectiveLine { file_path: testfile, line_number, .. } = line;
1269
1270    // Edition range is half-open: `[lower_bound, upper_bound)`
1271    if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1272        Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1273            (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1274                fatal!(
1275                    "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1276                );
1277            }
1278            (Some(lower_bound), Some(upper_bound)) => {
1279                EditionRange::Range { lower_bound, upper_bound }
1280            }
1281            (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1282            (None, Some(_)) => {
1283                fatal!(
1284                    "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1285                );
1286            }
1287            (None, None) => {
1288                fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1289            }
1290        })
1291    } else {
1292        match maybe_parse_edition(&raw) {
1293            Some(edition) => Some(EditionRange::Exact(edition)),
1294            None => {
1295                fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1296            }
1297        }
1298    }
1299}
1300
1301fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1302    input = input.trim();
1303    if input.is_empty() {
1304        return None;
1305    }
1306    Some(parse_edition(input))
1307}
1308
1309#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1310enum EditionRange {
1311    Exact(Edition),
1312    RangeFrom(Edition),
1313    /// Half-open range: `[lower_bound, upper_bound)`
1314    Range {
1315        lower_bound: Edition,
1316        upper_bound: Edition,
1317    },
1318}
1319
1320impl EditionRange {
1321    fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1322        let min_edition = Edition::Year(2015);
1323        let requested = requested.into().unwrap_or(min_edition);
1324
1325        match *self {
1326            EditionRange::Exact(exact) => exact,
1327            EditionRange::RangeFrom(lower_bound) => {
1328                if requested >= lower_bound {
1329                    requested
1330                } else {
1331                    lower_bound
1332                }
1333            }
1334            EditionRange::Range { lower_bound, upper_bound } => {
1335                if requested >= lower_bound && requested < upper_bound {
1336                    requested
1337                } else {
1338                    lower_bound
1339                }
1340            }
1341        }
1342    }
1343}
1344
1345fn split_flags(flags: &str) -> Vec<String> {
1346    // Individual flags can be single-quoted to preserve spaces; see
1347    // <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
1348    // FIXME(#147955): Replace this ad-hoc quoting with an escape/quote system that
1349    // is closer to what actual shells do, so that it's more flexible and familiar.
1350    flags
1351        .split('\'')
1352        .enumerate()
1353        .flat_map(|(i, f)| if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() })
1354        .map(move |s| s.to_owned())
1355        .collect::<Vec<_>>()
1356}