tidy/
target_specific_tests.rs

1//! Tidy check to ensure that all target specific tests (those that require a `--target` flag)
2//! also require the pre-requisite LLVM components to run.
3
4use std::collections::BTreeMap;
5use std::path::Path;
6
7use crate::diagnostics::{CheckId, DiagCtx};
8use crate::iter_header::{HeaderLine, iter_header};
9use crate::walk::filter_not_rust;
10
11const LLVM_COMPONENTS_HEADER: &str = "needs-llvm-components:";
12const COMPILE_FLAGS_HEADER: &str = "compile-flags:";
13
14#[derive(Default, Debug)]
15struct RevisionInfo<'a> {
16    target_arch: Option<Option<&'a str>>,
17    llvm_components: Option<Vec<&'a str>>,
18}
19
20pub fn check(tests_path: &Path, diag_ctx: DiagCtx) {
21    let mut check = diag_ctx.start_check(CheckId::new("target-specific-tests").path(tests_path));
22
23    crate::walk::walk(tests_path, |path, _is_dir| filter_not_rust(path), &mut |entry, content| {
24        if content.contains("// ignore-tidy-target-specific-tests") {
25            return;
26        }
27
28        let file = entry.path().display();
29        let mut header_map = BTreeMap::new();
30        iter_header(content, &mut |HeaderLine { revision, directive, .. }| {
31            if let Some(value) = directive.strip_prefix(LLVM_COMPONENTS_HEADER) {
32                let info = header_map.entry(revision).or_insert(RevisionInfo::default());
33                let comp_vec = info.llvm_components.get_or_insert(Vec::new());
34                for component in value.split(' ') {
35                    let component = component.trim();
36                    if !component.is_empty() {
37                        comp_vec.push(component);
38                    }
39                }
40            } else if let Some(compile_flags) = directive.strip_prefix(COMPILE_FLAGS_HEADER)
41                && let Some((_, v)) = compile_flags.split_once("--target")
42            {
43                let v = v.trim_start_matches([' ', '=']);
44                let info = header_map.entry(revision).or_insert(RevisionInfo::default());
45                if v.starts_with("{{") {
46                    info.target_arch.replace(None);
47                } else if let Some((arch, _)) = v.split_once("-") {
48                    info.target_arch.replace(Some(arch));
49                } else {
50                    check.error(format!("{file}: seems to have a malformed --target value"));
51                }
52            }
53        });
54
55        // Skip run-make tests as revisions are not supported.
56        if entry.path().strip_prefix(tests_path).is_ok_and(|rest| rest.starts_with("run-make")) {
57            return;
58        }
59
60        for (rev, RevisionInfo { target_arch, llvm_components }) in &header_map {
61            let rev = rev.unwrap_or("[unspecified]");
62            match (target_arch, llvm_components) {
63                (None, None) => {}
64                (Some(target_arch), None) => {
65                    let llvm_component =
66                        target_arch.map_or_else(|| "<arch>".to_string(), arch_to_llvm_component);
67                    check.error(format!(
68                        "{file}: revision {rev} should specify `{LLVM_COMPONENTS_HEADER} {llvm_component}` as it has `--target` set"
69                    ));
70                }
71                (None, Some(_)) => {
72                    check.error(format!(
73                        "{file}: revision {rev} should not specify `{LLVM_COMPONENTS_HEADER}` as it doesn't need `--target`"
74                    ));
75                }
76                (Some(target_arch), Some(llvm_components)) => {
77                    if let Some(target_arch) = target_arch {
78                        let llvm_component = arch_to_llvm_component(target_arch);
79                        if !llvm_components.contains(&llvm_component.as_str()) {
80                            check.error(format!(
81                                "{file}: revision {rev} should specify `{LLVM_COMPONENTS_HEADER} {llvm_component}` as it has `--target` set"
82                            ));
83                        }
84                    }
85                }
86            }
87        }
88    });
89}
90
91fn arch_to_llvm_component(arch: &str) -> String {
92    // NOTE: This is an *approximate* mapping of Rust's `--target` architecture to LLVM component
93    // names. It is not intended to be an authoritative source, but rather a best-effort that's good
94    // enough for the purpose of this tidy check.
95    match arch {
96        "amdgcn" => "amdgpu".into(),
97        "aarch64_be" | "arm64_32" | "arm64e" | "arm64ec" => "aarch64".into(),
98        "i386" | "i586" | "i686" | "x86" | "x86_64" | "x86_64h" => "x86".into(),
99        "loongarch32" | "loongarch64" => "loongarch".into(),
100        "nvptx64" => "nvptx".into(),
101        "s390x" => "systemz".into(),
102        "sparc64" | "sparcv9" => "sparc".into(),
103        "wasm32" | "wasm32v1" | "wasm64" => "webassembly".into(),
104        _ if arch.starts_with("armeb")
105            || arch.starts_with("armv")
106            || arch.starts_with("thumbv") =>
107        {
108            "arm".into()
109        }
110        _ if arch.starts_with("bpfe") => "bpf".into(),
111        _ if arch.starts_with("mips") => "mips".into(),
112        _ if arch.starts_with("powerpc") => "powerpc".into(),
113        _ if arch.starts_with("riscv") => "riscv".into(),
114        _ => arch.to_ascii_lowercase(),
115    }
116}