compiletest/
header.rs

1use std::collections::HashSet;
2use std::env;
3use std::fs::File;
4use std::io::BufReader;
5use std::io::prelude::*;
6use std::path::{Path, PathBuf};
7use std::process::Command;
8
9use semver::Version;
10use tracing::*;
11
12use crate::common::{Config, Debugger, FailMode, Mode, PassMode};
13use crate::debuggers::{extract_cdb_version, extract_gdb_version};
14use crate::header::auxiliary::{AuxProps, parse_and_update_aux};
15use crate::header::needs::CachedNeedsConditions;
16use crate::util::static_regex;
17
18pub(crate) mod auxiliary;
19mod cfg;
20mod needs;
21#[cfg(test)]
22mod tests;
23
24pub struct HeadersCache {
25    needs: CachedNeedsConditions,
26}
27
28impl HeadersCache {
29    pub fn load(config: &Config) -> Self {
30        Self { needs: CachedNeedsConditions::load(config) }
31    }
32}
33
34/// Properties which must be known very early, before actually running
35/// the test.
36#[derive(Default)]
37pub struct EarlyProps {
38    /// Auxiliary crates that should be built and made available to this test.
39    /// Included in [`EarlyProps`] so that the indicated files can participate
40    /// in up-to-date checking. Building happens via [`TestProps::aux`] instead.
41    pub(crate) aux: AuxProps,
42    pub revisions: Vec<String>,
43}
44
45impl EarlyProps {
46    pub fn from_file(config: &Config, testfile: &Path) -> Self {
47        let file = File::open(testfile).expect("open test file to parse earlyprops");
48        Self::from_reader(config, testfile, file)
49    }
50
51    pub fn from_reader<R: Read>(config: &Config, testfile: &Path, rdr: R) -> Self {
52        let mut props = EarlyProps::default();
53        let mut poisoned = false;
54        iter_header(
55            config.mode,
56            &config.suite,
57            &mut poisoned,
58            testfile,
59            rdr,
60            &mut |DirectiveLine { raw_directive: ln, .. }| {
61                parse_and_update_aux(config, ln, &mut props.aux);
62                config.parse_and_update_revisions(testfile, ln, &mut props.revisions);
63            },
64        );
65
66        if poisoned {
67            eprintln!("errors encountered during EarlyProps parsing: {}", testfile.display());
68            panic!("errors encountered during EarlyProps parsing");
69        }
70
71        props
72    }
73}
74
75#[derive(Clone, Debug)]
76pub struct TestProps {
77    // Lines that should be expected, in order, on standard out
78    pub error_patterns: Vec<String>,
79    // Regexes that should be expected, in order, on standard out
80    pub regex_error_patterns: Vec<String>,
81    // Extra flags to pass to the compiler
82    pub compile_flags: Vec<String>,
83    // Extra flags to pass when the compiled code is run (such as --bench)
84    pub run_flags: Vec<String>,
85    /// Extra flags to pass to rustdoc but not the compiler.
86    pub doc_flags: Vec<String>,
87    // If present, the name of a file that this test should match when
88    // pretty-printed
89    pub pp_exact: Option<PathBuf>,
90    /// Auxiliary crates that should be built and made available to this test.
91    pub(crate) aux: AuxProps,
92    // Environment settings to use for compiling
93    pub rustc_env: Vec<(String, String)>,
94    // Environment variables to unset prior to compiling.
95    // Variables are unset before applying 'rustc_env'.
96    pub unset_rustc_env: Vec<String>,
97    // Environment settings to use during execution
98    pub exec_env: Vec<(String, String)>,
99    // Environment variables to unset prior to execution.
100    // Variables are unset before applying 'exec_env'
101    pub unset_exec_env: Vec<String>,
102    // Build documentation for all specified aux-builds as well
103    pub build_aux_docs: bool,
104    /// Build the documentation for each crate in a unique output directory.
105    /// Uses `<root output directory>/docs/<test name>/doc`.
106    pub unique_doc_out_dir: bool,
107    // Flag to force a crate to be built with the host architecture
108    pub force_host: bool,
109    // Check stdout for error-pattern output as well as stderr
110    pub check_stdout: bool,
111    // Check stdout & stderr for output of run-pass test
112    pub check_run_results: bool,
113    // For UI tests, allows compiler to generate arbitrary output to stdout
114    pub dont_check_compiler_stdout: bool,
115    // For UI tests, allows compiler to generate arbitrary output to stderr
116    pub dont_check_compiler_stderr: bool,
117    // Don't force a --crate-type=dylib flag on the command line
118    //
119    // Set this for example if you have an auxiliary test file that contains
120    // a proc-macro and needs `#![crate_type = "proc-macro"]`. This ensures
121    // that the aux file is compiled as a `proc-macro` and not as a `dylib`.
122    pub no_prefer_dynamic: bool,
123    // Which pretty mode are we testing with, default to 'normal'
124    pub pretty_mode: String,
125    // Only compare pretty output and don't try compiling
126    pub pretty_compare_only: bool,
127    // Patterns which must not appear in the output of a cfail test.
128    pub forbid_output: Vec<String>,
129    // Revisions to test for incremental compilation.
130    pub revisions: Vec<String>,
131    // Directory (if any) to use for incremental compilation.  This is
132    // not set by end-users; rather it is set by the incremental
133    // testing harness and used when generating compilation
134    // arguments. (In particular, it propagates to the aux-builds.)
135    pub incremental_dir: Option<PathBuf>,
136    // If `true`, this test will use incremental compilation.
137    //
138    // This can be set manually with the `incremental` header, or implicitly
139    // by being a part of an incremental mode test. Using the `incremental`
140    // header should be avoided if possible; using an incremental mode test is
141    // preferred. Incremental mode tests support multiple passes, which can
142    // verify that the incremental cache can be loaded properly after being
143    // created. Just setting the header will only verify the behavior with
144    // creating an incremental cache, but doesn't check that it is created
145    // correctly.
146    //
147    // Compiletest will create the incremental directory, and ensure it is
148    // empty before the test starts. Incremental mode tests will reuse the
149    // incremental directory between passes in the same test.
150    pub incremental: bool,
151    // If `true`, this test is a known bug.
152    //
153    // When set, some requirements are relaxed. Currently, this only means no
154    // error annotations are needed, but this may be updated in the future to
155    // include other relaxations.
156    pub known_bug: bool,
157    // How far should the test proceed while still passing.
158    pass_mode: Option<PassMode>,
159    // Ignore `--pass` overrides from the command line for this test.
160    ignore_pass: bool,
161    // How far this test should proceed to start failing.
162    pub fail_mode: Option<FailMode>,
163    // rustdoc will test the output of the `--test` option
164    pub check_test_line_numbers_match: bool,
165    // customized normalization rules
166    pub normalize_stdout: Vec<(String, String)>,
167    pub normalize_stderr: Vec<(String, String)>,
168    pub failure_status: Option<i32>,
169    // For UI tests, allows compiler to exit with arbitrary failure status
170    pub dont_check_failure_status: bool,
171    // Whether or not `rustfix` should apply the `CodeSuggestion`s of this test and compile the
172    // resulting Rust code.
173    pub run_rustfix: bool,
174    // If true, `rustfix` will only apply `MachineApplicable` suggestions.
175    pub rustfix_only_machine_applicable: bool,
176    pub assembly_output: Option<String>,
177    // If true, the test is expected to ICE
178    pub should_ice: bool,
179    // If true, the stderr is expected to be different across bit-widths.
180    pub stderr_per_bitwidth: bool,
181    // The MIR opt to unit test, if any
182    pub mir_unit_test: Option<String>,
183    // Whether to tell `rustc` to remap the "src base" directory to a fake
184    // directory.
185    pub remap_src_base: bool,
186    /// Extra flags to pass to `llvm-cov` when producing coverage reports.
187    /// Only used by the "coverage-run" test mode.
188    pub llvm_cov_flags: Vec<String>,
189    /// Extra flags to pass to LLVM's `filecheck` tool, in tests that use it.
190    pub filecheck_flags: Vec<String>,
191    /// Don't automatically insert any `--check-cfg` args
192    pub no_auto_check_cfg: bool,
193    /// Run tests which require enzyme being build
194    pub has_enzyme: bool,
195    /// Build and use `minicore` as `core` stub for `no_core` tests in cross-compilation scenarios
196    /// that don't otherwise want/need `-Z build-std`.
197    pub add_core_stubs: bool,
198}
199
200mod directives {
201    pub const ERROR_PATTERN: &'static str = "error-pattern";
202    pub const REGEX_ERROR_PATTERN: &'static str = "regex-error-pattern";
203    pub const COMPILE_FLAGS: &'static str = "compile-flags";
204    pub const RUN_FLAGS: &'static str = "run-flags";
205    pub const DOC_FLAGS: &'static str = "doc-flags";
206    pub const SHOULD_ICE: &'static str = "should-ice";
207    pub const BUILD_AUX_DOCS: &'static str = "build-aux-docs";
208    pub const UNIQUE_DOC_OUT_DIR: &'static str = "unique-doc-out-dir";
209    pub const FORCE_HOST: &'static str = "force-host";
210    pub const CHECK_STDOUT: &'static str = "check-stdout";
211    pub const CHECK_RUN_RESULTS: &'static str = "check-run-results";
212    pub const DONT_CHECK_COMPILER_STDOUT: &'static str = "dont-check-compiler-stdout";
213    pub const DONT_CHECK_COMPILER_STDERR: &'static str = "dont-check-compiler-stderr";
214    pub const NO_PREFER_DYNAMIC: &'static str = "no-prefer-dynamic";
215    pub const PRETTY_MODE: &'static str = "pretty-mode";
216    pub const PRETTY_COMPARE_ONLY: &'static str = "pretty-compare-only";
217    pub const AUX_BIN: &'static str = "aux-bin";
218    pub const AUX_BUILD: &'static str = "aux-build";
219    pub const AUX_CRATE: &'static str = "aux-crate";
220    pub const PROC_MACRO: &'static str = "proc-macro";
221    pub const AUX_CODEGEN_BACKEND: &'static str = "aux-codegen-backend";
222    pub const EXEC_ENV: &'static str = "exec-env";
223    pub const RUSTC_ENV: &'static str = "rustc-env";
224    pub const UNSET_EXEC_ENV: &'static str = "unset-exec-env";
225    pub const UNSET_RUSTC_ENV: &'static str = "unset-rustc-env";
226    pub const FORBID_OUTPUT: &'static str = "forbid-output";
227    pub const CHECK_TEST_LINE_NUMBERS_MATCH: &'static str = "check-test-line-numbers-match";
228    pub const IGNORE_PASS: &'static str = "ignore-pass";
229    pub const FAILURE_STATUS: &'static str = "failure-status";
230    pub const DONT_CHECK_FAILURE_STATUS: &'static str = "dont-check-failure-status";
231    pub const RUN_RUSTFIX: &'static str = "run-rustfix";
232    pub const RUSTFIX_ONLY_MACHINE_APPLICABLE: &'static str = "rustfix-only-machine-applicable";
233    pub const ASSEMBLY_OUTPUT: &'static str = "assembly-output";
234    pub const STDERR_PER_BITWIDTH: &'static str = "stderr-per-bitwidth";
235    pub const INCREMENTAL: &'static str = "incremental";
236    pub const KNOWN_BUG: &'static str = "known-bug";
237    pub const TEST_MIR_PASS: &'static str = "test-mir-pass";
238    pub const REMAP_SRC_BASE: &'static str = "remap-src-base";
239    pub const LLVM_COV_FLAGS: &'static str = "llvm-cov-flags";
240    pub const FILECHECK_FLAGS: &'static str = "filecheck-flags";
241    pub const NO_AUTO_CHECK_CFG: &'static str = "no-auto-check-cfg";
242    pub const ADD_CORE_STUBS: &'static str = "add-core-stubs";
243    // This isn't a real directive, just one that is probably mistyped often
244    pub const INCORRECT_COMPILER_FLAGS: &'static str = "compiler-flags";
245}
246
247impl TestProps {
248    pub fn new() -> Self {
249        TestProps {
250            error_patterns: vec![],
251            regex_error_patterns: vec![],
252            compile_flags: vec![],
253            run_flags: vec![],
254            doc_flags: vec![],
255            pp_exact: None,
256            aux: Default::default(),
257            revisions: vec![],
258            rustc_env: vec![
259                ("RUSTC_ICE".to_string(), "0".to_string()),
260                ("RUST_BACKTRACE".to_string(), "short".to_string()),
261            ],
262            unset_rustc_env: vec![("RUSTC_LOG_COLOR".to_string())],
263            exec_env: vec![],
264            unset_exec_env: vec![],
265            build_aux_docs: false,
266            unique_doc_out_dir: false,
267            force_host: false,
268            check_stdout: false,
269            check_run_results: false,
270            dont_check_compiler_stdout: false,
271            dont_check_compiler_stderr: false,
272            no_prefer_dynamic: false,
273            pretty_mode: "normal".to_string(),
274            pretty_compare_only: false,
275            forbid_output: vec![],
276            incremental_dir: None,
277            incremental: false,
278            known_bug: false,
279            pass_mode: None,
280            fail_mode: None,
281            ignore_pass: false,
282            check_test_line_numbers_match: false,
283            normalize_stdout: vec![],
284            normalize_stderr: vec![],
285            failure_status: None,
286            dont_check_failure_status: false,
287            run_rustfix: false,
288            rustfix_only_machine_applicable: false,
289            assembly_output: None,
290            should_ice: false,
291            stderr_per_bitwidth: false,
292            mir_unit_test: None,
293            remap_src_base: false,
294            llvm_cov_flags: vec![],
295            filecheck_flags: vec![],
296            no_auto_check_cfg: false,
297            has_enzyme: false,
298            add_core_stubs: false,
299        }
300    }
301
302    pub fn from_aux_file(&self, testfile: &Path, revision: Option<&str>, config: &Config) -> Self {
303        let mut props = TestProps::new();
304
305        // copy over select properties to the aux build:
306        props.incremental_dir = self.incremental_dir.clone();
307        props.ignore_pass = true;
308        props.load_from(testfile, revision, config);
309
310        props
311    }
312
313    pub fn from_file(testfile: &Path, revision: Option<&str>, config: &Config) -> Self {
314        let mut props = TestProps::new();
315        props.load_from(testfile, revision, config);
316        props.exec_env.push(("RUSTC".to_string(), config.rustc_path.display().to_string()));
317
318        match (props.pass_mode, props.fail_mode) {
319            (None, None) if config.mode == Mode::Ui => props.fail_mode = Some(FailMode::Check),
320            (Some(_), Some(_)) => panic!("cannot use a *-fail and *-pass mode together"),
321            _ => {}
322        }
323
324        props
325    }
326
327    /// Loads properties from `testfile` into `props`. If a property is
328    /// tied to a particular revision `foo` (indicated by writing
329    /// `//@[foo]`), then the property is ignored unless `test_revision` is
330    /// `Some("foo")`.
331    fn load_from(&mut self, testfile: &Path, test_revision: Option<&str>, config: &Config) {
332        let mut has_edition = false;
333        if !testfile.is_dir() {
334            let file = File::open(testfile).unwrap();
335
336            let mut poisoned = false;
337
338            iter_header(
339                config.mode,
340                &config.suite,
341                &mut poisoned,
342                testfile,
343                file,
344                &mut |directive @ DirectiveLine { raw_directive: ln, .. }| {
345                    if !directive.applies_to_test_revision(test_revision) {
346                        return;
347                    }
348
349                    use directives::*;
350
351                    config.push_name_value_directive(
352                        ln,
353                        ERROR_PATTERN,
354                        &mut self.error_patterns,
355                        |r| r,
356                    );
357                    config.push_name_value_directive(
358                        ln,
359                        REGEX_ERROR_PATTERN,
360                        &mut self.regex_error_patterns,
361                        |r| r,
362                    );
363
364                    config.push_name_value_directive(ln, DOC_FLAGS, &mut self.doc_flags, |r| r);
365
366                    fn split_flags(flags: &str) -> Vec<String> {
367                        // Individual flags can be single-quoted to preserve spaces; see
368                        // <https://github.com/rust-lang/rust/pull/115948/commits/957c5db6>.
369                        flags
370                            .split('\'')
371                            .enumerate()
372                            .flat_map(|(i, f)| {
373                                if i % 2 == 1 { vec![f] } else { f.split_whitespace().collect() }
374                            })
375                            .map(move |s| s.to_owned())
376                            .collect::<Vec<_>>()
377                    }
378
379                    if let Some(flags) = config.parse_name_value_directive(ln, COMPILE_FLAGS) {
380                        self.compile_flags.extend(split_flags(&flags));
381                    }
382                    if config.parse_name_value_directive(ln, INCORRECT_COMPILER_FLAGS).is_some() {
383                        panic!("`compiler-flags` directive should be spelled `compile-flags`");
384                    }
385
386                    if let Some(edition) = config.parse_edition(ln) {
387                        self.compile_flags.push(format!("--edition={}", edition.trim()));
388                        has_edition = true;
389                    }
390
391                    config.parse_and_update_revisions(testfile, ln, &mut self.revisions);
392
393                    if let Some(flags) = config.parse_name_value_directive(ln, RUN_FLAGS) {
394                        self.run_flags.extend(split_flags(&flags));
395                    }
396
397                    if self.pp_exact.is_none() {
398                        self.pp_exact = config.parse_pp_exact(ln, testfile);
399                    }
400
401                    config.set_name_directive(ln, SHOULD_ICE, &mut self.should_ice);
402                    config.set_name_directive(ln, BUILD_AUX_DOCS, &mut self.build_aux_docs);
403                    config.set_name_directive(ln, UNIQUE_DOC_OUT_DIR, &mut self.unique_doc_out_dir);
404
405                    config.set_name_directive(ln, FORCE_HOST, &mut self.force_host);
406                    config.set_name_directive(ln, CHECK_STDOUT, &mut self.check_stdout);
407                    config.set_name_directive(ln, CHECK_RUN_RESULTS, &mut self.check_run_results);
408                    config.set_name_directive(
409                        ln,
410                        DONT_CHECK_COMPILER_STDOUT,
411                        &mut self.dont_check_compiler_stdout,
412                    );
413                    config.set_name_directive(
414                        ln,
415                        DONT_CHECK_COMPILER_STDERR,
416                        &mut self.dont_check_compiler_stderr,
417                    );
418                    config.set_name_directive(ln, NO_PREFER_DYNAMIC, &mut self.no_prefer_dynamic);
419
420                    if let Some(m) = config.parse_name_value_directive(ln, PRETTY_MODE) {
421                        self.pretty_mode = m;
422                    }
423
424                    config.set_name_directive(
425                        ln,
426                        PRETTY_COMPARE_ONLY,
427                        &mut self.pretty_compare_only,
428                    );
429
430                    // Call a helper method to deal with aux-related directives.
431                    parse_and_update_aux(config, ln, &mut self.aux);
432
433                    config.push_name_value_directive(
434                        ln,
435                        EXEC_ENV,
436                        &mut self.exec_env,
437                        Config::parse_env,
438                    );
439                    config.push_name_value_directive(
440                        ln,
441                        UNSET_EXEC_ENV,
442                        &mut self.unset_exec_env,
443                        |r| r,
444                    );
445                    config.push_name_value_directive(
446                        ln,
447                        RUSTC_ENV,
448                        &mut self.rustc_env,
449                        Config::parse_env,
450                    );
451                    config.push_name_value_directive(
452                        ln,
453                        UNSET_RUSTC_ENV,
454                        &mut self.unset_rustc_env,
455                        |r| r,
456                    );
457                    config.push_name_value_directive(
458                        ln,
459                        FORBID_OUTPUT,
460                        &mut self.forbid_output,
461                        |r| r,
462                    );
463                    config.set_name_directive(
464                        ln,
465                        CHECK_TEST_LINE_NUMBERS_MATCH,
466                        &mut self.check_test_line_numbers_match,
467                    );
468
469                    self.update_pass_mode(ln, test_revision, config);
470                    self.update_fail_mode(ln, config);
471
472                    config.set_name_directive(ln, IGNORE_PASS, &mut self.ignore_pass);
473
474                    if let Some(NormalizeRule { kind, regex, replacement }) =
475                        config.parse_custom_normalization(ln)
476                    {
477                        let rule_tuple = (regex, replacement);
478                        match kind {
479                            NormalizeKind::Stdout => self.normalize_stdout.push(rule_tuple),
480                            NormalizeKind::Stderr => self.normalize_stderr.push(rule_tuple),
481                            NormalizeKind::Stderr32bit => {
482                                if config.target_cfg().pointer_width == 32 {
483                                    self.normalize_stderr.push(rule_tuple);
484                                }
485                            }
486                            NormalizeKind::Stderr64bit => {
487                                if config.target_cfg().pointer_width == 64 {
488                                    self.normalize_stderr.push(rule_tuple);
489                                }
490                            }
491                        }
492                    }
493
494                    if let Some(code) = config
495                        .parse_name_value_directive(ln, FAILURE_STATUS)
496                        .and_then(|code| code.trim().parse::<i32>().ok())
497                    {
498                        self.failure_status = Some(code);
499                    }
500
501                    config.set_name_directive(
502                        ln,
503                        DONT_CHECK_FAILURE_STATUS,
504                        &mut self.dont_check_failure_status,
505                    );
506
507                    config.set_name_directive(ln, RUN_RUSTFIX, &mut self.run_rustfix);
508                    config.set_name_directive(
509                        ln,
510                        RUSTFIX_ONLY_MACHINE_APPLICABLE,
511                        &mut self.rustfix_only_machine_applicable,
512                    );
513                    config.set_name_value_directive(
514                        ln,
515                        ASSEMBLY_OUTPUT,
516                        &mut self.assembly_output,
517                        |r| r.trim().to_string(),
518                    );
519                    config.set_name_directive(
520                        ln,
521                        STDERR_PER_BITWIDTH,
522                        &mut self.stderr_per_bitwidth,
523                    );
524                    config.set_name_directive(ln, INCREMENTAL, &mut self.incremental);
525
526                    // Unlike the other `name_value_directive`s this needs to be handled manually,
527                    // because it sets a `bool` flag.
528                    if let Some(known_bug) = config.parse_name_value_directive(ln, KNOWN_BUG) {
529                        let known_bug = known_bug.trim();
530                        if known_bug == "unknown"
531                            || known_bug.split(',').all(|issue_ref| {
532                                issue_ref
533                                    .trim()
534                                    .split_once('#')
535                                    .filter(|(_, number)| {
536                                        number.chars().all(|digit| digit.is_numeric())
537                                    })
538                                    .is_some()
539                            })
540                        {
541                            self.known_bug = true;
542                        } else {
543                            panic!(
544                                "Invalid known-bug value: {known_bug}\nIt requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
545                            );
546                        }
547                    } else if config.parse_name_directive(ln, KNOWN_BUG) {
548                        panic!(
549                            "Invalid known-bug attribute, requires comma-separated issue references (`#000` or `chalk#000`) or `known-bug: unknown`."
550                        );
551                    }
552
553                    config.set_name_value_directive(
554                        ln,
555                        TEST_MIR_PASS,
556                        &mut self.mir_unit_test,
557                        |s| s.trim().to_string(),
558                    );
559                    config.set_name_directive(ln, REMAP_SRC_BASE, &mut self.remap_src_base);
560
561                    if let Some(flags) = config.parse_name_value_directive(ln, LLVM_COV_FLAGS) {
562                        self.llvm_cov_flags.extend(split_flags(&flags));
563                    }
564
565                    if let Some(flags) = config.parse_name_value_directive(ln, FILECHECK_FLAGS) {
566                        self.filecheck_flags.extend(split_flags(&flags));
567                    }
568
569                    config.set_name_directive(ln, NO_AUTO_CHECK_CFG, &mut self.no_auto_check_cfg);
570
571                    self.update_add_core_stubs(ln, config);
572                },
573            );
574
575            if poisoned {
576                eprintln!("errors encountered during TestProps parsing: {}", testfile.display());
577                panic!("errors encountered during TestProps parsing");
578            }
579        }
580
581        if self.should_ice {
582            self.failure_status = Some(101);
583        }
584
585        if config.mode == Mode::Incremental {
586            self.incremental = true;
587        }
588
589        if config.mode == Mode::Crashes {
590            // we don't want to pollute anything with backtrace-files
591            // also turn off backtraces in order to save some execution
592            // time on the tests; we only need to know IF it crashes
593            self.rustc_env = vec![
594                ("RUST_BACKTRACE".to_string(), "0".to_string()),
595                ("RUSTC_ICE".to_string(), "0".to_string()),
596            ];
597        }
598
599        for key in &["RUST_TEST_NOCAPTURE", "RUST_TEST_THREADS"] {
600            if let Ok(val) = env::var(key) {
601                if !self.exec_env.iter().any(|&(ref x, _)| x == key) {
602                    self.exec_env.push(((*key).to_owned(), val))
603                }
604            }
605        }
606
607        if let (Some(edition), false) = (&config.edition, has_edition) {
608            self.compile_flags.push(format!("--edition={}", edition));
609        }
610    }
611
612    fn update_fail_mode(&mut self, ln: &str, config: &Config) {
613        let check_ui = |mode: &str| {
614            // Mode::Crashes may need build-fail in order to trigger llvm errors or stack overflows
615            if config.mode != Mode::Ui && config.mode != Mode::Crashes {
616                panic!("`{}-fail` header is only supported in UI tests", mode);
617            }
618        };
619        if config.mode == Mode::Ui && config.parse_name_directive(ln, "compile-fail") {
620            panic!("`compile-fail` header is useless in UI tests");
621        }
622        let fail_mode = if config.parse_name_directive(ln, "check-fail") {
623            check_ui("check");
624            Some(FailMode::Check)
625        } else if config.parse_name_directive(ln, "build-fail") {
626            check_ui("build");
627            Some(FailMode::Build)
628        } else if config.parse_name_directive(ln, "run-fail") {
629            check_ui("run");
630            Some(FailMode::Run)
631        } else {
632            None
633        };
634        match (self.fail_mode, fail_mode) {
635            (None, Some(_)) => self.fail_mode = fail_mode,
636            (Some(_), Some(_)) => panic!("multiple `*-fail` headers in a single test"),
637            (_, None) => {}
638        }
639    }
640
641    fn update_pass_mode(&mut self, ln: &str, revision: Option<&str>, config: &Config) {
642        let check_no_run = |s| match (config.mode, s) {
643            (Mode::Ui, _) => (),
644            (Mode::Crashes, _) => (),
645            (Mode::Codegen, "build-pass") => (),
646            (Mode::Incremental, _) => {
647                if revision.is_some() && !self.revisions.iter().all(|r| r.starts_with("cfail")) {
648                    panic!("`{s}` header is only supported in `cfail` incremental tests")
649                }
650            }
651            (mode, _) => panic!("`{s}` header is not supported in `{mode}` tests"),
652        };
653        let pass_mode = if config.parse_name_directive(ln, "check-pass") {
654            check_no_run("check-pass");
655            Some(PassMode::Check)
656        } else if config.parse_name_directive(ln, "build-pass") {
657            check_no_run("build-pass");
658            Some(PassMode::Build)
659        } else if config.parse_name_directive(ln, "run-pass") {
660            check_no_run("run-pass");
661            Some(PassMode::Run)
662        } else {
663            None
664        };
665        match (self.pass_mode, pass_mode) {
666            (None, Some(_)) => self.pass_mode = pass_mode,
667            (Some(_), Some(_)) => panic!("multiple `*-pass` headers in a single test"),
668            (_, None) => {}
669        }
670    }
671
672    pub fn pass_mode(&self, config: &Config) -> Option<PassMode> {
673        if !self.ignore_pass && self.fail_mode.is_none() {
674            if let mode @ Some(_) = config.force_pass_mode {
675                return mode;
676            }
677        }
678        self.pass_mode
679    }
680
681    // does not consider CLI override for pass mode
682    pub fn local_pass_mode(&self) -> Option<PassMode> {
683        self.pass_mode
684    }
685
686    pub fn update_add_core_stubs(&mut self, ln: &str, config: &Config) {
687        let add_core_stubs = config.parse_name_directive(ln, directives::ADD_CORE_STUBS);
688        if add_core_stubs {
689            if !matches!(config.mode, Mode::Ui | Mode::Codegen | Mode::Assembly) {
690                panic!(
691                    "`add-core-stubs` is currently only supported for ui, codegen and assembly test modes"
692                );
693            }
694
695            // FIXME(jieyouxu): this check is currently order-dependent, but we should probably
696            // collect all directives in one go then perform a validation pass after that.
697            if self.local_pass_mode().is_some_and(|pm| pm == PassMode::Run) {
698                // `minicore` can only be used with non-run modes, because it's `core` prelude stubs
699                // and can't run.
700                panic!("`add-core-stubs` cannot be used to run the test binary");
701            }
702
703            self.add_core_stubs = add_core_stubs;
704        }
705    }
706}
707
708/// If the given line begins with the appropriate comment prefix for a directive,
709/// returns a struct containing various parts of the directive.
710fn line_directive<'line>(
711    line_number: usize,
712    original_line: &'line str,
713) -> Option<DirectiveLine<'line>> {
714    // Ignore lines that don't start with the comment prefix.
715    let after_comment =
716        original_line.trim_start().strip_prefix(COMPILETEST_DIRECTIVE_PREFIX)?.trim_start();
717
718    let revision;
719    let raw_directive;
720
721    if let Some(after_open_bracket) = after_comment.strip_prefix('[') {
722        // A comment like `//@[foo]` only applies to revision `foo`.
723        let Some((line_revision, after_close_bracket)) = after_open_bracket.split_once(']') else {
724            panic!(
725                "malformed condition directive: expected `{COMPILETEST_DIRECTIVE_PREFIX}[foo]`, found `{original_line}`"
726            )
727        };
728
729        revision = Some(line_revision);
730        raw_directive = after_close_bracket.trim_start();
731    } else {
732        revision = None;
733        raw_directive = after_comment;
734    };
735
736    Some(DirectiveLine { line_number, revision, raw_directive })
737}
738
739// To prevent duplicating the list of directives between `compiletest`,`htmldocck` and `jsondocck`,
740// we put it into a common file which is included in rust code and parsed here.
741// FIXME: This setup is temporary until we figure out how to improve this situation.
742//        See <https://github.com/rust-lang/rust/issues/125813#issuecomment-2141953780>.
743include!("directive-list.rs");
744
745const KNOWN_HTMLDOCCK_DIRECTIVE_NAMES: &[&str] = &[
746    "count",
747    "!count",
748    "files",
749    "!files",
750    "has",
751    "!has",
752    "has-dir",
753    "!has-dir",
754    "hasraw",
755    "!hasraw",
756    "matches",
757    "!matches",
758    "matchesraw",
759    "!matchesraw",
760    "snapshot",
761    "!snapshot",
762];
763
764const KNOWN_JSONDOCCK_DIRECTIVE_NAMES: &[&str] =
765    &["count", "!count", "has", "!has", "is", "!is", "ismany", "!ismany", "set", "!set"];
766
767/// The (partly) broken-down contents of a line containing a test directive,
768/// which [`iter_header`] passes to its callback function.
769///
770/// For example:
771///
772/// ```text
773/// //@ compile-flags: -O
774///     ^^^^^^^^^^^^^^^^^ raw_directive
775///
776/// //@ [foo] compile-flags: -O
777///      ^^^                    revision
778///           ^^^^^^^^^^^^^^^^^ raw_directive
779/// ```
780struct DirectiveLine<'ln> {
781    line_number: usize,
782    /// Some test directives start with a revision name in square brackets
783    /// (e.g. `[foo]`), and only apply to that revision of the test.
784    /// If present, this field contains the revision name (e.g. `foo`).
785    revision: Option<&'ln str>,
786    /// The main part of the directive, after removing the comment prefix
787    /// and the optional revision specifier.
788    ///
789    /// This is "raw" because the directive's name and colon-separated value
790    /// (if present) have not yet been extracted or checked.
791    raw_directive: &'ln str,
792}
793
794impl<'ln> DirectiveLine<'ln> {
795    fn applies_to_test_revision(&self, test_revision: Option<&str>) -> bool {
796        self.revision.is_none() || self.revision == test_revision
797    }
798}
799
800pub(crate) struct CheckDirectiveResult<'ln> {
801    is_known_directive: bool,
802    trailing_directive: Option<&'ln str>,
803}
804
805pub(crate) fn check_directive<'a>(
806    directive_ln: &'a str,
807    mode: Mode,
808    original_line: &str,
809) -> CheckDirectiveResult<'a> {
810    let (directive_name, post) = directive_ln.split_once([':', ' ']).unwrap_or((directive_ln, ""));
811
812    let trailing = post.trim().split_once(' ').map(|(pre, _)| pre).unwrap_or(post);
813    let is_known = |s: &str| {
814        KNOWN_DIRECTIVE_NAMES.contains(&s)
815            || match mode {
816                Mode::Rustdoc | Mode::RustdocJson => {
817                    original_line.starts_with("//@")
818                        && match mode {
819                            Mode::Rustdoc => KNOWN_HTMLDOCCK_DIRECTIVE_NAMES,
820                            Mode::RustdocJson => KNOWN_JSONDOCCK_DIRECTIVE_NAMES,
821                            _ => unreachable!(),
822                        }
823                        .contains(&s)
824                }
825                _ => false,
826            }
827    };
828    let trailing_directive = {
829        // 1. is the directive name followed by a space? (to exclude `:`)
830        matches!(directive_ln.get(directive_name.len()..), Some(s) if s.starts_with(' '))
831            // 2. is what is after that directive also a directive (ex: "only-x86 only-arm")
832            && is_known(trailing)
833    }
834    .then_some(trailing);
835
836    CheckDirectiveResult { is_known_directive: is_known(&directive_name), trailing_directive }
837}
838
839const COMPILETEST_DIRECTIVE_PREFIX: &str = "//@";
840
841fn iter_header(
842    mode: Mode,
843    _suite: &str,
844    poisoned: &mut bool,
845    testfile: &Path,
846    rdr: impl Read,
847    it: &mut dyn FnMut(DirectiveLine<'_>),
848) {
849    if testfile.is_dir() {
850        return;
851    }
852
853    // Coverage tests in coverage-run mode always have these extra directives, without needing to
854    // specify them manually in every test file.
855    //
856    // FIXME(jieyouxu): I feel like there's a better way to do this, leaving for later.
857    if mode == Mode::CoverageRun {
858        let extra_directives: &[&str] = &[
859            "needs-profiler-runtime",
860            // FIXME(pietroalbini): this test currently does not work on cross-compiled targets
861            // because remote-test is not capable of sending back the *.profraw files generated by
862            // the LLVM instrumentation.
863            "ignore-cross-compile",
864        ];
865        // Process the extra implied directives, with a dummy line number of 0.
866        for raw_directive in extra_directives {
867            it(DirectiveLine { line_number: 0, revision: None, raw_directive });
868        }
869    }
870
871    let mut rdr = BufReader::with_capacity(1024, rdr);
872    let mut ln = String::new();
873    let mut line_number = 0;
874
875    loop {
876        line_number += 1;
877        ln.clear();
878        if rdr.read_line(&mut ln).unwrap() == 0 {
879            break;
880        }
881        let ln = ln.trim();
882
883        let Some(directive_line) = line_directive(line_number, ln) else {
884            continue;
885        };
886
887        // Perform unknown directive check on Rust files.
888        if testfile.extension().map(|e| e == "rs").unwrap_or(false) {
889            let CheckDirectiveResult { is_known_directive, trailing_directive } =
890                check_directive(directive_line.raw_directive, mode, ln);
891
892            if !is_known_directive {
893                *poisoned = true;
894
895                eprintln!(
896                    "error: detected unknown compiletest test directive `{}` in {}:{}",
897                    directive_line.raw_directive,
898                    testfile.display(),
899                    line_number,
900                );
901
902                return;
903            }
904
905            if let Some(trailing_directive) = &trailing_directive {
906                *poisoned = true;
907
908                eprintln!(
909                    "error: detected trailing compiletest test directive `{}` in {}:{}\n \
910                      help: put the trailing directive in it's own line: `//@ {}`",
911                    trailing_directive,
912                    testfile.display(),
913                    line_number,
914                    trailing_directive,
915                );
916
917                return;
918            }
919        }
920
921        it(directive_line);
922    }
923}
924
925impl Config {
926    fn parse_and_update_revisions(&self, testfile: &Path, line: &str, existing: &mut Vec<String>) {
927        const FORBIDDEN_REVISION_NAMES: [&str; 9] =
928            ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
929
930        if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
931            if self.mode == Mode::RunMake {
932                panic!("`run-make` tests do not support revisions: {}", testfile.display());
933            }
934
935            let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
936            for revision in raw.split_whitespace().map(|r| r.to_string()) {
937                if !duplicates.insert(revision.clone()) {
938                    panic!(
939                        "duplicate revision: `{}` in line `{}`: {}",
940                        revision,
941                        raw,
942                        testfile.display()
943                    );
944                } else if matches!(self.mode, Mode::Assembly | Mode::Codegen | Mode::MirOpt)
945                    && FORBIDDEN_REVISION_NAMES.contains(&revision.as_str())
946                {
947                    panic!(
948                        "revision name `{revision}` is not permitted in a test suite that uses `FileCheck` annotations\n\
949                         as it is confusing when used as custom `FileCheck` prefix: `{revision}` in line `{}`: {}",
950                        raw,
951                        testfile.display()
952                    );
953                }
954                existing.push(revision);
955            }
956        }
957    }
958
959    fn parse_env(nv: String) -> (String, String) {
960        // nv is either FOO or FOO=BAR
961        let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
962
963        match strs.len() {
964            1 => (strs.pop().unwrap(), String::new()),
965            2 => {
966                let end = strs.pop().unwrap();
967                (strs.pop().unwrap(), end)
968            }
969            n => panic!("Expected 1 or 2 strings, not {}", n),
970        }
971    }
972
973    fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
974        if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
975            Some(PathBuf::from(&s))
976        } else if self.parse_name_directive(line, "pp-exact") {
977            testfile.file_name().map(PathBuf::from)
978        } else {
979            None
980        }
981    }
982
983    fn parse_custom_normalization(&self, raw_directive: &str) -> Option<NormalizeRule> {
984        // FIXME(Zalathar): Integrate name/value splitting into `DirectiveLine`
985        // instead of doing it here.
986        let (directive_name, raw_value) = raw_directive.split_once(':')?;
987
988        let kind = match directive_name {
989            "normalize-stdout" => NormalizeKind::Stdout,
990            "normalize-stderr" => NormalizeKind::Stderr,
991            "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
992            "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
993            _ => return None,
994        };
995
996        let Some((regex, replacement)) = parse_normalize_rule(raw_value) else {
997            panic!(
998                "couldn't parse custom normalization rule: `{raw_directive}`\n\
999                help: expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`"
1000            );
1001        };
1002        Some(NormalizeRule { kind, regex, replacement })
1003    }
1004
1005    fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
1006        // Ensure the directive is a whole word. Do not match "ignore-x86" when
1007        // the line says "ignore-x86_64".
1008        line.starts_with(directive)
1009            && matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
1010    }
1011
1012    fn parse_negative_name_directive(&self, line: &str, directive: &str) -> bool {
1013        line.starts_with("no-") && self.parse_name_directive(&line[3..], directive)
1014    }
1015
1016    pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
1017        let colon = directive.len();
1018        if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
1019            let value = line[(colon + 1)..].to_owned();
1020            debug!("{}: {}", directive, value);
1021            Some(expand_variables(value, self))
1022        } else {
1023            None
1024        }
1025    }
1026
1027    fn parse_edition(&self, line: &str) -> Option<String> {
1028        self.parse_name_value_directive(line, "edition")
1029    }
1030
1031    fn set_name_directive(&self, line: &str, directive: &str, value: &mut bool) {
1032        match value {
1033            true => {
1034                if self.parse_negative_name_directive(line, directive) {
1035                    *value = false;
1036                }
1037            }
1038            false => {
1039                if self.parse_name_directive(line, directive) {
1040                    *value = true;
1041                }
1042            }
1043        }
1044    }
1045
1046    fn set_name_value_directive<T>(
1047        &self,
1048        line: &str,
1049        directive: &str,
1050        value: &mut Option<T>,
1051        parse: impl FnOnce(String) -> T,
1052    ) {
1053        if value.is_none() {
1054            *value = self.parse_name_value_directive(line, directive).map(parse);
1055        }
1056    }
1057
1058    fn push_name_value_directive<T>(
1059        &self,
1060        line: &str,
1061        directive: &str,
1062        values: &mut Vec<T>,
1063        parse: impl FnOnce(String) -> T,
1064    ) {
1065        if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
1066            values.push(value);
1067        }
1068    }
1069}
1070
1071// FIXME(jieyouxu): fix some of these variable names to more accurately reflect what they do.
1072fn expand_variables(mut value: String, config: &Config) -> String {
1073    const CWD: &str = "{{cwd}}";
1074    const SRC_BASE: &str = "{{src-base}}";
1075    const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
1076    const RUST_SRC_BASE: &str = "{{rust-src-base}}";
1077    const SYSROOT_BASE: &str = "{{sysroot-base}}";
1078    const TARGET_LINKER: &str = "{{target-linker}}";
1079    const TARGET: &str = "{{target}}";
1080
1081    if value.contains(CWD) {
1082        let cwd = env::current_dir().unwrap();
1083        value = value.replace(CWD, &cwd.to_string_lossy());
1084    }
1085
1086    if value.contains(SRC_BASE) {
1087        value = value.replace(SRC_BASE, &config.src_test_suite_root.to_str().unwrap());
1088    }
1089
1090    if value.contains(TEST_SUITE_BUILD_BASE) {
1091        value =
1092            value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.to_str().unwrap());
1093    }
1094
1095    if value.contains(SYSROOT_BASE) {
1096        value = value.replace(SYSROOT_BASE, &config.sysroot_base.to_str().unwrap());
1097    }
1098
1099    if value.contains(TARGET_LINKER) {
1100        value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
1101    }
1102
1103    if value.contains(TARGET) {
1104        value = value.replace(TARGET, &config.target);
1105    }
1106
1107    if value.contains(RUST_SRC_BASE) {
1108        let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
1109        src_base.try_exists().expect(&*format!("{} should exists", src_base.display()));
1110        let src_base = src_base.read_link().unwrap_or(src_base);
1111        value = value.replace(RUST_SRC_BASE, &src_base.to_string_lossy());
1112    }
1113
1114    value
1115}
1116
1117struct NormalizeRule {
1118    kind: NormalizeKind,
1119    regex: String,
1120    replacement: String,
1121}
1122
1123enum NormalizeKind {
1124    Stdout,
1125    Stderr,
1126    Stderr32bit,
1127    Stderr64bit,
1128}
1129
1130/// Parses the regex and replacement values of a `//@ normalize-*` header,
1131/// in the format:
1132/// ```text
1133/// "REGEX" -> "REPLACEMENT"
1134/// ```
1135fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
1136    // FIXME: Support escaped double-quotes in strings.
1137    let captures = static_regex!(
1138        r#"(?x) # (verbose mode regex)
1139        ^
1140        \s*                     # (leading whitespace)
1141        "(?<regex>[^"]*)"       # "REGEX"
1142        \s+->\s+                # ->
1143        "(?<replacement>[^"]*)" # "REPLACEMENT"
1144        $
1145        "#
1146    )
1147    .captures(raw_value)?;
1148    let regex = captures["regex"].to_owned();
1149    let replacement = captures["replacement"].to_owned();
1150    // A `\n` sequence in the replacement becomes an actual newline.
1151    // FIXME: Do unescaping in a less ad-hoc way, and perhaps support escaped
1152    // backslashes and double-quotes.
1153    let replacement = replacement.replace("\\n", "\n");
1154    Some((regex, replacement))
1155}
1156
1157/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this
1158/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting
1159/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of
1160/// having to write `//@ min-llvm-version: 19.0.0`.
1161///
1162/// Currently panics if the input string is malformed, though we really should not use panic as an
1163/// error handling strategy.
1164///
1165/// FIXME(jieyouxu): improve error handling
1166pub fn extract_llvm_version(version: &str) -> Version {
1167    // The version substring we're interested in usually looks like the `1.2.3`, without any of the
1168    // fancy suffix like `-rc1` or `meow`.
1169    let version = version.trim();
1170    let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
1171    let version_without_suffix = match version.split_once(uninterested) {
1172        Some((prefix, _suffix)) => prefix,
1173        None => version,
1174    };
1175
1176    let components: Vec<u64> = version_without_suffix
1177        .split('.')
1178        .map(|s| s.parse().expect("llvm version component should consist of only digits"))
1179        .collect();
1180
1181    match &components[..] {
1182        [major] => Version::new(*major, 0, 0),
1183        [major, minor] => Version::new(*major, *minor, 0),
1184        [major, minor, patch] => Version::new(*major, *minor, *patch),
1185        _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
1186    }
1187}
1188
1189pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
1190    let output = Command::new(binary_path).arg("--version").output().ok()?;
1191    if !output.status.success() {
1192        return None;
1193    }
1194    let version = String::from_utf8(output.stdout).ok()?;
1195    for line in version.lines() {
1196        if let Some(version) = line.split("LLVM version ").nth(1) {
1197            return Some(extract_llvm_version(version));
1198        }
1199    }
1200    None
1201}
1202
1203/// For tests using the `needs-llvm-zstd` directive:
1204/// - for local LLVM builds, try to find the static zstd library in the llvm-config system libs.
1205/// - for `download-ci-llvm`, see if `lld` was built with zstd support.
1206pub fn llvm_has_libzstd(config: &Config) -> bool {
1207    // Strategy 1: works for local builds but not with `download-ci-llvm`.
1208    //
1209    // We check whether `llvm-config` returns the zstd library. Bootstrap's `llvm.libzstd` will only
1210    // ask to statically link it when building LLVM, so we only check if the list of system libs
1211    // contains a path to that static lib, and that it exists.
1212    //
1213    // See compiler/rustc_llvm/build.rs for more details and similar expectations.
1214    fn is_zstd_in_config(llvm_bin_dir: &Path) -> Option<()> {
1215        let llvm_config_path = llvm_bin_dir.join("llvm-config");
1216        let output = Command::new(llvm_config_path).arg("--system-libs").output().ok()?;
1217        assert!(output.status.success(), "running llvm-config --system-libs failed");
1218
1219        let libs = String::from_utf8(output.stdout).ok()?;
1220        for lib in libs.split_whitespace() {
1221            if lib.ends_with("libzstd.a") && Path::new(lib).exists() {
1222                return Some(());
1223            }
1224        }
1225
1226        None
1227    }
1228
1229    // Strategy 2: `download-ci-llvm`'s `llvm-config --system-libs` will not return any libs to
1230    // use.
1231    //
1232    // The CI artifacts also don't contain the bootstrap config used to build them: otherwise we
1233    // could have looked at the `llvm.libzstd` config.
1234    //
1235    // We infer whether `LLVM_ENABLE_ZSTD` was used to build LLVM as a byproduct of testing whether
1236    // `lld` supports it. If not, an error will be emitted: "LLVM was not built with
1237    // LLVM_ENABLE_ZSTD or did not find zstd at build time".
1238    #[cfg(unix)]
1239    fn is_lld_built_with_zstd(llvm_bin_dir: &Path) -> Option<()> {
1240        let lld_path = llvm_bin_dir.join("lld");
1241        if lld_path.exists() {
1242            // We can't call `lld` as-is, it expects to be invoked by a compiler driver using a
1243            // different name. Prepare a temporary symlink to do that.
1244            let lld_symlink_path = llvm_bin_dir.join("ld.lld");
1245            if !lld_symlink_path.exists() {
1246                std::os::unix::fs::symlink(lld_path, &lld_symlink_path).ok()?;
1247            }
1248
1249            // Run `lld` with a zstd flag. We expect this command to always error here, we don't
1250            // want to link actual files and don't pass any.
1251            let output = Command::new(&lld_symlink_path)
1252                .arg("--compress-debug-sections=zstd")
1253                .output()
1254                .ok()?;
1255            assert!(!output.status.success());
1256
1257            // Look for a specific error caused by LLVM not being built with zstd support. We could
1258            // also look for the "no input files" message, indicating the zstd flag was accepted.
1259            let stderr = String::from_utf8(output.stderr).ok()?;
1260            let zstd_available = !stderr.contains("LLVM was not built with LLVM_ENABLE_ZSTD");
1261
1262            // We don't particularly need to clean the link up (so the previous commands could fail
1263            // in theory but won't in practice), but we can try.
1264            std::fs::remove_file(lld_symlink_path).ok()?;
1265
1266            if zstd_available {
1267                return Some(());
1268            }
1269        }
1270
1271        None
1272    }
1273
1274    #[cfg(not(unix))]
1275    fn is_lld_built_with_zstd(_llvm_bin_dir: &Path) -> Option<()> {
1276        None
1277    }
1278
1279    if let Some(llvm_bin_dir) = &config.llvm_bin_dir {
1280        // Strategy 1: for local LLVM builds.
1281        if is_zstd_in_config(llvm_bin_dir).is_some() {
1282            return true;
1283        }
1284
1285        // Strategy 2: for LLVM artifacts built on CI via `download-ci-llvm`.
1286        //
1287        // It doesn't work for cases where the artifacts don't contain the linker, but it's
1288        // best-effort: CI has `llvm.libzstd` and `lld` enabled on the x64 linux artifacts, so it
1289        // will at least work there.
1290        //
1291        // If this can be improved and expanded to less common cases in the future, it should.
1292        if config.target == "x86_64-unknown-linux-gnu"
1293            && config.host == config.target
1294            && is_lld_built_with_zstd(llvm_bin_dir).is_some()
1295        {
1296            return true;
1297        }
1298    }
1299
1300    // Otherwise, all hope is lost.
1301    false
1302}
1303
1304/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation
1305/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`.
1306///
1307/// If the `<version2>` part is omitted, the second component of the tuple is the same as
1308/// `<version1>`.
1309fn extract_version_range<'a, F, VersionTy: Clone>(
1310    line: &'a str,
1311    parse: F,
1312) -> Option<(VersionTy, VersionTy)>
1313where
1314    F: Fn(&'a str) -> Option<VersionTy>,
1315{
1316    let mut splits = line.splitn(2, "- ").map(str::trim);
1317    let min = splits.next().unwrap();
1318    if min.ends_with('-') {
1319        return None;
1320    }
1321
1322    let max = splits.next();
1323
1324    if min.is_empty() {
1325        return None;
1326    }
1327
1328    let min = parse(min)?;
1329    let max = match max {
1330        Some("") => return None,
1331        Some(max) => parse(max)?,
1332        _ => min.clone(),
1333    };
1334
1335    Some((min, max))
1336}
1337
1338pub fn make_test_description<R: Read>(
1339    config: &Config,
1340    cache: &HeadersCache,
1341    name: test::TestName,
1342    path: &Path,
1343    src: R,
1344    test_revision: Option<&str>,
1345    poisoned: &mut bool,
1346) -> test::TestDesc {
1347    let mut ignore = false;
1348    let mut ignore_message = None;
1349    let mut should_fail = false;
1350
1351    let mut local_poisoned = false;
1352
1353    // Scan through the test file to handle `ignore-*`, `only-*`, and `needs-*` directives.
1354    iter_header(
1355        config.mode,
1356        &config.suite,
1357        &mut local_poisoned,
1358        path,
1359        src,
1360        &mut |directive @ DirectiveLine { line_number, raw_directive: ln, .. }| {
1361            if !directive.applies_to_test_revision(test_revision) {
1362                return;
1363            }
1364
1365            macro_rules! decision {
1366                ($e:expr) => {
1367                    match $e {
1368                        IgnoreDecision::Ignore { reason } => {
1369                            ignore = true;
1370                            // The ignore reason must be a &'static str, so we have to leak memory to
1371                            // create it. This is fine, as the header is parsed only at the start of
1372                            // compiletest so it won't grow indefinitely.
1373                            ignore_message = Some(&*Box::leak(Box::<str>::from(reason)));
1374                        }
1375                        IgnoreDecision::Error { message } => {
1376                            eprintln!("error: {}:{line_number}: {message}", path.display());
1377                            *poisoned = true;
1378                            return;
1379                        }
1380                        IgnoreDecision::Continue => {}
1381                    }
1382                };
1383            }
1384
1385            decision!(cfg::handle_ignore(config, ln));
1386            decision!(cfg::handle_only(config, ln));
1387            decision!(needs::handle_needs(&cache.needs, config, ln));
1388            decision!(ignore_llvm(config, path, ln));
1389            decision!(ignore_cdb(config, ln));
1390            decision!(ignore_gdb(config, ln));
1391            decision!(ignore_lldb(config, ln));
1392
1393            if config.target == "wasm32-unknown-unknown"
1394                && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
1395            {
1396                decision!(IgnoreDecision::Ignore {
1397                    reason: "ignored on WASM as the run results cannot be checked there".into(),
1398                });
1399            }
1400
1401            should_fail |= config.parse_name_directive(ln, "should-fail");
1402        },
1403    );
1404
1405    if local_poisoned {
1406        eprintln!("errors encountered when trying to make test description: {}", path.display());
1407        panic!("errors encountered when trying to make test description");
1408    }
1409
1410    // The `should-fail` annotation doesn't apply to pretty tests,
1411    // since we run the pretty printer across all tests by default.
1412    // If desired, we could add a `should-fail-pretty` annotation.
1413    let should_panic = match config.mode {
1414        crate::common::Pretty => test::ShouldPanic::No,
1415        _ if should_fail => test::ShouldPanic::Yes,
1416        _ => test::ShouldPanic::No,
1417    };
1418
1419    test::TestDesc {
1420        name,
1421        ignore,
1422        ignore_message,
1423        source_file: "",
1424        start_line: 0,
1425        start_col: 0,
1426        end_line: 0,
1427        end_col: 0,
1428        should_panic,
1429        compile_fail: false,
1430        no_run: false,
1431        test_type: test::TestType::Unknown,
1432    }
1433}
1434
1435fn ignore_cdb(config: &Config, line: &str) -> IgnoreDecision {
1436    if config.debugger != Some(Debugger::Cdb) {
1437        return IgnoreDecision::Continue;
1438    }
1439
1440    if let Some(actual_version) = config.cdb_version {
1441        if let Some(rest) = line.strip_prefix("min-cdb-version:").map(str::trim) {
1442            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1443                panic!("couldn't parse version range: {:?}", rest);
1444            });
1445
1446            // Ignore if actual version is smaller than the minimum
1447            // required version
1448            if actual_version < min_version {
1449                return IgnoreDecision::Ignore {
1450                    reason: format!("ignored when the CDB version is lower than {rest}"),
1451                };
1452            }
1453        }
1454    }
1455    IgnoreDecision::Continue
1456}
1457
1458fn ignore_gdb(config: &Config, line: &str) -> IgnoreDecision {
1459    if config.debugger != Some(Debugger::Gdb) {
1460        return IgnoreDecision::Continue;
1461    }
1462
1463    if let Some(actual_version) = config.gdb_version {
1464        if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
1465            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1466                .unwrap_or_else(|| {
1467                    panic!("couldn't parse version range: {:?}", rest);
1468                });
1469
1470            if start_ver != end_ver {
1471                panic!("Expected single GDB version")
1472            }
1473            // Ignore if actual version is smaller than the minimum
1474            // required version
1475            if actual_version < start_ver {
1476                return IgnoreDecision::Ignore {
1477                    reason: format!("ignored when the GDB version is lower than {rest}"),
1478                };
1479            }
1480        } else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
1481            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1482                .unwrap_or_else(|| {
1483                    panic!("couldn't parse version range: {:?}", rest);
1484                });
1485
1486            if max_version < min_version {
1487                panic!("Malformed GDB version range: max < min")
1488            }
1489
1490            if actual_version >= min_version && actual_version <= max_version {
1491                if min_version == max_version {
1492                    return IgnoreDecision::Ignore {
1493                        reason: format!("ignored when the GDB version is {rest}"),
1494                    };
1495                } else {
1496                    return IgnoreDecision::Ignore {
1497                        reason: format!("ignored when the GDB version is between {rest}"),
1498                    };
1499                }
1500            }
1501        }
1502    }
1503    IgnoreDecision::Continue
1504}
1505
1506fn ignore_lldb(config: &Config, line: &str) -> IgnoreDecision {
1507    if config.debugger != Some(Debugger::Lldb) {
1508        return IgnoreDecision::Continue;
1509    }
1510
1511    if let Some(actual_version) = config.lldb_version {
1512        if let Some(rest) = line.strip_prefix("min-lldb-version:").map(str::trim) {
1513            let min_version = rest.parse().unwrap_or_else(|e| {
1514                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1515            });
1516            // Ignore if actual version is smaller the minimum required
1517            // version
1518            if actual_version < min_version {
1519                return IgnoreDecision::Ignore {
1520                    reason: format!("ignored when the LLDB version is {rest}"),
1521                };
1522            }
1523        }
1524    }
1525    IgnoreDecision::Continue
1526}
1527
1528fn ignore_llvm(config: &Config, path: &Path, line: &str) -> IgnoreDecision {
1529    if let Some(needed_components) =
1530        config.parse_name_value_directive(line, "needs-llvm-components")
1531    {
1532        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1533        if let Some(missing_component) = needed_components
1534            .split_whitespace()
1535            .find(|needed_component| !components.contains(needed_component))
1536        {
1537            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1538                panic!(
1539                    "missing LLVM component {}, and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {}",
1540                    missing_component,
1541                    path.display()
1542                );
1543            }
1544            return IgnoreDecision::Ignore {
1545                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1546            };
1547        }
1548    }
1549    if let Some(actual_version) = &config.llvm_version {
1550        // Note that these `min` versions will check for not just major versions.
1551
1552        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1553            let min_version = extract_llvm_version(&version_string);
1554            // Ignore if actual version is smaller than the minimum required version.
1555            if *actual_version < min_version {
1556                return IgnoreDecision::Ignore {
1557                    reason: format!(
1558                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1559                    ),
1560                };
1561            }
1562        } else if let Some(version_string) =
1563            config.parse_name_value_directive(line, "max-llvm-major-version")
1564        {
1565            let max_version = extract_llvm_version(&version_string);
1566            // Ignore if actual major version is larger than the maximum required major version.
1567            if actual_version.major > max_version.major {
1568                return IgnoreDecision::Ignore {
1569                    reason: format!(
1570                        "ignored when the LLVM version ({actual_version}) is newer than major\
1571                        version {}",
1572                        max_version.major
1573                    ),
1574                };
1575            }
1576        } else if let Some(version_string) =
1577            config.parse_name_value_directive(line, "min-system-llvm-version")
1578        {
1579            let min_version = extract_llvm_version(&version_string);
1580            // Ignore if using system LLVM and actual version
1581            // is smaller the minimum required version
1582            if config.system_llvm && *actual_version < min_version {
1583                return IgnoreDecision::Ignore {
1584                    reason: format!(
1585                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1586                    ),
1587                };
1588            }
1589        } else if let Some(version_range) =
1590            config.parse_name_value_directive(line, "ignore-llvm-version")
1591        {
1592            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1593            let (v_min, v_max) =
1594                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1595                    .unwrap_or_else(|| {
1596                        panic!("couldn't parse version range: \"{version_range}\"");
1597                    });
1598            if v_max < v_min {
1599                panic!("malformed LLVM version range where {v_max} < {v_min}")
1600            }
1601            // Ignore if version lies inside of range.
1602            if *actual_version >= v_min && *actual_version <= v_max {
1603                if v_min == v_max {
1604                    return IgnoreDecision::Ignore {
1605                        reason: format!("ignored when the LLVM version is {actual_version}"),
1606                    };
1607                } else {
1608                    return IgnoreDecision::Ignore {
1609                        reason: format!(
1610                            "ignored when the LLVM version is between {v_min} and {v_max}"
1611                        ),
1612                    };
1613                }
1614            }
1615        } else if let Some(version_string) =
1616            config.parse_name_value_directive(line, "exact-llvm-major-version")
1617        {
1618            // Syntax is "exact-llvm-major-version: <version>"
1619            let version = extract_llvm_version(&version_string);
1620            if actual_version.major != version.major {
1621                return IgnoreDecision::Ignore {
1622                    reason: format!(
1623                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1624                        actual_version.major, version.major
1625                    ),
1626                };
1627            }
1628        }
1629    }
1630    IgnoreDecision::Continue
1631}
1632
1633enum IgnoreDecision {
1634    Ignore { reason: String },
1635    Continue,
1636    Error { message: String },
1637}