tidy/
pal.rs

1//! Tidy check to enforce rules about platform-specific code in std.
2//!
3//! This is intended to maintain existing standards of code
4//! organization in hopes that the standard library will continue to
5//! be refactored to isolate platform-specific bits, making porting
6//! easier; where "standard library" roughly means "all the
7//! dependencies of the std and test crates".
8//!
9//! This generally means placing restrictions on where `cfg(unix)`,
10//! `cfg(windows)`, `cfg(target_os)` and `cfg(target_env)` may appear,
11//! the basic objective being to isolate platform-specific code to the
12//! platform-specific `std::sys` modules, and to the allocation,
13//! unwinding, and libc crates.
14//!
15//! Following are the basic rules, though there are currently
16//! exceptions:
17//!
18//! - core may not have platform-specific code.
19//! - libpanic_abort may have platform-specific code.
20//! - libpanic_unwind may have platform-specific code.
21//! - libunwind may have platform-specific code.
22//! - other crates in the std facade may not.
23//! - std may have platform-specific code in the following places:
24//!   - `sys/`
25//!   - `os/`
26//!
27//! Finally, because std contains tests with platform-specific
28//! `ignore` attributes, once the parser encounters `mod tests`,
29//! platform-specific cfgs are allowed. Not sure yet how to deal with
30//! this in the long term.
31
32use std::path::Path;
33
34use crate::diagnostics::{CheckId, RunningCheck, TidyCtx};
35use crate::walk::{filter_dirs, walk};
36
37// Paths that may contain platform-specific code.
38const EXCEPTION_PATHS: &[&str] = &[
39    "library/compiler-builtins",
40    "library/std_detect",
41    "library/windows_targets",
42    "library/panic_abort",
43    "library/panic_unwind",
44    "library/unwind",
45    "library/rtstartup", // Not sure what to do about this. magic stuff for mingw
46    "library/test",      // Probably should defer to unstable `std::sys` APIs.
47    // The `VaList` implementation must have platform specific code.
48    // The Windows implementation of a `va_list` is always a character
49    // pointer regardless of the target architecture. As a result,
50    // we must use `#[cfg(windows)]` to conditionally compile the
51    // correct `VaList` structure for windows.
52    "library/core/src/ffi/va_list.rs",
53    // core::ffi contains platform-specific type and linkage configuration
54    "library/core/src/ffi/mod.rs",
55    "library/core/src/ffi/primitives.rs",
56    "library/core/src/os", // Platform-specific public interfaces
57    "library/std/src/sys", // Platform-specific code for std lives here.
58    "library/std/src/os",  // Platform-specific public interfaces
59    // Temporary `std` exceptions
60    // FIXME: platform-specific code should be moved to `sys`
61    "library/std/src/io/stdio.rs",
62    "library/std/src/lib.rs", // for miniz_oxide leaking docs, which itself workaround
63    "library/std/src/path.rs",
64    "library/std/src/io/error.rs", // Repr unpacked needed for UEFI
65];
66
67pub fn check(library_path: &Path, tidy_ctx: TidyCtx) {
68    let mut check = tidy_ctx.start_check(CheckId::new("pal").path(library_path));
69
70    let root_path = library_path.parent().unwrap();
71    // Let's double-check that this is the root path by making sure it has `x.py`.
72    assert!(root_path.join("x.py").is_file());
73
74    // Sanity check that the complex parsing here works.
75    let mut saw_target_arch = false;
76    let mut saw_cfg_bang = false;
77    walk(library_path, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
78        let file = entry.path();
79        // We don't want the absolute path to matter, so make it relative.
80        let file = file.strip_prefix(root_path).unwrap();
81        let filestr = file.to_string_lossy().replace("\\", "/");
82        if !filestr.ends_with(".rs") {
83            return;
84        }
85
86        let is_exception_path = EXCEPTION_PATHS.iter().any(|s| filestr.contains(&**s));
87        if is_exception_path {
88            return;
89        }
90
91        // exclude tests and benchmarks as some platforms do not support all tests
92        if filestr.contains("tests") || filestr.contains("benches") {
93            return;
94        }
95
96        check_cfgs(contents, file, &mut check, &mut saw_target_arch, &mut saw_cfg_bang);
97    });
98
99    assert!(saw_target_arch);
100    assert!(saw_cfg_bang);
101}
102
103fn check_cfgs(
104    contents: &str,
105    file: &Path,
106    check: &mut RunningCheck,
107    saw_target_arch: &mut bool,
108    saw_cfg_bang: &mut bool,
109) {
110    // Pull out all `cfg(...)` and `cfg!(...)` strings.
111    let cfgs = parse_cfgs(contents);
112
113    let mut line_numbers: Option<Vec<usize>> = None;
114    let mut err = |idx: usize, cfg: &str| {
115        if line_numbers.is_none() {
116            line_numbers = Some(contents.match_indices('\n').map(|(i, _)| i).collect());
117        }
118        let line_numbers = line_numbers.as_ref().expect("");
119        let line = match line_numbers.binary_search(&idx) {
120            Ok(_) => unreachable!(),
121            Err(i) => i + 1,
122        };
123        check.error(format!("{}:{line}: platform-specific cfg: {cfg}", file.display()));
124    };
125
126    for (idx, cfg) in cfgs {
127        // Sanity check that the parsing here works.
128        if !*saw_target_arch && cfg.contains("target_arch") {
129            *saw_target_arch = true
130        }
131        if !*saw_cfg_bang && cfg.contains("cfg!") {
132            *saw_cfg_bang = true
133        }
134
135        let contains_platform_specific_cfg = cfg.contains("target_os")
136            || cfg.contains("target_env")
137            || cfg.contains("target_abi")
138            || cfg.contains("target_vendor")
139            || cfg.contains("target_family")
140            || cfg.contains("unix")
141            || cfg.contains("windows");
142
143        if !contains_platform_specific_cfg {
144            continue;
145        }
146
147        let preceded_by_doc_comment = {
148            let pre_contents = &contents[..idx];
149            let pre_newline = pre_contents.rfind('\n');
150            let pre_doc_comment = pre_contents.rfind("///");
151            match (pre_newline, pre_doc_comment) {
152                (Some(n), Some(c)) => n < c,
153                (None, Some(_)) => true,
154                (_, None) => false,
155            }
156        };
157
158        if preceded_by_doc_comment {
159            continue;
160        }
161
162        // exclude tests as some platforms do not support all tests
163        if cfg.contains("test") {
164            continue;
165        }
166
167        err(idx, cfg);
168    }
169}
170
171fn parse_cfgs(contents: &str) -> Vec<(usize, &str)> {
172    let candidate_cfgs = contents.match_indices("cfg");
173    let candidate_cfg_idxs = candidate_cfgs.map(|(i, _)| i);
174    // This is puling out the indexes of all "cfg" strings
175    // that appear to be tokens followed by a parenthesis.
176    let cfgs = candidate_cfg_idxs.filter(|i| {
177        let pre_idx = i.saturating_sub(1);
178        let succeeds_non_ident = !contents
179            .as_bytes()
180            .get(pre_idx)
181            .cloned()
182            .map(char::from)
183            .map(char::is_alphanumeric)
184            .unwrap_or(false);
185        let contents_after = &contents[*i..];
186        let first_paren = contents_after.find('(');
187        let paren_idx = first_paren.map(|ip| i + ip);
188        let preceeds_whitespace_and_paren = paren_idx
189            .map(|ip| {
190                let maybe_space = &contents[*i + "cfg".len()..ip];
191                maybe_space.chars().all(|c| char::is_whitespace(c) || c == '!')
192            })
193            .unwrap_or(false);
194
195        succeeds_non_ident && preceeds_whitespace_and_paren
196    });
197
198    cfgs.flat_map(|i| {
199        let mut depth = 0;
200        let contents_from = &contents[i..];
201        for (j, byte) in contents_from.bytes().enumerate() {
202            match byte {
203                b'(' => {
204                    depth += 1;
205                }
206                b')' => {
207                    depth -= 1;
208                    if depth == 0 {
209                        return Some((i, &contents_from[..=j]));
210                    }
211                }
212                _ => {}
213            }
214        }
215
216        // if the parentheses are unbalanced just ignore this cfg -- it'll be caught when attempting
217        // to run the compiler, and there's no real reason to lint it separately here
218        None
219    })
220    .collect()
221}