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(crate) 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(crate) fn load(config: &Config) -> Self {
48        Self {
49            cfg_conditions: cfg::prepare_conditions(config),
50            needs: CachedNeedsConditions::load(config),
51        }
52    }
53}
54
55/// Properties which must be known very early, before actually running
56/// the test.
57#[derive(Default)]
58pub(crate) struct EarlyProps {
59    pub(crate) revisions: Vec<String>,
60}
61
62impl EarlyProps {
63    pub(crate) fn from_file_directives(
64        config: &Config,
65        file_directives: &FileDirectives<'_>,
66    ) -> Self {
67        let mut props = EarlyProps::default();
68
69        iter_directives(
70            config,
71            file_directives,
72            // (dummy comment to force args into vertical layout)
73            &mut |ln: &DirectiveLine<'_>| {
74                config.parse_and_update_revisions(ln, &mut props.revisions);
75            },
76        );
77
78        props
79    }
80}
81
82#[derive(Clone, Debug)]
83pub(crate) struct TestProps {
84    // Lines that should be expected, in order, on standard out
85    pub(crate) error_patterns: Vec<String>,
86    // Regexes that should be expected, in order, on standard out
87    pub(crate) 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(crate) edition: Option<Edition>,
92    // Extra flags to pass to the compiler
93    pub(crate) compile_flags: Vec<String>,
94    // Extra flags to pass when the compiled code is run (such as --bench)
95    pub(crate) run_flags: Vec<String>,
96    /// Extra flags to pass to rustdoc but not the compiler.
97    pub(crate) doc_flags: Vec<String>,
98    // If present, the name of a file that this test should match when
99    // pretty-printed
100    pub(crate) 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(crate) rustc_env: Vec<(String, String)>,
105    // Environment variables to unset prior to compiling.
106    // Variables are unset before applying 'rustc_env'.
107    pub(crate) unset_rustc_env: Vec<String>,
108    // Environment settings to use during execution
109    pub(crate) exec_env: Vec<(String, String)>,
110    // Environment variables to unset prior to execution.
111    // Variables are unset before applying 'exec_env'
112    pub(crate) unset_exec_env: Vec<String>,
113    // Build documentation for all specified aux-builds as well
114    pub(crate) 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(crate) unique_doc_out_dir: bool,
118    // Flag to force a crate to be built with the host architecture
119    pub(crate) force_host: bool,
120    // Check stdout for error-pattern output as well as stderr
121    pub(crate) check_stdout: bool,
122    // Check stdout & stderr for output of run-pass test
123    pub(crate) check_run_results: bool,
124    // For UI tests, allows compiler to generate arbitrary output to stdout
125    pub(crate) dont_check_compiler_stdout: bool,
126    // For UI tests, allows compiler to generate arbitrary output to stderr
127    pub(crate) 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(crate) no_prefer_dynamic: bool,
134    // Which pretty mode are we testing with, default to 'normal'
135    pub(crate) pretty_mode: String,
136    // Only compare pretty output and don't try compiling
137    pub(crate) pretty_compare_only: bool,
138    /// Strings that must not appear in compile/run output.
139    pub(crate) forbid_output: Vec<String>,
140    // Revisions to test for incremental compilation.
141    pub(crate) 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(crate) 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(crate) 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(crate) 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(crate) fail_mode: Option<FailMode>,
174    // rustdoc will test the output of the `--test` option
175    pub(crate) check_test_line_numbers_match: bool,
176    // customized normalization rules
177    pub(crate) normalize_stdout: Vec<(String, String)>,
178    pub(crate) normalize_stderr: Vec<(String, String)>,
179    pub(crate) failure_status: Option<i32>,
180    // For UI tests, allows compiler to exit with arbitrary failure status
181    pub(crate) 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(crate) run_rustfix: bool,
185    // If true, `rustfix` will only apply `MachineApplicable` suggestions.
186    pub(crate) rustfix_only_machine_applicable: bool,
187    pub(crate) assembly_output: Option<String>,
188    // If true, the stderr is expected to be different across bit-widths.
189    pub(crate) stderr_per_bitwidth: bool,
190    // The MIR opt to unit test, if any
191    pub(crate) mir_unit_test: Option<String>,
192    // Whether to tell `rustc` to remap the "src base" directory to a fake
193    // directory.
194    pub(crate) remap_src_base: bool,
195    /// Extra flags to pass to `llvm-cov` when producing coverage reports.
196    /// Only used by the "coverage-run" test mode.
197    pub(crate) llvm_cov_flags: Vec<String>,
198    /// Extra flags to pass to LLVM's `filecheck` tool, in tests that use it.
199    pub(crate) filecheck_flags: Vec<String>,
200    /// Don't automatically insert any `--check-cfg` args
201    pub(crate) no_auto_check_cfg: bool,
202    /// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
203    /// that don't otherwise want/need `-Z build-std`.
204    pub(crate) add_minicore: bool,
205    /// Add these flags to the build of `minicore`.
206    pub(crate) minicore_compile_flags: Vec<String>,
207    /// Whether line annotations are required for the given error kind.
208    pub(crate) dont_require_annotations: HashSet<ErrorKind>,
209    /// Whether pretty printers should be disabled in gdb.
210    pub(crate) disable_gdb_pretty_printers: bool,
211    /// Compare the output by lines, rather than as a single string.
212    pub(crate) compare_output_by_lines: bool,
213}
214
215mod directives {
216    pub(crate) const ERROR_PATTERN: &str = "error-pattern";
217    pub(crate) const REGEX_ERROR_PATTERN: &str = "regex-error-pattern";
218    pub(crate) const COMPILE_FLAGS: &str = "compile-flags";
219    pub(crate) const RUN_FLAGS: &str = "run-flags";
220    pub(crate) const DOC_FLAGS: &str = "doc-flags";
221    pub(crate) const BUILD_AUX_DOCS: &str = "build-aux-docs";
222    pub(crate) const UNIQUE_DOC_OUT_DIR: &str = "unique-doc-out-dir";
223    pub(crate) const FORCE_HOST: &str = "force-host";
224    pub(crate) const CHECK_STDOUT: &str = "check-stdout";
225    pub(crate) const CHECK_RUN_RESULTS: &str = "check-run-results";
226    pub(crate) const DONT_CHECK_COMPILER_STDOUT: &str = "dont-check-compiler-stdout";
227    pub(crate) const DONT_CHECK_COMPILER_STDERR: &str = "dont-check-compiler-stderr";
228    pub(crate) const DONT_REQUIRE_ANNOTATIONS: &str = "dont-require-annotations";
229    pub(crate) const NO_PREFER_DYNAMIC: &str = "no-prefer-dynamic";
230    pub(crate) const PRETTY_MODE: &str = "pretty-mode";
231    pub(crate) const PRETTY_COMPARE_ONLY: &str = "pretty-compare-only";
232    pub(crate) const AUX_BIN: &str = "aux-bin";
233    pub(crate) const AUX_BUILD: &str = "aux-build";
234    pub(crate) const AUX_CRATE: &str = "aux-crate";
235    pub(crate) const PROC_MACRO: &str = "proc-macro";
236    pub(crate) const AUX_CODEGEN_BACKEND: &str = "aux-codegen-backend";
237    pub(crate) const EXEC_ENV: &str = "exec-env";
238    pub(crate) const RUSTC_ENV: &str = "rustc-env";
239    pub(crate) const UNSET_EXEC_ENV: &str = "unset-exec-env";
240    pub(crate) const UNSET_RUSTC_ENV: &str = "unset-rustc-env";
241    pub(crate) const FORBID_OUTPUT: &str = "forbid-output";
242    pub(crate) const CHECK_TEST_LINE_NUMBERS_MATCH: &str = "check-test-line-numbers-match";
243    pub(crate) const IGNORE_PASS: &str = "ignore-pass";
244    pub(crate) const FAILURE_STATUS: &str = "failure-status";
245    pub(crate) const DONT_CHECK_FAILURE_STATUS: &str = "dont-check-failure-status";
246    pub(crate) const RUN_RUSTFIX: &str = "run-rustfix";
247    pub(crate) const RUSTFIX_ONLY_MACHINE_APPLICABLE: &str = "rustfix-only-machine-applicable";
248    pub(crate) const ASSEMBLY_OUTPUT: &str = "assembly-output";
249    pub(crate) const STDERR_PER_BITWIDTH: &str = "stderr-per-bitwidth";
250    pub(crate) const INCREMENTAL: &str = "incremental";
251    pub(crate) const KNOWN_BUG: &str = "known-bug";
252    pub(crate) const TEST_MIR_PASS: &str = "test-mir-pass";
253    pub(crate) const REMAP_SRC_BASE: &str = "remap-src-base";
254    pub(crate) const LLVM_COV_FLAGS: &str = "llvm-cov-flags";
255    pub(crate) const FILECHECK_FLAGS: &str = "filecheck-flags";
256    pub(crate) const NO_AUTO_CHECK_CFG: &str = "no-auto-check-cfg";
257    pub(crate) const ADD_MINICORE: &str = "add-minicore";
258    pub(crate) const MINICORE_COMPILE_FLAGS: &str = "minicore-compile-flags";
259    pub(crate) const DISABLE_GDB_PRETTY_PRINTERS: &str = "disable-gdb-pretty-printers";
260    pub(crate) const COMPARE_OUTPUT_BY_LINES: &str = "compare-output-by-lines";
261}
262
263impl TestProps {
264    pub(crate) fn new() -> Self {
265        TestProps {
266            error_patterns: vec![],
267            regex_error_patterns: vec![],
268            edition: None,
269            compile_flags: vec![],
270            run_flags: vec![],
271            doc_flags: vec![],
272            pp_exact: None,
273            aux: Default::default(),
274            revisions: vec![],
275            rustc_env: vec![
276                ("RUSTC_ICE".to_string(), "0".to_string()),
277                ("RUST_BACKTRACE".to_string(), "short".to_string()),
278            ],
279            unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
280            exec_env: vec![],
281            unset_exec_env: vec![],
282            build_aux_docs: false,
283            unique_doc_out_dir: false,
284            force_host: false,
285            check_stdout: false,
286            check_run_results: false,
287            dont_check_compiler_stdout: false,
288            dont_check_compiler_stderr: false,
289            no_prefer_dynamic: false,
290            pretty_mode: "normal".to_string(),
291            pretty_compare_only: false,
292            forbid_output: vec![],
293            incremental_dir: None,
294            incremental: false,
295            known_bug: false,
296            pass_mode: None,
297            fail_mode: None,
298            ignore_pass: false,
299            check_test_line_numbers_match: false,
300            normalize_stdout: vec![],
301            normalize_stderr: vec![],
302            failure_status: None,
303            dont_check_failure_status: false,
304            run_rustfix: false,
305            rustfix_only_machine_applicable: false,
306            assembly_output: None,
307            stderr_per_bitwidth: false,
308            mir_unit_test: None,
309            remap_src_base: false,
310            llvm_cov_flags: vec![],
311            filecheck_flags: vec![],
312            no_auto_check_cfg: false,
313            add_minicore: false,
314            minicore_compile_flags: vec![],
315            dont_require_annotations: Default::default(),
316            disable_gdb_pretty_printers: false,
317            compare_output_by_lines: false,
318        }
319    }
320
321    pub(crate) fn from_aux_file(
322        &self,
323        testfile: &Utf8Path,
324        revision: Option<&str>,
325        config: &Config,
326    ) -> Self {
327        let mut props = TestProps::new();
328
329        // copy over select properties to the aux build:
330        props.incremental_dir = self.incremental_dir.clone();
331        props.ignore_pass = true;
332        props.load_from(testfile, revision, config);
333
334        props
335    }
336
337    pub(crate) fn from_file(testfile: &Utf8Path, revision: Option<&str>, config: &Config) -> Self {
338        let mut props = TestProps::new();
339        props.load_from(testfile, revision, config);
340        props.exec_env.push(("RUSTC".to_string(), config.rustc_path.to_string()));
341
342        match (props.pass_mode, props.fail_mode) {
343            (None, None) if config.mode == TestMode::Ui => props.fail_mode = Some(FailMode::Check),
344            (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
345            _ => {}
346        }
347
348        props
349    }
350
351    /// Loads properties from `testfile` into `props`. If a property is
352    /// tied to a particular revision `foo` (indicated by writing
353    /// `//@[foo]`), then the property is ignored unless `test_revision` is
354    /// `Some("foo")`.
355    fn load_from(&mut self, testfile: &Utf8Path, test_revision: Option<&str>, config: &Config) {
356        if !testfile.is_dir() {
357            let file_contents = fs::read_to_string(testfile).unwrap();
358            let file_directives = FileDirectives::from_file_contents(testfile, &file_contents);
359
360            iter_directives(
361                config,
362                &file_directives,
363                // (dummy comment to force args into vertical layout)
364                &mut |ln: &DirectiveLine<'_>| {
365                    if !ln.applies_to_test_revision(test_revision) {
366                        return;
367                    }
368
369                    if let Some(handler) = DIRECTIVE_HANDLERS_MAP.get(ln.name) {
370                        handler.handle(config, ln, self);
371                    }
372                },
373            );
374        }
375
376        if config.mode == TestMode::Incremental {
377            self.incremental = true;
378        }
379
380        if config.mode == TestMode::Crashes {
381            // we don't want to pollute anything with backtrace-files
382            // also turn off backtraces in order to save some execution
383            // time on the tests; we only need to know IF it crashes
384            self.rustc_env = vec![
385                ("RUST_BACKTRACE".to_string(), "0".to_string()),
386                ("RUSTC_ICE".to_string(), "0".to_string()),
387            ];
388        }
389
390        for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
391            if let Ok(val) = env::var(key) {
392                if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
393                    self.exec_env.push(((*key).to_owned(), val))
394                }
395            }
396        }
397
398        if let Some(edition) = self.edition.or(config.edition) {
399            // The edition is added at the start, since flags from //@compile-flags must be passed
400            // to rustc last.
401            self.compile_flags.insert(0, format!("--edition={edition}"));
402        }
403    }
404
405    fn update_fail_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
406        let check_ui = |mode: &str| {
407            // Mode::Crashes may need build-fail in order to trigger llvm errors or stack overflows
408            if config.mode != TestMode::Ui && config.mode != TestMode::Crashes {
409                panic!("`{}-fail` directive is only supported in UI tests", mode);
410            }
411        };
412        let fail_mode = if config.parse_name_directive(ln, "check-fail") {
413            check_ui("check");
414            Some(FailMode::Check)
415        } else if config.parse_name_directive(ln, "build-fail") {
416            check_ui("build");
417            Some(FailMode::Build)
418        } else if config.parse_name_directive(ln, "run-fail") {
419            check_ui("run");
420            Some(FailMode::Run(RunFailMode::Fail))
421        } else if config.parse_name_directive(ln, "run-crash") {
422            check_ui("run");
423            Some(FailMode::Run(RunFailMode::Crash))
424        } else if config.parse_name_directive(ln, "run-fail-or-crash") {
425            check_ui("run");
426            Some(FailMode::Run(RunFailMode::FailOrCrash))
427        } else {
428            None
429        };
430        match (self.fail_mode, fail_mode) {
431            (None, Some(_)) => self.fail_mode = fail_mode,
432            (Some(_), Some(_)) => panic!("multiple `*-fail` directives in a single test"),
433            (_, None) => {}
434        }
435    }
436
437    fn update_pass_mode(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
438        let check_no_run = |s| match (config.mode, s) {
439            (TestMode::Ui, _) => (),
440            (TestMode::Crashes, _) => (),
441            (TestMode::Codegen, "build-pass") => (),
442            (TestMode::Incremental, _) => {
443                // FIXME(Zalathar): This only detects forbidden directives that are
444                // declared _after_ the incompatible `//@ revisions:` directive(s).
445                if self.revisions.iter().any(|r| !r.starts_with("bfail")) {
446                    panic!("`{s}` directive is only supported in `bfail` incremental tests")
447                }
448            }
449            (mode, _) => panic!("`{s}` directive is not supported in `{mode}` tests"),
450        };
451        let pass_mode = if config.parse_name_directive(ln, "check-pass") {
452            check_no_run("check-pass");
453            Some(PassMode::Check)
454        } else if config.parse_name_directive(ln, "build-pass") {
455            check_no_run("build-pass");
456            Some(PassMode::Build)
457        } else if config.parse_name_directive(ln, "run-pass") {
458            check_no_run("run-pass");
459            Some(PassMode::Run)
460        } else {
461            None
462        };
463        match (self.pass_mode, pass_mode) {
464            (None, Some(_)) => self.pass_mode = pass_mode,
465            (Some(_), Some(_)) => panic!("multiple `*-pass` directives in a single test"),
466            (_, None) => {}
467        }
468    }
469
470    pub(crate) fn pass_mode(&self, config: &Config) -> Option<PassMode> {
471        if !self.ignore_pass && self.fail_mode.is_none() {
472            if let mode @ Some(_) = config.force_pass_mode {
473                return mode;
474            }
475        }
476        self.pass_mode
477    }
478
479    // does not consider CLI override for pass mode
480    pub(crate) fn local_pass_mode(&self) -> Option<PassMode> {
481        self.pass_mode
482    }
483
484    fn update_add_minicore(&mut self, ln: &DirectiveLine<'_>, config: &Config) {
485        let add_minicore = config.parse_name_directive(ln, directives::ADD_MINICORE);
486        if add_minicore {
487            if !matches!(config.mode, TestMode::Ui | TestMode::Codegen | TestMode::Assembly) {
488                panic!(
489                    "`add-minicore` is currently only supported for ui, codegen and assembly test modes"
490                );
491            }
492
493            // FIXME(jieyouxu): this check is currently order-dependent, but we should probably
494            // collect all directives in one go then perform a validation pass after that.
495            if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
496                // `minicore` can only be used with non-run modes, because it's `core` prelude stubs
497                // and can't run.
498                panic!("`add-minicore` cannot be used to run the test binary");
499            }
500
501            self.add_minicore = add_minicore;
502        }
503    }
504}
505
506pub(crate) fn do_early_directives_check(
507    mode: TestMode,
508    file_directives: &FileDirectives<'_>,
509) -> Result<(), String> {
510    let testfile = file_directives.path;
511
512    for directive_line @ DirectiveLine { line_number, .. } in &file_directives.lines {
513        let CheckDirectiveResult { is_known_directive, trailing_directive } =
514            check_directive(directive_line, mode);
515
516        if !is_known_directive {
517            return Err(format!(
518                "ERROR: unknown compiletest directive `{directive}` at {testfile}:{line_number}",
519                directive = directive_line.display(),
520            ));
521        }
522
523        if let Some(trailing_directive) = &trailing_directive {
524            return Err(format!(
525                "ERROR: detected trailing compiletest directive `{trailing_directive}` at {testfile}:{line_number}\n\
526                HELP: put the directive on its own line: `//@ {trailing_directive}`"
527            ));
528        }
529    }
530
531    Ok(())
532}
533
534pub(crate) struct CheckDirectiveResult<'ln> {
535    is_known_directive: bool,
536    trailing_directive: Option<&'ln str>,
537}
538
539fn check_directive<'a>(
540    directive_ln: &DirectiveLine<'a>,
541    mode: TestMode,
542) -> CheckDirectiveResult<'a> {
543    let &DirectiveLine { name: directive_name, .. } = directive_ln;
544
545    let is_known_directive = KNOWN_DIRECTIVE_NAMES_SET.contains(&directive_name)
546        || match mode {
547            TestMode::RustdocHtml => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
548            TestMode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES.contains(&directive_name),
549            _ => false,
550        };
551
552    // If it looks like the user tried to put two directives on the same line
553    // (e.g. `//@ only-linux only-x86_64`), signal an error, because the
554    // second "directive" would actually be ignored with no effect.
555    let trailing_directive = directive_ln
556        .remark_after_space()
557        .map(|remark| remark.trim_start().split(' ').next().unwrap())
558        .filter(|token| KNOWN_DIRECTIVE_NAMES_SET.contains(token));
559
560    // FIXME(Zalathar): Consider emitting specialized error/help messages for
561    // bogus directive names that are similar to real ones, e.g.:
562    // - *`compiler-flags` => `compile-flags`
563    // - *`compile-fail` => `check-fail` or `build-fail`
564
565    CheckDirectiveResult { is_known_directive, trailing_directive }
566}
567
568fn iter_directives(
569    config: &Config,
570    file_directives: &FileDirectives<'_>,
571    it: &mut dyn FnMut(&DirectiveLine<'_>),
572) {
573    let testfile = file_directives.path;
574
575    let extra_directives = match config.mode {
576        TestMode::CoverageRun => {
577            // Coverage tests in coverage-run mode always have these extra directives, without needing to
578            // specify them manually in every test file.
579            //
580            // FIXME(jieyouxu): I feel like there's a better way to do this, leaving for later.
581            vec![
582                "//@ needs-profiler-runtime",
583                // FIXME(pietroalbini): this test currently does not work on cross-compiled targets
584                // because remote-test is not capable of sending back the *.profraw files generated by
585                // the LLVM instrumentation.
586                "//@ ignore-cross-compile",
587            ]
588        }
589        TestMode::Codegen if !file_directives.has_explicit_no_std_core_attribute => {
590            // Note: affects all codegen test suites under test mode `codegen`, e.g. `codegen-llvm`.
591            //
592            // Codegen tests automatically receive implied `//@ needs-target-std`, unless
593            // `#![no_std]`/`#![no_core]` attribute was explicitly seen. The rationale is basically to avoid
594            // having to manually maintain a bunch of `//@ needs-target-std` directives esp. for targets
595            // tested/built out-of-tree.
596            vec!["//@ needs-target-std"]
597        }
598        TestMode::Ui if config.parallel_frontend_enabled() => {
599            // UI tests in parallel-frontend mode always have this extra directive, without needing to
600            // specify it manually in every test file.
601            vec!["//@ compare-output-by-lines"]
602        }
603
604        _ => {
605            // No extra directives for other test modes.
606            vec![]
607        }
608    };
609
610    for directive_str in extra_directives {
611        let directive_line = line_directive(testfile, LineNumber::ZERO, directive_str)
612            .unwrap_or_else(|| panic!("bad extra-directive line: {directive_str:?}"));
613        it(&directive_line);
614    }
615
616    for directive_line in &file_directives.lines {
617        it(directive_line);
618    }
619}
620
621impl Config {
622    fn parse_and_update_revisions(&self, line: &DirectiveLine<'_>, existing: &mut Vec<String>) {
623        const FORBIDDEN_REVISION_NAMES: [&str; 2] = [
624            // `//@ 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(crate) 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(crate) fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
891    let output = Command::new(binary_path).arg("--version").output().ok()?;
892    if !output.status.success() {
893        return None;
894    }
895    let version = String::from_utf8(output.stdout).ok()?;
896    for line in version.lines() {
897        if let Some(version) = line.split("LLVM version ").nth(1) {
898            return Some(extract_llvm_version(version));
899        }
900    }
901    None
902}
903
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(config, file_directives, &mut |ln @ &DirectiveLine { line_number, .. }| {
955        if !ln.applies_to_test_revision(test_revision) {
956            return;
957        }
958
959        // Parse `aux-*` directives, for use by up-to-date checks.
960        parse_and_update_aux(config, ln, aux_props);
961
962        macro_rules! decision {
963            ($e:expr) => {
964                match $e {
965                    IgnoreDecision::Ignore { reason } => {
966                        ignore = true;
967                        ignore_message = Some(reason.into());
968                    }
969                    IgnoreDecision::Error { message } => {
970                        error!("{path}:{line_number}: {message}");
971                        *poisoned = true;
972                        return;
973                    }
974                    IgnoreDecision::Continue => {}
975                }
976            };
977        }
978
979        decision!(cfg::handle_ignore(&cache.cfg_conditions, ln));
980        decision!(cfg::handle_only(&cache.cfg_conditions, ln));
981        decision!(needs::handle_needs(&cache.needs, config, ln));
982        decision!(ignore_llvm(config, ln));
983        decision!(ignore_backends(config, ln));
984        decision!(needs_backends(config, ln));
985        decision!(ignore_cdb(config, ln));
986        decision!(ignore_gdb(config, ln));
987        decision!(ignore_lldb(config, ln));
988        decision!(ignore_parallel_frontend(config, ln));
989
990        if config.target == "wasm32-unknown-unknown"
991            && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
992        {
993            decision!(IgnoreDecision::Ignore {
994                reason: "ignored on WASM as the run results cannot be checked there".into(),
995            });
996        }
997
998        should_fail |= config.parse_name_directive(ln, "should-fail");
999    });
1000
1001    // The `should-fail` annotation doesn't apply to pretty tests,
1002    // since we run the pretty printer across all tests by default.
1003    // If desired, we could add a `should-fail-pretty` annotation.
1004    let should_fail = if should_fail && config.mode != TestMode::Pretty {
1005        ShouldFail::Yes
1006    } else {
1007        ShouldFail::No
1008    };
1009
1010    CollectedTestDesc {
1011        name,
1012        filterable_path: filterable_path.to_owned(),
1013        ignore,
1014        ignore_message,
1015        should_fail,
1016    }
1017}
1018
1019fn ignore_cdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1020    if config.debugger != Some(Debugger::Cdb) {
1021        return IgnoreDecision::Continue;
1022    }
1023
1024    if let Some(actual_version) = config.cdb_version {
1025        if line.name == "min-cdb-version"
1026            && let Some(rest) = line.value_after_colon().map(str::trim)
1027        {
1028            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1029                panic!("couldn't parse version range: {:?}", rest);
1030            });
1031
1032            // Ignore if actual version is smaller than the minimum
1033            // required version
1034            if actual_version < min_version {
1035                return IgnoreDecision::Ignore {
1036                    reason: format!("ignored when the CDB version is lower than {rest}"),
1037                };
1038            }
1039        }
1040    }
1041    IgnoreDecision::Continue
1042}
1043
1044fn ignore_gdb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1045    if config.debugger != Some(Debugger::Gdb) {
1046        return IgnoreDecision::Continue;
1047    }
1048
1049    if let Some(actual_version) = config.gdb_version {
1050        if line.name == "min-gdb-version"
1051            && let Some(rest) = line.value_after_colon().map(str::trim)
1052        {
1053            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1054                .unwrap_or_else(|| {
1055                    panic!("couldn't parse version range: {:?}", rest);
1056                });
1057
1058            if start_ver != end_ver {
1059                panic!("Expected single GDB version")
1060            }
1061            // Ignore if actual version is smaller than the minimum
1062            // required version
1063            if actual_version < start_ver {
1064                return IgnoreDecision::Ignore {
1065                    reason: format!("ignored when the GDB version is lower than {rest}"),
1066                };
1067            }
1068        } else if line.name == "ignore-gdb-version"
1069            && let Some(rest) = line.value_after_colon().map(str::trim)
1070        {
1071            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1072                .unwrap_or_else(|| {
1073                    panic!("couldn't parse version range: {:?}", rest);
1074                });
1075
1076            if max_version < min_version {
1077                panic!("Malformed GDB version range: max < min")
1078            }
1079
1080            if actual_version >= min_version && actual_version <= max_version {
1081                if min_version == max_version {
1082                    return IgnoreDecision::Ignore {
1083                        reason: format!("ignored when the GDB version is {rest}"),
1084                    };
1085                } else {
1086                    return IgnoreDecision::Ignore {
1087                        reason: format!("ignored when the GDB version is between {rest}"),
1088                    };
1089                }
1090            }
1091        }
1092    }
1093    IgnoreDecision::Continue
1094}
1095
1096fn ignore_lldb(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1097    if config.debugger != Some(Debugger::Lldb) {
1098        return IgnoreDecision::Continue;
1099    }
1100
1101    if let Some(actual_version) = config.lldb_version {
1102        if line.name == "min-lldb-version"
1103            && let Some(rest) = line.value_after_colon().map(str::trim)
1104        {
1105            let min_version = rest.parse().unwrap_or_else(|e| {
1106                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1107            });
1108            // Ignore if actual version is smaller the minimum required
1109            // version
1110            if actual_version < min_version {
1111                return IgnoreDecision::Ignore {
1112                    reason: format!("ignored when the LLDB version is {rest}"),
1113                };
1114            }
1115        }
1116    }
1117    IgnoreDecision::Continue
1118}
1119
1120fn ignore_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1121    let path = line.file_path;
1122    if let Some(backends_to_ignore) = config.parse_name_value_directive(line, "ignore-backends") {
1123        for backend in backends_to_ignore.split_whitespace().map(|backend| {
1124            match CodegenBackend::try_from(backend) {
1125                Ok(backend) => backend,
1126                Err(error) => {
1127                    panic!("Invalid ignore-backends value `{backend}` in `{path}`: {error}")
1128                }
1129            }
1130        }) {
1131            if !config.bypass_ignore_backends && config.default_codegen_backend == backend {
1132                return IgnoreDecision::Ignore {
1133                    reason: format!("{} backend is marked as ignore", backend.as_str()),
1134                };
1135            }
1136        }
1137    }
1138    IgnoreDecision::Continue
1139}
1140
1141fn needs_backends(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1142    let path = line.file_path;
1143    if let Some(needed_backends) = config.parse_name_value_directive(line, "needs-backends") {
1144        if !needed_backends
1145            .split_whitespace()
1146            .map(|backend| match CodegenBackend::try_from(backend) {
1147                Ok(backend) => backend,
1148                Err(error) => {
1149                    panic!("Invalid needs-backends value `{backend}` in `{path}`: {error}")
1150                }
1151            })
1152            .any(|backend| config.default_codegen_backend == backend)
1153        {
1154            return IgnoreDecision::Ignore {
1155                reason: format!(
1156                    "{} backend is not part of required backends",
1157                    config.default_codegen_backend.as_str()
1158                ),
1159            };
1160        }
1161    }
1162    IgnoreDecision::Continue
1163}
1164
1165fn ignore_llvm(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1166    let path = line.file_path;
1167    if let Some(needed_components) =
1168        config.parse_name_value_directive(line, "needs-llvm-components")
1169    {
1170        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1171        if let Some(missing_component) = needed_components
1172            .split_whitespace()
1173            .find(|needed_component| !components.contains(needed_component))
1174        {
1175            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1176                panic!(
1177                    "missing LLVM component {missing_component}, \
1178                    and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {path}",
1179                );
1180            }
1181            return IgnoreDecision::Ignore {
1182                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1183            };
1184        }
1185    }
1186    if let Some(actual_version) = &config.llvm_version {
1187        // Note that these `min` versions will check for not just major versions.
1188
1189        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1190            let min_version = extract_llvm_version(&version_string);
1191            // Ignore if actual version is smaller than the minimum required version.
1192            if *actual_version < min_version {
1193                return IgnoreDecision::Ignore {
1194                    reason: format!(
1195                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1196                    ),
1197                };
1198            }
1199        } else if let Some(version_string) =
1200            config.parse_name_value_directive(line, "max-llvm-major-version")
1201        {
1202            let max_version = extract_llvm_version(&version_string);
1203            // Ignore if actual major version is larger than the maximum required major version.
1204            if actual_version.major > max_version.major {
1205                return IgnoreDecision::Ignore {
1206                    reason: format!(
1207                        "ignored when the LLVM version ({actual_version}) is newer than major\
1208                        version {}",
1209                        max_version.major
1210                    ),
1211                };
1212            }
1213        } else if let Some(version_string) =
1214            config.parse_name_value_directive(line, "min-system-llvm-version")
1215        {
1216            let min_version = extract_llvm_version(&version_string);
1217            // Ignore if using system LLVM and actual version
1218            // is smaller the minimum required version
1219            if config.system_llvm && *actual_version < min_version {
1220                return IgnoreDecision::Ignore {
1221                    reason: format!(
1222                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1223                    ),
1224                };
1225            }
1226        } else if let Some(version_range) =
1227            config.parse_name_value_directive(line, "ignore-llvm-version")
1228        {
1229            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1230            let (v_min, v_max) =
1231                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1232                    .unwrap_or_else(|| {
1233                        panic!("couldn't parse version range: \"{version_range}\"");
1234                    });
1235            if v_max < v_min {
1236                panic!("malformed LLVM version range where {v_max} < {v_min}")
1237            }
1238            // Ignore if version lies inside of range.
1239            if *actual_version >= v_min && *actual_version <= v_max {
1240                if v_min == v_max {
1241                    return IgnoreDecision::Ignore {
1242                        reason: format!("ignored when the LLVM version is {actual_version}"),
1243                    };
1244                } else {
1245                    return IgnoreDecision::Ignore {
1246                        reason: format!(
1247                            "ignored when the LLVM version is between {v_min} and {v_max}"
1248                        ),
1249                    };
1250                }
1251            }
1252        } else if let Some(version_string) =
1253            config.parse_name_value_directive(line, "exact-llvm-major-version")
1254        {
1255            // Syntax is "exact-llvm-major-version: <version>"
1256            let version = extract_llvm_version(&version_string);
1257            if actual_version.major != version.major {
1258                return IgnoreDecision::Ignore {
1259                    reason: format!(
1260                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1261                        actual_version.major, version.major
1262                    ),
1263                };
1264            }
1265        }
1266    }
1267    IgnoreDecision::Continue
1268}
1269
1270fn ignore_parallel_frontend(config: &Config, line: &DirectiveLine<'_>) -> IgnoreDecision {
1271    if config.parallel_frontend_enabled()
1272        && config.parse_name_directive(line, "ignore-parallel-frontend")
1273    {
1274        return IgnoreDecision::Ignore {
1275            reason: "ignored when the parallel frontend is enabled".into(),
1276        };
1277    }
1278    IgnoreDecision::Continue
1279}
1280
1281enum IgnoreDecision {
1282    Ignore { reason: String },
1283    Continue,
1284    Error { message: String },
1285}
1286
1287fn parse_edition_range(config: &Config, line: &DirectiveLine<'_>) -> Option<EditionRange> {
1288    let raw = config.parse_name_value_directive(line, "edition")?;
1289    let &DirectiveLine { file_path: testfile, line_number, .. } = line;
1290
1291    // Edition range is half-open: `[lower_bound, upper_bound)`
1292    if let Some((lower_bound, upper_bound)) = raw.split_once("..") {
1293        Some(match (maybe_parse_edition(lower_bound), maybe_parse_edition(upper_bound)) {
1294            (Some(lower_bound), Some(upper_bound)) if upper_bound <= lower_bound => {
1295                fatal!(
1296                    "{testfile}:{line_number}: the left side of `//@ edition` cannot be greater than or equal to the right side"
1297                );
1298            }
1299            (Some(lower_bound), Some(upper_bound)) => {
1300                EditionRange::Range { lower_bound, upper_bound }
1301            }
1302            (Some(lower_bound), None) => EditionRange::RangeFrom(lower_bound),
1303            (None, Some(_)) => {
1304                fatal!(
1305                    "{testfile}:{line_number}: `..edition` is not a supported range in `//@ edition`"
1306                );
1307            }
1308            (None, None) => {
1309                fatal!("{testfile}:{line_number}: `..` is not a supported range in `//@ edition`");
1310            }
1311        })
1312    } else {
1313        match maybe_parse_edition(&raw) {
1314            Some(edition) => Some(EditionRange::Exact(edition)),
1315            None => {
1316                fatal!("{testfile}:{line_number}: empty value for `//@ edition`");
1317            }
1318        }
1319    }
1320}
1321
1322fn maybe_parse_edition(mut input: &str) -> Option<Edition> {
1323    input = input.trim();
1324    if input.is_empty() {
1325        return None;
1326    }
1327    Some(parse_edition(input))
1328}
1329
1330#[derive(Debug, PartialEq, Eq, Clone, Copy)]
1331enum EditionRange {
1332    Exact(Edition),
1333    RangeFrom(Edition),
1334    /// Half-open range: `[lower_bound, upper_bound)`
1335    Range {
1336        lower_bound: Edition,
1337        upper_bound: Edition,
1338    },
1339}
1340
1341impl EditionRange {
1342    fn edition_to_test(&self, requested: impl Into<Option<Edition>>) -> Edition {
1343        let min_edition = Edition::Year(2015);
1344        let requested = requested.into().unwrap_or(min_edition);
1345
1346        match *self {
1347            EditionRange::Exact(exact) => exact,
1348            EditionRange::RangeFrom(lower_bound) => {
1349                if requested >= lower_bound {
1350                    requested
1351                } else {
1352                    lower_bound
1353                }
1354            }
1355            EditionRange::Range { lower_bound, upper_bound } => {
1356                if requested >= lower_bound && requested < upper_bound {
1357                    requested
1358                } else {
1359                    lower_bound
1360                }
1361            }
1362        }
1363    }
1364}
1365
1366fn split_flags(flags: &str) -> Vec<String> {
1367    // Individual flags can be single-quoted to preserve spaces; see
1368    // <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
1369    // FIXME(#147955): Replace this ad-hoc quoting with an escape/quote system that
1370    // is closer to what actual shells do, so that it's more flexible and familiar.
1371    flags
1372        .split('\'')
1373        .enumerate()
1374        .flat_map(|(i, f)| if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() })
1375        .map(move |s| s.to_owned())
1376        .collect::<Vec<_>>()
1377}