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::iter_header::{HeaderLine, iter_header};
8use crate::walk::filter_not_rust;
9
10const LLVM_COMPONENTS_HEADER: &str = "needs-llvm-components:";
11const COMPILE_FLAGS_HEADER: &str = "compile-flags:";
12
13#[derive(Default, Debug)]
14struct RevisionInfo<'a> {
15    target_arch: Option<&'a str>,
16    llvm_components: Option<Vec<&'a str>>,
17}
18
19pub fn check(tests_path: &Path, bad: &mut bool) {
20    crate::walk::walk(tests_path, |path, _is_dir| filter_not_rust(path), &mut |entry, content| {
21        let file = entry.path().display();
22        let mut header_map = BTreeMap::new();
23        iter_header(content, &mut |HeaderLine { revision, directive, .. }| {
24            if let Some(value) = directive.strip_prefix(LLVM_COMPONENTS_HEADER) {
25                let info = header_map.entry(revision).or_insert(RevisionInfo::default());
26                let comp_vec = info.llvm_components.get_or_insert(Vec::new());
27                for component in value.split(' ') {
28                    let component = component.trim();
29                    if !component.is_empty() {
30                        comp_vec.push(component);
31                    }
32                }
33            } else if directive.starts_with(COMPILE_FLAGS_HEADER) {
34                let compile_flags = &directive[COMPILE_FLAGS_HEADER.len()..];
35                if let Some((_, v)) = compile_flags.split_once("--target") {
36                    let v = v.trim_start_matches(|c| c == ' ' || c == '=');
37                    let v = if v == "{{target}}" { Some((v, v)) } else { v.split_once("-") };
38                    if let Some((arch, _)) = v {
39                        let info = header_map.entry(revision).or_insert(RevisionInfo::default());
40                        info.target_arch.replace(arch);
41                    } else {
42                        eprintln!("{file}: seems to have a malformed --target value");
43                        *bad = true;
44                    }
45                }
46            }
47        });
48
49        // Skip run-make tests as revisions are not supported.
50        if entry.path().strip_prefix(tests_path).is_ok_and(|rest| rest.starts_with("run-make")) {
51            return;
52        }
53
54        for (rev, RevisionInfo { target_arch, llvm_components }) in &header_map {
55            let rev = rev.unwrap_or("[unspecified]");
56            match (target_arch, llvm_components) {
57                (None, None) => {}
58                (Some(_), None) => {
59                    eprintln!(
60                        "{}: revision {} should specify `{}` as it has `--target` set",
61                        file, rev, LLVM_COMPONENTS_HEADER
62                    );
63                    *bad = true;
64                }
65                (None, Some(_)) => {
66                    eprintln!(
67                        "{}: revision {} should not specify `{}` as it doesn't need `--target`",
68                        file, rev, LLVM_COMPONENTS_HEADER
69                    );
70                    *bad = true;
71                }
72                (Some(_), Some(_)) => {
73                    // FIXME: check specified components against the target architectures we
74                    // gathered.
75                }
76            }
77        }
78    });
79}