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; 2] = [
928            // `//@ revisions: true false` Implying `--cfg=true` and `--cfg=false` makes it very
929            // weird for the test, since if the test writer wants a cfg of the same revision name
930            // they'd have to use `cfg(r#true)` and `cfg(r#false)`.
931            "true", "false",
932        ];
933
934        const FILECHECK_FORBIDDEN_REVISION_NAMES: [&str; 9] =
935            ["CHECK", "COM", "NEXT", "SAME", "EMPTY", "NOT", "COUNT", "DAG", "LABEL"];
936
937        if let Some(raw) = self.parse_name_value_directive(line, "revisions") {
938            if self.mode == Mode::RunMake {
939                panic!("`run-make` tests do not support revisions: {}", testfile.display());
940            }
941
942            let mut duplicates: HashSet<_> = existing.iter().cloned().collect();
943            for revision in raw.split_whitespace() {
944                if !duplicates.insert(revision.to_string()) {
945                    panic!(
946                        "duplicate revision: `{}` in line `{}`: {}",
947                        revision,
948                        raw,
949                        testfile.display()
950                    );
951                }
952
953                if FORBIDDEN_REVISION_NAMES.contains(&revision) {
954                    panic!(
955                        "revision name `{revision}` is not permitted: `{}` in line `{}`: {}",
956                        revision,
957                        raw,
958                        testfile.display()
959                    );
960                }
961
962                if matches!(self.mode, Mode::Assembly | Mode::Codegen | Mode::MirOpt)
963                    && FILECHECK_FORBIDDEN_REVISION_NAMES.contains(&revision)
964                {
965                    panic!(
966                        "revision name `{revision}` is not permitted in a test suite that uses \
967                        `FileCheck` annotations as it is confusing when used as custom `FileCheck` \
968                        prefix: `{revision}` in line `{}`: {}",
969                        raw,
970                        testfile.display()
971                    );
972                }
973
974                existing.push(revision.to_string());
975            }
976        }
977    }
978
979    fn parse_env(nv: String) -> (String, String) {
980        // nv is either FOO or FOO=BAR
981        let mut strs: Vec<String> = nv.splitn(2, '=').map(str::to_owned).collect();
982
983        match strs.len() {
984            1 => (strs.pop().unwrap(), String::new()),
985            2 => {
986                let end = strs.pop().unwrap();
987                (strs.pop().unwrap(), end)
988            }
989            n => panic!("Expected 1 or 2 strings, not {}", n),
990        }
991    }
992
993    fn parse_pp_exact(&self, line: &str, testfile: &Path) -> Option<PathBuf> {
994        if let Some(s) = self.parse_name_value_directive(line, "pp-exact") {
995            Some(PathBuf::from(&s))
996        } else if self.parse_name_directive(line, "pp-exact") {
997            testfile.file_name().map(PathBuf::from)
998        } else {
999            None
1000        }
1001    }
1002
1003    fn parse_custom_normalization(&self, raw_directive: &str) -> Option<NormalizeRule> {
1004        // FIXME(Zalathar): Integrate name/value splitting into `DirectiveLine`
1005        // instead of doing it here.
1006        let (directive_name, raw_value) = raw_directive.split_once(':')?;
1007
1008        let kind = match directive_name {
1009            "normalize-stdout" => NormalizeKind::Stdout,
1010            "normalize-stderr" => NormalizeKind::Stderr,
1011            "normalize-stderr-32bit" => NormalizeKind::Stderr32bit,
1012            "normalize-stderr-64bit" => NormalizeKind::Stderr64bit,
1013            _ => return None,
1014        };
1015
1016        let Some((regex, replacement)) = parse_normalize_rule(raw_value) else {
1017            panic!(
1018                "couldn't parse custom normalization rule: `{raw_directive}`\n\
1019                help: expected syntax is: `{directive_name}: \"REGEX\" -> \"REPLACEMENT\"`"
1020            );
1021        };
1022        Some(NormalizeRule { kind, regex, replacement })
1023    }
1024
1025    fn parse_name_directive(&self, line: &str, directive: &str) -> bool {
1026        // Ensure the directive is a whole word. Do not match "ignore-x86" when
1027        // the line says "ignore-x86_64".
1028        line.starts_with(directive)
1029            && matches!(line.as_bytes().get(directive.len()), None | Some(&b' ') | Some(&b':'))
1030    }
1031
1032    fn parse_negative_name_directive(&self, line: &str, directive: &str) -> bool {
1033        line.starts_with("no-") && self.parse_name_directive(&line[3..], directive)
1034    }
1035
1036    pub fn parse_name_value_directive(&self, line: &str, directive: &str) -> Option<String> {
1037        let colon = directive.len();
1038        if line.starts_with(directive) && line.as_bytes().get(colon) == Some(&b':') {
1039            let value = line[(colon + 1)..].to_owned();
1040            debug!("{}: {}", directive, value);
1041            Some(expand_variables(value, self))
1042        } else {
1043            None
1044        }
1045    }
1046
1047    fn parse_edition(&self, line: &str) -> Option<String> {
1048        self.parse_name_value_directive(line, "edition")
1049    }
1050
1051    fn set_name_directive(&self, line: &str, directive: &str, value: &mut bool) {
1052        match value {
1053            true => {
1054                if self.parse_negative_name_directive(line, directive) {
1055                    *value = false;
1056                }
1057            }
1058            false => {
1059                if self.parse_name_directive(line, directive) {
1060                    *value = true;
1061                }
1062            }
1063        }
1064    }
1065
1066    fn set_name_value_directive<T>(
1067        &self,
1068        line: &str,
1069        directive: &str,
1070        value: &mut Option<T>,
1071        parse: impl FnOnce(String) -> T,
1072    ) {
1073        if value.is_none() {
1074            *value = self.parse_name_value_directive(line, directive).map(parse);
1075        }
1076    }
1077
1078    fn push_name_value_directive<T>(
1079        &self,
1080        line: &str,
1081        directive: &str,
1082        values: &mut Vec<T>,
1083        parse: impl FnOnce(String) -> T,
1084    ) {
1085        if let Some(value) = self.parse_name_value_directive(line, directive).map(parse) {
1086            values.push(value);
1087        }
1088    }
1089}
1090
1091// FIXME(jieyouxu): fix some of these variable names to more accurately reflect what they do.
1092fn expand_variables(mut value: String, config: &Config) -> String {
1093    const CWD: &str = "{{cwd}}";
1094    const SRC_BASE: &str = "{{src-base}}";
1095    const TEST_SUITE_BUILD_BASE: &str = "{{build-base}}";
1096    const RUST_SRC_BASE: &str = "{{rust-src-base}}";
1097    const SYSROOT_BASE: &str = "{{sysroot-base}}";
1098    const TARGET_LINKER: &str = "{{target-linker}}";
1099    const TARGET: &str = "{{target}}";
1100
1101    if value.contains(CWD) {
1102        let cwd = env::current_dir().unwrap();
1103        value = value.replace(CWD, &cwd.to_string_lossy());
1104    }
1105
1106    if value.contains(SRC_BASE) {
1107        value = value.replace(SRC_BASE, &config.src_test_suite_root.to_str().unwrap());
1108    }
1109
1110    if value.contains(TEST_SUITE_BUILD_BASE) {
1111        value =
1112            value.replace(TEST_SUITE_BUILD_BASE, &config.build_test_suite_root.to_str().unwrap());
1113    }
1114
1115    if value.contains(SYSROOT_BASE) {
1116        value = value.replace(SYSROOT_BASE, &config.sysroot_base.to_str().unwrap());
1117    }
1118
1119    if value.contains(TARGET_LINKER) {
1120        value = value.replace(TARGET_LINKER, config.target_linker.as_deref().unwrap_or(""));
1121    }
1122
1123    if value.contains(TARGET) {
1124        value = value.replace(TARGET, &config.target);
1125    }
1126
1127    if value.contains(RUST_SRC_BASE) {
1128        let src_base = config.sysroot_base.join("lib/rustlib/src/rust");
1129        src_base.try_exists().expect(&*format!("{} should exists", src_base.display()));
1130        let src_base = src_base.read_link().unwrap_or(src_base);
1131        value = value.replace(RUST_SRC_BASE, &src_base.to_string_lossy());
1132    }
1133
1134    value
1135}
1136
1137struct NormalizeRule {
1138    kind: NormalizeKind,
1139    regex: String,
1140    replacement: String,
1141}
1142
1143enum NormalizeKind {
1144    Stdout,
1145    Stderr,
1146    Stderr32bit,
1147    Stderr64bit,
1148}
1149
1150/// Parses the regex and replacement values of a `//@ normalize-*` header,
1151/// in the format:
1152/// ```text
1153/// "REGEX" -> "REPLACEMENT"
1154/// ```
1155fn parse_normalize_rule(raw_value: &str) -> Option<(String, String)> {
1156    // FIXME: Support escaped double-quotes in strings.
1157    let captures = static_regex!(
1158        r#"(?x) # (verbose mode regex)
1159        ^
1160        \s*                     # (leading whitespace)
1161        "(?<regex>[^"]*)"       # "REGEX"
1162        \s+->\s+                # ->
1163        "(?<replacement>[^"]*)" # "REPLACEMENT"
1164        $
1165        "#
1166    )
1167    .captures(raw_value)?;
1168    let regex = captures["regex"].to_owned();
1169    let replacement = captures["replacement"].to_owned();
1170    // A `\n` sequence in the replacement becomes an actual newline.
1171    // FIXME: Do unescaping in a less ad-hoc way, and perhaps support escaped
1172    // backslashes and double-quotes.
1173    let replacement = replacement.replace("\\n", "\n");
1174    Some((regex, replacement))
1175}
1176
1177/// Given an llvm version string that looks like `1.2.3-rc1`, extract as semver. Note that this
1178/// accepts more than just strict `semver` syntax (as in `major.minor.patch`); this permits omitting
1179/// minor and patch version components so users can write e.g. `//@ min-llvm-version: 19` instead of
1180/// having to write `//@ min-llvm-version: 19.0.0`.
1181///
1182/// Currently panics if the input string is malformed, though we really should not use panic as an
1183/// error handling strategy.
1184///
1185/// FIXME(jieyouxu): improve error handling
1186pub fn extract_llvm_version(version: &str) -> Version {
1187    // The version substring we're interested in usually looks like the `1.2.3`, without any of the
1188    // fancy suffix like `-rc1` or `meow`.
1189    let version = version.trim();
1190    let uninterested = |c: char| !c.is_ascii_digit() && c != '.';
1191    let version_without_suffix = match version.split_once(uninterested) {
1192        Some((prefix, _suffix)) => prefix,
1193        None => version,
1194    };
1195
1196    let components: Vec<u64> = version_without_suffix
1197        .split('.')
1198        .map(|s| s.parse().expect("llvm version component should consist of only digits"))
1199        .collect();
1200
1201    match &components[..] {
1202        [major] => Version::new(*major, 0, 0),
1203        [major, minor] => Version::new(*major, *minor, 0),
1204        [major, minor, patch] => Version::new(*major, *minor, *patch),
1205        _ => panic!("malformed llvm version string, expected only 1-3 components: {version}"),
1206    }
1207}
1208
1209pub fn extract_llvm_version_from_binary(binary_path: &str) -> Option<Version> {
1210    let output = Command::new(binary_path).arg("--version").output().ok()?;
1211    if !output.status.success() {
1212        return None;
1213    }
1214    let version = String::from_utf8(output.stdout).ok()?;
1215    for line in version.lines() {
1216        if let Some(version) = line.split("LLVM version ").nth(1) {
1217            return Some(extract_llvm_version(version));
1218        }
1219    }
1220    None
1221}
1222
1223/// For tests using the `needs-llvm-zstd` directive:
1224/// - for local LLVM builds, try to find the static zstd library in the llvm-config system libs.
1225/// - for `download-ci-llvm`, see if `lld` was built with zstd support.
1226pub fn llvm_has_libzstd(config: &Config) -> bool {
1227    // Strategy 1: works for local builds but not with `download-ci-llvm`.
1228    //
1229    // We check whether `llvm-config` returns the zstd library. Bootstrap's `llvm.libzstd` will only
1230    // ask to statically link it when building LLVM, so we only check if the list of system libs
1231    // contains a path to that static lib, and that it exists.
1232    //
1233    // See compiler/rustc_llvm/build.rs for more details and similar expectations.
1234    fn is_zstd_in_config(llvm_bin_dir: &Path) -> Option<()> {
1235        let llvm_config_path = llvm_bin_dir.join("llvm-config");
1236        let output = Command::new(llvm_config_path).arg("--system-libs").output().ok()?;
1237        assert!(output.status.success(), "running llvm-config --system-libs failed");
1238
1239        let libs = String::from_utf8(output.stdout).ok()?;
1240        for lib in libs.split_whitespace() {
1241            if lib.ends_with("libzstd.a") && Path::new(lib).exists() {
1242                return Some(());
1243            }
1244        }
1245
1246        None
1247    }
1248
1249    // Strategy 2: `download-ci-llvm`'s `llvm-config --system-libs` will not return any libs to
1250    // use.
1251    //
1252    // The CI artifacts also don't contain the bootstrap config used to build them: otherwise we
1253    // could have looked at the `llvm.libzstd` config.
1254    //
1255    // We infer whether `LLVM_ENABLE_ZSTD` was used to build LLVM as a byproduct of testing whether
1256    // `lld` supports it. If not, an error will be emitted: "LLVM was not built with
1257    // LLVM_ENABLE_ZSTD or did not find zstd at build time".
1258    #[cfg(unix)]
1259    fn is_lld_built_with_zstd(llvm_bin_dir: &Path) -> Option<()> {
1260        let lld_path = llvm_bin_dir.join("lld");
1261        if lld_path.exists() {
1262            // We can't call `lld` as-is, it expects to be invoked by a compiler driver using a
1263            // different name. Prepare a temporary symlink to do that.
1264            let lld_symlink_path = llvm_bin_dir.join("ld.lld");
1265            if !lld_symlink_path.exists() {
1266                std::os::unix::fs::symlink(lld_path, &lld_symlink_path).ok()?;
1267            }
1268
1269            // Run `lld` with a zstd flag. We expect this command to always error here, we don't
1270            // want to link actual files and don't pass any.
1271            let output = Command::new(&lld_symlink_path)
1272                .arg("--compress-debug-sections=zstd")
1273                .output()
1274                .ok()?;
1275            assert!(!output.status.success());
1276
1277            // Look for a specific error caused by LLVM not being built with zstd support. We could
1278            // also look for the "no input files" message, indicating the zstd flag was accepted.
1279            let stderr = String::from_utf8(output.stderr).ok()?;
1280            let zstd_available = !stderr.contains("LLVM was not built with LLVM_ENABLE_ZSTD");
1281
1282            // We don't particularly need to clean the link up (so the previous commands could fail
1283            // in theory but won't in practice), but we can try.
1284            std::fs::remove_file(lld_symlink_path).ok()?;
1285
1286            if zstd_available {
1287                return Some(());
1288            }
1289        }
1290
1291        None
1292    }
1293
1294    #[cfg(not(unix))]
1295    fn is_lld_built_with_zstd(_llvm_bin_dir: &Path) -> Option<()> {
1296        None
1297    }
1298
1299    if let Some(llvm_bin_dir) = &config.llvm_bin_dir {
1300        // Strategy 1: for local LLVM builds.
1301        if is_zstd_in_config(llvm_bin_dir).is_some() {
1302            return true;
1303        }
1304
1305        // Strategy 2: for LLVM artifacts built on CI via `download-ci-llvm`.
1306        //
1307        // It doesn't work for cases where the artifacts don't contain the linker, but it's
1308        // best-effort: CI has `llvm.libzstd` and `lld` enabled on the x64 linux artifacts, so it
1309        // will at least work there.
1310        //
1311        // If this can be improved and expanded to less common cases in the future, it should.
1312        if config.target == "x86_64-unknown-linux-gnu"
1313            && config.host == config.target
1314            && is_lld_built_with_zstd(llvm_bin_dir).is_some()
1315        {
1316            return true;
1317        }
1318    }
1319
1320    // Otherwise, all hope is lost.
1321    false
1322}
1323
1324/// Takes a directive of the form `"<version1> [- <version2>]"`, returns the numeric representation
1325/// of `<version1>` and `<version2>` as tuple: `(<version1>, <version2>)`.
1326///
1327/// If the `<version2>` part is omitted, the second component of the tuple is the same as
1328/// `<version1>`.
1329fn extract_version_range<'a, F, VersionTy: Clone>(
1330    line: &'a str,
1331    parse: F,
1332) -> Option<(VersionTy, VersionTy)>
1333where
1334    F: Fn(&'a str) -> Option<VersionTy>,
1335{
1336    let mut splits = line.splitn(2, "- ").map(str::trim);
1337    let min = splits.next().unwrap();
1338    if min.ends_with('-') {
1339        return None;
1340    }
1341
1342    let max = splits.next();
1343
1344    if min.is_empty() {
1345        return None;
1346    }
1347
1348    let min = parse(min)?;
1349    let max = match max {
1350        Some("") => return None,
1351        Some(max) => parse(max)?,
1352        _ => min.clone(),
1353    };
1354
1355    Some((min, max))
1356}
1357
1358pub fn make_test_description<R: Read>(
1359    config: &Config,
1360    cache: &HeadersCache,
1361    name: test::TestName,
1362    path: &Path,
1363    src: R,
1364    test_revision: Option<&str>,
1365    poisoned: &mut bool,
1366) -> test::TestDesc {
1367    let mut ignore = false;
1368    let mut ignore_message = None;
1369    let mut should_fail = false;
1370
1371    let mut local_poisoned = false;
1372
1373    // Scan through the test file to handle `ignore-*`, `only-*`, and `needs-*` directives.
1374    iter_header(
1375        config.mode,
1376        &config.suite,
1377        &mut local_poisoned,
1378        path,
1379        src,
1380        &mut |directive @ DirectiveLine { line_number, raw_directive: ln, .. }| {
1381            if !directive.applies_to_test_revision(test_revision) {
1382                return;
1383            }
1384
1385            macro_rules! decision {
1386                ($e:expr) => {
1387                    match $e {
1388                        IgnoreDecision::Ignore { reason } => {
1389                            ignore = true;
1390                            // The ignore reason must be a &'static str, so we have to leak memory to
1391                            // create it. This is fine, as the header is parsed only at the start of
1392                            // compiletest so it won't grow indefinitely.
1393                            ignore_message = Some(&*Box::leak(Box::<str>::from(reason)));
1394                        }
1395                        IgnoreDecision::Error { message } => {
1396                            eprintln!("error: {}:{line_number}: {message}", path.display());
1397                            *poisoned = true;
1398                            return;
1399                        }
1400                        IgnoreDecision::Continue => {}
1401                    }
1402                };
1403            }
1404
1405            decision!(cfg::handle_ignore(config, ln));
1406            decision!(cfg::handle_only(config, ln));
1407            decision!(needs::handle_needs(&cache.needs, config, ln));
1408            decision!(ignore_llvm(config, path, ln));
1409            decision!(ignore_cdb(config, ln));
1410            decision!(ignore_gdb(config, ln));
1411            decision!(ignore_lldb(config, ln));
1412
1413            if config.target == "wasm32-unknown-unknown"
1414                && config.parse_name_directive(ln, directives::CHECK_RUN_RESULTS)
1415            {
1416                decision!(IgnoreDecision::Ignore {
1417                    reason: "ignored on WASM as the run results cannot be checked there".into(),
1418                });
1419            }
1420
1421            should_fail |= config.parse_name_directive(ln, "should-fail");
1422        },
1423    );
1424
1425    if local_poisoned {
1426        eprintln!("errors encountered when trying to make test description: {}", path.display());
1427        panic!("errors encountered when trying to make test description");
1428    }
1429
1430    // The `should-fail` annotation doesn't apply to pretty tests,
1431    // since we run the pretty printer across all tests by default.
1432    // If desired, we could add a `should-fail-pretty` annotation.
1433    let should_panic = match config.mode {
1434        crate::common::Pretty => test::ShouldPanic::No,
1435        _ if should_fail => test::ShouldPanic::Yes,
1436        _ => test::ShouldPanic::No,
1437    };
1438
1439    test::TestDesc {
1440        name,
1441        ignore,
1442        ignore_message,
1443        source_file: "",
1444        start_line: 0,
1445        start_col: 0,
1446        end_line: 0,
1447        end_col: 0,
1448        should_panic,
1449        compile_fail: false,
1450        no_run: false,
1451        test_type: test::TestType::Unknown,
1452    }
1453}
1454
1455fn ignore_cdb(config: &Config, line: &str) -> IgnoreDecision {
1456    if config.debugger != Some(Debugger::Cdb) {
1457        return IgnoreDecision::Continue;
1458    }
1459
1460    if let Some(actual_version) = config.cdb_version {
1461        if let Some(rest) = line.strip_prefix("min-cdb-version:").map(str::trim) {
1462            let min_version = extract_cdb_version(rest).unwrap_or_else(|| {
1463                panic!("couldn't parse version range: {:?}", rest);
1464            });
1465
1466            // Ignore if actual version is smaller than the minimum
1467            // required version
1468            if actual_version < min_version {
1469                return IgnoreDecision::Ignore {
1470                    reason: format!("ignored when the CDB version is lower than {rest}"),
1471                };
1472            }
1473        }
1474    }
1475    IgnoreDecision::Continue
1476}
1477
1478fn ignore_gdb(config: &Config, line: &str) -> IgnoreDecision {
1479    if config.debugger != Some(Debugger::Gdb) {
1480        return IgnoreDecision::Continue;
1481    }
1482
1483    if let Some(actual_version) = config.gdb_version {
1484        if let Some(rest) = line.strip_prefix("min-gdb-version:").map(str::trim) {
1485            let (start_ver, end_ver) = extract_version_range(rest, extract_gdb_version)
1486                .unwrap_or_else(|| {
1487                    panic!("couldn't parse version range: {:?}", rest);
1488                });
1489
1490            if start_ver != end_ver {
1491                panic!("Expected single GDB version")
1492            }
1493            // Ignore if actual version is smaller than the minimum
1494            // required version
1495            if actual_version < start_ver {
1496                return IgnoreDecision::Ignore {
1497                    reason: format!("ignored when the GDB version is lower than {rest}"),
1498                };
1499            }
1500        } else if let Some(rest) = line.strip_prefix("ignore-gdb-version:").map(str::trim) {
1501            let (min_version, max_version) = extract_version_range(rest, extract_gdb_version)
1502                .unwrap_or_else(|| {
1503                    panic!("couldn't parse version range: {:?}", rest);
1504                });
1505
1506            if max_version < min_version {
1507                panic!("Malformed GDB version range: max < min")
1508            }
1509
1510            if actual_version >= min_version && actual_version <= max_version {
1511                if min_version == max_version {
1512                    return IgnoreDecision::Ignore {
1513                        reason: format!("ignored when the GDB version is {rest}"),
1514                    };
1515                } else {
1516                    return IgnoreDecision::Ignore {
1517                        reason: format!("ignored when the GDB version is between {rest}"),
1518                    };
1519                }
1520            }
1521        }
1522    }
1523    IgnoreDecision::Continue
1524}
1525
1526fn ignore_lldb(config: &Config, line: &str) -> IgnoreDecision {
1527    if config.debugger != Some(Debugger::Lldb) {
1528        return IgnoreDecision::Continue;
1529    }
1530
1531    if let Some(actual_version) = config.lldb_version {
1532        if let Some(rest) = line.strip_prefix("min-lldb-version:").map(str::trim) {
1533            let min_version = rest.parse().unwrap_or_else(|e| {
1534                panic!("Unexpected format of LLDB version string: {}\n{:?}", rest, e);
1535            });
1536            // Ignore if actual version is smaller the minimum required
1537            // version
1538            if actual_version < min_version {
1539                return IgnoreDecision::Ignore {
1540                    reason: format!("ignored when the LLDB version is {rest}"),
1541                };
1542            }
1543        }
1544    }
1545    IgnoreDecision::Continue
1546}
1547
1548fn ignore_llvm(config: &Config, path: &Path, line: &str) -> IgnoreDecision {
1549    if let Some(needed_components) =
1550        config.parse_name_value_directive(line, "needs-llvm-components")
1551    {
1552        let components: HashSet<_> = config.llvm_components.split_whitespace().collect();
1553        if let Some(missing_component) = needed_components
1554            .split_whitespace()
1555            .find(|needed_component| !components.contains(needed_component))
1556        {
1557            if env::var_os("COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS").is_some() {
1558                panic!(
1559                    "missing LLVM component {}, and COMPILETEST_REQUIRE_ALL_LLVM_COMPONENTS is set: {}",
1560                    missing_component,
1561                    path.display()
1562                );
1563            }
1564            return IgnoreDecision::Ignore {
1565                reason: format!("ignored when the {missing_component} LLVM component is missing"),
1566            };
1567        }
1568    }
1569    if let Some(actual_version) = &config.llvm_version {
1570        // Note that these `min` versions will check for not just major versions.
1571
1572        if let Some(version_string) = config.parse_name_value_directive(line, "min-llvm-version") {
1573            let min_version = extract_llvm_version(&version_string);
1574            // Ignore if actual version is smaller than the minimum required version.
1575            if *actual_version < min_version {
1576                return IgnoreDecision::Ignore {
1577                    reason: format!(
1578                        "ignored when the LLVM version {actual_version} is older than {min_version}"
1579                    ),
1580                };
1581            }
1582        } else if let Some(version_string) =
1583            config.parse_name_value_directive(line, "max-llvm-major-version")
1584        {
1585            let max_version = extract_llvm_version(&version_string);
1586            // Ignore if actual major version is larger than the maximum required major version.
1587            if actual_version.major > max_version.major {
1588                return IgnoreDecision::Ignore {
1589                    reason: format!(
1590                        "ignored when the LLVM version ({actual_version}) is newer than major\
1591                        version {}",
1592                        max_version.major
1593                    ),
1594                };
1595            }
1596        } else if let Some(version_string) =
1597            config.parse_name_value_directive(line, "min-system-llvm-version")
1598        {
1599            let min_version = extract_llvm_version(&version_string);
1600            // Ignore if using system LLVM and actual version
1601            // is smaller the minimum required version
1602            if config.system_llvm && *actual_version < min_version {
1603                return IgnoreDecision::Ignore {
1604                    reason: format!(
1605                        "ignored when the system LLVM version {actual_version} is older than {min_version}"
1606                    ),
1607                };
1608            }
1609        } else if let Some(version_range) =
1610            config.parse_name_value_directive(line, "ignore-llvm-version")
1611        {
1612            // Syntax is: "ignore-llvm-version: <version1> [- <version2>]"
1613            let (v_min, v_max) =
1614                extract_version_range(&version_range, |s| Some(extract_llvm_version(s)))
1615                    .unwrap_or_else(|| {
1616                        panic!("couldn't parse version range: \"{version_range}\"");
1617                    });
1618            if v_max < v_min {
1619                panic!("malformed LLVM version range where {v_max} < {v_min}")
1620            }
1621            // Ignore if version lies inside of range.
1622            if *actual_version >= v_min && *actual_version <= v_max {
1623                if v_min == v_max {
1624                    return IgnoreDecision::Ignore {
1625                        reason: format!("ignored when the LLVM version is {actual_version}"),
1626                    };
1627                } else {
1628                    return IgnoreDecision::Ignore {
1629                        reason: format!(
1630                            "ignored when the LLVM version is between {v_min} and {v_max}"
1631                        ),
1632                    };
1633                }
1634            }
1635        } else if let Some(version_string) =
1636            config.parse_name_value_directive(line, "exact-llvm-major-version")
1637        {
1638            // Syntax is "exact-llvm-major-version: <version>"
1639            let version = extract_llvm_version(&version_string);
1640            if actual_version.major != version.major {
1641                return IgnoreDecision::Ignore {
1642                    reason: format!(
1643                        "ignored when the actual LLVM major version is {}, but the test only targets major version {}",
1644                        actual_version.major, version.major
1645                    ),
1646                };
1647            }
1648        }
1649    }
1650    IgnoreDecision::Continue
1651}
1652
1653enum IgnoreDecision {
1654    Ignore { reason: String },
1655    Continue,
1656    Error { message: String },
1657}